src/org/objectweb/cjdbc/controller/core/ControllerFactory.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.core; 00026 00027 import java.io.File; 00028 import java.io.FileReader; 00029 import java.net.InetAddress; 00030 import java.net.URL; 00031 import java.net.URLDecoder; 00032 import java.net.UnknownHostException; 00033 import java.util.Hashtable; 00034 00035 import org.apache.commons.cli.CommandLine; 00036 import org.apache.commons.cli.CommandLineParser; 00037 import org.apache.commons.cli.GnuParser; 00038 import org.apache.commons.cli.HelpFormatter; 00039 import org.apache.commons.cli.Option; 00040 import org.apache.commons.cli.OptionGroup; 00041 import org.apache.commons.cli.Options; 00042 import org.apache.commons.cli.ParseException; 00043 import org.objectweb.cjdbc.common.i18n.Translate; 00044 import org.objectweb.cjdbc.common.jmx.JmxConstants; 00045 import org.objectweb.cjdbc.common.jmx.JmxException; 00046 import org.objectweb.cjdbc.common.log.Trace; 00047 import org.objectweb.cjdbc.common.net.SSLConfiguration; 00048 import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator; 00049 import org.objectweb.cjdbc.controller.core.security.ControllerSecurityManager; 00050 import org.objectweb.cjdbc.controller.jmx.HttpAdaptor; 00051 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager; 00052 import org.objectweb.cjdbc.controller.jmx.RmiConnector; 00053 import org.objectweb.cjdbc.controller.monitoring.datacollector.DataCollector; 00054 import org.objectweb.cjdbc.controller.xml.ControllerParser; 00055 00065 public class ControllerFactory extends Hashtable { 00070 public static final String RMI_PORT = "rmiPort"; 00071 00073 public static final String JMX_PORT = "jmxPort"; 00074 00076 public static final String JMX_ENABLE = "jmxEnable"; 00077 00079 public static final String XML_FILE = "xmlFile"; 00080 00082 public static final String CONTROLLER_IP = "controllerIP"; 00083 00085 public static final String CONTROLLER_PORT = "controllerPort"; 00086 00088 public static final String CONTROLLER_BACKLOG = "controllerBackLogSize"; 00089 00091 public static final String ADD_DRIVER_ENABLE = "addDriverEnable"; 00092 00094 static Trace logger = Trace.getLogger(Controller.class.getName()); 00095 00096 private Controller controller = null; 00097 00103 public ControllerFactory(String args[]) { 00104 System.setProperty("org.xml.sax.driver", 00105 "org.apache.crimson.parser.XMLReaderImpl"); 00106 00107 URL defaultControllerXmlFile = ControllerFactory.class.getResource("/" 00108 + ControllerConstants.DEFAULT_CONFIG_FILE); 00109 if (defaultControllerXmlFile == null) 00110 logger 00111 .warn("Unable to find default controller.xml configuration file in CLASSPATH."); 00112 else { 00113 String file = URLDecoder.decode(defaultControllerXmlFile.getFile()); 00114 this.put(XML_FILE, file); 00115 } 00116 this.put(CONTROLLER_IP, ControllerConstants.DEFAULT_IP); 00117 this.put(CONTROLLER_PORT, "" + ControllerConstants.DEFAULT_PORT); 00118 this.put(CONTROLLER_BACKLOG, "" 00119 + ControllerConstants.DEFAULT_BACKLOG_SIZE); 00120 00121 // Create options object 00122 Options options = createOptions(); 00123 00124 // Parse command line 00125 CommandLineParser parser = new GnuParser(); 00126 CommandLine commandLine = null; 00127 try { 00128 commandLine = parser.parse(options, args); 00129 } catch (ParseException e) { 00130 logger.fatal(Translate.get( 00131 "controller.configure.commandline.error", e), e); 00132 printUsage(options); 00133 Runtime.getRuntime().exit(1); 00134 } 00135 00136 // Non-recognized options 00137 int n = commandLine.getArgs().length; 00138 for (int i = 0; i < n; i++) { 00139 logger.fatal(Translate.get("controller.configure.unknown.option", 00140 commandLine.getArgs()[i])); 00141 printUsage(options); 00142 Runtime.getRuntime().exit(1); 00143 } 00144 // Handle --help option 00145 if (commandLine.hasOption('h')) { 00146 if (commandLine.getOptions().length > 1) 00147 logger.fatal(Translate 00148 .get("controller.configure.commandline.error")); 00149 00150 printUsage(options); 00151 Runtime.getRuntime().exit(1); 00152 } 00153 00154 // Handle --version option 00155 if (commandLine.hasOption('v')) { 00156 if (commandLine.getOptions().length > 1) { 00157 logger.fatal(Translate 00158 .get("controller.configure.commandline.error")); 00159 printUsage(options); 00160 } else 00161 logger.info(Controller.getVersion()); 00162 Runtime.getRuntime().exit(1); 00163 } 00164 00165 // Handle -rmi option 00166 if (commandLine.hasOption('r')) { 00167 String s = commandLine.getOptionValue('r'); 00168 if (s != null) { 00169 this.put(JMX_ENABLE, "true"); 00170 this.put(RMI_PORT, s); 00171 this.put(JmxConstants.ADAPTOR_TYPE_RMI, s); 00172 } 00173 } 00174 00175 // Handle -jmx option 00176 if (commandLine.hasOption('j')) { 00177 String s = commandLine.getOptionValue('j'); 00178 if (s != null) { 00179 this.put(JMX_ENABLE, "true"); 00180 this.put(JMX_PORT, s); 00181 this.put(JmxConstants.ADAPTOR_TYPE_HTTP, s); 00182 } 00183 } 00184 00185 // Handle --ip option 00186 if (commandLine.hasOption('i')) { 00187 String ipAddress = commandLine.getOptionValue('i'); 00188 if (ipAddress != null) 00189 this.put(CONTROLLER_IP, ipAddress); 00190 } 00191 00192 // Handle --port option 00193 if (commandLine.hasOption('p')) { 00194 String port = commandLine.getOptionValue('p'); 00195 if (port != null) 00196 this.put(CONTROLLER_PORT, port); 00197 } 00198 00199 // Handle -f option 00200 if (commandLine.hasOption('f')) { 00201 // If a config file is specified we ignore the default file. 00202 this.remove(XML_FILE); 00203 String filePath = commandLine.getOptionValue('f'); 00204 File f = new File(filePath); 00205 logger.debug(f.getAbsolutePath()); 00206 if (f.exists() == false || f.isFile() == false) 00207 logger.warn(Translate 00208 .get("controller.configure.optional.file.invalid")); 00209 else 00210 this.put(XML_FILE, filePath); 00211 } 00212 } 00213 00223 public void setUpByXml(String filename) throws Exception { 00224 logger.info(Translate 00225 .get("controller.configure.loading.file", filename)); 00226 try { 00227 FileReader fileReader = new FileReader(filename); 00228 ControllerParser cparser = new ControllerParser(this); 00229 cparser.readXML(fileReader, true); 00230 } catch (Exception e) { 00231 logger.warn( 00232 Translate.get("controller.configure.xml.file.error", e), e); 00233 throw e; 00234 } 00235 } 00236 00245 private Controller setup() throws Exception { 00246 String xml = (String) this.get(XML_FILE); 00247 00248 int portNumber = Integer.parseInt((String) this.get(CONTROLLER_PORT)); 00249 int backlog = Integer.parseInt((String) this.get(CONTROLLER_BACKLOG)); 00250 String ipAddress = (String) this.get(CONTROLLER_IP); 00251 00252 controller = new Controller(ipAddress, portNumber, backlog); 00253 controller.setConfiguration(this); 00254 00255 if (xml != null) { 00256 try { 00257 setUpByXml(xml); 00258 } catch (Exception e) { 00259 logger 00260 .error( 00261 Translate 00262 .get( 00263 "controller.configure.load.file.failed.minimum.configuration", 00264 new String[] { xml, 00265 e.getMessage() }), e); 00266 } 00267 } else 00268 setUpJmx(); 00269 00270 return this.controller; 00271 } 00272 00281 public Controller getController() throws Exception { 00282 if (controller == null) 00283 setup(); 00284 return this.controller; 00285 } 00286 00292 public void setUpJmx() throws JmxException { 00293 boolean jmxEnable = new Boolean((String) get(JMX_ENABLE)) 00294 .booleanValue(); 00295 if (jmxEnable == false) { 00296 MBeanServerManager.setJmxEnabled(false); 00297 logger.info(Translate.get("jmx.configure.disabled")); 00298 } else { 00299 MBeanServerManager.setJmxEnabled(true); 00300 logger.info(Translate.get("jmx.configure.enabled")); 00301 // Create and start the JMX agent 00302 try { 00303 new DataCollector(controller); 00304 String hostIP = controller.getIPAddress(); 00305 00306 logger.info(Translate.get("controller.configure.start.jmx", 00307 hostIP)); 00308 00309 if (this.containsKey(JmxConstants.ADAPTOR_TYPE_HTTP)) { 00310 int port = Integer.parseInt((String) this 00311 .get(JmxConstants.ADAPTOR_TYPE_HTTP)); 00312 HttpAdaptor http = new HttpAdaptor(hostIP, port, null, null); 00313 http.start(); 00314 } 00315 if (this.containsKey(JmxConstants.ADAPTOR_TYPE_RMI)) { 00316 SSLConfiguration ssl = null; 00317 PasswordAuthenticator authenticator = null; 00318 int port = Integer.parseInt((String) this 00319 .get(JmxConstants.ADAPTOR_TYPE_RMI)); 00320 if (this.containsKey(JmxConstants.CONNECTOR_AUTH_USERNAME)) { 00321 String username = (String) this 00322 .get(JmxConstants.CONNECTOR_AUTH_USERNAME); 00323 String password = (String) this 00324 .get(JmxConstants.CONNECTOR_AUTH_PASSWORD); 00325 authenticator = new PasswordAuthenticator(username, 00326 password); 00327 } 00328 if (this.containsKey(JmxConstants.CONNECTOR_RMI_SSL)) { 00329 ssl = (SSLConfiguration) this 00330 .get(JmxConstants.CONNECTOR_RMI_SSL); 00331 } 00332 RmiConnector rmi = new RmiConnector(controller.getName(), 00333 hostIP, port, authenticator, ssl); 00334 rmi.start(); 00335 } 00336 logger.debug(Translate.get("controller.configure.jmx.started")); 00337 } catch (Exception e) { 00338 logger.error(Translate.get( 00339 "controller.configure.jmx.fail.start", e), e); 00340 } 00341 } 00342 controller.setJmxEnable(jmxEnable); 00343 } 00344 00350 public void setUpSecurity(ControllerSecurityManager security) { 00351 controller.setSecurity(security); 00352 } 00353 00363 public void setUpVirtualDatabase(String filePath, String virtualName, 00364 int autoLoad, String checkPoint) { 00365 try { 00366 controller.loadXmlConfiguration(filePath, virtualName, autoLoad, 00367 checkPoint); 00368 if (logger.isDebugEnabled()) 00369 logger.debug(Translate.get( 00370 "controller.configure.file.autoload", new String[] { 00371 filePath, "" + autoLoad })); 00372 00373 } catch (Exception e) { 00374 logger.error(Translate.get("controller.configure.load.file.failed", 00375 new String[] { filePath, e.getMessage() }), e); 00376 } 00377 } 00378 00384 private static void printUsage(Options options) { 00385 String header = Translate.get("controller.commandline.header"); 00386 header += System.getProperty("line.separator"); 00387 header += Translate.get("controller.commandline.options"); 00388 String footer = Translate.get("controller.commandline.footer"); 00389 00390 (new HelpFormatter()).printHelp(80, "controller(.sh|.bat) [options]", 00391 header, options, footer); 00392 } 00393 00400 private static Options createOptions() { 00401 Options options = new Options(); 00402 OptionGroup group = new OptionGroup(); 00403 00404 // help and verbose options 00405 group.addOption(new Option("h", "help", false, Translate 00406 .get("controller.commandline.option.help"))); 00407 group.addOption(new Option("v", "version", false, Translate 00408 .get("controller.commandline.option.version"))); 00409 options.addOptionGroup(group); 00410 00411 // RMI port option 00412 options.addOption(new Option("r", "rmi", true, Translate.get( 00413 "controller.commandline.option.rmi", "" 00414 + JmxConstants.DEFAULT_JMX_RMI_PORT))); 00415 // JMX port option 00416 options.addOption(new Option("j", "jmx", true, Translate.get( 00417 "controller.commandline.option.jmx", "" 00418 + JmxConstants.DEFAULT_JMX_HTTP_PORT))); 00419 00420 // IP option 00421 String defaultIp = "127.0.0.1"; 00422 try { 00423 defaultIp = InetAddress.getLocalHost().getHostAddress(); 00424 } catch (UnknownHostException e) { 00425 00426 } 00427 options.addOption(new Option("i", "ip", true, Translate.get( 00428 "controller.commandline.option.ip", "" + defaultIp))); 00429 00430 // Port options 00431 options.addOption(new Option("p", "port", true, Translate.get( 00432 "controller.commandline.option.port", "" 00433 + ControllerConstants.DEFAULT_PORT))); 00434 00435 // configuration file option 00436 options.addOption(new Option("f", "file", true, Translate 00437 .get("controller.commandline.option.file"))); 00438 00439 return options; 00440 } 00441 00442 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:01 2004に生成されました。 doxygen 1.3.8