org.eclipse.osgi_3.8.2.v20130124-134944

882 3 0
    //   246: aload 11
    //   248: athrow
    //   249: aload 8
    //   251: ifnull +84 -> 335
    //   254: aload 8
    //   256: aload 6
    //   258: iconst_2
    //   259: invokeinterface 1882 3 0
    //   264: goto +71 -> 335
    //   267: astore 7
    //   269: aload 5
    //   271: invokeinterface 1879 1 0
    //   276: goto +16 -> 292
    //   279: astore 8
    //   281: aload_0
    //   282: iconst_2
    //   283: aload_0
    //   284: getfield 1568	org/eclipse/osgi/framework/internal/core/Framework:systemBundle	Lorg/eclipse/osgi/framework/internal/core/InternalSystemBundle;
    //   287: aload 8
    //   289: invokevirtual 1755	org/eclipse/osgi/framework/internal/core/Framework:publishFrameworkEvent	(ILorg/osgi/framework/Bundle;Ljava/lang/Throwable;)V
    //   292: aload 7
    //   294: instanceof 826
    //   297: ifeq +9 -> 306
    //   300: aload 7
    //   302: checkcast 826	java/lang/SecurityException
    //   305: athrow
    //   306: aload 7
    //   308: instanceof 914
    //   311: ifeq +9 -> 320
    //   314: aload 7
    //   316: checkcast 914	org/osgi/framework/BundleException
    //   319: athrow
    //   320: new 914	org/osgi/framework/BundleException
    //   323: dup
    //   324: aload 7
    //   326: invokevirtual 1653	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   329: aload 7
    //   331: invokespecial 1839	org/osgi/framework/BundleException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   334: athrow
    //   335: aload 6
    //   337: areturn
    // Line number table:
    //   Java source line #938	-> byte code offset #0
    //   Java source line #941	-> byte code offset #13
    //   Java source line #942	-> byte code offset #22
    //   Java source line #944	-> byte code offset #37
    //   Java source line #945	-> byte code offset #48
    //   Java source line #946	-> byte code offset #53
    //   Java source line #949	-> byte code offset #63
    //   Java source line #950	-> byte code offset #68
    //   Java source line #951	-> byte code offset #74
    //   Java source line #953	-> byte code offset #94
    //   Java source line #954	-> byte code offset #114
    //   Java source line #956	-> byte code offset #137
    //   Java source line #963	-> byte code offset #149
    //   Java source line #956	-> byte code offset #150
    //   Java source line #964	-> byte code offset #154
    //   Java source line #965	-> byte code offset #159
    //   Java source line #969	-> byte code offset #165
    //   Java source line #970	-> byte code offset #174
    //   Java source line #971	-> byte code offset #182
    //   Java source line #972	-> byte code offset #187
    //   Java source line #973	-> byte code offset #197
    //   Java source line #974	-> byte code offset #205
    //   Java source line #973	-> byte code offset #211
    //   Java source line #976	-> byte code offset #221
    //   Java source line #977	-> byte code offset #226
    //   Java source line #978	-> byte code offset #229
    //   Java source line #979	-> byte code offset #231
    //   Java source line #980	-> byte code offset #236
    //   Java source line #981	-> byte code offset #246
    //   Java source line #979	-> byte code offset #249
    //   Java source line #980	-> byte code offset #254
    //   Java source line #983	-> byte code offset #264
    //   Java source line #985	-> byte code offset #269
    //   Java source line #986	-> byte code offset #276
    //   Java source line #987	-> byte code offset #281
    //   Java source line #989	-> byte code offset #292
    //   Java source line #990	-> byte code offset #300
    //   Java source line #991	-> byte code offset #306
    //   Java source line #992	-> byte code offset #314
    //   Java source line #993	-> byte code offset #320
    //   Java source line #995	-> byte code offset #335
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	338	0	this	Framework
    //   0	338	1	location	String
    //   0	338	2	source	URLConnection
    //   0	338	3	callerContext	AccessControlContext
    //   0	338	4	origin	BundleContextImpl
    //   11	259	5	storage	org.eclipse.osgi.framework.adaptor.BundleOperation
    //   35	222	6	bundle	AbstractBundle
    //   335	1	6	bundle	AbstractBundle
    //   20	55	7	bundledata	BundleData
    //   267	63	7	t	Throwable
    //   46	209	8	bundleStats	org.eclipse.osgi.framework.adaptor.BundleWatcher
    //   279	9	8	ee	BundleException
    //   92	53	9	extension	boolean
    //   185	42	9	error	Throwable
    //   157	3	10	e	PrivilegedActionException
    //   202	16	10	Ljava/lang/Object;	Object
    //   229	18	11	localObject1	Object
    // Exception table:
    //   from	to	target	type
    //   137	154	157	java/security/PrivilegedActionException
    //   63	182	185	java/lang/Throwable
    //   205	214	217	finally
    //   217	220	217	finally
    //   63	229	229	finally
    //   13	264	267	java/lang/Throwable
    //   269	276	279	org/osgi/framework/BundleException
  }
  
  /* Error */
  public AbstractBundle getBundle(long id)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   4: dup
    //   5: astore_3
    //   6: monitorenter
    //   7: aload_0
    //   8: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: lload_1
    //   12: invokevirtual 1724	org/eclipse/osgi/framework/internal/core/BundleRepository:getBundle	(J)Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;
    //   15: aload_3
    //   16: monitorexit
    //   17: areturn
    //   18: aload_3
    //   19: monitorexit
    //   20: athrow
    // Line number table:
    //   Java source line #1008	-> byte code offset #0
    //   Java source line #1009	-> byte code offset #7
    //   Java source line #1008	-> byte code offset #18
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	21	0	this	Framework
    //   0	21	1	id	long
    //   5	14	3	Ljava/lang/Object;	Object
    // Exception table:
    //   from	to	target	type
    //   7	17	18	finally
    //   18	20	18	finally
  }
  
  AbstractBundle getBundle(BundleContextImpl context, long id)
  {
    AbstractBundle bundle = getBundle(id);
    if ((bundle == null) || (context.getBundle().getBundleId() == 0L)) {
      return bundle;
    }
    List<AbstractBundle> single = new ArrayList(1);
    single.add(bundle);
    notifyFindHooks(context, single);
    return single.size() == 0 ? null : bundle;
  }
  
  public BundleContextImpl getSystemBundleContext()
  {
    if (systemBundle == null) {
      return null;
    }
    return systemBundle.context;
  }
  
  public PackageAdminImpl getPackageAdmin()
  {
    return packageAdmin;
  }
  
  /* Error */
  public List<AbstractBundle> getBundleBySymbolicName(String symbolicName, Version version)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   4: dup
    //   5: astore_3
    //   6: monitorenter
    //   7: aload_0
    //   8: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: aload_1
    //   12: aload_2
    //   13: invokevirtual 1728	org/eclipse/osgi/framework/internal/core/BundleRepository:getBundles	(Ljava/lang/String;Lorg/osgi/framework/Version;)Ljava/util/List;
    //   16: aload_3
    //   17: monitorexit
    //   18: areturn
    //   19: aload_3
    //   20: monitorexit
    //   21: athrow
    // Line number table:
    //   Java source line #1044	-> byte code offset #0
    //   Java source line #1045	-> byte code offset #7
    //   Java source line #1044	-> byte code offset #19
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	22	0	this	Framework
    //   0	22	1	symbolicName	String
    //   0	22	2	version	Version
    //   5	15	3	Ljava/lang/Object;	Object
    // Exception table:
    //   from	to	target	type
    //   7	18	19	finally
    //   19	21	19	finally
  }
  
  protected BundleRepository getBundles()
  {
    return bundles;
  }
  
  protected AbstractBundle[] getAllBundles()
  {
    synchronized (bundles)
    {
      List<AbstractBundle> allBundles = bundles.getBundles();
      int size = allBundles.size();
      if (size == 0) {
        return null;
      }
      AbstractBundle[] bundlelist = new AbstractBundle[size];
      allBundles.toArray(bundlelist);
      return bundlelist;
    }
  }
  
  AbstractBundle[] getBundles(BundleContextImpl context)
  {
    List<AbstractBundle> allBundles;
    synchronized (bundles)
    {
      allBundles = new ArrayList(bundles.getBundles());
    }
    List<AbstractBundle> allBundles;
    notifyFindHooks(context, allBundles);
    return (AbstractBundle[])allBundles.toArray(new AbstractBundle[allBundles.size()]);
  }
  
  private void notifyFindHooks(final BundleContextImpl context, List<AbstractBundle> allBundles)
  {
    final Collection<Bundle> shrinkable = new ShrinkableCollection(allBundles);
    if (System.getSecurityManager() == null) {
      notifyFindHooksPriviledged(context, shrinkable);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          notifyFindHooksPriviledged(context, shrinkable);
          return null;
        }
      });
    }
  }
  
  void notifyFindHooksPriviledged(final BundleContextImpl context, final Collection<Bundle> allBundles)
  {
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyBundleFindHooks(" + allBundles + ")");
    }
    getServiceRegistry().notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof FindHook)) {
          ((FindHook)hook).find(context, allBundles);
        }
      }
      
      public String getHookClassName()
      {
        return Framework.findHookName;
      }
      
      public String getHookMethodName()
      {
        return "find";
      }
    });
  }
  
  private void notifyCollisionHooks(final int operationType, final Bundle target, List<AbstractBundle> collisionCandidates)
  {
    final Collection<Bundle> shrinkable = new ShrinkableCollection(collisionCandidates);
    if (System.getSecurityManager() == null) {
      notifyCollisionHooksPriviledged(operationType, target, shrinkable);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          notifyCollisionHooksPriviledged(operationType, target, shrinkable);
          return null;
        }
      });
    }
  }
  
  void notifyCollisionHooksPriviledged(final int operationType, final Bundle target, final Collection<Bundle> collisionCandidates)
  {
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyCollisionHooks(" + operationType + ", " + target + ", " + collisionCandidates + ")");
    }
    getServiceRegistry().notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof CollisionHook)) {
          ((CollisionHook)hook).filterCollisions(operationType, target, collisionCandidates);
        }
      }
      
      public String getHookClassName()
      {
        return Framework.collisionHookName;
      }
      
      public String getHookMethodName()
      {
        return "filterCollisions";
      }
    });
  }
  
  protected void resumeBundle(AbstractBundle bundle)
  {
    if (bundle.isActive()) {
      return;
    }
    try
    {
      if (Debug.DEBUG_GENERAL) {
        Debug.println("Trying to resume bundle " + bundle);
      }
      bundle.resume();
    }
    catch (BundleException be)
    {
      if (Debug.DEBUG_GENERAL)
      {
        Debug.println("Bundle resume exception: " + be.getMessage());
        Debug.printStackTrace(be.getNestedException() == null ? be : be.getNestedException());
      }
      publishFrameworkEvent(2, bundle, be);
    }
  }
  
  protected boolean suspendBundle(AbstractBundle bundle, boolean lock)
  {
    boolean changed = false;
    if ((!bundle.isActive()) || (bundle.isFragment())) {
      return changed;
    }
    try
    {
      if (Debug.DEBUG_GENERAL) {
        Debug.println("Trying to suspend bundle " + bundle);
      }
      bundle.suspend(lock);
    }
    catch (BundleException be)
    {
      if (Debug.DEBUG_GENERAL)
      {
        Debug.println("Bundle suspend exception: " + be.getMessage());
        Debug.printStackTrace(be.getNestedException() == null ? be : be.getNestedException());
      }
      publishFrameworkEvent(2, bundle, be);
    }
    if (!bundle.isActive()) {
      changed = true;
    }
    return changed;
  }
  
  protected AbstractBundle getBundleByLocation(String location)
  {
    synchronized (bundles)
    {
      final String finalLocation = location;
      
      (AbstractBundle)AccessController.doPrivileged(new PrivilegedAction()
      {
        public AbstractBundle run()
        {
          List<AbstractBundle> allBundles = bundles.getBundles();
          int size = allBundles.size();
          for (int i = 0; i < size; i++)
          {
            AbstractBundle bundle = (AbstractBundle)allBundles.get(i);
            if (finalLocation.equals(bundle.getLocation())) {
              return bundle;
            }
          }
          return null;
        }
      });
    }
  }
  
  /* Error */
  protected AbstractBundle[] getBundleBySymbolicName(String symbolicName)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   4: dup
    //   5: astore_2
    //   6: monitorenter
    //   7: aload_0
    //   8: getfield 1567	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: aload_1
    //   12: invokevirtual 1727	org/eclipse/osgi/framework/internal/core/BundleRepository:getBundles	(Ljava/lang/String;)[Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;
    //   15: aload_2
    //   16: monitorexit
    //   17: areturn
    //   18: aload_2
    //   19: monitorexit
    //   20: athrow
    // Line number table:
    //   Java source line #1260	-> byte code offset #0
    //   Java source line #1261	-> byte code offset #7
    //   Java source line #1260	-> byte code offset #18
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	21	0	this	Framework
    //   0	21	1	symbolicName	String
    //   5	14	2	Ljava/lang/Object;	Object
    // Exception table:
    //   from	to	target	type
    //   7	17	18	finally
    //   18	20	18	finally
  }
  
  protected File getDataFile(AbstractBundle bundle, String filename)
  {
    return (File)AccessController.doPrivileged(new GetDataFileAction(bundle, filename));
  }
  
  protected void checkAdminPermission(Bundle bundle, String action)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
      sm.checkPermission(getAdminPermission(bundle, action));
    }
  }
  
  private AdminPermission getAdminPermission(Bundle bundle, String action)
  {
    synchronized (adminPermissions)
    {
      Long ID = new Long(bundle.getBundleId());
      Map<String, AdminPermission> bundlePermissions = (Map)adminPermissions.get(ID);
      if (bundlePermissions == null)
      {
        bundlePermissions = new HashMap();
        adminPermissions.put(ID, bundlePermissions);
      }
      AdminPermission result = (AdminPermission)bundlePermissions.get(action);
      if (result == null)
      {
        result = new AdminPermission(bundle, action);
        bundlePermissions.put(action, result);
      }
      return result;
    }
  }
  
  protected void installSecurityManager()
  {
    String securityManager = FrameworkProperties.getProperty("org.osgi.framework.security", FrameworkProperties.getProperty("eclipse.security", FrameworkProperties.getProperty("java.security.manager")));
    if (securityManager != null)
    {
      SecurityManager sm = System.getSecurityManager();
      if (sm == null)
      {
        if (securityManager.length() == 0) {
          sm = new SecurityManager();
        } else if (securityManager.equals("osgi")) {
          sm = new EquinoxSecurityManager();
        } else {
          try
          {
            Class<?> clazz = Class.forName(securityManager);
            sm = (SecurityManager)clazz.newInstance();
          }
          catch (ClassNotFoundException localClassNotFoundException) {}catch (ClassCastException localClassCastException) {}catch (InstantiationException localInstantiationException) {}catch (IllegalAccessException localIllegalAccessException) {}
        }
        if (sm == null) {
          throw new NoClassDefFoundError(securityManager);
        }
        if (Debug.DEBUG_SECURITY) {
          Debug.println("Setting SecurityManager to: " + sm);
        }
        System.setSecurityManager(sm);
        return;
      }
    }
  }
  
  void addFrameworkListener(FrameworkListener listener, BundleContextImpl context)
  {
    synchronized (allFrameworkListeners)
    {
      CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener> listeners = (CopyOnWriteIdentityMap)allFrameworkListeners.get(context);
      if (listeners == null)
      {
        listeners = new CopyOnWriteIdentityMap();
        allFrameworkListeners.put(context, listeners);
      }
      listeners.put(listener, listener);
    }
  }
  
  void removeFrameworkListener(FrameworkListener listener, BundleContextImpl context)
  {
    synchronized (allFrameworkListeners)
    {
      CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener> listeners = (CopyOnWriteIdentityMap)allFrameworkListeners.get(context);
      if (listeners != null) {
        listeners.remove(listener);
      }
    }
  }
  
  void removeAllListeners(BundleContextImpl context)
  {
    synchronized (allBundleListeners)
    {
      allBundleListeners.remove(context);
    }
    synchronized (allSyncBundleListeners)
    {
      allSyncBundleListeners.remove(context);
    }
    synchronized (allFrameworkListeners)
    {
      allFrameworkListeners.remove(context);
    }
  }
  
  public void publishFrameworkEvent(int type, Bundle bundle, Throwable throwable)
  {
    publishFrameworkEvent(type, bundle, throwable, null);
  }
  
  public void publishFrameworkEvent(int type, Bundle bundle, Throwable throwable, final FrameworkListener[] listeners)
  {
    if (bundle == null) {
      bundle = systemBundle;
    }
    final FrameworkEvent event = new FrameworkEvent(type, bundle, throwable);
    if (System.getSecurityManager() == null) {
      publishFrameworkEventPrivileged(event, listeners);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          publishFrameworkEventPrivileged(event, listeners);
          return null;
        }
      });
    }
  }
  
  public void publishFrameworkEventPrivileged(FrameworkEvent event, FrameworkListener[] callerListeners)
  {
    CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener> listeners;
    synchronized (allFrameworkListeners)
    {
      Map<BundleContextImpl, Set<Map.Entry<FrameworkListener, FrameworkListener>>> listenerSnapshot = new HashMap(allFrameworkListeners.size());
      for (Iterator localIterator = allFrameworkListeners.entrySet().iterator(); localIterator.hasNext();)
      {
        Map.Entry<BundleContextImpl, CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener>> entry = (Map.Entry)localIterator.next();
        listeners = (CopyOnWriteIdentityMap)entry.getValue();
        if (!listeners.isEmpty()) {
          listenerSnapshot.put((BundleContextImpl)entry.getKey(), listeners.entrySet());
        }
      }
    }
    Map<BundleContextImpl, Set<Map.Entry<FrameworkListener, FrameworkListener>>> listenerSnapshot;
    ListenerQueue<FrameworkListener, FrameworkListener, FrameworkEvent> queue = newListenerQueue();
    if ((callerListeners != null) && (callerListeners.length > 0))
    {
      Map<FrameworkListener, FrameworkListener> listeners = new HashMap();
      FrameworkListener[] arrayOfFrameworkListener;
      CopyOnWriteIdentityMap<FrameworkListener, FrameworkListener> localCopyOnWriteIdentityMap1 = (arrayOfFrameworkListener = callerListeners).length;
      for (listeners = 0; listeners < localCopyOnWriteIdentityMap1; listeners++)
      {
        FrameworkListener listener = arrayOfFrameworkListener[listeners];
        if (listener != null) {
          listeners.put(listener, listener);
        }
      }
      if (listeners.size() > 0)
      {
        dispatcher = getSystemBundleContext();
        queue.queueListeners(listeners.entrySet(), (EventDispatcher)dispatcher);
      }
    }
    for (Object dispatcher = listenerSnapshot.entrySet().iterator(); ((Iterator)dispatcher).hasNext();)
    {
      Map.Entry<BundleContextImpl, Set<Map.Entry<FrameworkListener, FrameworkListener>>> entry = (Map.Entry)((Iterator)dispatcher).next();
      
      EventDispatcher<FrameworkListener, FrameworkListener, FrameworkEvent> dispatcher = (EventDispatcher)entry.getKey();
      Object listeners = (Set)entry.getValue();
      queue.queueListeners((Set)listeners, dispatcher);
    }
    queue.dispatchEventAsynchronous(4, event);
  }
  
  void addBundleListener(BundleListener listener, BundleContextImpl context)
  {
    if ((listener instanceof SynchronousBundleListener))
    {
      checkAdminPermission(context.getBundle(), "listener");
      synchronized (allSyncBundleListeners)
      {
        CopyOnWriteIdentityMap<SynchronousBundleListener, SynchronousBundleListener> listeners = (CopyOnWriteIdentityMap)allSyncBundleListeners.get(context);
        if (listeners == null)
        {
          listeners = new CopyOnWriteIdentityMap();
          allSyncBundleListeners.put(context, listeners);
        }
        listeners.put((SynchronousBundleListener)listener, (SynchronousBundleListener)listener);
      }
    }
    synchronized (allBundleListeners)
    {
      CopyOnWriteIdentityMap<BundleListener, BundleListener> listeners = (CopyOnWriteIdentityMap)allBundleListeners.get(context);
      if (listeners == null)
      {
        listeners = new CopyOnWriteIdentityMap();
        allBundleListeners.put(context, listeners);
      }
      listeners.put(listener, listener);
    }
  }
  
  void removeBundleListener(BundleListener listener, BundleContextImpl context)
  {
    if ((listener instanceof SynchronousBundleListener))
    {
      checkAdminPermission(context.getBundle(), "listener");
      synchronized (allSyncBundleListeners)
      {
        CopyOnWriteIdentityMap<SynchronousBundleListener, SynchronousBundleListener> listeners = (CopyOnWriteIdentityMap)allSyncBundleListeners.get(context);
        if (listeners != null) {
          listeners.remove(listener);
        }
      }
    }
    synchronized (allBundleListeners)
    {
      CopyOnWriteIdentityMap<BundleListener, BundleListener> listeners = (CopyOnWriteIdentityMap)allBundleListeners.get(context);
      if (listeners != null) {
        listeners.remove(listener);
      }
    }
  }
  
  public void publishBundleEvent(int type, Bundle bundle)
  {
    publishBundleEvent(new BundleEvent(type, bundle));
  }
  
  private void publishBundleEvent(final BundleEvent event)
  {
    if (System.getSecurityManager() == null) {
      publishBundleEventPrivileged(event);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          publishBundleEventPrivileged(event);
          return null;
        }
      });
    }
  }
  
  public void publishBundleEventPrivileged(BundleEvent event)
  {
    CopyOnWriteIdentityMap<SynchronousBundleListener, SynchronousBundleListener> listeners;
    synchronized (allSyncBundleListeners)
    {
      Map<BundleContextImpl, Set<Map.Entry<SynchronousBundleListener, SynchronousBundleListener>>> listenersSync = new HashMap(allSyncBundleListeners.size());
      for (Iterator localIterator = allSyncBundleListeners.entrySet().iterator(); localIterator.hasNext();)
      {
        Map.Entry<BundleContextImpl, CopyOnWriteIdentityMap<SynchronousBundleListener, SynchronousBundleListener>> entry = (Map.Entry)localIterator.next();
        listeners = (CopyOnWriteIdentityMap)entry.getValue();
        if (!listeners.isEmpty()) {
          listenersSync.put((BundleContextImpl)entry.getKey(), listeners.entrySet());
        }
      }
    }
    Map<BundleContextImpl, Set<Map.Entry<SynchronousBundleListener, SynchronousBundleListener>>> listenersSync;
    Map<BundleContextImpl, Set<Map.Entry<BundleListener, BundleListener>>> listenersAsync = null;
    CopyOnWriteIdentityMap<BundleListener, BundleListener> listeners;
    if ((event.getType() & 0x380) == 0) {
      synchronized (allBundleListeners)
      {
        listenersAsync = new HashMap(allBundleListeners.size());
        for (listeners = allBundleListeners.entrySet().iterator(); listeners.hasNext();)
        {
          Object entry = (Map.Entry)listeners.next();
          listeners = (CopyOnWriteIdentityMap)((Map.Entry)entry).getValue();
          if (!listeners.isEmpty()) {
            listenersAsync.put((BundleContextImpl)((Map.Entry)entry).getKey(), listeners.entrySet());
          }
        }
      }
    }
    Collection<BundleContext> shrinkable;
    Collection<BundleContext> shrinkable;
    if (listenersAsync == null) {
      shrinkable = asBundleContexts(listenersSync.keySet());
    } else {
      shrinkable = new ShrinkableCollection(asBundleContexts(listenersSync.keySet()), asBundleContexts(listenersAsync.keySet()));
    }
    notifyEventHooksPrivileged(event, shrinkable);
    if (!listenersSync.isEmpty())
    {
      Object queue = newListenerQueue();
      for (listeners = listenersSync.entrySet().iterator(); listeners.hasNext();)
      {
        Map.Entry<BundleContextImpl, Set<Map.Entry<SynchronousBundleListener, SynchronousBundleListener>>> entry = (Map.Entry)listeners.next();
        
        EventDispatcher<SynchronousBundleListener, SynchronousBundleListener, BundleEvent> dispatcher = (EventDispatcher)entry.getKey();
        Set<Map.Entry<SynchronousBundleListener, SynchronousBundleListener>> listeners = (Set)entry.getValue();
        ((ListenerQueue)queue).queueListeners(listeners, dispatcher);
      }
      ((ListenerQueue)queue).dispatchEventSynchronous(2, event);
    }
    if ((listenersAsync != null) && (!listenersAsync.isEmpty()))
    {
      Object queue = newListenerQueue();
      for (listeners = listenersAsync.entrySet().iterator(); listeners.hasNext();)
      {
        Map.Entry<BundleContextImpl, Set<Map.Entry<BundleListener, BundleListener>>> entry = (Map.Entry)listeners.next();
        
        EventDispatcher<BundleListener, BundleListener, BundleEvent> dispatcher = (EventDispatcher)entry.getKey();
        Set<Map.Entry<BundleListener, BundleListener>> listeners = (Set)entry.getValue();
        ((ListenerQueue)queue).queueListeners(listeners, dispatcher);
      }
      ((ListenerQueue)queue).dispatchEventAsynchronous(1, event);
    }
  }
  
  public static Collection<BundleContext> asBundleContexts(Collection<? extends BundleContext> c)
  {
    return c;
  }
  
  private void notifyEventHooksPrivileged(final BundleEvent event, final Collection<BundleContext> result)
  {
    if ((event.getType() == -2147483647) || (event.getType() == Integer.MIN_VALUE)) {
      return;
    }
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyBundleEventHooks(" + event.getType() + ":" + event.getBundle() + ", " + result + " )");
    }
    getServiceRegistry().notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof EventHook)) {
          ((EventHook)hook).event(event, result);
        }
      }
      
      public String getHookClassName()
      {
        return Framework.eventHookName;
      }
      
      public String getHookMethodName()
      {
        return "event";
      }
    });
  }
  
  public <K, V, E> ListenerQueue<K, V, E> newListenerQueue()
  {
    return new ListenerQueue(eventManager);
  }
  
  private void initializeContextFinder()
  {
    Thread current = Thread.currentThread();
    try
    {
      ClassLoader parent = null;
      
      String type = FrameworkProperties.getProperty("osgi.contextClassLoaderParent");
      if ("app".equals(type))
      {
        parent = ClassLoader.getSystemClassLoader();
      }
      else if ("boot".equals(type))
      {
        parent = null;
      }
      else if ("fwk".equals(type))
      {
        parent = Framework.class.getClassLoader();
      }
      else if ("ext".equals(type))
      {
        ClassLoader appCL = ClassLoader.getSystemClassLoader();
        if (appCL != null) {
          parent = appCL.getParent();
        }
      }
      else
      {
        parent = current.getContextClassLoader();
      }
      contextFinder = new ContextFinder(parent);
      current.setContextClassLoader(contextFinder);
      return;
    }
    catch (Exception e)
    {
      FrameworkLogEntry entry = new FrameworkLogEntry("org.eclipse.osgi", 1, 0, NLS.bind(Msg.CANNOT_SET_CONTEXTFINDER, null), 0, e, null);
      adaptor.getFrameworkLog().log(entry);
    }
  }
  
  public static Field getField(Class<?> clazz, Class<?> type, boolean instance)
  {
    Field[] fields = clazz.getDeclaredFields();
    for (int i = 0; i < fields.length; i++)
    {
      boolean isStatic = Modifier.isStatic(fields[i].getModifiers());
      if ((instance != isStatic) && (fields[i].getType().equals(type)))
      {
        fields[i].setAccessible(true);
        return fields[i];
      }
    }
    return null;
  }
  
  private void installContentHandlerFactory(BundleContext context, FrameworkAdaptor frameworkAdaptor)
  {
    org.eclipse.osgi.framework.internal.protocol.ContentHandlerFactory chf = new org.eclipse.osgi.framework.internal.protocol.ContentHandlerFactory(context, frameworkAdaptor);
    try
    {
      URLConnection.setContentHandlerFactory(chf);
    }
    catch (Error err)
    {
      try
      {
        forceContentHandlerFactory(chf);
      }
      catch (Exception ex)
      {
        adaptor.getFrameworkLog().log(new FrameworkEvent(2, context.getBundle(), ex));
        throw err;
      }
    }
    contentHandlerFactory = chf;
  }
  
  private static void forceContentHandlerFactory(org.eclipse.osgi.framework.internal.protocol.ContentHandlerFactory chf)
    throws Exception
  {
    Field factoryField = getField(URLConnection.class, java.net.ContentHandlerFactory.class, false);
    if (factoryField == null) {
      throw new Exception("Could not find ContentHandlerFactory field");
    }
    synchronized (URLConnection.class)
    {
      java.net.ContentHandlerFactory factory = (java.net.ContentHandlerFactory)factoryField.get(null);
      if (factory != null) {
        try
        {
          factory.getClass().getMethod("isMultiplexing", null);
          Method register = factory.getClass().getMethod("register", new Class[] { Object.class });
          register.invoke(factory, new Object[] { chf });
        }
        catch (NoSuchMethodException localNoSuchMethodException)
        {
          chf.setParentFactory(factory);
          factory = chf;
        }
      }
      factoryField.set(null, null);
      
      resetContentHandlers();
      URLConnection.setContentHandlerFactory(factory);
    }
  }
  
  private void uninstallContentHandlerFactory()
  {
    try
    {
      Field factoryField = getField(URLConnection.class, java.net.ContentHandlerFactory.class, false);
      if (factoryField == null) {
        return;
      }
      synchronized (URLConnection.class)
      {
        java.net.ContentHandlerFactory factory = (java.net.ContentHandlerFactory)factoryField.get(null);
        if (factory == contentHandlerFactory)
        {
          factory = (java.net.ContentHandlerFactory)contentHandlerFactory.designateSuccessor();
        }
        else
        {
          Method unregister = factory.getClass().getMethod("unregister", new Class[] { Object.class });
          unregister.invoke(factory, new Object[] { contentHandlerFactory });
        }
        factoryField.set(null, null);
        
        resetContentHandlers();
        if (factory != null) {
          URLConnection.setContentHandlerFactory(factory);
        }
      }
      return;
    }
    catch (Exception localException) {}
  }
  
  private static void resetContentHandlers()
    throws IllegalAccessException
  {
    Field handlersField = getField(URLConnection.class, Hashtable.class, false);
    if (handlersField != null)
    {
      Hashtable<?, ?> handlers = (Hashtable)handlersField.get(null);
      if (handlers != null) {
        handlers.clear();
      }
    }
  }
  
  private void installURLStreamHandlerFactory(BundleContext context, FrameworkAdaptor frameworkAdaptor)
  {
    StreamHandlerFactory shf = new StreamHandlerFactory(context, frameworkAdaptor);
    try
    {
      URL.setURLStreamHandlerFactory(shf);
    }
    catch (Error err)
    {
      try
      {
        forceURLStreamHandlerFactory(shf);
      }
      catch (Exception ex)
      {
        adaptor.getFrameworkLog().log(new FrameworkEvent(2, context.getBundle(), ex));
        throw err;
      }
    }
    streamHandlerFactory = shf;
  }
  
  private static void forceURLStreamHandlerFactory(StreamHandlerFactory shf)
    throws Exception
  {
    Field factoryField = getField(URL.class, URLStreamHandlerFactory.class, false);
    if (factoryField == null) {
      throw new Exception("Could not find URLStreamHandlerFactory field");
    }
    Object lock = getURLStreamHandlerFactoryLock();
    synchronized (lock)
    {
      URLStreamHandlerFactory factory = (URLStreamHandlerFactory)factoryField.get(null);
      if (factory != null) {
        try
        {
          factory.getClass().getMethod("isMultiplexing", null);
          Method register = factory.getClass().getMethod("register", new Class[] { Object.class });
          register.invoke(factory, new Object[] { shf });
        }
        catch (NoSuchMethodException localNoSuchMethodException)
        {
          shf.setParentFactory(factory);
          factory = shf;
        }
      }
      factoryField.set(null, null);
      
      resetURLStreamHandlers();
      URL.setURLStreamHandlerFactory(factory);
    }
  }
  
  private void uninstallURLStreamHandlerFactory()
  {
    try
    {
      Field factoryField = getField(URL.class, URLStreamHandlerFactory.class, false);
      if (factoryField == null) {
        return;
      }
      Object lock = getURLStreamHandlerFactoryLock();
      synchronized (lock)
      {
        URLStreamHandlerFactory factory = (URLStreamHandlerFactory)factoryField.get(null);
        if (factory == streamHandlerFactory)
        {
          factory = (URLStreamHandlerFactory)streamHandlerFactory.designateSuccessor();
        }
        else
        {
          Method unregister = factory.getClass().getMethod("unregister", new Class[] { Object.class });
          unregister.invoke(factory, new Object[] { streamHandlerFactory });
        }
        factoryField.set(null, null);
        
        resetURLStreamHandlers();
        if (factory != null) {
          URL.setURLStreamHandlerFactory(factory);
        }
      }
      return;
    }
    catch (Exception localException) {}
  }
  
  private static Object getURLStreamHandlerFactoryLock()
    throws IllegalAccessException
  {
    Object lock;
    try
    {
      Field streamHandlerLockField = URL.class.getDeclaredField("streamHandlerLock");
      streamHandlerLockField.setAccessible(true);
      lock = streamHandlerLockField.get(null);
    }
    catch (NoSuchFieldException localNoSuchFieldException)
    {
      Object lock;
      lock = URL.class;
    }
    return lock;
  }
  
  private static void resetURLStreamHandlers()
    throws IllegalAccessException
  {
    Field handlersField = getField(URL.class, Hashtable.class, false);
    if (handlersField != null)
    {
      Hashtable<?, ?> handlers = (Hashtable)handlersField.get(null);
      if (handlers != null) {
        handlers.clear();
      }
    }
  }
  
  public void run()
  {
    synchronized (this)
    {
      while (active) {
        try
        {
          wait(1000L);
        }
        catch (InterruptedException localInterruptedException) {}
      }
    }
  }
  
  void setForcedRestart(boolean forcedRestart)
  {
    this.forcedRestart = forcedRestart;
  }
  
  boolean isForcedRestart()
  {
    return forcedRestart;
  }
  
  public FrameworkEvent waitForStop(long timeout)
    throws InterruptedException
  {
    boolean waitForEver = timeout == 0L;
    long start = System.currentTimeMillis();
    long timeLeft = timeout;
    synchronized (this)
    {
      FrameworkEvent[] event = shutdownEvent;
      while ((event != null) && (event[0] == null))
      {
        wait(timeLeft);
        if (!waitForEver)
        {
          timeLeft = start + timeout - System.currentTimeMillis();
          if (timeLeft <= 0L) {
            break;
          }
        }
      }
      if ((event == null) || (event[0] == null)) {
        return new FrameworkEvent(512, systemBundle, null);
      }
      return event[0];
    }
  }
  
  public boolean isServiceAssignableTo(Bundle registrant, Bundle client, String className, Class<?> serviceClass)
  {
    AbstractBundle consumer = (AbstractBundle)client;
    if (consumer.isFragment()) {
      return false;
    }
    AbstractBundle producer = (AbstractBundle)registrant;
    if (consumer == producer) {
      return true;
    }
    String pkgName = BundleLoader.getPackageName(className);
    if (pkgName.startsWith("java.")) {
      return true;
    }
    BundleLoader producerBL = producer.getBundleLoader();
    if (producerBL == null) {
      return false;
    }
    BundleLoader consumerBL = consumer.getBundleLoader();
    if (consumerBL == null) {
      return false;
    }
    PackageSource consumerSource = consumerBL.getPackageSource(pkgName);
    if (consumerSource == null) {
      return true;
    }
    if (isBootDelegationPackage(pkgName))
    {
      SystemBundleLoader systemLoader = (SystemBundleLoader)systemBundle.getBundleLoader();
      if (systemLoader.isEEPackage(pkgName)) {
        return true;
      }
    }
    PackageSource producerSource = producerBL.getPackageSource(pkgName);
    if (producerSource == null)
    {
      if ((serviceClass != null) && (ServiceFactory.class.isAssignableFrom(serviceClass)))
      {
        Bundle bundle = packageAdmin.getBundle(serviceClass);
        if ((bundle != null) && (bundle != registrant)) {
          return true;
        }
      }
      producerSource = getPackageSource(serviceClass, pkgName);
      if (producerSource == null) {
        return false;
      }
    }
    return producerSource.hasCommonSource(consumerSource);
  }
  
  private PackageSource getPackageSource(Class<?> serviceClass, String pkgName)
  {
    if (serviceClass == null) {
      return null;
    }
    AbstractBundle serviceBundle = (AbstractBundle)packageAdmin.getBundle(serviceClass);
    if (serviceBundle == null) {
      return null;
    }
    BundleLoader producerBL = serviceBundle.getBundleLoader();
    if (producerBL == null) {
      return null;
    }
    PackageSource producerSource = producerBL.getPackageSource(pkgName);
    if (producerSource != null) {
      return producerSource;
    }
    Class[] interfaces = serviceClass.getInterfaces();
    for (int i = 0; i < interfaces.length; i++)
    {
      producerSource = getPackageSource(interfaces[
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd