org.osgi.framework.FrameworkListener Java Examples

The following examples show how to use org.osgi.framework.FrameworkListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SystemBundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Initialize this framework and call listeners.
 *
 * @see org.osgi.framework.launch.Framework#init(FrameworkListener...)
 */
@Override
public void init(FrameworkListener... listeners) throws BundleException {
  secure.checkExecuteAdminPerm(this);

  synchronized (lock) {
    waitOnOperation(lock, "Framework.init", true);

    switch (state) {
    case INSTALLED:
      state = RESOLVED;
    case RESOLVED:
      break;
    case STARTING:
    case ACTIVE:
      return;
    default:
      throw new IllegalStateException("INTERNAL ERROR, Illegal state, " + state);
    }
    doInit(listeners);
  }
}
 
Example #2
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * remove a framework listener.
 * 
 * @param listener
 *            a framework listener.
 * @see org.osgi.framework.BundleContext#removeFrameworkListener(org.osgi.framework.FrameworkListener)
 * 
 */
public void removeFrameworkListener(final FrameworkListener listener) {
	checkValid();

	if (bundle == Concierge.this) {
		return;
	}
	final AbstractBundle b = bundle;

	frameworkListeners.remove(listener);
	if (b.registeredFrameworkListeners != null) {
		b.registeredFrameworkListeners.remove(listener);
		if (b.registeredFrameworkListeners.isEmpty()) {
			b.registeredFrameworkListeners = null;
		}
	}
}
 
Example #3
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * add a framework listener.
 * 
 * @param listener
 *            a framework listener.
 * @see org.osgi.framework.BundleContext#addFrameworkListener(org.osgi.framework.FrameworkListener)
 * 
 */
public void addFrameworkListener(final FrameworkListener listener) {
	checkValid();

	// if (bundle == Concierge.this) {
	// return;
	// }

	if (bundle.registeredFrameworkListeners == null) {
		bundle.registeredFrameworkListeners = new ArrayList<FrameworkListener>(
				1);
	}
	if (!bundle.registeredFrameworkListeners.contains(listener)) {
		frameworkListeners.add(listener);
		bundle.registeredFrameworkListeners.add(listener);
	}
}
 
Example #4
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * notify all framework listeners.
 * 
 * @param state
 *            the new state.
 * @param bundle
 *            the bundle.
 * @param throwable
 *            a throwable.
 */
protected void notifyFrameworkListeners(final FrameworkListener[] listeners,
		final int state, final Bundle bundle, final Throwable throwable) {

	if (listeners.length == 0) {
		return;
	}

	final FrameworkEvent event = new FrameworkEvent(state, bundle,
			throwable);

	for (int i = 0; i < listeners.length; i++) {
		final FrameworkListener listener = listeners[i];
		if (SECURITY_ENABLED) {
			AccessController.doPrivileged(new PrivilegedAction<Object>() {
				public Object run() {
					listener.frameworkEvent(event);
					return null;
				}
			});
		} else {
			listener.frameworkEvent(event);
		}
	}
}
 
Example #5
Source File: BundleUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static void refreshBundles(Set<Bundle> toRefresh, FrameworkWiring frameworkWiring) {
	if (!toRefresh.isEmpty()) {
		JavaLanguageServerPlugin.logInfo("Refresh the bundles");
		final CountDownLatch latch = new CountDownLatch(1);
		frameworkWiring.refreshBundles(toRefresh, new FrameworkListener() {
			@Override
			public void frameworkEvent(FrameworkEvent event) {
				if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) {
					latch.countDown();
				} else if (event.getType() == FrameworkEvent.ERROR) {
					JavaLanguageServerPlugin.logException("Error happens when refreshing the bundles", event.getThrowable());
					latch.countDown();
				}
			}
		});
		try {
			latch.await();
		} catch (InterruptedException e) {
			JavaLanguageServerPlugin.logException("InterruptedException happened when refreshing", e);
		}
		JavaLanguageServerPlugin.logInfo("Finished Refreshing bundles");
	}
}
 
Example #6
Source File: Framework.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * notify all framework listeners.
 *
 * @param state the new state.
 * @param bundle the bundle.
 * @param throwable a throwable.
 */
