📄 oscar.java
字号:
try { Class clazz = Class.forName(className); m_cache = (BundleCache) clazz.newInstance(); m_cache.initialize(this); } catch (Exception ex) { System.err.println("Error creating bundle cache:"); ex.printStackTrace(); System.err.println("\nThis may result from the fact that Oscar 1.0 uses a"); System.err.println("different bundle cache format than previous versions"); System.err.println("of Oscar. Please read the bundle cache documentation for"); System.err.println("more details: http://oscar.objectweb.org/cache.html."); // Only shutdown the JVM if Oscar is running stand-alone. String embedded = getConfigProperty( OscarConstants.EMBEDDED_EXECUTION_PROP); boolean isEmbedded = (embedded == null) ? false : embedded.equals("true"); if (!isEmbedded) { System.exit(-1); } else { throw new RuntimeException(ex.toString()); } } // Initialize. initialize(activatorList); } /** * <p> * Oscar uses this method whenever it needs to access any * configuration properties. This will look for a configuration * property either in the system properties or in the configuration * property map that was passed into the Oscar constructor; that is, * Oscar will look in one place or the other, but not both. This * approach was taken to simplify support for having multiple * instances of Oscar in memory at the same time. * </p> * <p> * The approach is very simple. If no <tt>Properties</tt> instance * was passed into the constructor, then this method only searches * <tt>System.getProperty()</tt> to find property values. If a * <tt>Properties</tt> instance was passed into the constructor, * then it only searches the supplied instance. When creating multiple * instances of Oscar, a <tt>Properties</tt> instance should be * supplied to the constructor so that all instances do not end up * using the same bundle cache directory. * </p> * @param name the name of the configuration property to retrieve. * @return the value of the specified configuration property or <tt>null</tt> * if the property is not defined. **/ public String getConfigProperty(String name) { if (m_configPropMap != null) { return (String) m_configPropMap.get(name); } return System.getProperty(name); } private void setConfigProperty(String name, String value) { // If there is a config property map, then // set the value in it, otherwise put it in // the system properties. if (m_configPropMap != null) { m_configPropMap.put(name, value); } else { System.setProperty(name, value); } } /** * Called to initialize a new instance. **/ private void initialize(List activatorList) { ImportSearchPolicy searchPolicy = new OSGiImportSearchPolicy(this); m_mgr = new ModuleManager(searchPolicy, new OSGiURLPolicy(this)); // Add the OSGi selection policy as a module listener, since // it needs to keep track of when modules are removed so that // it can flush its package export cache. m_mgr.addModuleListener((OSGiSelectionPolicy) searchPolicy.getSelectionPolicy()); // Add a validation listener to the import/export search policy // so that we will be notified when modules are validated // (i.e., resolved) in order to update the bundle state. searchPolicy.addValidationListener(new ValidationListener() { public void moduleValidated(ModuleEvent event) { synchronized (m_quickLock) { try { long id = BundleInfo.getBundleIdFromModuleId( event.getModule().getId()); if (id >= 0) { // Update the bundle's state to resolved when the // current module is resolved; just ignore resolve // events for older revisions since this only occurs // when an update is done on an unresolved bundle // and there was no refresh performed. BundleImpl bundle = (BundleImpl) getBundle(id); if (bundle.getInfo().getCurrentModule() == event.getModule()) { bundle.getInfo().setState(Bundle.RESOLVED); } } } catch (NumberFormatException ex) { // Ignore. } } } public void moduleInvalidated(ModuleEvent event) { // We can ignore this, because the only time it // should happen is when a refresh occurs. The // refresh operation resets the bundle's state // by calling BundleInfo.reset(), thus it is not // necessary for us to reset the bundle's state // here. } }); // Oscar is now in its startup sequence. m_oscarStatus = STARTING_STATUS; // Turn on error information... m_errorOut = System.err; // Initialize private members. m_dispatchQueue = new OscarDispatchQueue(); m_installedBundleMap = new HashMap(); // Set up the framework properties. m_frameworkPropMap = new CaseInsensitiveMap(); initializeOsgiProperties(); initializeBundleProperties(); // Before we reload any cached bundles, let's create a system // bundle that is responsible for providing specific container // related services. SystemBundle systembundle = null; try { // Create a simple bundle info for the system bundle. BundleInfo info = new BundleInfo( new SystemBundleArchive(), null); systembundle = new SystemBundle(this, info, activatorList); systembundle.getInfo().addModule( m_mgr.addModule( "0", systembundle.getAttributes(), systembundle.getResourceSources(), systembundle.getLibrarySources())); m_installedBundleMap.put( systembundle.getInfo().getLocation(), systembundle); // Manually validate the System Bundle, which will cause its // state to be set to RESOLVED. try { searchPolicy.validate(systembundle.getInfo().getCurrentModule()); } catch (ValidationException ex) { // This should never happen. int[] v = (int[]) ex.getVersion(); throw new BundleException("Unresolved package: " + ex.getIdentifier() + "; specification-version=\"" + v[0] + "." + v[1] + "." + v[2] + "\""); } // Start the system bundle; this will set its state // to STARTING, we must set its state to ACTIVE after // all bundles are restarted below according to the spec. systembundle.start(); } catch (Exception ex) { m_mgr = null; Oscar.error("Unable to start system bundle: " + ex); throw new RuntimeException("Unable to start system bundle."); } // Reload and cached bundles. BundleArchive[] archives = null; // First get cached bundle identifiers. try { archives = m_cache.getArchives(); } catch (Exception ex) { Oscar.error("Oscar: Unable to list saved bundles: " + ex); archives = null; } BundleImpl bundle = null; // Now install all cached bundles. for (int i = 0; (archives != null) && (i < archives.length); i++) { // Make sure our id generator is not going to overlap. // TODO: This is not correct since it may lead to re-used // ids, which is not okay according to OSGi. m_nextId = Math.max(m_nextId, archives[i].getId() + 1); try { // It is possible that a bundle in the cache was previously // uninstalled, but not completely deleted (perhaps because // of a crash or a locked file), so if we see an archive // with an UNINSTALLED persistent state, then try to remove // it now. if (archives[i].getPersistentState() == Bundle.UNINSTALLED) { m_cache.remove(archives[i]); } // Otherwise re-install the cached bundle. else { // Install the cached bundle. bundle = (BundleImpl) installBundle( archives[i].getId(), archives[i].getLocation(), null); } } catch (BundleException ex) { fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex); try { Oscar.error("Oscar: Unable to re-install " + archives[i].getLocation()); } catch (Exception ex2) { Oscar.error("Oscar: Unable to re-install bundle " + archives[i].getId()); } Oscar.error("Oscar: " + ex); // TODO: Perhaps we should remove the cached bundle? } catch (Exception ex) { fireFrameworkEvent(FrameworkEvent.ERROR, bundle, ex); try { Oscar.error("Oscar: Exception while re-installing " + archives[i].getLocation()); } catch (Exception ex2) { Oscar.error("Oscar: Exception while re-installing bundle " + archives[i].getId()); } // TODO: Perhaps we should remove the cached bundle? } } // Get the framework's default start level. int startLevel = OscarConstants.FRAMEWORK_DEFAULT_STARTLEVEL; String s = getConfigProperty(OscarConstants.FRAMEWORK_STARTLEVEL_PROP); if (s != null) { try { startLevel = Integer.parseInt(s); } catch (NumberFormatException ex) { startLevel = OscarConstants.FRAMEWORK_DEFAULT_STARTLEVEL; } } // Load bundles from auto-install and auto-start properties; processAutoProperties(); // This will restart bundles if necessary. setStartLevelInternal(startLevel); // Oscar is now running. m_oscarStatus = RUNNING_STATUS; // Set the system bundle state to ACTIVE. systembundle.getInfo().setState(Bundle.ACTIVE); // Fire started event for system bundle. fireBundleEvent(BundleEvent.STARTED, systembundle); // Send a framework event to indicate Oscar has started. fireFrameworkEvent(FrameworkEvent.STARTED, getBundle(0), null); } /** * This method cleanly shuts down Oscar, it must be called at the * end of a session in order to shutdown all active bundles. **/ public void shutdown() { if (System.getSecurityManager() != null) { AccessController.checkPermission(m_adminPerm); } // Change Oscar status from running to stopping. synchronized (m_adminLock) { // If Oscar is not running, then just return. if (m_oscarStatus != RUNNING_STATUS) { return; } // Oscar is now in its shutdown sequence. m_oscarStatus = STOPPING_STATUS; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -