1 5 package ve.luz.ica.jackass.daemon; 6 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.util.Enumeration ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 import java.util.StringTokenizer ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.omg.CORBA.UserException ; 19 import org.omg.CosNaming.NamingContextExt ; 20 import org.omg.CosNaming.NamingContextExtHelper ; 21 import org.omg.PortableServer.IdAssignmentPolicyValue ; 22 import org.omg.PortableServer.POA ; 23 import org.omg.PortableServer.POAHelper ; 24 import org.omg.PortableServer.RequestProcessingPolicyValue ; 25 import org.omg.PortableServer.ServantRetentionPolicyValue ; 26 27 import ve.luz.ica.jackass.component.ComponentInfoManager; 28 import ve.luz.ica.jackass.daemon.group.GroupManager; 29 import ve.luz.ica.jackass.daemon.proxy.ProxyServantLocator; 30 import ve.luz.ica.jackass.deploy.Deployer; 31 import ve.luz.ica.jackass.deploy.DeployerHelper; 32 import ve.luz.ica.jackass.deploy.daemon.NodeDeployer; 33 import ve.luz.ica.jackass.deploy.daemon.NodeDeployerHelper; 34 import ve.luz.ica.jackass.solver.ComponentProxyManager; 35 import ve.luz.ica.jackass.solver.ComponentSolverImpl; 36 import ve.luz.ica.jackass.solver.SingleHostComponentProxyManager; 37 import ve.luz.ica.jackass.util.ConfigurationManager; 38 39 44 public class Daemon 45 { 46 private static final Log LOG = LogFactory.getLog(Daemon.class); 47 48 private static final String NS_DELAY_PROPERTY = "name_service_delay"; 49 private static final String ERROR_READING_JACORB_ORB_PROPERTIES = "Error reading file jackass.orb.properties"; 50 private static final String JACKASS_ORB_PROPERTIES = "jackass-orb.properties"; 51 private static final String NODE_DEPLOYER_ID = "NodeDeployer"; 52 private static final String DEPLOYER_ID = "Deployer"; 53 54 private static final String DEFAULT_NS_DELAY = "5000"; 55 private static final String DEFAULT_DAEMON_PORT = "8000"; 56 private static final String PROPERTY_NAME_SEPARATOR = "."; 57 58 private static final String JACKASS_RUNTIME_PROPERTY = "jackass_runtime"; 59 private static final String DEPLOYMENT_DIR_PROPERTY = "deployment_dir"; 60 private static final String DEFAULT_DEPLOYMENT_DIR = "deploy"; 61 private static final String TEMP_DIR_PROPERTY = "temp_dir"; 62 private static final String DEFAULT_TEMP_DIR = "temp"; 63 64 private static final String COMMAND_PREFIX_SEPARATOR = ":"; 65 private static final String ROOT_POA_NAME = "RootPOA"; 66 private static final String NAME_SERVICE = "NameService"; 67 private static final String INSTANTIATOR_PROPERTY_PREFIX = "instantiator."; 68 private static final String CONTEXT_POA_NAME = "ContextPOA"; 69 private static final String PROXY_POA_NAME = "ProxyPOA"; 70 private static final String INPROCESS_STRING = "inprocess:"; 71 private static final String NAME_SERVICE_COMMAND = "name_service_class"; 72 private static final String NAME_SERVICE_PARAMS = "name_service_params"; 73 private static final String SERVICES_POA_NAME = "JackassServices"; 74 private static final String SOLVER_OBJ_ID = "Solver"; 75 76 private org.omg.CORBA.ORB orb = null; 77 private POA rootPoa = null; 78 79 84 public static void main(String [] args) 85 { 86 try 87 { 88 Daemon daemon = new Daemon(); 89 daemon.init(args); 90 daemon.run(); 91 } 92 catch (Exception e) 93 { 94 LOG.fatal("Error at system startup", e); 95 System.exit(0); 96 } 97 } 98 99 105 public void init(String [] args) throws Exception 106 { 107 Properties cf = ConfigurationManager.getConfigFile(); 108 109 this.createDirectories(cf); 111 112 this.startNameService(); 114 115 try 117 { 118 int delay = Integer.parseInt(cf.getProperty(NS_DELAY_PROPERTY, DEFAULT_NS_DELAY)); 119 Thread.sleep(delay); 120 } 121 catch (InterruptedException e1) 122 { 123 if (LOG.isDebugEnabled()) LOG.debug("This should never happen"); 124 } 125 126 Properties props = new Properties (); 127 try 128 { 129 InputStream in = ClassLoader.getSystemResourceAsStream(JACKASS_ORB_PROPERTIES); 130 props.load(in); 131 } 132 catch (Exception e) 133 { 134 if (LOG.isErrorEnabled()) LOG.error(ERROR_READING_JACORB_ORB_PROPERTIES, e); 135 throw new IOException (ERROR_READING_JACORB_ORB_PROPERTIES + e.getMessage()); 136 } 137 138 orb = org.omg.CORBA.ORB.init(args, props); 139 rootPoa = POAHelper.narrow(orb.resolve_initial_references(ROOT_POA_NAME)); 140 rootPoa.the_POAManager().activate(); 141 142 POA proxyPoa = this.createProxyPoa(); 144 POA contextPoa = this.createContextPoa(); 145 POA servicesPoa = this.createServicesPoa(); 146 147 org.omg.CORBA.Object obj = orb.resolve_initial_references(NAME_SERVICE); 149 NamingContextExt rootNameContext = NamingContextExtHelper.narrow(obj); 150 151 ComponentProxyManager compProxyManager = new SingleHostComponentProxyManager(); 152 NodeDeployerManager nodeDepManager = new SingleHostNodeDeployerManager(); 153 this.createComponentSolver(compProxyManager, servicesPoa); 154 155 Deployer deployer = this.createDeployer(rootNameContext, compProxyManager, 156 nodeDepManager, servicesPoa); 157 158 int instantiatorCount = this.getInstantiatorCount(); 159 NodeDeployer nodeDep = this.createNodeDeployer(instantiatorCount, contextPoa, proxyPoa, 160 rootNameContext, servicesPoa); 161 162 GroupManager gm = GroupManager.getManager(); 163 for (Iterator i = gm.getGroupNames(); i.hasNext();) 164 { 165 String groupName = (String ) i.next(); 166 nodeDepManager.addNodeDeployer(groupName, nodeDep); 167 } 168 169 this.startInstantiators(); 170 171 InitialDeployer initialDeployer = new InitialDeployer(deployer, rootPoa); 172 Thread thread = new Thread (initialDeployer); 173 thread.start(); 174 } 175 176 179 public void run() 180 { 181 LOG.info("Daemon ready"); 182 orb.run(); 183 } 184 185 191 private void createDirectories(Properties cf) throws IOException 192 { 193 String jackassHome = cf.getProperty(JACKASS_RUNTIME_PROPERTY); 194 if (jackassHome == null) 195 { 196 String daemonSource = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 197 File file = new File (daemonSource); 198 String jackassBin = file.getPath(); 199 if (!file.isDirectory()) 200 { 201 jackassBin = jackassBin.substring(0, jackassBin.lastIndexOf(File.separator)); 202 } 203 if (LOG.isDebugEnabled()) LOG.debug("Jackass bin directory: "+jackassBin); 204 jackassHome = jackassBin.substring(0, jackassBin.lastIndexOf(File.separator)); 205 cf.setProperty(JACKASS_RUNTIME_PROPERTY, jackassHome); 206 } 207 208 File jackassHomeDir = new File (jackassHome); 209 if (!jackassHomeDir.exists()) 210 { 211 if (!jackassHomeDir.mkdirs()) 212 { 213 LOG.fatal("The Jackass home directory does not exist and jackass was unable to create it"); 214 throw new IOException ("Unable to create the jackass runtime directory"); 215 } 216 } 217 if (LOG.isDebugEnabled()) LOG.debug("Startup directory: "+jackassHome); 218 219 String deploymentDir = cf.getProperty(DEPLOYMENT_DIR_PROPERTY, DEFAULT_DEPLOYMENT_DIR); 220 File depDirFile = new File (jackassHome, deploymentDir); 221 String deploymentPath = depDirFile.getPath(); 222 cf.setProperty(ConfigurationManager.DEPLOYMENT_PATH_PROPERTY, deploymentPath); 223 224 if (!depDirFile.exists()) 225 { 226 if (!depDirFile.mkdir()) 227 { 228 LOG.fatal("The Jackass deployment directory does not exist and jackass was unable to create it"); 229 throw new IOException ("Unable to create the deployment directory "+ deploymentPath); 230 } 231 } 232 if (LOG.isDebugEnabled()) LOG.debug("deployment directory: "+deploymentPath); 233 234 String tempDir = cf.getProperty(TEMP_DIR_PROPERTY, DEFAULT_TEMP_DIR); 235 File tempDirFile = new File (jackassHome, tempDir); 236 String tempDirectory = tempDirFile.getPath(); 237 cf.setProperty(ConfigurationManager.TEMP_PATH_PROPERTY, tempDirectory); 238 239 if (!tempDirFile.exists()) 240 { 241 if (!tempDirFile.mkdir()) 242 { 243 LOG.fatal("The Jackass temp directory does not exist and jackass was unable to create it"); 244 throw new IOException ("Unable to create the temporary directory "+ tempDirectory); 245 } 246 } 247 if (LOG.isDebugEnabled()) LOG.debug("temp directory: "+tempDirectory); 248 } 249 250 256 private void startNameService() 257 { 258 Properties cf = ConfigurationManager.getConfigFile(); 259 String className = cf.getProperty(NAME_SERVICE_COMMAND); 260 261 if (className != null) 262 { 263 String [] params = null; 264 265 String paramString = cf.getProperty(NAME_SERVICE_PARAMS); 266 if (paramString != null) 267 { 268 StringTokenizer stringTokenizer = new StringTokenizer (paramString); 269 params = new String [stringTokenizer.countTokens()]; 270 int i = 0; 271 while (stringTokenizer.hasMoreTokens()) 272 { 273 params[i++] = stringTokenizer.nextToken(); 274 } 275 } 276 277 if (LOG.isDebugEnabled()) LOG.debug("Starting name service " + className); 279 NameServiceThread nsThread = new NameServiceThread(className, params); 280 nsThread.start(); 281 } 282 } 283 284 292 private POA createServicesPoa() throws UserException 293 { 294 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [1]; 295 policies[0] = rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); 296 297 String poaName = SERVICES_POA_NAME; 298 POA servicesPoa = rootPoa.create_POA(poaName, null, policies); 299 servicesPoa.the_POAManager().activate(); 300 return servicesPoa; 301 } 302 303 310 private void createComponentSolver(ComponentProxyManager cpMgr, POA poa) throws UserException 311 { 312 String objectId = SOLVER_OBJ_ID; 313 ComponentSolverImpl jSolverImpl = new ComponentSolverImpl(cpMgr); 314 poa.activate_object_with_id(objectId.getBytes(), jSolverImpl); 315 org.omg.CORBA.Object obj = poa.id_to_reference(objectId.getBytes()); 316 317 if (LOG.isDebugEnabled()) LOG.debug("ComponentSolver object created: " + obj); 318 } 319 320 332 private Deployer createDeployer(NamingContextExt rootNameCtx, ComponentProxyManager compProxyManager, 333 NodeDeployerManager nodeDepManager, POA poa) 334 throws UserException 335 { 336 DeployerImpl deployerImpl = new DeployerImpl(orb, rootPoa, rootNameCtx, compProxyManager, 337 nodeDepManager); 338 String objectId = DEPLOYER_ID; 339 poa.activate_object_with_id(objectId.getBytes(), deployerImpl); 340 org.omg.CORBA.Object obj = poa.id_to_reference(objectId.getBytes()); 341 Deployer deployer = DeployerHelper.narrow(obj); 342 343 return deployer; 344 } 345 346 358 private NodeDeployer createNodeDeployer(int numInstantiators, POA contextPoa, POA proxyPoa, 359 NamingContextExt rootNamingCtx, POA poa) throws UserException , IOException 360 { 361 NodeDeployerImpl nodeDeployerImpl = new NodeDeployerImpl(numInstantiators, contextPoa, proxyPoa, 363 rootNamingCtx); 364 String objectId = NODE_DEPLOYER_ID; 365 poa.activate_object_with_id(objectId.getBytes(), nodeDeployerImpl); 366 org.omg.CORBA.Object obj = poa.id_to_reference(objectId.getBytes()); 367 NodeDeployer nodeDeployer = NodeDeployerHelper.narrow(obj); 368 369 FileWriter out; 371 try 372 { 373 out = new FileWriter ("node_deployer.ior"); 374 out.write(orb.object_to_string(nodeDeployer)); 375 out.close(); 376 } 377 catch (IOException e) 378 { 379 LOG.error("Error when writting reference to file", e); 380 } 381 382 return nodeDeployer; 383 } 384 385 389 private int getInstantiatorCount() 390 { 391 Properties cf = ConfigurationManager.getConfigFile(); 392 Enumeration e = cf.propertyNames(); 393 int count = 0; 394 while (e.hasMoreElements()) 395 { 396 String name = (String ) e.nextElement(); 397 if (name.startsWith(INSTANTIATOR_PROPERTY_PREFIX)) 398 { 399 count++; 400 } 401 } 402 return count; 403 } 404 405 409 private void startInstantiators() 410 { 411 Properties cf = ConfigurationManager.getConfigFile(); 412 Enumeration e = cf.propertyNames(); 413 while (e.hasMoreElements()) 414 { 415 String name = (String ) e.nextElement(); 416 if (name.startsWith(INSTANTIATOR_PROPERTY_PREFIX)) 417 { 418 String number = name.substring(name.indexOf(PROPERTY_NAME_SEPARATOR)+1); 419 String command = cf.getProperty(name); 420 String [] params = new String [] {number}; 421 422 if (command.startsWith(INPROCESS_STRING)) 423 { 424 if (LOG.isDebugEnabled()) LOG.debug("Starting instantiator " + number); 425 String className = command.substring(command.indexOf(COMMAND_PREFIX_SEPARATOR)+1); 426 InstantiatorThread instThread = new InstantiatorThread(className, params); 427 instThread.start(); 428 } 429 else 430 { 431 this.startExternalInstantiator(command); 432 } 433 } 434 } 435 } 436 437 441 private void startExternalInstantiator(String className) 442 { 443 444 } 445 446 451 private POA createProxyPoa() throws UserException 452 { 453 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [] 454 { 455 rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID), 456 rootPoa.create_servant_retention_policy(ServantRetentionPolicyValue.NON_RETAIN), 457 rootPoa.create_request_processing_policy(RequestProcessingPolicyValue.USE_SERVANT_MANAGER) 458 }; 459 460 POA proxyPoa = rootPoa.create_POA(PROXY_POA_NAME, null, policies); 461 proxyPoa.the_POAManager().activate(); 462 463 ComponentInfoManager manager = ComponentInfoManager.getManager(); 464 ProxyServantLocator compServantLocator = new ProxyServantLocator(manager); 465 proxyPoa.set_servant_manager(compServantLocator); 466 467 return proxyPoa; 468 } 469 470 476 private POA createContextPoa() throws UserException 477 { 478 org.omg.CORBA.Policy [] policies = new org.omg.CORBA.Policy [1]; 479 policies[0] = rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID); 480 481 POA contextPoa = rootPoa.create_POA(CONTEXT_POA_NAME, null, policies); 482 contextPoa.the_POAManager().activate(); 483 return contextPoa; 484 } 485 486 } 487 | Popular Tags |