📄 fwservicestarter.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.fwService.starter;import java.lang.reflect.Method;import java.util.Vector;import org.butor.config.IConfig;import org.butor.config.IConfigService;import org.butor.config.lowlevel.IProperties;import org.butor.fwService.FwServiceImpl;import org.butor.fwService.IFwServiceImpl;import org.butor.helper.PropertiesHelper;import org.butor.log.ILog;import org.butor.log.Log;/** * The main purpose of this object is to create at startup the thread * that will monitor the ServiceClass objects. * * @author Denis Benoit */public class FwServiceStarter { public static final String PROPERTY_SERVICE_STARTER = "service_starter"; public static final String PROPERTY_SERVICE_LIST = "service_list"; public static final String PROPERTY_STARTUP_SLEEP_TIME = "startup_sleep_time"; public static final String PROPERTY_IS_ALIVE_TIME = "is_alive_time"; public static final int PROPERTY_DEFAULT_STARTUP_SLEEP_TIME = 0; public static final int PROPERTY_DEFAULT_IS_ALIVE_TIME = 1000; protected Vector f_serviceList = new Vector(10, 10); protected FwServiceMonitor f_monitorThread = null; protected IFwServiceStarterListener f_serviceStarterListener = null; /** * Config to find above properties. */ protected IConfig f_config = null; protected String[] f_serviceNameList = null; protected int f_startSleepTime = 0; protected int f_isAliveTime = 0; /** * For every service class listed in the service property * file. This method will: * 1- Load the service class in the java virtual machine; * 2- Create an instance of the service class; * 3- Call the service class init method; * 4- Add the service class instance to the vector of * service classes; */ private synchronized void startServices() { if ((f_serviceNameList == null) || (f_serviceNameList.length <= 0)) { return; } for (int i = 0; i < f_serviceNameList.length; i++) { if (f_serviceStarterListener != null) { f_serviceStarterListener.serviceStartedEvent(f_serviceNameList[i], (i +1), f_serviceNameList.length); } // get service class String propertyList = FwServiceImpl.PROPERTY_PREFIX_SERVICE + f_serviceNameList[i]; IProperties props = f_config.getPropertyList(propertyList); if (null == props) { Log.logStr(this, Log.ERROR, "startServices()", "Config property list not found [" +propertyList + "]"); continue; } // service impl class name String implClassName = props.getProperty(FwServiceImpl.PROPERTY_IMPL_CLASS, null); if (null == implClassName) { Log.logStr(this, Log.ERROR, "startServices()", "Config property not found [" +FwServiceImpl.PROPERTY_IMPL_CLASS+ "] in property list [" +propertyList +"]"); continue; } // service class name String serviceClassName = props.getProperty(FwServiceImpl.PROPERTY_SERVICE_CLASS, null); if (null == serviceClassName) { Log.logStr(this, Log.ERROR, "startServices()", "Config property not found [" +FwServiceImpl.PROPERTY_SERVICE_CLASS+ "] in property list [" +propertyList +"]"); continue; } // service isAlive period. // is alive must be disabled if period <= 0. int serviceIsAlivePeriod = props.getPropertyInt(FwServiceImpl.PROPERTY_IS_ALIVE_PERIOD, 0); try { // service facade with static methods // valid presence of minimal methods Class serviceClass = Class.forName(serviceClassName); Class[] argClasses = new Class[] {IFwServiceImpl.class}; Method registerImpl = serviceClass.getMethod("registerImpl", argClasses); Method unregisterImpl = serviceClass.getMethod("unregisterImpl", argClasses); Method isImplCompatible = serviceClass.getMethod("isImplCompatible", argClasses); Method getServiceName = serviceClass.getMethod("getServiceName", null); if (registerImpl == null) { throw new Exception("Missing registerImpl() method in service " +serviceClassName +"!"); } if (unregisterImpl == null) { throw new Exception("Missing unregisterImpl() method in service " +serviceClassName +"!"); } if (isImplCompatible == null) { throw new Exception("Missing isImplCompatible() method in service " +serviceClassName +"!"); } if (getServiceName == null) { throw new Exception("Missing getServiceName() method in service " +serviceClassName +"!"); } // get an instance of the service implementation Class implClass = Class.forName(implClassName); FwServiceImpl serv = (FwServiceImpl) implClass.newInstance(); serv.initService(f_serviceNameList[i], serviceIsAlivePeriod, unregisterImpl); // special init for config service. if (serv instanceof IConfigService) { ((IConfigService)serv).init(f_config); } // register by invoquing the static register() of the service registerImpl.invoke(null, new Object[] {serv}); // if log service, log "startup message" so we can see it in the log file. if (serv instanceof ILog) { Log.logStr(this, Log.INFO, "startServices()", "***************************** STARTUP *****************************"); } f_serviceList.addElement(serv); FwServiceMonitor.register(serv); } catch (Exception e) { Log.logException( this, Log.LOG_TYPE_ERROR, "startServices()" + " Service: " +f_serviceNameList[i], e); } // wait before starting next service. if (f_startSleepTime > 0 && i<f_serviceNameList.length) { try { Thread.sleep(f_startSleepTime); } catch (InterruptedException e) { // Ok } } } } /** * Default constructor */ public FwServiceStarter(IConfig config) { this(config, null); } /** * Default constructor */ public FwServiceStarter(IConfig config, IFwServiceStarterListener listener) { super(); PropertiesHelper.loadSystemProperties(); Log.logStr(this, Log.LOG_TYPE_INFO, "FwServiceStarter", "Starting ..."); f_serviceStarterListener = listener; if (f_serviceStarterListener != null) { Log.logStr(this, Log.LOG_TYPE_INFO, "FwServiceStarter", "registered org.butor.web.listener " +listener); } if (null == config) { Log.logStr(this, Log.LOG_TYPE_ERROR, "FwServiceStarter", "Got NULL Config !"); return; } f_config = config; IProperties services = f_config.getPropertyList( PROPERTY_SERVICE_STARTER); if (null == services) { Log.logStr(this, Log.LOG_TYPE_ERROR, "FwServiceStarter", "Property list not found [" +PROPERTY_SERVICE_STARTER +"]"); return; } f_startSleepTime = services.getPropertyInt( PROPERTY_STARTUP_SLEEP_TIME, PROPERTY_DEFAULT_STARTUP_SLEEP_TIME); if (f_startSleepTime < PROPERTY_DEFAULT_STARTUP_SLEEP_TIME) { f_startSleepTime = PROPERTY_DEFAULT_STARTUP_SLEEP_TIME; } f_isAliveTime = services.getPropertyInt( PROPERTY_IS_ALIVE_TIME, PROPERTY_DEFAULT_IS_ALIVE_TIME); if (f_isAliveTime < PROPERTY_DEFAULT_IS_ALIVE_TIME) { f_startSleepTime = PROPERTY_DEFAULT_IS_ALIVE_TIME; } IProperties plist = f_config.getPropertyList( PROPERTY_SERVICE_LIST); String[] serviceList = null; if (plist != null) { serviceList = new String[plist.size()]; for (int i = 0; i < plist.size(); i++) { serviceList[i] = (String) plist.getProperty(i); } } startMonitor(); f_serviceNameList = serviceList; if ((null == f_serviceNameList) || (f_serviceNameList.length <= 0)) { Log.logStr(this, Log.LOG_TYPE_WARN, "FwServiceStarter", "No services to start!"); } else { startServices(); } } /** * This method starts the daemon thread that will periodically check * the sanity of the service classes of the system by calling * their isAlive() methods. */ private synchronized void startMonitor() { f_monitorThread = new FwServiceMonitor(f_isAliveTime); if (f_monitorThread == null) { Log.logStr( this, Log.LOG_TYPE_ERROR, "startMonitor()", "Can't start FwServiceMonitor!"); } } /** * Shutdown and release all resources. */ public void shutdown() { if (f_monitorThread == null) { return; } f_monitorThread.shutdown(); f_monitorThread = null; Log.logStr(this, Log.LOG_TYPE_INFO, "shutdown()", "Stopped."); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -