⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runlevelmanager.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                  if (numErrors == 0)                     log.fine("Successful shutdown to run level=" + toRunlevelStr(dest) + Timestamp.millisToNice(elapsed));                  else                     log.info("Shutdown to run level=" + toRunlevelStr(dest) + " done with " + numErrors + " errors.");               }            }         }      }      if (log.isLoggable(Level.FINER)) log.finer("Leaving changeRunlevel with runlevel = " + toRunlevelStr(currRunlevel));       return numErrors;   }   /**    *    */   private void startupPlugins(int from, int to) throws XmlBlasterException {      TreeSet pluginSet = this.glob.getPluginHolder().getStartupSequence(this.glob.getStrippedId(), from+1, to);      if (log.isLoggable(Level.FINER)) log.finer("startupPlugins. the size of the plugin set is '" + pluginSet.size() + "'");      Iterator iter = pluginSet.iterator();      while (iter.hasNext()) {         PluginConfig pluginConfig = (PluginConfig)iter.next();         if (pluginConfig == null) {             log.warning("startupPlugins. the pluginConfig object is null");            continue;         }         if (!pluginConfig.isCreate()) {            log.fine("startupPlugins. the plugin + " + pluginConfig.getId() + " is ignored, create='false'");            continue;         }         if (log.isLoggable(Level.FINEST)) log.finest("startupPlugins " + pluginConfig.toXml());         try {            long startTime = System.currentTimeMillis();            PluginInfo pluginInfo = pluginConfig.getPluginInfo();            if (log.isLoggable(Level.FINER)) {               if (pluginInfo != null) {                  log.finer("startupPlugins pluginInfo object: " + pluginInfo.getId() + " classname: " + pluginInfo.getClassName());               }               else log.finer("startupPlugins: the pluginInfo is null");            }            this.glob.getPluginManager().getPluginObject(pluginInfo);            long deltaTime = System.currentTimeMillis() - startTime;            log.fine("Run level '" + from + "' to '" + to + "' plugin '" + pluginConfig.getId() + "' successful loaded in '" + deltaTime + "' ms");         }         catch (Throwable ex) {            ErrorCode code = pluginConfig.getUpAction().getOnFail();            if (code == null) {               log.warning("Exception when loading the plugin '" + pluginConfig.getId() + "' reason: " + ex.toString());               ex.printStackTrace();            }            else {               throw new XmlBlasterException(this.glob, code, ME + ".startupPlugins",  "Can't load plugin '" + pluginConfig.getId() + "'", ex);            }         }      }   }      /**    * Called by JMX, throws IllegalArgumentExcetion instead of XmlBlasterException.    * @param pluginConfig    * @param create    */   void toggleCreate(PluginConfig pluginConfig, boolean create) {      log.info("Changing plugin '" + pluginConfig.getId() + "' create=" + pluginConfig.isCreate() + " to " + create);      if (pluginConfig.isCreate() != create) {         if (create) {            try {               this.glob.getPluginManager().getPluginObject(pluginConfig.getPluginInfo());            }            catch (XmlBlasterException e) {               log.warning("Failed to create plugin: " + e.toString());               throw new IllegalArgumentException("Failed to create plugin: " + e.toString());            }            catch (Throwable e) {               e.printStackTrace();            }         }         else {            try {               I_Plugin plugin = this.glob.getPluginManager().getPluginObject(pluginConfig.getPluginInfo());               plugin.shutdown();               this.glob.getPluginManager().removeFromPluginCache(pluginConfig.getPluginInfo().getId());            }            catch (XmlBlasterException e) {               log.warning("Failed to remove plugin: " + e.toString());               throw new IllegalArgumentException("Failed to create plugin: " + e.toString());            }            catch (Throwable e) {               e.printStackTrace();            }         }      }   }   /**    * Add a new plugin, if it exists remove the old first.     * @param pluginConfig    */   private void addPlugin(PluginConfig pluginConfig) throws XmlBlasterException {      log.info("New runlevel plugin configuration arrived: " + pluginConfig.getPluginInfo().getId());      I_Plugin oldPlugin = this.glob.getPluginManager().removeFromPluginCache(pluginConfig.getPluginInfo().getId());            PluginHolder holder = this.glob.getPluginHolder();      PluginConfig oldConfig = holder.removePluginConfig(null, pluginConfig.getId());      if (oldConfig != null)         log.info("Removed old plugin " + oldConfig.getId());      if (oldPlugin != null && oldConfig == null)         log.severe("Unexpected plugin cache entry:" + oldPlugin.getType());      holder.addDefaultPluginConfig(pluginConfig);            pluginConfig.registerMBean();      if (pluginConfig.isCreate())         this.glob.getPluginManager().getPluginObject(pluginConfig.getPluginInfo());   }   /**    *    */   private void shutdownPlugins(int from, int to) throws XmlBlasterException {      TreeSet pluginSet = this.glob.getPluginHolder().getShutdownSequence(this.glob.getStrippedId(), to, from-1);      Iterator iter = pluginSet.iterator();      while (iter.hasNext()) {         PluginConfig pluginConfig = (PluginConfig)iter.next();         if (pluginConfig == null || !pluginConfig.isCreate())            continue;         try {            PluginInfo pluginInfo = pluginConfig.getPluginInfo();            I_Plugin plugin = this.glob.getPluginManager().getPluginObject(pluginInfo);            plugin.shutdown();            this.glob.getPluginManager().removeFromPluginCache(pluginInfo.getId());            log.fine("fireRunlevelEvent: run level '" + from + "' to '" + to + "' plugin '" + pluginConfig.getId() + "' shutdown");         }         catch (Throwable ex) {            ErrorCode code = pluginConfig.getDownAction().getOnFail();            if (code == null) {               log.warning(".fireRunlevelEvent. Exception when shutting down the plugin '" + pluginConfig.getId() + "' reason: " + ex.toString());            }            else {               throw new XmlBlasterException(this.glob, code, ME + ".fireRunlevelEvent",  ".fireRunlevelEvent. Exception when shutting down the plugin '" + pluginConfig.getId() + "'", ex);            }         }      }   }   /**    * The static plugins are loaded from (exclusive) to (inclusive) when startup and    * the same when shutting down. For example if you define LOAD on r 3, and STOP on    * r 2, then LOAD is fired when from=2,to=3 and STOP when from=3,to=2    */   private final int fireRunlevelEvent(int from, int to, boolean force) throws XmlBlasterException {      int numErrors = 0;      // Take a snapshot of current listeners (to avoid ConcurrentModificationException in iterator)      I_RunlevelListener[] listeners;      synchronized (runlevelListenerSet) {         if (runlevelListenerSet.size() == 0)            return numErrors;         listeners = (I_RunlevelListener[])runlevelListenerSet.toArray(DUMMY_ARR);      }      for (int ii=0; ii<listeners.length; ii++) {         I_RunlevelListener li = listeners[ii];         try {            li.runlevelChange(from, to, force);            if (log.isLoggable(Level.FINE)) {               if (isMajorLevel(to)) {                  if (from < to)                     log.fine(li.getName() + " successful startup to run level=" + to + ", errors=" + numErrors + ".");                  else                     log.fine(li.getName() + " successful shutdown to run level=" + to + ", errors=" + numErrors + ".");               }            }         }         catch (XmlBlasterException e) {            if (e.isInternal()) {               log.severe("Changing from run level=" + from + " to level=" + to + " failed for component " + li.getName() + ": " + e.getMessage());            }            else {               log.warning("Changing from run level=" + from + " to level=" + to + " failed for component " + li.getName() + ": " + e.getMessage());            }            numErrors++;         }      }      return numErrors;   }   /**    * See java for runlevels    */   public final int getCurrentRunlevel() {      return currRunlevel;   }   public boolean isHalted() {      return currRunlevel <= RUNLEVEL_HALTED;   }   public boolean isStandby() {      return currRunlevel == RUNLEVEL_STANDBY;   }   public boolean isCleanup() {      return currRunlevel == RUNLEVEL_CLEANUP;   }   public boolean isRunning() {      return currRunlevel == RUNLEVEL_RUNNING;   }   /**    * @return true if one of the major run levels. false if pre or post event level    */   public boolean isMajorLevel() {      return isMajorLevel(currRunlevel);   }   //======== static methods ============   private static final boolean isMajorLevel(int level) {      if (level == RUNLEVEL_HALTED || level == RUNLEVEL_STANDBY ||          level == RUNLEVEL_CLEANUP || level == RUNLEVEL_RUNNING)         return true;      return false;   }   /**    * @return true if one of the major levels    */   public static final boolean checkRunlevel(int level) {      return isMajorLevel(level);   }   /**    * Parses given string to extract the priority of a message    * @param level For example 7    * @return "RUNLEVEL_UNKNOWN" if no valid run level, else for    * example "STANDBY_POST"    */   public final static String toRunlevelStr(int level) {      if (level == RUNLEVEL_HALTED_PRE)         return "HALTED_PRE";      else if (level == RUNLEVEL_HALTED)         return "HALTED";      else if (level == RUNLEVEL_HALTED_POST)         return "HALTED_POST";      else if (level == RUNLEVEL_STANDBY_PRE)         return "STANDBY_PRE";      else if (level == RUNLEVEL_STANDBY)         return "STANDBY";      else if (level == RUNLEVEL_STANDBY_POST)         return "STANDBY_POST";      else if (level == RUNLEVEL_CLEANUP_PRE)         return "CLEANUP_PRE";      else if (level == RUNLEVEL_CLEANUP)         return "CLEANUP";      else if (level == RUNLEVEL_CLEANUP_POST)         return "CLEANUP_POST";      else if (level == RUNLEVEL_RUNNING_PRE)         return "RUNNING_PRE";      else if (level == RUNLEVEL_RUNNING)         return "RUNNING";      else if (level == RUNLEVEL_RUNNING_POST)         return "RUNNING_POST";      else         return "RUNLEVEL_UNKNOWN(" + level + ")";   }   /**    * Parses given string to extract the priority of a message    * @param level For example "STANDBY" or 7    * @param defaultPriority Value to use if not parseable    * @return -10 if no valid run level    */   public final static int toRunlevelInt(String level) {      if (level == null) return -10;      level = level.trim();      try {         return Integer.parseInt(level);      }      catch(NumberFormatException e) {}      if (level.equalsIgnoreCase("HALTED_PRE"))         return RUNLEVEL_HALTED_PRE;      else if (level.equalsIgnoreCase("HALTED"))         return RUNLEVEL_HALTED;      else if (level.equalsIgnoreCase("HALTED_POST"))         return RUNLEVEL_HALTED_POST;      else if (level.equalsIgnoreCase("STANDBY_PRE"))         return RUNLEVEL_STANDBY_PRE;      else if (level.equalsIgnoreCase("STANDBY"))         return RUNLEVEL_STANDBY;      else if (level.equalsIgnoreCase("STANDBY_POST"))         return RUNLEVEL_STANDBY_POST;      else if (level.equalsIgnoreCase("CLEANUP_PRE"))         return RUNLEVEL_CLEANUP_PRE;      else if (level.equalsIgnoreCase("CLEANUP"))         return RUNLEVEL_CLEANUP;      else if (level.equalsIgnoreCase("CLEANUP_POST"))         return RUNLEVEL_CLEANUP_POST;      else if (level.equalsIgnoreCase("RUNNING_PRE"))         return RUNLEVEL_RUNNING_PRE;      else if (level.equalsIgnoreCase("RUNNING"))         return RUNLEVEL_RUNNING;      else if (level.equalsIgnoreCase("RUNNING_POST"))         return RUNLEVEL_RUNNING_POST;      else         return -10;   }      public void shutdown() {      if (this.mbeanHandle != null)         this.glob.unregisterMBean(this.mbeanHandle);   }   /* (non-Javadoc)    * @see org.xmlBlaster.util.admin.I_AdminUsage#usage()    */   public java.lang.String usage() {      return ServerScope.getJmxUsageLinkInfo(this.getClass().getName(), null);   }   /* (non-Javadoc)    * @see org.xmlBlaster.util.admin.I_AdminUsage#getUsageUrl()    */   public java.lang.String getUsageUrl() {      return ServerScope.getJavadocUrl(this.getClass().getName(), null);   }   /* (non-Javadoc)    * JMX dummy to have a copy/paste functionality in jconsole    * @see org.xmlBlaster.util.admin.I_AdminUsage#setUsageUrl(java.lang.String)    */   public void setUsageUrl(java.lang.String url) {}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -