Merge "Issue #1522 - disable Spring during installs and uninstalls" into development

Former-commit-id: de2829de069a3f3280b25fd8a8b7b831377aaebc
This commit is contained in:
Steve Harris 2013-01-25 16:02:47 -06:00 committed by Gerrit Code Review
commit ffb536cd46

View file

@ -16,6 +16,7 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
import org.eclipse.core.runtime.Platform;
/** /**
* *
@ -30,6 +31,8 @@ import org.osgi.framework.Constants;
* Date Ticket# Engineer Description * Date Ticket# Engineer Description
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* Oct 12, 2010 mschenke Initial creation * Oct 12, 2010 mschenke Initial creation
* Jan 24, 2013 1522 bkowal Halt initialization if a p2 installation
* has been started
* *
* </pre> * </pre>
* *
@ -38,133 +41,162 @@ import org.osgi.framework.Constants;
*/ */
public class Activator implements BundleActivator { public class Activator implements BundleActivator {
// The plug-in ID // The plug-in ID
public static final String PLUGIN_ID = "com.raytheon.uf.viz.spring.dm"; public static final String PLUGIN_ID = "com.raytheon.uf.viz.spring.dm";
private static final String SPRING_PATH = "res" + IPath.SEPARATOR private static final String SPRING_PATH = "res" + IPath.SEPARATOR
+ "spring"; + "spring";
private static final String SPRING_FILE_EXT = "*.xml"; private static final String SPRING_FILE_EXT = "*.xml";
// The shared instance // The shared instance
private static Activator plugin; private static Activator plugin;
/** /**
* The constructor * The constructor
*/ */
public Activator() { public Activator() {
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see * @see
* org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext) * org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
*/ */
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
plugin = this; if (this.isInstallOperation()) {
return;
}
plugin = this;
Map<String, OSGIXmlApplicationContext> contextMap = new HashMap<String, OSGIXmlApplicationContext>(); Map<String, OSGIXmlApplicationContext> contextMap = new HashMap<String, OSGIXmlApplicationContext>();
Set<String> processing = new HashSet<String>(); Set<String> processing = new HashSet<String>();
Bundle[] bundles = context.getBundles(); Bundle[] bundles = context.getBundles();
Map<String, Bundle> bundleMap = new HashMap<String, Bundle>(); Map<String, Bundle> bundleMap = new HashMap<String, Bundle>();
for (Bundle b : bundles) { for (Bundle b : bundles) {
bundleMap.put(b.getSymbolicName(), b); bundleMap.put(b.getSymbolicName(), b);
} }
for (Bundle b : bundles) { for (Bundle b : bundles) {
createContext(bundleMap, contextMap, b, processing); createContext(bundleMap, contextMap, b, processing);
} }
} }
private OSGIXmlApplicationContext createContext( private OSGIXmlApplicationContext createContext(
Map<String, Bundle> bundles, Map<String, Bundle> bundles,
Map<String, OSGIXmlApplicationContext> contextMap, Bundle bundle, Map<String, OSGIXmlApplicationContext> contextMap, Bundle bundle,
Set<String> processing) { Set<String> processing) {
String bundleName = bundle.getSymbolicName(); String bundleName = bundle.getSymbolicName();
OSGIXmlApplicationContext appCtx = contextMap.get(bundleName); OSGIXmlApplicationContext appCtx = contextMap.get(bundleName);
if (contextMap.containsKey(bundleName) == false if (contextMap.containsKey(bundleName) == false
&& bundleName.contains(".edex.") == false) { && bundleName.contains(".edex.") == false) {
if (processing.contains(bundleName)) { if (processing.contains(bundleName)) {
throw new RuntimeException( throw new RuntimeException(
"Found recursive spring dependency while processing plugins: " "Found recursive spring dependency while processing plugins: "
+ bundleName); + bundleName);
} }
processing.add(bundleName); processing.add(bundleName);
// No context created yet and not edex project, check for files // No context created yet and not edex project, check for files
Enumeration<?> entries = bundle.findEntries(SPRING_PATH, Enumeration<?> entries = bundle.findEntries(SPRING_PATH,
SPRING_FILE_EXT, true); SPRING_FILE_EXT, true);
if (entries != null) { if (entries != null) {
List<String> files = new ArrayList<String>(); List<String> files = new ArrayList<String>();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
URL url = (URL) entries.nextElement(); URL url = (URL) entries.nextElement();
try { try {
url = FileLocator.toFileURL(url); url = FileLocator.toFileURL(url);
files.add(url.toString()); files.add(url.toString());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException( throw new RuntimeException(
"Error resolving spring file: " + url, e); "Error resolving spring file: " + url, e);
} }
} }
if (files.size() > 0) { if (files.size() > 0) {
// Files found, check for dependencies // Files found, check for dependencies
String requiredBundlesHeader = (String) bundle.getHeaders() String requiredBundlesHeader = (String) bundle.getHeaders()
.get(Constants.REQUIRE_BUNDLE); .get(Constants.REQUIRE_BUNDLE);
// Split comma separated string from MANIFEST // Split comma separated string from MANIFEST
String[] requiredBundles = requiredBundlesHeader String[] requiredBundles = requiredBundlesHeader
.split("[,]"); .split("[,]");
List<OSGIXmlApplicationContext> parentContexts = new ArrayList<OSGIXmlApplicationContext>(); List<OSGIXmlApplicationContext> parentContexts = new ArrayList<OSGIXmlApplicationContext>();
for (String requiredBndl : requiredBundles) { for (String requiredBndl : requiredBundles) {
// Extract bundle name which is first item in // Extract bundle name which is first item in
// semicolon // semicolon
// split list // split list
String[] bndlParts = requiredBndl.split("[;]"); String[] bndlParts = requiredBndl.split("[;]");
Bundle reqBndl = bundles.get(bndlParts[0]); Bundle reqBndl = bundles.get(bndlParts[0]);
if (reqBndl != null) { if (reqBndl != null) {
// Found bundle, process context for bundle // Found bundle, process context for bundle
OSGIXmlApplicationContext parent = createContext( OSGIXmlApplicationContext parent = createContext(
bundles, contextMap, reqBndl, processing); bundles, contextMap, reqBndl, processing);
if (parent != null) { if (parent != null) {
// Context found, add to list // Context found, add to list
parentContexts.add(parent); parentContexts.add(parent);
} }
} }
} }
if (parentContexts.size() > 0) { if (parentContexts.size() > 0) {
// Context with parent context // Context with parent context
appCtx = new OSGIXmlApplicationContext( appCtx = new OSGIXmlApplicationContext(
new OSGIGroupApplicationContext(parentContexts), new OSGIGroupApplicationContext(parentContexts),
files.toArray(new String[0]), bundle); files.toArray(new String[0]), bundle);
} else { } else {
// No parent context required // No parent context required
appCtx = new OSGIXmlApplicationContext( appCtx = new OSGIXmlApplicationContext(
files.toArray(new String[0]), bundle); files.toArray(new String[0]), bundle);
} }
} }
} }
contextMap.put(bundleName, appCtx); contextMap.put(bundleName, appCtx);
} }
processing.remove(bundleName); processing.remove(bundleName);
return appCtx; return appCtx;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
* @see * @see
* org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
*/ */
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
plugin = null; plugin = null;
} }
/** /**
* Returns the shared instance * Returns the shared instance
* *
* @return the shared instance * @return the shared instance
*/ */
public static Activator getDefault() { public static Activator getDefault() {
return plugin; return plugin;
} }
/**
* Based on the command line arguments, determine whether or not an Eclipse
* p2 repository will be installed
*
* @return true if an Eclipse p2 repository is going to be installed, false
* otherwise
*/
private boolean isInstallOperation() {
final String P2_DIRECTOR = "org.eclipse.equinox.p2.director";
/**
* We look at the command line arguments instead of the program
* arguments (com.raytheon.uf.viz.application.ProgramArguments) because
* the command line arguments include almost everything that was passed
* as an argument to the Eclipse executable instead of just what CAVE is
* interested in.
*/
for (String argument : Platform.getCommandLineArgs()) {
if (P2_DIRECTOR.equals(argument)) {
return Boolean.TRUE;
}
}
return Boolean.FALSE;
}
} }