static void notifyFrameworkListeners(final int state, final Bundle bundle, final Throwable throwable) {

    if (frameworkListeners.isEmpty()) {
        return;
    }

    final FrameworkEvent event = new FrameworkEvent(state);

    final FrameworkListener[] listeners = frameworkListeners.toArray(new FrameworkListener[frameworkListeners.size()]);

    for (int i = 0; i < listeners.length; i++) {
        final FrameworkListener listener = listeners[i];

        listener.frameworkEvent(event);
    }
}
 
Example #7
Source File: Framework.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
static void notifyFrameworkListeners(int i, Bundle bundle, Throwable th) {
    if (!frameworkListeners.isEmpty()) {
        FrameworkEvent frameworkEvent = new FrameworkEvent(i, bundle, th);
        FrameworkListener[] frameworkListenerArr = (FrameworkListener[]) frameworkListeners.toArray(new FrameworkListener[frameworkListeners.size()]);
        for (FrameworkListener frameworkEvent2 : frameworkListenerArr) {
            frameworkEvent2.frameworkEvent(frameworkEvent);
        }
    }
}
 
Example #8
Source File: Listeners.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Listeners(FrameworkContext framework, PermissionOps perm, FrameworkListener... initListeners) {
  this.fwCtx = framework;
  secure = perm;
  nocacheldap = framework.props.getBooleanProperty(FWProps.LDAP_NOCACHE_PROP);
  serviceListeners = new ServiceListenerState(this);
  final String ets = framework.props.getProperty(FWProps.LISTENER_N_THREADS_PROP);
  int n_threads = 1;
  Throwable error = null;
  if (ets != null) {
    try {
      n_threads = Integer.parseInt(ets);
    } catch (final NumberFormatException nfe) {
      error = nfe;
    }
  }
  this.initListeners = initListeners;
  if (n_threads > 0) {
    asyncEventQueue = new LinkedList<AsyncEvent>();
    threads = new AsyncEventThread[n_threads];
    for (int i = 0; i < n_threads; i++) {
      threads[i] = new AsyncEventThread(i);
      threads[i].start();
    }
    if (n_threads > 1) {
      activeListeners = new HashMap<ListenerEntry, Thread>();
    }
    if (error != null) {
      Throwable t = new Exception("Failed to parse " + FWProps.LISTENER_N_THREADS_PROP, error);
      framework.frameworkWarning(framework.systemBundle, t);
    }
  }
}
 
Example #9
Source File: Listeners.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add a bundle listener to current framework.
 *
 * @param bc Who wants to add listener.
 * @param listener Object to add.
 */
void addFrameworkListener(BundleContextImpl bc, FrameworkListener listener) {
  final ListenerEntry le = new ListenerEntry(bc, listener);
  synchronized (frameworkListeners) {
    frameworkListeners.add(le);
  }
}
 
Example #10
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void removeFrameworkListener(FrameworkListener frameworkListener) {
    checkValid();
    Framework.frameworkListeners.remove(frameworkListener);
    this.bundle.registeredFrameworkListeners.remove(frameworkListener);
    if (this.bundle.registeredFrameworkListeners.isEmpty()) {
        this.bundle.registeredFrameworkListeners = null;
    }
}
 
Example #11
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see org.osgi.framework.startlevel.FrameworkStartLevel#setStartLevel(int,
 *      org.osgi.framework.FrameworkListener[])
 * @category FrameworkStartLevel
 */
public void setStartLevel(final int targetLevel,
		final FrameworkListener... listeners) {
	// TODO: check AdminPermission(this, STARTLEVEL);
	
	// cannot set the startlevel during init - required for extension bundle activators
	if(state == STARTING){
		throw new IllegalStateException("Cannot set the start level while starting");
	}
	
	if (targetLevel <= 0) {
		throw new IllegalArgumentException(
				"Start level " + targetLevel + " is not a valid level");
	}

	new Thread() {
		public void run() {
			setLevel(bundles.toArray(new Bundle[bundles.size()]),
					targetLevel, false);
			notifyFrameworkListeners(FrameworkEvent.STARTLEVEL_CHANGED,
					Concierge.this, null);
			if (listeners != null) {
				notifyFrameworkListeners(listeners,
						FrameworkEvent.STARTLEVEL_CHANGED, Concierge.this,
						null);
			}
			storeMetadata();
		}
	}.start();
}
 
