1 24 25 package org.objectweb.cjdbc.scenario.tools.components.controller; 26 27 import java.io.File ; 28 29 import org.objectweb.cjdbc.common.util.Constants; 30 import org.objectweb.cjdbc.controller.core.Controller; 31 import org.objectweb.cjdbc.controller.core.ControllerConstants; 32 import org.objectweb.cjdbc.controller.core.ControllerFactory; 33 import org.objectweb.cjdbc.scenario.tools.ScenarioConstants; 34 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface; 35 36 44 public class ControllerProcess implements ComponentInterface 45 { 46 String port; 47 String database; 48 Controller controller; 49 50 57 public ControllerProcess(String port, String database) throws Exception 58 { 59 this.port = port; 60 this.database = database; 61 start(); 62 } 63 64 67 public void start() throws Exception 68 { 69 String controllerConfig = getClass().getResource( 70 ScenarioConstants.CONTROLLER_DIR 71 + ScenarioConstants.CONTROLLER_DEFAULT_FILE).getPath(); 72 String [] args = new String []{"-p", port, "-f", controllerConfig}; 73 ControllerFactory factory = new ControllerFactory(args); 74 Controller controller = factory.getController(); 75 controller.setConfiguration(factory); 76 if (controller != null) 77 controller.launch(); 78 this.controller = controller; 79 } 80 81 84 public String getDatabase() 85 { 86 return database; 87 } 88 89 92 public void loadDatabase(String xml) throws Exception 93 { 94 this.loadDatabase(xml, ScenarioConstants.DEFAULT_VDB_NAME); 95 } 96 97 101 public void loadDatabase(String xml, String targetDB) throws Exception 102 { 103 File f = new File (xml); 104 if (!f.exists()) 105 { 106 try 107 { 108 f = new File (getClass().getResource( 109 ScenarioConstants.VIRTUALDATABASE_DIR + File.separator + xml) 110 .getFile()); 111 } 112 catch (Exception e) 113 { 114 throw new Exception ("could not find configuration file:" + xml); 115 } 116 if (!f.exists()) 117 throw new Exception ("could not find configuration file:" + xml); 118 } 119 if (!f.exists()) 120 throw new Exception ("File configuration not found"); 121 controller.loadXmlConfiguration(f.getAbsolutePath(), targetDB, 122 ControllerConstants.AUTO_ENABLE_TRUE, ""); 123 if (controller.hasVirtualDatabase(targetDB) == false) 124 throw new Exception ("Virtual Database Configuration failed"); 125 } 126 127 130 public void loadDatabase() throws Exception 131 { 132 loadDatabase(ScenarioConstants.DATABASE_CONFIG_FILE); 133 } 134 135 138 public String getPort() 139 { 140 return String.valueOf(controller.getPortNumber()); 141 } 142 143 146 public Object getProcess() 147 { 148 return controller; 149 } 150 151 154 public void release() 155 { 156 try 157 { 158 controller.shutdown(Constants.SHUTDOWN_SAFE); 159 } 160 catch (Exception e) 161 { 162 e.printStackTrace(); 163 } 164 } 165 166 } | Popular Tags |