@Override public void log(FrameworkEvent fe) { Level l = Level.FINE; if ((fe.getType() & FrameworkEvent.ERROR) != 0) { l = Level.SEVERE; } else if ((fe.getType() & FrameworkEvent.WARNING) != 0) { l = Level.WARNING; } else if ((fe.getType() & FrameworkEvent.INFO) != 0) { l = Level.INFO; } LogRecord lr = new LogRecord(l, "framework event {0} type {1}"); lr.setParameters(new Object[]{fe.getBundle().getSymbolicName(), fe.getType()}); lr.setThrown(fe.getThrowable()); lr.setLoggerName(NetbinoxFactory.LOG.getName()); NetbinoxFactory.LOG.log(lr); }
/** * uninstall the bundle. * * @throws BundleException if bundle is already uninstalled * @see org.osgi.framework.Bundle#uninstall() * @category Bundle */ public synchronized void uninstall() throws BundleException { if (state == UNINSTALLED) { throw new IllegalStateException("Bundle " + toString() + " is already uninstalled."); } if (state == ACTIVE) { try { stop(); } catch (Throwable t) { Framework.notifyFrameworkListeners(FrameworkEvent.ERROR, this, t); } } state = UNINSTALLED; classloader.cleanup(true); classloader = null; Framework.bundles.remove(getLocation()); Framework.notifyBundleListeners(BundleEvent.UNINSTALLED, this); }
/** * 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); } }
public void testNullSafeToStringFrameworkEvent() throws Exception { Bundle bundle = new MockBundle(); Throwable th = new Exception(); assertEquals("STARTED", OsgiStringUtils.nullSafeToString(new FrameworkEvent(FrameworkEvent.STARTED, bundle, th))); assertEquals("ERROR", OsgiStringUtils.nullSafeToString(new FrameworkEvent(FrameworkEvent.ERROR, bundle, th))); assertEquals("WARNING", OsgiStringUtils.nullSafeToString(new FrameworkEvent(FrameworkEvent.WARNING, bundle, th))); assertEquals("INFO", OsgiStringUtils.nullSafeToString(new FrameworkEvent(FrameworkEvent.INFO, bundle, th))); assertEquals("PACKAGES_REFRESHED", OsgiStringUtils.nullSafeToString(new FrameworkEvent( FrameworkEvent.PACKAGES_REFRESHED, bundle, th))); assertEquals("STARTLEVEL_CHANGED", OsgiStringUtils.nullSafeToString(new FrameworkEvent( FrameworkEvent.STARTLEVEL_CHANGED, bundle, th))); }
/** * Handles OSGi Framework restart, does following on System Bundle STARTED event. * <p> * 1. Removes the BundleContext from ServletContext attributes. <br> * 2. Gets the new BundleContext from System Bundle and sets that to ServletContext. <br> * 3. Closes the DispatcherServletTracker and open again with new BundleContext. <br> * 4. As the BundleContext is removed and added from ServletContext the corresponding BridgeServletContextAttributeListener * is fired with events which in turn closes the EventDispatcherTracker and open again with new BundleContext. */ @Override public void frameworkEvent(FrameworkEvent event) { Logger logger = LoggerFactory.getLogger(FrameworkRestartHandler.class); switch (event.getType()) { case FrameworkEvent.STARTED: logger.info("Handling OSGi Framework Restart!!"); // Add the new BundleContext as a ServletContext attribute, remove the stale BundleContext. BundleContext bundleContext = event.getBundle().getBundleContext(); this.handleBundleContext(bundleContext); this.handleDispatcherServletTracker(logger); break; case FrameworkEvent.STOPPED_UPDATE: logger.info("Closing DispatcherServletTracker!!"); ServiceTrackers.INSTANCE.closeDispatcherServletTracker(); logger.info("Closing EventDispatcherTracker!!"); ServiceTrackers.INSTANCE.closeEventDispatcherTracker(); break; default: // log it and ignore. if (logger.isDebugEnabled()) { logger.debug("Ignoring the OSGi FrameworkEvent: [{}]", FrameworkEvents.asString(event.getType())); } break; } }
private String getFrameworkEventTypeText(int type) { switch (type) { case FrameworkEvent.STARTED: return "Started"; case FrameworkEvent.ERROR: return "Error"; case FrameworkEvent.WARNING: return "Warning"; case FrameworkEvent.INFO: return "Info"; case FrameworkEvent.PACKAGES_REFRESHED: return "Packages Refreshed"; case FrameworkEvent.STARTLEVEL_CHANGED: return "Start Level Changed"; case FrameworkEvent.STOPPED: return "Stopped"; case FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED: return "Stopped Bootclasspath Modified"; case FrameworkEvent.STOPPED_UPDATE: return "Stopped Update"; case FrameworkEvent.WAIT_TIMEDOUT: return "Wait Timeout"; default: return type + " ???"; } }
@Override public void refreshPackages() { FrameworkWiring wiring = felix.adapt(FrameworkWiring.class); final CountDownLatch latch = new CountDownLatch(1); wiring.refreshBundles(null, event -> { switch (event.getType()) { case FrameworkEvent.PACKAGES_REFRESHED: latch.countDown(); break; case FrameworkEvent.ERROR: log.log(Level.SEVERE, "ERROR FrameworkEvent received.", event.getThrowable()); break; } }); try { long TIMEOUT_SECONDS = 60L; if (!latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)) { log.warning("No PACKAGES_REFRESHED FrameworkEvent received within " + TIMEOUT_SECONDS + " seconds of calling FrameworkWiring.refreshBundles()"); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
/** * Wait until this Framework has completely stopped. * * @param framework OSGi framework * @throws java.lang.Exception */ private void waitForServerStop(Framework framework) throws Exception { if (!isFrameworkActive()) { return; } while (true) { FrameworkEvent event = framework.waitForStop(0); // We should not stop the framework if the user has updated the system bundle via the OSGi console or // programmatically. In this case, framework will shutdown and start itself. if (event.getType() != FrameworkEvent.STOPPED_UPDATE) { if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "OSGi framework is stopped for update."); } break; } } }
/** * Starting the blocker instance. */ public void start() { frameworkListener = new FrameworkListener() { @Override public void frameworkEvent(final FrameworkEvent event) { if (event.getType() == FrameworkEvent.STARTED) { blocking = false; unblock(); } } }; bundleContext.addFrameworkListener(frameworkListener); Bundle frameworkBundle = bundleContext.getBundle(0); if (frameworkBundle.getState() != Bundle.ACTIVE) { blocking = true; block(); } else { blocking = false; } }
@Override public synchronized void frameworkEvent(FrameworkEvent fe) { if (fe.getType() == FrameworkEvent.STARTLEVEL_CHANGED) { levelChanged = true; notifyAll(); } }
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(); } }
private static BundleFile createBundleFile(Object content, BaseData sourcedata) { try { return sourcedata.getAdaptor().createBundleFile(content, sourcedata); } catch (IOException e) { sourcedata.getAdaptor().getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, sourcedata.getBundle(), e); } return null; }
@Override public void frameworkEvent(FrameworkEvent event) { switch (event.getType()) { case 0:/* STARTING */ starting(); break; case FrameworkEvent.STARTED: started(); break; case FrameworkEvent.STARTLEVEL_CHANGED: case FrameworkEvent.PACKAGES_REFRESHED: case FrameworkEvent.ERROR: } }
/** * launch the framework. * * @throws Throwable */ static void startup(boolean updated) throws BundleException { AtlasBundleInfoManager.instance().getBundleInfo(); AtlasHotPatchManager.getInstance(); notifyFrameworkListeners(0 /* STARTING */, null, null); notifyFrameworkListeners(FrameworkEvent.STARTED, null, null); }
/** * Returns a String representation for the given <code>FrameworkEvent</code> . * * @param event OSGi <code>FrameworkEvent</code> (can be <code>null</code>) * @return String representation of the given event */ public static String nullSafeToString(FrameworkEvent event) { if (event == null) return NULL_STRING; try { return FRAMEWORK_EVENTS.toCode(Integer.valueOf(event.getType()), EMPTY_STRING); } catch (ConstantException cex) { return UNKNOWN_EVENT_TYPE; } }
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); } } }
public void log(FrameworkEvent event) { // if (event.getType() == FrameworkEvent.ERROR) if (event.getThrowable() == null) { LOG.info("FrameworkEvent {} : {}", getFrameworkEventTypeText(event.getType()), event.getBundle().getSymbolicName()); } else { LOG.error("FrameworkEvent {} : {}", getFrameworkEventTypeText(event.getType()), event.getBundle().getSymbolicName(), event.getThrowable()); } }
private void dump(FrameworkEvent fe) { Throwable t = fe.getThrowable(); String tString = null; if (t != null) { tString = t.toString(); } System.out.println("Framework ERROR:" + ", source " + fe.getSource() + ", bundle=" + fe.getBundle() + ", ex=" + tString); if (t != null) { t.printStackTrace(); } }
private void dump(FrameworkEvent fe) { Throwable t = fe.getThrowable(); String tString = null; if (t != null) { tString = t.toString(); } System.out.println("Framework ERROR:" + ", source " + fe.getSource() + ", bundle=" + fe.getBundle() + ", ex=" + tString); if(t != null) { t.printStackTrace(); } }
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); } } }
/** * Returns a String representation for the given <code>FrameworkEvent</code>. * * @param event OSGi <code>FrameworkEvent</code> (can be <code>null</code>) * @return String representation of the given event */ public static String nullSafeToString(FrameworkEvent event) { if (event == null) return NULL_STRING; try { return FRAMEWORK_EVENTS.toCode(new Integer(event.getType()), EMPTY_STRING); } catch (ConstantException cex) { return UNKNOWN_EVENT_TYPE; } }
@Override public void start(BundleContext context) throws Exception { this.context = context; packages = load(context.getBundle(), "guardinfo/packages.properties", packages); services = load(context.getBundle(), "guardinfo/services.properties", services); Hashtable<String,Object> properties = new Hashtable<>(); properties.put("command.scope", "guard"); properties.put("command.function", new String[] { "restart", "clear" }); context.registerService(Object.class, new Object(), properties); final AtomicBoolean started = new AtomicBoolean(false); // // Wait before polling context.addFrameworkListener(new FrameworkListener() { @Override public void frameworkEvent(FrameworkEvent event) { if (event.getType() == FrameworkEvent.STARTED && started.getAndSet(true)) { start(); } } }); Thread.sleep(5000); if (started.getAndSet(true)) start(); }
public void frameworkEvent(FrameworkEvent event) { if (event.getType() == FrameworkEvent.ERROR) { } else if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) { } else if (event.getType() == FrameworkEvent.STARTED) { } else if (event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) { } }
@Override public void frameworkEvent(final FrameworkEvent event) { int eventType = event.getType(); if ((eventType == FrameworkEvent.ERROR) || (eventType == FrameworkEvent.PACKAGES_REFRESHED)) { refreshFinishLock.lock(); try { refreshFinished.set(true); Logger.info("Framework refresh finished with code " + BundleUtil.convertFrameworkEventTypeCode(eventType)); refreshFinishCondition.signal(); } finally { refreshFinishLock.unlock(); } } else { StringBuilder sb = new StringBuilder("Event caught during refreshing packages with data:"); if (event.getBundle() != null) { sb.append("\n\tBundle: ").append(event.getBundle().toString()); } if (event.getSource() != null) { sb.append("\n\tSource object: ").append(event.getSource().toString()); } sb.append("\n\tEvent type: ").append(BundleUtil.convertFrameworkEventTypeCode(event.getType())); if (event.getThrowable() != null) { Logger.error(sb.toString(), event.getThrowable()); } else { Logger.info(sb.toString()); } } }
public void setFrameworkStartLevel(final int startLevel) { Logger.info("Setting framework startlevel to " + startLevel); final AtomicBoolean startLevelReached = new AtomicBoolean(false); final Lock lock = new ReentrantLock(); final Condition startLevelReachedCondition = lock.newCondition(); frameworkStartLevel.setStartLevel(startLevel, new FrameworkListener() { @Override public void frameworkEvent(final FrameworkEvent event) { lock.lock(); int eventType = event.getType(); if ((eventType == FrameworkEvent.STARTLEVEL_CHANGED) || (eventType == FrameworkEvent.ERROR)) { if (eventType == FrameworkEvent.ERROR) { Logger.error("Setting framework startlevel to " + startLevel + " finished with error: ", event.getThrowable()); } else { Logger.info("Setting framework startlevel to " + startLevel + " finished with success"); } startLevelReached.set(true); startLevelReachedCondition.signal(); } lock.unlock(); } }); lock.lock(); try { while (!startLevelReached.get()) { startLevelReachedCondition.await(); } } catch (InterruptedException e) { Logger.error("Startlevel reaching wait interrupted", e); } finally { lock.unlock(); } currentFrameworkStartLevelValue = startLevel; }
public static String convertFrameworkEventTypeCode(final int code) { switch (code) { case FrameworkEvent.ERROR: return "ERROR"; case FrameworkEvent.INFO: return "INFO"; case FrameworkEvent.PACKAGES_REFRESHED: return "PACKAGES_REFRESHED"; case FrameworkEvent.STARTED: return "STARTED"; case FrameworkEvent.STARTLEVEL_CHANGED: return "STARTLEVEL_CHANGED"; case FrameworkEvent.STOPPED: return "STOPPED"; case FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED: return "STOPPED_BOOTCLASSPATH_MODIFIED"; case FrameworkEvent.STOPPED_UPDATE: return "STOPPED_UPDATE"; case FrameworkEvent.WAIT_TIMEDOUT: return "WAIT_TIMEDOUT"; case FrameworkEvent.WARNING: return "WARNING"; default: break; } return String.valueOf(code); }
public FrameworkEvent waitForStop(long l) throws InterruptedException { return null; }
@Override public void frameworkEvent(FrameworkEvent ev) { if (ev.getType() == FrameworkEvent.ERROR) { log(ev); } }
@Override public void frameworkEvent(FrameworkEvent event) { if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) { invalidateAllUnits("Packaged Refreshed"); } }
public void testNullSafeToStringFrameworkEventNull() throws Exception { assertNotNull(OsgiStringUtils.nullSafeToString((FrameworkEvent) null)); }
public void testNullSafeToStringFrameworkEventInvalidType() throws Exception { assertEquals("UNKNOWN EVENT TYPE", OsgiStringUtils.nullSafeToString(new FrameworkEvent(-123, bundle, new Exception()))); }
@Override public void frameworkEvent(FrameworkEvent event) { if(event.getType() ==FrameworkEvent.STARTLEVEL_CHANGED){ PluginFrameworkListener.Processor.execute(true); } }
@Override public void frameworkEvent(FrameworkEvent event) { Level level; String type; switch (event.getType()) { case FrameworkEvent.STARTED: level = Level.INFO; type = "STARTED"; break; case FrameworkEvent.ERROR: level = Level.SEVERE; type = "ERROR"; break; case FrameworkEvent.WARNING: level = Level.WARNING; type = "WARNING"; break; case FrameworkEvent.INFO: level = Level.INFO; type = "INFO"; break; case FrameworkEvent.PACKAGES_REFRESHED: level = Level.FINE; type = "PACKAGES_REFRESHED"; break; case FrameworkEvent.STARTLEVEL_CHANGED: level = Level.FINE; type = "STARTLEVEL_CHANGED"; break; case FrameworkEvent.STOPPED: level = Level.INFO; type = "STOPPED"; break; case FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED: level = Level.INFO; type = "STOPPED_BOOTCLASSPATH_MODIFIED"; break; case FrameworkEvent.STOPPED_UPDATE: level = Level.INFO; type = "STOPPED_UPDATE"; break; case FrameworkEvent.WAIT_TIMEDOUT: level = Level.FINE; type = "WAIT_TIMEDOUT"; break; default: level = Level.OFF; type = "<Unknown type: " + event.getType() + ">"; break; } StringBuilder sb = new StringBuilder(); sb.append(type); if (event.getBundle() != null) { sb.append(" bundle=[") .append(event.getBundle()) .append("]"); } LOGGER_FRAMEWORK.log(level, sb.toString(), event.getThrowable()); }
public static void main(String[] args) { try { Metadata.initMetadata(); setupSystemProperties(); setupWorkingDir(); configureJUL(); initFx(); felixConfiguration = loadConfiguration(); processConfiguration(felixConfiguration); AccessEndpoint.internalBundleRepository = new InternalBundleRepository(); clearFelixCache(); felix = new Felix(felixConfiguration); felix.start(); OSGiListener osgiListener = new OSGiListener(); felix.getBundleContext().addBundleListener(osgiListener); felix.getBundleContext().addFrameworkListener(osgiListener); setupFelix(felix); FrameworkEvent event; do { event = felix.waitForStop(Long.MAX_VALUE); if ((event.getType() & FrameworkEvent.ERROR) == FrameworkEvent.ERROR) { throw new IllegalStateException("Felix stopped because of an error: " + event, event.getThrowable()); } } while (event.getType() == FrameworkEvent.WAIT_TIMEDOUT || event.getType() == FrameworkEvent.STOPPED_UPDATE); clearFelixCache(); exitFx(); } catch (Throwable e) { if (felix != null) { try { felix.stop(); } catch (Throwable e1) { e.addSuppressed(e1); } } clearFelixCache(); FatalErrorReporter.process(e); exitFx(); System.exit(1); } }
@Override public void frameworkEvent(FrameworkEvent event) { log(event); }
public void stop() throws InterruptedException, BundleException { framework.stop(); FrameworkEvent event = framework.waitForStop(7000); // 7s loggingFrameworkListener.log(event); }
protected void activate(final ComponentContext context) { this.context = context.getBundleContext(); workingDir = OSGiUtils.getRooWorkingDirectory(context); this.context.addFrameworkListener( new FrameworkListener() { public void frameworkEvent(final FrameworkEvent event) { if(startLevel == null){ startLevel = getStartLevel(); } Validate.notNull(startLevel, "StartLevel is required"); if (startLevel.getStartLevel() >= 99) { // We check we haven't already started, as this // event listener will be called several times at SL // >= 99 if (getProcessManagerStatus() == ProcessManagerStatus.STARTING) { // A proper synchronized process manager status // check will take place in the // completeStartup() method completeStartup(); } } } }); // Now start a thread that will undertake a background poll every second final Thread t = new Thread(new Runnable() { public void run() { // Unsynchronized lookup of terminated status to avoid anything // blocking the termination of the thread while (getProcessManagerStatus() != ProcessManagerStatus.TERMINATED) { // We only bother doing a poll if we seem to be available (a // proper synchronized check happens later) if (getProcessManagerStatus() == ProcessManagerStatus.AVAILABLE) { timerBasedPoll(); } try { Thread.sleep(1000); } catch (final InterruptedException ignoreAndContinue) { } } } }, "Spring Roo Process Manager Background Polling Thread"); t.start(); }
@Override public void frameworkEvent(FrameworkEvent event) { if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED) { createNewWiringLayers(); } }
public void frameworkEvent(FrameworkEvent fe) { if (fe.getType() == FrameworkEvent.ERROR) { errorList.add(fe); } }