org.eclipse.osgi_3.7.2.v20120110-1415

 PrivilegedExceptionAction<AbstractBundle> action, BundleContext origin)
    throws BundleException
  {
    Thread current;
    synchronized (installLock)
    {
      for (;;)
      {
        bundle = getBundleByLocation(location);
        if (bundle != null)
        {
          Bundle visible = origin.getBundle(bundle.getBundleId());
          if (visible == null)
          {
            BundleData data = bundle.getBundleData();
            String msg = NLS.bind(Msg.BUNDLE_INSTALL_SAME_UNIQUEID, new Object[] { data.getSymbolicName(), data.getVersion().toString(), data.getLocation() });
            throw new BundleException(msg, 12);
          }
          return bundle;
        }
        current = Thread.currentThread();
        
        Thread reservation = (Thread)installLock.put(location, current);
        if (reservation != null)
        {
          if (current.equals(reservation)) {
            throw new BundleException(Msg.BUNDLE_INSTALL_RECURSION_EXCEPTION, 7);
          }
          try
          {
            installLock.wait();
          }
          catch (InterruptedException e)
          {
            Thread.currentThread().interrupt();
            throw new BundleException("Thread has been interrupted while waiting for the location lock.", e);
          }
        }
      }
    }
    try
    {
      AbstractBundle bundle = (AbstractBundle)AccessController.doPrivileged(action);
      publishBundleEvent(new BundleEvent(1, bundle, origin.getBundle()));
      return bundle;
    }
    catch (PrivilegedActionException e)
    {
      if ((e.getException() instanceof RuntimeException)) {
        throw ((RuntimeException)e.getException());
      }
      throw ((BundleException)e.getException());
    }
    finally
    {
      synchronized (installLock)
      {
        installLock.remove(location);
        
        installLock.notifyAll();
      }
    }
  }
  
  /* Error */
  protected AbstractBundle installWorkerPrivileged(String location, URLConnection source, AccessControlContext callerContext)
    throws BundleException
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1532	org/eclipse/osgi/framework/internal/core/Framework:adaptor	Lorg/eclipse/osgi/framework/adaptor/FrameworkAdaptor;
    //   4: aload_1
    //   5: aload_2
    //   6: invokeinterface 1859 3 0
    //   11: astore 4
    //   13: aload 4
    //   15: invokeinterface 1848 1 0
    //   20: astore 6
    //   22: aload_0
    //   23: aload 6
    //   25: iconst_1
    //   26: invokevirtual 1736	org/eclipse/osgi/framework/internal/core/Framework:createAndVerifyBundle	(Lorg/eclipse/osgi/framework/adaptor/BundleData;Z)Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;
    //   29: astore 5
    //   31: aload_0
    //   32: getfield 1532	org/eclipse/osgi/framework/internal/core/Framework:adaptor	Lorg/eclipse/osgi/framework/adaptor/FrameworkAdaptor;
    //   35: invokeinterface 1854 1 0
    //   40: astore 7
    //   42: aload 7
    //   44: ifnull +13 -> 57
    //   47: aload 7
    //   49: aload 5
    //   51: iconst_1
    //   52: invokeinterface 1849 3 0
    //   57: aload 5
    //   59: invokevirtual 1675	org/eclipse/osgi/framework/internal/core/AbstractBundle:load	()V
    //   62: invokestatic 1612	java/lang/System:getSecurityManager	()Ljava/lang/SecurityManager;
    //   65: ifnull +94 -> 159
    //   68: aload 6
    //   70: invokeinterface 1840 1 0
    //   75: bipush 22
    //   77: iand
    //   78: ifeq +7 -> 85
    //   81: iconst_1
    //   82: goto +4 -> 86
    //   85: iconst_0
    //   86: istore 8
    //   88: iload 8
    //   90: ifeq +41 -> 131
    //   93: aload 5
    //   95: new 824	java/security/AllPermission
    //   98: dup
    //   99: invokespecial 1637	java/security/AllPermission:<init>	()V
    //   102: invokevirtual 1681	org/eclipse/osgi/framework/internal/core/AbstractBundle:hasPermission	(Ljava/lang/Object;)Z
    //   105: ifne +26 -> 131
    //   108: new 894	org/osgi/framework/BundleException
    //   111: dup
    //   112: getstatic 1548	org/eclipse/osgi/framework/internal/core/Msg:BUNDLE_EXTENSION_PERMISSION	Ljava/lang/String;
    //   115: bipush 6
    //   117: new 808	java/lang/SecurityException
    //   120: dup
    //   121: getstatic 1548	org/eclipse/osgi/framework/internal/core/Msg:BUNDLE_EXTENSION_PERMISSION	Ljava/lang/String;
    //   124: invokespecial 1588	java/lang/SecurityException:<init>	(Ljava/lang/String;)V
    //   127: invokespecial 1806	org/osgi/framework/BundleException:<init>	(Ljava/lang/String;ILjava/lang/Throwable;)V
    //   130: athrow
    //   131: new 859	org/eclipse/osgi/framework/internal/core/Framework$2
    //   134: dup
    //   135: aload_0
    //   136: aload 5
    //   138: iload 8
    //   140: invokespecial 1747	org/eclipse/osgi/framework/internal/core/Framework$2:<init>	(Lorg/eclipse/osgi/framework/internal/core/Framework;Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;Z)V
    //   143: aload_3
    //   144: invokestatic 1636	java/security/AccessController:doPrivileged	(Ljava/security/PrivilegedExceptionAction;Ljava/security/AccessControlContext;)Ljava/lang/Object;
    //   147: pop
    //   148: goto +11 -> 159
    //   151: astore 9
    //   153: aload 9
    //   155: invokevirtual 1638	java/security/PrivilegedActionException:getException	()Ljava/lang/Exception;
    //   158: athrow
    //   159: aload_0
    //   160: getfield 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   163: aload 5
    //   165: invokevirtual 1696	org/eclipse/osgi/framework/internal/core/BundleRepository:add	(Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;)V
    //   168: aload 4
    //   170: iconst_0
    //   171: invokeinterface 1847 2 0
    //   176: goto +67 -> 243
    //   179: astore 8
    //   181: aload_0
    //   182: getfield 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   185: aload 5
    //   187: invokevirtual 1697	org/eclipse/osgi/framework/internal/core/BundleRepository:remove	(Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;)Z
    //   190: pop
    //   191: aload_0
    //   192: getfield 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   195: dup
    //   196: astore 9
    //   198: monitorenter
    //   199: aload 5
    //   201: invokevirtual 1679	org/eclipse/osgi/framework/internal/core/AbstractBundle:unload	()Z
    //   204: pop
    //   205: aload 9
    //   207: monitorexit
    //   208: goto +7 -> 215
    //   211: aload 9
    //   213: monitorexit
    //   214: athrow
    //   215: aload 5
    //   217: invokevirtual 1674	org/eclipse/osgi/framework/internal/core/AbstractBundle:close	()V
    //   220: aload 8
    //   222: athrow
    //   223: astore 10
    //   225: aload 7
    //   227: ifnull +13 -> 240
    //   230: aload 7
    //   232: aload 5
    //   234: iconst_2
    //   235: invokeinterface 1849 3 0
    //   240: aload 10
    //   242: athrow
    //   243: aload 7
    //   245: ifnull +84 -> 329
    //   248: aload 7
    //   250: aload 5
    //   252: iconst_2
    //   253: invokeinterface 1849 3 0
    //   258: goto +71 -> 329
    //   261: astore 6
    //   263: aload 4
    //   265: invokeinterface 1846 1 0
    //   270: goto +16 -> 286
    //   273: astore 7
    //   275: aload_0
    //   276: iconst_2
    //   277: aload_0
    //   278: getfield 1536	org/eclipse/osgi/framework/internal/core/Framework:systemBundle	Lorg/eclipse/osgi/framework/internal/core/InternalSystemBundle;
    //   281: aload 7
    //   283: invokevirtual 1727	org/eclipse/osgi/framework/internal/core/Framework:publishFrameworkEvent	(ILorg/osgi/framework/Bundle;Ljava/lang/Throwable;)V
    //   286: aload 6
    //   288: instanceof 808
    //   291: ifeq +9 -> 300
    //   294: aload 6
    //   296: checkcast 808	java/lang/SecurityException
    //   299: athrow
    //   300: aload 6
    //   302: instanceof 894
    //   305: ifeq +9 -> 314
    //   308: aload 6
    //   310: checkcast 894	org/osgi/framework/BundleException
    //   313: athrow
    //   314: new 894	org/osgi/framework/BundleException
    //   317: dup
    //   318: aload 6
    //   320: invokevirtual 1621	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   323: aload 6
    //   325: invokespecial 1807	org/osgi/framework/BundleException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   328: athrow
    //   329: aload 5
    //   331: areturn
    // Line number table:
    //   Java source line #927	-> byte code offset #0
    //   Java source line #930	-> byte code offset #13
    //   Java source line #931	-> byte code offset #22
    //   Java source line #933	-> byte code offset #31
    //   Java source line #934	-> byte code offset #42
    //   Java source line #935	-> byte code offset #47
    //   Java source line #938	-> byte code offset #57
    //   Java source line #939	-> byte code offset #62
    //   Java source line #940	-> byte code offset #68
    //   Java source line #942	-> byte code offset #88
    //   Java source line #943	-> byte code offset #108
    //   Java source line #945	-> byte code offset #131
    //   Java source line #952	-> byte code offset #143
    //   Java source line #945	-> byte code offset #144
    //   Java source line #953	-> byte code offset #151
    //   Java source line #954	-> byte code offset #153
    //   Java source line #958	-> byte code offset #159
    //   Java source line #959	-> byte code offset #168
    //   Java source line #960	-> byte code offset #179
    //   Java source line #961	-> byte code offset #181
    //   Java source line #962	-> byte code offset #191
    //   Java source line #963	-> byte code offset #199
    //   Java source line #962	-> byte code offset #205
    //   Java source line #965	-> byte code offset #215
    //   Java source line #966	-> byte code offset #220
    //   Java source line #967	-> byte code offset #223
    //   Java source line #968	-> byte code offset #225
    //   Java source line #969	-> byte code offset #230
    //   Java source line #970	-> byte code offset #240
    //   Java source line #968	-> byte code offset #243
    //   Java source line #969	-> byte code offset #248
    //   Java source line #970	-> byte code offset #258
    //   Java source line #972	-> byte code offset #261
    //   Java source line #974	-> byte code offset #263
    //   Java source line #975	-> byte code offset #273
    //   Java source line #976	-> byte code offset #275
    //   Java source line #978	-> byte code offset #286
    //   Java source line #979	-> byte code offset #294
    //   Java source line #980	-> byte code offset #300
    //   Java source line #981	-> byte code offset #308
    //   Java source line #982	-> byte code offset #314
    //   Java source line #984	-> byte code offset #329
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	332	0	this	Framework
    //   0	332	1	location	String
    //   0	332	2	source	URLConnection
    //   0	332	3	callerContext	AccessControlContext
    //   11	253	4	storage	org.eclipse.osgi.framework.adaptor.BundleOperation
    //   29	222	5	bundle	AbstractBundle
    //   329	1	5	bundle	AbstractBundle
    //   20	49	6	bundledata	BundleData
    //   261	63	6	t	Throwable
    //   40	209	7	bundleStats	org.eclipse.osgi.framework.adaptor.BundleWatcher
    //   273	9	7	ee	BundleException
    //   86	53	8	extension	boolean
    //   179	42	8	error	Throwable
    //   151	3	9	e	PrivilegedActionException
    //   196	16	9	Ljava/lang/Object;	Object
    //   223	18	10	localObject1	Object
    // Exception table:
    //   from	to	target	type
    //   131	148	151	java/security/PrivilegedActionException
    //   57	176	179	java/lang/Throwable
    //   199	208	211	finally
    //   211	214	211	finally
    //   57	223	223	finally
    //   13	258	261	java/lang/Throwable
    //   263	270	273	org/osgi/framework/BundleException
  }
  
  /* Error */
  public AbstractBundle getBundle(long id)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1535	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 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: lload_1
    //   12: invokevirtual 1695	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 #997	-> byte code offset #0
    //   Java source line #998	-> byte code offset #7
    //   Java source line #997	-> 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 AbstractBundle getBundleBySymbolicName(String symbolicName, Version version)
  {
    // Byte code:
    //   0: aload_0
    //   1: getfield 1535	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 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: aload_1
    //   12: aload_2
    //   13: invokevirtual 1699	org/eclipse/osgi/framework/internal/core/BundleRepository:getBundle	(Ljava/lang/String;Lorg/osgi/framework/Version;)Lorg/eclipse/osgi/framework/internal/core/AbstractBundle;
    //   16: aload_3
    //   17: monitorexit
    //   18: areturn
    //   19: aload_3
    //   20: monitorexit
    //   21: athrow
    // Line number table:
    //   Java source line #1034	-> byte code offset #0
    //   Java source line #1035	-> byte code offset #7
    //   Java source line #1034	-> 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";
      }
    });
  }
  
  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 1535	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 1535	org/eclipse/osgi/framework/internal/core/Framework:bundles	Lorg/eclipse/osgi/framework/internal/core/BundleRepository;
    //   11: aload_1
    //   12: invokevirtual 1698	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 #1215	-> byte code offset #0
    //   Java source line #1216	-> byte code offset #7
    //   Java source line #1215	-> 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);
   
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

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