Example #12
Source File: Concierge.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
void notifyFrameworkListeners(final int state, final Bundle bundle,
		final Throwable throwable) {
	notifyFrameworkListeners(
			frameworkListeners.toArray(
					new FrameworkListener[frameworkListeners.size()]),
			state, bundle, throwable);
}
 
Example #13
Source File: Activator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public @Override void start(final BundleContext context) throws Exception {
        if (System.getProperty("netbeans.home") != null) {
            throw new IllegalStateException("Should not be run from inside regular NetBeans module system");
        }
        String storage = context.getProperty(Constants.FRAMEWORK_STORAGE);
        if (storage != null) {
            System.setProperty("netbeans.user", storage);
        }
        System.setProperty("TopSecurityManager.disable", "true");
        NbBundle.setBranding(System.getProperty("branding.token"));
        OSGiMainLookup.initialize(context);
        queue = new DependencyQueue<String,Bundle>();
        this.context = context;
        framework = ((Framework) context.getBundle(0));
        if (framework.getState() == Bundle.STARTING) {
            LOG.fine("framework still starting");
            final AtomicReference<FrameworkListener> frameworkListener = new AtomicReference<FrameworkListener>();
            frameworkListener.set(new FrameworkListener() {
                public @Override void frameworkEvent(FrameworkEvent event) {
                    if (event.getType() == FrameworkEvent.STARTED) {
//                        System.err.println("framework started");
                        context.removeFrameworkListener(frameworkListener.get());
                        context.addBundleListener(Activator.this);
                        processLoadedBundles();
                    }
                }
            });
            context.addFrameworkListener(frameworkListener.get());
        } else {
            LOG.fine("framework already started");
            context.addBundleListener(this);
            processLoadedBundles();
        }
    }
 
Example #14
Source File: SecurePermissionOps.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
void callRefreshPackages0(final PackageAdminImpl pa,
                          final Bundle[] bundles,
                          final FrameworkListener[] fl)
{
  AccessController.doPrivileged(new PrivilegedAction<Object>() {
    public Object run()
    {
      pa.refreshPackages0(bundles, fl);
      return null;
    }
  });
}
 
Example #15
Source File: BundleContextImpl.java    From AtlasForAndroid with MIT License 5 votes vote down vote up
public void addFrameworkListener(FrameworkListener frameworkListener) {
    checkValid();
    if (this.bundle.registeredFrameworkListeners == null) {
        this.bundle.registeredFrameworkListeners = new ArrayList();
    }
    if (!this.bundle.registeredFrameworkListeners.contains(frameworkListener)) {
        Framework.frameworkListeners.add(frameworkListener);
        this.bundle.registeredFrameworkListeners.add(frameworkListener);
    }
}
 
Example #16
Source File: Listeners.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 *
 */
private void frameworkEvent(final ListenerEntry le, FrameworkEvent evt) {
  try {
    ((FrameworkListener)le.listener).frameworkEvent(evt);
  } catch (final Exception pe) {
    // Don't report Error events again, since probably would go into an infinite loop.
    if (evt.getType() != FrameworkEvent.ERROR) {
      fwCtx.frameworkError(le != null ? le.bc : null, pe);
    }
  }
}
 
Example #17
Source File: StartLevelController.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void setStartLevel(final int startLevel, final FrameworkListener... listeners)
{
  if (!bRun) {
    throw new IllegalStateException("StartLevelService isn't running yet");
  }
  fwCtx.perm.checkStartLevelAdminPerm();
  if (startLevel <= 0) {
    throw new IllegalArgumentException("Initial start level must be > 0, is "
                                       + startLevel);
  }
  // No start-level changed events if called before open() or after close().
  setStartLevel0(startLevel, true, listeners);
}
 
Example #18
Source File: Framework.java    From ACDD with MIT License 5 votes vote down vote up
static void notifyFrameworkListeners(int event, Bundle bundle, Throwable th) {
    if (!frameworkListeners.isEmpty()) {
        FrameworkEvent frameworkEvent = new FrameworkEvent(event, bundle, th);
        FrameworkListener[] frameworkListenerArr = frameworkListeners.toArray(new FrameworkListener[frameworkListeners.size()]);
        for (FrameworkListener frameworkListener : frameworkListenerArr) {
            frameworkListener.frameworkEvent(frameworkEvent);
        }
    }
}
 
