5757import org .eclipse .emf .ecore .resource .URIConverter ;
5858import org .osgi .framework .BundleActivator ;
5959import org .osgi .framework .BundleContext ;
60+ import org .osgi .util .tracker .ServiceTracker ;
61+ import org .osgi .util .tracker .ServiceTrackerCustomizer ;
6062
6163
6264/**
@@ -586,13 +588,18 @@ public static Map<String, URI> getEPackageNsURIToDynamicModelLocationMap(boolean
586588 */
587589 static public class Implementation extends EclipsePlugin
588590 {
589- /**
591+
592+
593+ private PlainOSGiBundleActivator plainOSGiBundleActivator ;
594+
595+ /**
590596 * Creates the singleton instance.
591597 */
592598 public Implementation ()
593599 {
594600 super ();
595601 plugin = this ;
602+ plainOSGiBundleActivator = new PlainOSGiBundleActivator ();
596603 }
597604
598605 /**
@@ -656,10 +663,60 @@ public Implementation()
656663 public void start (BundleContext context ) throws Exception
657664 {
658665 super .start (context );
659- ExtensionProcessor .internalProcessExtensions ();
666+ try {
667+ ExtensionProcessor .internalProcessExtensions ();
668+ } catch (NoClassDefFoundError e ) {
669+ // If EMF runs in a non Equinox OSGi Framework, it occasionally runs in some NoClassDefFoundError Exceptions, that can safely be ignored.
670+ }
671+ plainOSGiBundleActivator .start (context );
672+ }
673+
674+ /*
675+ * (non-Javadoc)
676+ * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
677+ */
678+ @ Override
679+ public void stop (BundleContext context ) throws Exception {
680+ super .stop (context );
681+ plainOSGiBundleActivator .stop (context );
682+ }
683+
684+ /**
685+ * In Case we are not running in Eclipse, this BundleActivator will be used
686+ * @since 2.40
687+ */
688+ public static class PlainOSGiBundleActivator implements BundleActivator {
689+
690+ private ServiceTracker <EPackage .Registry , Object > osgiEpackageRegistryTracker = null ;
660691
692+ /*
693+ * (non-Javadoc)
694+ * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
695+ */
696+ @ SuppressWarnings ("unchecked" )
697+ @ Override
698+ public void start (BundleContext context ) throws Exception {
699+ if (getDefaultRegistryImplementation () != null && getDefaultRegistryImplementation () instanceof OSGiDelegatEPackageRegistry )
700+ {
701+ osgiEpackageRegistryTracker = new ServiceTracker <>(context , OSGiDelegatEPackageRegistry .FILTER , (ServiceTrackerCustomizer <EPackage .Registry , Object >) getDefaultRegistryImplementation ());
702+ osgiEpackageRegistryTracker .open ();
703+ }
661704 }
662705
706+ /*
707+ * (non-Javadoc)
708+ * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
709+ */
710+ @ Override
711+ public void stop (BundleContext context ) throws Exception {
712+ if (osgiEpackageRegistryTracker != null )
713+ {
714+ osgiEpackageRegistryTracker .close ();
715+ }
716+ }
717+
718+ }
719+
663720 /**
664721 * @since 2.10
665722 */
@@ -668,7 +725,12 @@ public static final class Activator extends EMFPlugin.OSGiDelegatingBundleActiva
668725 @ Override
669726 protected BundleActivator createBundle ()
670727 {
671- return new Implementation ();
728+ try
729+ {
730+ return new Implementation ();
731+ } catch (Throwable t ) {
732+ return new PlainOSGiBundleActivator ();
733+ }
672734 }
673735 }
674736 }
@@ -1098,6 +1160,11 @@ public String getSymbolicName()
10981160 */
10991161 public static EPackage .Registry getDefaultRegistryImplementation ()
11001162 {
1163+ if (defaultRegistryImplementation == null && EMFPlugin .IS_OSGI_RUNNING )
1164+ {
1165+ OSGiDelegatEPackageRegistry delegatorRegsitry = new OSGiDelegatEPackageRegistry ();
1166+ defaultRegistryImplementation = delegatorRegsitry ;
1167+ }
11011168 return defaultRegistryImplementation ;
11021169 }
11031170
0 commit comments