1 24 25 package org.objectweb.cjdbc.controller.core; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.net.InetAddress ; 30 import java.net.URL ; 31 import java.net.URLDecoder ; 32 import java.net.UnknownHostException ; 33 import java.util.Hashtable ; 34 35 import org.apache.commons.cli.CommandLine; 36 import org.apache.commons.cli.CommandLineParser; 37 import org.apache.commons.cli.GnuParser; 38 import org.apache.commons.cli.HelpFormatter; 39 import org.apache.commons.cli.Option; 40 import org.apache.commons.cli.OptionGroup; 41 import org.apache.commons.cli.Options; 42 import org.apache.commons.cli.ParseException; 43 import org.objectweb.cjdbc.common.i18n.Translate; 44 import org.objectweb.cjdbc.common.jmx.JmxConstants; 45 import org.objectweb.cjdbc.common.jmx.JmxException; 46 import org.objectweb.cjdbc.common.log.Trace; 47 import org.objectweb.cjdbc.common.net.SSLConfiguration; 48 import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator; 49 import org.objectweb.cjdbc.controller.core.security.ControllerSecurityManager; 50 import org.objectweb.cjdbc.controller.jmx.HttpAdaptor; 51 import org.objectweb.cjdbc.controller.jmx.MBeanServerManager; 52 import org.objectweb.cjdbc.controller.jmx.RmiConnector; 53 import org.objectweb.cjdbc.controller.monitoring.datacollector.DataCollector; 54 import org.objectweb.cjdbc.controller.xml.ControllerParser; 55 56 65 public class ControllerFactory extends Hashtable 66 { 67 private static final long serialVersionUID = -3766086549425915891L; 68 69 72 73 public static final String RMI_PORT = "rmiPort"; 74 75 76 public static final String JMX_PORT = "jmxPort"; 77 78 79 public static final String JMX_ENABLE = "jmxEnable"; 80 81 82 public static final String XML_FILE = "xmlFile"; 83 84 85 public static final String CONTROLLER_IP = "controllerIP"; 86 87 88 public static final String CONTROLLER_PORT = "controllerPort"; 89 90 91 public static final String CONTROLLER_BACKLOG = "controllerBackLogSize"; 92 93 94 public static final String ADD_DRIVER_ENABLE = "addDriverEnable"; 95 96 97 static Trace logger = Trace 98 .getLogger(Controller.class 99 .getName()); 100 101 private Controller controller = null; 102 103 108 public ControllerFactory(String [] args) 109 { 110 System.setProperty("org.xml.sax.driver", 111 "org.apache.crimson.parser.XMLReaderImpl"); 112 113 URL defaultControllerXmlFile = ControllerFactory.class.getResource("/" 114 + ControllerConstants.DEFAULT_CONFIG_FILE); 115 if (defaultControllerXmlFile == null) 116 logger 117 .warn("Unable to find default controller.xml configuration file in CLASSPATH."); 118 else 119 { 120 String file = URLDecoder.decode(defaultControllerXmlFile.getFile()); 121 this.put(XML_FILE, file); 122 } 123 this.put(CONTROLLER_IP, ControllerConstants.DEFAULT_IP); 124 this.put(CONTROLLER_PORT, "" + ControllerConstants.DEFAULT_PORT); 125 this.put(CONTROLLER_BACKLOG, "" + ControllerConstants.DEFAULT_BACKLOG_SIZE); 126 127 Options options = createOptions(); 129 130 CommandLineParser parser = new GnuParser(); 132 CommandLine commandLine = null; 133 try 134 { 135 commandLine = parser.parse(options, args); 136 } 137 catch (ParseException e) 138 { 139 logger.fatal(Translate.get("controller.configure.commandline.error", e), 140 e); 141 printUsage(options); 142 Runtime.getRuntime().exit(1); 143 } 144 145 int n = commandLine.getArgs().length; 147 for (int i = 0; i < n; i++) 148 { 149 logger.fatal(Translate.get("controller.configure.unknown.option", 150 commandLine.getArgs()[i])); 151 printUsage(options); 152 Runtime.getRuntime().exit(1); 153 } 154 if (commandLine.hasOption('h')) 156 { 157 if (commandLine.getOptions().length > 1) 158 logger.fatal(Translate.get("controller.configure.commandline.error")); 159 160 printUsage(options); 161 Runtime.getRuntime().exit(1); 162 } 163 164 if (commandLine.hasOption('v')) 166 { 167 if (commandLine.getOptions().length > 1) 168 { 169 logger.fatal(Translate.get("controller.configure.commandline.error")); 170 printUsage(options); 171 } 172 else 173 logger.info(Controller.getVersion()); 174 Runtime.getRuntime().exit(1); 175 } 176 177 if (commandLine.hasOption('r')) 179 { 180 String s = commandLine.getOptionValue('r'); 181 if (s != null) 182 { 183 this.put(JMX_ENABLE, "true"); 184 this.put(RMI_PORT, s); 185 this.put(JmxConstants.ADAPTOR_TYPE_RMI, s); 186 } 187 } 188 189 if (commandLine.hasOption('j')) 191 { 192 String s = commandLine.getOptionValue('j'); 193 if (s != null) 194 { 195 this.put(JMX_ENABLE, "true"); 196 this.put(JMX_PORT, s); 197 this.put(JmxConstants.ADAPTOR_TYPE_HTTP, s); 198 } 199 } 200 201 if (commandLine.hasOption('i')) 203 { 204 String ipAddress = commandLine.getOptionValue('i'); 205 if (ipAddress != null) 206 this.put(CONTROLLER_IP, ipAddress); 207 } 208 209 if (commandLine.hasOption('p')) 211 { 212 String port = commandLine.getOptionValue('p'); 213 if (port != null) 214 this.put(CONTROLLER_PORT, port); 215 } 216 217 if (commandLine.hasOption('f')) 219 { 220 this.remove(XML_FILE); 222 String filePath = commandLine.getOptionValue('f'); 223 File f = new File (filePath); 224 logger.debug(f.getAbsolutePath()); 225 if (f.exists() == false || f.isFile() == false) 226 logger 227 .warn(Translate.get("controller.configure.optional.file.invalid")); 228 else 229 this.put(XML_FILE, filePath); 230 } 231 } 232 233 242 public void setUpByXml(String filename) throws Exception 243 { 244 logger.info(Translate.get("controller.configure.loading.file", filename)); 245 FileReader fileReader = null; 246 try 247 { 248 fileReader = new FileReader (filename); 249 ControllerParser cparser = new ControllerParser(this); 250 cparser.readXML(fileReader, true); 251 fileReader.close(); 252 } 253 catch (Exception e) 254 { 255 256 logger.warn(Translate.get("controller.configure.xml.file.error", e), e); 257 throw e; 258 } 259 finally 260 { 261 if (fileReader != null) 262 fileReader.close(); 263 } 264 } 265 266 274 private Controller setup() throws Exception 275 { 276 String xml = (String ) this.get(XML_FILE); 277 278 int portNumber = Integer.parseInt((String ) this.get(CONTROLLER_PORT)); 279 int backlog = Integer.parseInt((String ) this.get(CONTROLLER_BACKLOG)); 280 String ipAddress = (String ) this.get(CONTROLLER_IP); 281 282 controller = new Controller(ipAddress, portNumber, backlog); 283 controller.setConfiguration(this); 284 285 if (xml != null) 286 { 287 try 288 { 289 setUpByXml(xml); 290 } 291 catch (Exception e) 292 { 293 logger.error(Translate.get( 294 "controller.configure.load.file.failed.minimum.configuration", 295 new String []{xml, e.getMessage()}), e); 296 } 297 } 298 else 299 setUpJmx(); 300 301 return this.controller; 302 } 303 304 312 public Controller getController() throws Exception 313 { 314 if (controller == null) 315 setup(); 316 return this.controller; 317 } 318 319 324 public void setUpJmx() throws JmxException 325 { 326 boolean jmxEnable = new Boolean ((String ) get(JMX_ENABLE)).booleanValue(); 327 if (jmxEnable == false) 328 { 329 MBeanServerManager.setJmxEnabled(false); 330 logger.info(Translate.get("jmx.configure.disabled")); 331 } 332 else 333 { 334 MBeanServerManager.setJmxEnabled(true); 335 logger.info(Translate.get("jmx.configure.enabled")); 336 try 338 { 339 new DataCollector(controller); 340 String hostIP = controller.getIPAddress(); 341 342 logger.info(Translate.get("controller.configure.start.jmx", hostIP)); 343 344 if (this.containsKey(JmxConstants.ADAPTOR_TYPE_HTTP)) 345 { 346 int port = Integer.parseInt((String ) this 347 .get(JmxConstants.ADAPTOR_TYPE_HTTP)); 348 HttpAdaptor http = new HttpAdaptor(hostIP, port, null); 349 http.start(); 350 } 351 if (this.containsKey(JmxConstants.ADAPTOR_TYPE_RMI)) 352 { 353 SSLConfiguration ssl = null; 354 PasswordAuthenticator authenticator = null; 355 int port = Integer.parseInt((String ) this 356 .get(JmxConstants.ADAPTOR_TYPE_RMI)); 357 if (this.containsKey(JmxConstants.CONNECTOR_AUTH_USERNAME)) 358 { 359 String username = (String ) this 360 .get(JmxConstants.CONNECTOR_AUTH_USERNAME); 361 String password = (String ) this 362 .get(JmxConstants.CONNECTOR_AUTH_PASSWORD); 363 authenticator = new PasswordAuthenticator(username, password); 364 } 365 if (this.containsKey(JmxConstants.CONNECTOR_RMI_SSL)) 366 { 367 ssl = (SSLConfiguration) this.get(JmxConstants.CONNECTOR_RMI_SSL); 368 } 369 RmiConnector rmi = new RmiConnector(controller.getControllerName(), 370 hostIP, port, authenticator, ssl); 371 rmi.start(); 372 } 373 logger.debug(Translate.get("controller.configure.jmx.started")); 374 } 375 catch (Exception e) 376 { 377 logger 378 .error(Translate.get("controller.configure.jmx.fail.start", e), e); 379 } 380 } 381 controller.setJmxEnable(jmxEnable); 382 } 383 384 389 public void setUpSecurity(ControllerSecurityManager security) 390 { 391 controller.setSecurity(security); 392 } 393 394 403 public void setUpVirtualDatabase(String filePath, String virtualName, 404 int autoLoad, String checkPoint) 405 { 406 try 407 { 408 controller.loadXmlConfiguration(filePath, virtualName, autoLoad, 409 checkPoint); 410 if (logger.isDebugEnabled()) 411 logger.debug(Translate.get("controller.configure.file.autoload", 412 new String []{filePath, "" + autoLoad})); 413 414 } 415 catch (Exception e) 416 { 417 logger.error(Translate.get("controller.configure.load.file.failed", 418 new String []{filePath, e.getMessage()}), e); 419 } 420 } 421 422 427 private static void printUsage(Options options) 428 { 429 String header = Translate.get("controller.commandline.header"); 430 header += System.getProperty("line.separator"); 431 header += Translate.get("controller.commandline.options"); 432 String footer = Translate.get("controller.commandline.footer"); 433 434 (new HelpFormatter()).printHelp(80, "controller(.sh|.bat) [options]", 435 header, options, footer); 436 } 437 438 444 private static Options createOptions() 445 { 446 Options options = new Options(); 447 OptionGroup group = new OptionGroup(); 448 449 group.addOption(new Option("h", "help", false, Translate 451 .get("controller.commandline.option.help"))); 452 group.addOption(new Option("v", "version", false, Translate 453 .get("controller.commandline.option.version"))); 454 options.addOptionGroup(group); 455 456 options.addOption(new Option("r", "rmi", true, Translate.get( 458 "controller.commandline.option.rmi", "" 459 + JmxConstants.DEFAULT_JMX_RMI_PORT))); 460 options.addOption(new Option("j", "jmx", true, Translate.get( 462 "controller.commandline.option.jmx", "" 463 + JmxConstants.DEFAULT_JMX_HTTP_PORT))); 464 465 String defaultIp = "127.0.0.1"; 467 try 468 { 469 defaultIp = InetAddress.getLocalHost().getHostAddress(); 470 } 471 catch (UnknownHostException e) 472 { 473 474 } 475 options.addOption(new Option("i", "ip", true, Translate.get( 476 "controller.commandline.option.ip", "" + defaultIp))); 477 478 options.addOption(new Option("p", "port", true, Translate.get( 480 "controller.commandline.option.port", "" 481 + ControllerConstants.DEFAULT_PORT))); 482 483 options.addOption(new Option("f", "file", true, Translate 485 .get("controller.commandline.option.file"))); 486 487 return options; 488 } 489 490 } | Popular Tags |