Example #19
Source File: Desktop.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void refreshBundles(final Bundle[] b)
{
  final Bundle systemBundle = Activator.getTargetBC().getBundle(0);
  final FrameworkWiring fw = systemBundle.adapt(FrameworkWiring.class);

  if (fw != null) {
    final ArrayList<Bundle> bundles = new ArrayList<Bundle>();
    final boolean refreshAll = b == null || 0 == b.length;
    final StringBuffer sb = new StringBuffer("Desktop-RefreshPackages ");
    if (refreshAll) {
      sb.append("all packages pending removal");
    } else {
      sb.append("bundle packages for ");
      for (int i = 0; i < b.length; i++) {
        if (i > 0) {
          sb.append(", ");
        }
        sb.append(b[i].getBundleId());
        bundles.add(b[i]);
      }
    }

    final FrameworkListener refreshListener = new FrameworkListener() {

      @Override
      public void frameworkEvent(FrameworkEvent event)
      {
        Activator.log.info(sb.toString() + " DONE.");
      }
    };

    try {
      fw.refreshBundles(bundles, refreshListener);
    } catch (final Exception e) {
      showErr(sb.toString() + " failed to refresh bundles: " + e, e);
    }
  }
}
 
Example #20
Source File: CapabilityTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void runTest() throws Throwable {
  fw = bc.getBundle(0).adapt(FrameworkWiring.class);
  FrameworkListener fl = new FrameworkListener() {
    @Override
    public void frameworkEvent(FrameworkEvent event) {
      synchronized (this) {
        notifyAll();
      }
    }
  };
  try {
    synchronized (fl) {
      fw.refreshBundles(null, fl);
      fl.wait(3000);
    }
    buCUC1 = Util.installBundle(bc, "bundleCUC1_test-1.0.0.jar");
    assertNotNull(buCUC1);
    buCUC2 = Util.installBundle(bc, "bundleCUC2_test-2.0.0.jar");
    assertNotNull(buCUC2);
    buCUP1 = Util.installBundle(bc, "bundleCUP1_test-1.0.0.jar");
    assertNotNull(buCUP1);
    buCUP2 = Util.installBundle(bc, "bundleCUP2_test-2.0.0.jar");
    assertNotNull(buCUP2);
  } catch (Exception e) {
    fail("Failed to refresh packages: " + e);
  }
}
 
Example #21
Source File: OpenAtlas.java    From ACDD with MIT License 4 votes vote down vote up
public void removeFrameworkListener(FrameworkListener frameworkListener) {
    Framework.removeFrameworkListener(frameworkListener);
}
 
Example #22
Source File: OpenAtlas.java    From ACDD with MIT License 4 votes vote down vote up
public void addFrameworkListener(FrameworkListener frameworkListener) {
    Framework.addFrameworkListener(frameworkListener);
}
 
Example #23
Source File: Atlas.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public void removeFrameworkListener(FrameworkListener frameworkListener) {
    Framework.removeFrameworkListener(frameworkListener);
}
 
Example #24
Source File: SystemBundle.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 *
 */
private void doInit(FrameworkListener... listeners) throws BundleException {
  bootClassPathHasChanged = false;
  fwCtx.init(listeners);
}
 
Example #25
Source File: Framework.java    From ACDD with MIT License 4 votes vote down vote up
static void addFrameworkListener(FrameworkListener frameworkListener) {
    frameworkListeners.add(frameworkListener);
}
 
Example #26
Source File: Framework.java    From ACDD with MIT License 4 votes vote down vote up
static void removeFrameworkListener(FrameworkListener frameworkListener) {
    frameworkListeners.remove(frameworkListener);
}
 
Example #27
Source File: Atlas.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public void addFrameworkListener(FrameworkListener frameworkListener) {
    Framework.addFrameworkListener(frameworkListener);
}
 
Example #28
Source File: FrameworkWiringImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void refreshBundles(Collection<Bundle> bundles, FrameworkListener... listeners) {
  final Bundle[] bs = bundles != null ?
                bundles.toArray(new Bundle[bundles.size()]) :
                null;
  fwCtx.packageAdmin.refreshPackages(bs, listeners);
}
 
Example #29
Source File: Framework.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
static void addFrameworkListener(FrameworkListener frameworkListener) {
    frameworkListeners.add(frameworkListener);
}
 
Example #30
Source File: Framework.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
static void removeFrameworkListener(FrameworkListener frameworkListener) {
    frameworkListeners.remove(frameworkListener);
}