package org.eclipse.team.internal.core;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.debug.DebugOptionsListener;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.RepositoryProviderType;
import org.eclipse.team.core.Team;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.mapping.DelegatingStorageMerger;
import org.eclipse.team.internal.core.mapping.IStreamMergerDelegate;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
final public class TeamPlugin extends Plugin {
public static final String ID = "org.eclipse.team.core";
public static final String PROVIDER_EXTENSION = "repository-provider-type";
public static final String FILE_TYPES_EXTENSION = "fileTypes";
public static final String IGNORE_EXTENSION = "ignore";
public static final String PROJECT_SET_EXTENSION = "projectSets";
public static final String REPOSITORY_EXTENSION = "repository";
public static final String DEFAULT_FILE_MODIFICATION_VALIDATOR_EXTENSION = "defaultFileModificationValidator";
public final static QualifiedName PROVIDER_PROP_KEY =
new QualifiedName("org.eclipse.team.core", "repository");
public static final String EXTENSION_POINT_BUNDLE_IMPORTERS = ID + ".bundleImporters";
private static TeamPlugin plugin;
private ServiceRegistration debugRegistration;
private IStreamMergerDelegate mergerDelegate;
public TeamPlugin() {
super();
plugin = this;
}
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
Hashtable<String, String> properties = new Hashtable<>(2);
properties.put(DebugOptions.LISTENER_SYMBOLICNAME, ID);
debugRegistration = context.registerService(DebugOptionsListener.class, Policy.DEBUG_OPTIONS_LISTENER, properties);
Team.startup();
}
@Override
public void stop(BundleContext context) throws Exception {
try {
debugRegistration.unregister();
debugRegistration = null;
Team.shutdown();
ResourceVariantCache.shutdown();
} finally {
super.stop(context);
}
}
public static TeamPlugin getPlugin() {
return plugin;
}
public static void log(int severity, String message, Throwable e) {
plugin.getLog().log(new Status(severity, ID, 0, message, e));
}
public static void log(CoreException e) {
log(e.getStatus().getSeverity(), e.getMessage(), e);
}
public static TeamException wrapException(CoreException e) {
IStatus status = e.getStatus();
return new TeamException(new Status(status.getSeverity(), ID, status.getCode(), status.getMessage(), e));
}
public static String getCharset(String name, InputStream stream) throws IOException {
IContentDescription description = getContentDescription(name, stream);
return description == null ? null : description.getCharset();
}
public static IContentDescription getContentDescription(String name, InputStream stream) throws IOException {
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
try {
return contentTypeManager.getDescriptionFor(stream, name, IContentDescription.ALL);
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
}
}
}
public static RepositoryProviderType getAliasType(String id) {
IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.REPOSITORY_EXTENSION);
if (extension != null) {
IExtension[] extensions = extension.getExtensions();
for (IExtension ext : extensions) {
IConfigurationElement[] configElements = ext.getConfigurationElements();
for (IConfigurationElement configElement : configElements) {
String aliasId = configElement.getAttribute("canImportId");
if (aliasId != null && aliasId.equals(id)) {
String extensionId = configElement.getAttribute("id");
if (extensionId != null) {
return RepositoryProviderType.getProviderType(extensionId);
}
}
}
}
}
return null;
}
public static IPath[] getMetaFilePaths(String id) {
IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.REPOSITORY_EXTENSION);
if (extension != null) {
IExtension[] extensions = extension.getExtensions();
for (IExtension ext : extensions) {
IConfigurationElement[] configElements = ext.getConfigurationElements();
for (IConfigurationElement configElement : configElements) {
String extensionId = configElement.getAttribute("id");
String metaFilePaths = configElement.getAttribute("metaFilePaths");
if (extensionId != null && extensionId.equals(id) && metaFilePaths != null) {
return getPaths(metaFilePaths);
}
}
}
}
return null;
}
private static IPath[] getPaths(String metaFilePaths) {
List<IPath> result = new ArrayList<>();
StringTokenizer t = new StringTokenizer(metaFilePaths, ",");
while (t.hasMoreTokens()) {
String next = t.nextToken();
IPath path = new Path(null, next);
result.add(path);
}
return result.toArray(new IPath[result.size()]);
}
public void setMergerDelegate(IStreamMergerDelegate merger) {
mergerDelegate = merger;
}
public IStreamMergerDelegate getMergerDelegate() {
return mergerDelegate;
}
}