KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > daemon > Daemon


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package ve.luz.ica.jackass.daemon;
6
7 import java.io.File JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.omg.CORBA.UserException JavaDoc;
19 import org.omg.CosNaming.NamingContextExt JavaDoc;
20 import org.omg.CosNaming.NamingContextExtHelper JavaDoc;
21 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
22 import org.omg.PortableServer.POA JavaDoc;
23 import org.omg.PortableServer.POAHelper JavaDoc;
24 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
25 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
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 /**
40  * The Daemon is the main Jackass's bootstrapp class. It starts all Jackass services:
41  * name service, instantiators, deployer, node deployer and solver.
42  * @author Carlos Arévalo
43  */

44 public class Daemon
45 {
46     private static final Log LOG = LogFactory.getLog(Daemon.class);
47
48     private static final String JavaDoc NS_DELAY_PROPERTY = "name_service_delay";
49     private static final String JavaDoc ERROR_READING_JACORB_ORB_PROPERTIES = "Error reading file jackass.orb.properties";
50     private static final String JavaDoc JACKASS_ORB_PROPERTIES = "jackass-orb.properties";
51     private static final String JavaDoc NODE_DEPLOYER_ID = "NodeDeployer";
52     private static final String JavaDoc DEPLOYER_ID = "Deployer";
53
54     private static final String JavaDoc DEFAULT_NS_DELAY = "5000";
55     private static final String JavaDoc DEFAULT_DAEMON_PORT = "8000";
56     private static final String JavaDoc PROPERTY_NAME_SEPARATOR = ".";
57
58     private static final String JavaDoc JACKASS_RUNTIME_PROPERTY = "jackass_runtime";
59     private static final String JavaDoc DEPLOYMENT_DIR_PROPERTY = "deployment_dir";
60     private static final String JavaDoc DEFAULT_DEPLOYMENT_DIR = "deploy";
61     private static final String JavaDoc TEMP_DIR_PROPERTY = "temp_dir";
62     private static final String JavaDoc DEFAULT_TEMP_DIR = "temp";
63
64     private static final String JavaDoc COMMAND_PREFIX_SEPARATOR = ":";
65     private static final String JavaDoc ROOT_POA_NAME = "RootPOA";
66     private static final String JavaDoc NAME_SERVICE = "NameService";
67     private static final String JavaDoc INSTANTIATOR_PROPERTY_PREFIX = "instantiator.";
68     private static final String JavaDoc CONTEXT_POA_NAME = "ContextPOA";
69     private static final String JavaDoc PROXY_POA_NAME = "ProxyPOA";
70     private static final String JavaDoc INPROCESS_STRING = "inprocess:";
71     private static final String JavaDoc NAME_SERVICE_COMMAND = "name_service_class";
72     private static final String JavaDoc NAME_SERVICE_PARAMS = "name_service_params";
73     private static final String JavaDoc SERVICES_POA_NAME = "JackassServices";
74     private static final String JavaDoc SOLVER_OBJ_ID = "Solver";
75
76     private org.omg.CORBA.ORB JavaDoc orb = null;
77     private POA JavaDoc rootPoa = null;
78
79     /**
80      * The application entry point.
81      * Creates a Daemon object initializes it and invokes the run method
82      * @param args the array of command line parameters
83      */

84     public static void main(String JavaDoc[] args)
85     {
86         try
87         {
88             Daemon daemon = new Daemon();
89             daemon.init(args);
90             daemon.run();
91         }
92         catch (Exception JavaDoc e)
93         {
94             LOG.fatal("Error at system startup", e);
95             System.exit(0);
96         }
97     }
98
99     /**
100      * Initializes the orb and the root POA. Creates other POAs needded by the system
101      * and the Deployers
102      * @param args the command line arguments to be passed on to the orb
103      * @throws Exception if there is an error during the initialization process
104      */

105     public void init(String JavaDoc[] args) throws Exception JavaDoc
106     {
107         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
108
109         // set the jackass home directory
110
this.createDirectories(cf);
111
112         // start the name service
113
this.startNameService();
114
115         // wait for the name service to start
116
try
117         {
118             int delay = Integer.parseInt(cf.getProperty(NS_DELAY_PROPERTY, DEFAULT_NS_DELAY));
119             Thread.sleep(delay);
120         }
121         catch (InterruptedException JavaDoc e1)
122         {
123             if (LOG.isDebugEnabled()) LOG.debug("This should never happen");
124         }
125
126         Properties JavaDoc props = new Properties JavaDoc();
127         try
128         {
129             InputStream JavaDoc in = ClassLoader.getSystemResourceAsStream(JACKASS_ORB_PROPERTIES);
130             props.load(in);
131         }
132         catch (Exception JavaDoc e)
133         {
134             if (LOG.isErrorEnabled()) LOG.error(ERROR_READING_JACORB_ORB_PROPERTIES, e);
135             throw new IOException JavaDoc(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         // create poas
143
POA JavaDoc proxyPoa = this.createProxyPoa();
144         POA JavaDoc contextPoa = this.createContextPoa();
145         POA JavaDoc servicesPoa = this.createServicesPoa();
146
147         // resolve the name service root context
148
org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references(NAME_SERVICE);
149         NamingContextExt JavaDoc 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 JavaDoc i = gm.getGroupNames(); i.hasNext();)
164         {
165             String JavaDoc groupName = (String JavaDoc) i.next();
166             nodeDepManager.addNodeDeployer(groupName, nodeDep);
167         }
168
169         this.startInstantiators();
170
171         InitialDeployer initialDeployer = new InitialDeployer(deployer, rootPoa);
172         Thread JavaDoc thread = new Thread JavaDoc(initialDeployer);
173         thread.start();
174     }
175
176     /**
177      * Invokes orb.run to wait for requests
178      */

179     public void run()
180     {
181         LOG.info("Daemon ready");
182         orb.run();
183     }
184
185     /**
186      * Set the jackass home directory property in the properties object.
187      * If the directory does not exist create it.
188      * @param cf the jackass PropertiesManager
189      * @throws IOException if jackass is unable to create the home directory
190      */

191     private void createDirectories(Properties JavaDoc cf) throws IOException JavaDoc
192     {
193         String JavaDoc jackassHome = cf.getProperty(JACKASS_RUNTIME_PROPERTY);
194         if (jackassHome == null)
195         {
196             String JavaDoc daemonSource = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
197             File JavaDoc file = new File JavaDoc(daemonSource);
198             String JavaDoc 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 JavaDoc jackassHomeDir = new File JavaDoc(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 JavaDoc("Unable to create the jackass runtime directory");
215             }
216         }
217         if (LOG.isDebugEnabled()) LOG.debug("Startup directory: "+jackassHome);
218
219         String JavaDoc deploymentDir = cf.getProperty(DEPLOYMENT_DIR_PROPERTY, DEFAULT_DEPLOYMENT_DIR);
220         File JavaDoc depDirFile = new File JavaDoc(jackassHome, deploymentDir);
221         String JavaDoc 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 JavaDoc("Unable to create the deployment directory "+ deploymentPath);
230             }
231         }
232         if (LOG.isDebugEnabled()) LOG.debug("deployment directory: "+deploymentPath);
233
234         String JavaDoc tempDir = cf.getProperty(TEMP_DIR_PROPERTY, DEFAULT_TEMP_DIR);
235         File JavaDoc tempDirFile = new File JavaDoc(jackassHome, tempDir);
236         String JavaDoc 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 JavaDoc("Unable to create the temporary directory "+ tempDirectory);
245             }
246         }
247         if (LOG.isDebugEnabled()) LOG.debug("temp directory: "+tempDirectory);
248     }
249
250     /**
251      * Starts the name service.
252      * The java class and command line parameters required to start the name
253      * service are read from the configuration file. The name service is
254      * started in a separate thread.
255      */

256     private void startNameService()
257     {
258         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
259         String JavaDoc className = cf.getProperty(NAME_SERVICE_COMMAND);
260
261         if (className != null)
262         {
263             String JavaDoc[] params = null;
264
265             String JavaDoc paramString = cf.getProperty(NAME_SERVICE_PARAMS);
266             if (paramString != null)
267             {
268                 StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(paramString);
269                 params = new String JavaDoc[stringTokenizer.countTokens()];
270                 int i = 0;
271                 while (stringTokenizer.hasMoreTokens())
272                 {
273                     params[i++] = stringTokenizer.nextToken();
274                 }
275             }
276
277             // start the name service in this process
278
if (LOG.isDebugEnabled()) LOG.debug("Starting name service " + className);
279             NameServiceThread nsThread = new NameServiceThread(className, params);
280             nsThread.start();
281         }
282     }
283
284     /**
285      * Creates the poa where the jackass services will created. These services
286      * are: Deployer, NodeDeployer y Solver. This poa is created with a specific
287      * object ID (JackassServices) so that the services can be accessed by means
288      * of corbalocs.
289      * @return the reference to the created POA
290      * @throws UserException thrown if an error occurs when creating the POA
291      */

292     private POA JavaDoc createServicesPoa() throws UserException JavaDoc
293     {
294         org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[1];
295         policies[0] = rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
296
297         String JavaDoc poaName = SERVICES_POA_NAME;
298         POA JavaDoc servicesPoa = rootPoa.create_POA(poaName, null, policies);
299         servicesPoa.the_POAManager().activate();
300         return servicesPoa;
301     }
302
303     /**
304      * Creates the ComponentSolver.
305      * @param cpMgr a ComponentProxyManager used by the ComponentSolver to get the
306      * list of proxy references associated to the component ID's
307      * @param poa the poa used to create the ComponentSolver
308      * @throws UserException if there is an error during the creation process
309      */

310     private void createComponentSolver(ComponentProxyManager cpMgr, POA JavaDoc poa) throws UserException JavaDoc
311     {
312         String JavaDoc objectId = SOLVER_OBJ_ID;
313         ComponentSolverImpl jSolverImpl = new ComponentSolverImpl(cpMgr);
314         poa.activate_object_with_id(objectId.getBytes(), jSolverImpl);
315         org.omg.CORBA.Object JavaDoc obj = poa.id_to_reference(objectId.getBytes());
316
317         if (LOG.isDebugEnabled()) LOG.debug("ComponentSolver object created: " + obj);
318     }
319
320     /**
321      * Creates the Deployer.
322      * @param rootNameCtx the naming service root context.
323      * @param compProxyManager a ComponentProxyManager. The deployer will add entries to this object
324      * that will allow the ComponentSolver to obtain the object references
325      * corresponding the the component ID's.
326      * @param nodeDepManager a NodeDeployerManager. This object will be used by the deployer to
327      * obtain the list of NodeDeployers corresponding to a group.
328      * @param poa the POA that will be used to create the Deployer
329      * @return the reference to the created Deployer object
330      * @throws UserException thrown if there is an error during the creation of the Deployer
331      */

332     private Deployer createDeployer(NamingContextExt JavaDoc rootNameCtx, ComponentProxyManager compProxyManager,
333             NodeDeployerManager nodeDepManager, POA JavaDoc poa)
334             throws UserException JavaDoc
335     {
336         DeployerImpl deployerImpl = new DeployerImpl(orb, rootPoa, rootNameCtx, compProxyManager,
337                 nodeDepManager);
338         String JavaDoc objectId = DEPLOYER_ID;
339         poa.activate_object_with_id(objectId.getBytes(), deployerImpl);
340         org.omg.CORBA.Object JavaDoc obj = poa.id_to_reference(objectId.getBytes());
341         Deployer deployer = DeployerHelper.narrow(obj);
342
343         return deployer;
344     }
345
346     /**
347      * Creates the NodeDeployer.
348      * @param numInstantiators the number of instantiators running in this node as
349      * configured in the configuration file.
350      * @param contextPoa the poa used to create the component context objects.
351      * @param proxyPoa the poa used to create the proxy objects.
352      * @param rootNamingCtx the naming service root context.
353      * @param poa the poa used to create the NodeDeployer.
354      * @return the reference to the created NodeDeployer object
355      * @throws UserException if there is a Corba related error
356      * @throws IOException if there is an IO error
357      */

358     private NodeDeployer createNodeDeployer(int numInstantiators, POA JavaDoc contextPoa, POA JavaDoc proxyPoa,
359             NamingContextExt JavaDoc rootNamingCtx, POA JavaDoc poa) throws UserException JavaDoc, IOException JavaDoc
360     {
361         // create the implementation
362
NodeDeployerImpl nodeDeployerImpl = new NodeDeployerImpl(numInstantiators, contextPoa, proxyPoa,
363                 rootNamingCtx);
364         String JavaDoc objectId = NODE_DEPLOYER_ID;
365         poa.activate_object_with_id(objectId.getBytes(), nodeDeployerImpl);
366         org.omg.CORBA.Object JavaDoc obj = poa.id_to_reference(objectId.getBytes());
367         NodeDeployer nodeDeployer = NodeDeployerHelper.narrow(obj);
368
369         // export the object reference
370
FileWriter JavaDoc out;
371         try
372         {
373             out = new FileWriter JavaDoc("node_deployer.ior");
374             out.write(orb.object_to_string(nodeDeployer));
375             out.close();
376         }
377         catch (IOException JavaDoc e)
378         {
379             LOG.error("Error when writting reference to file", e);
380         }
381
382         return nodeDeployer;
383     }
384
385     /**
386      * Returns the list of instantiators that appear in the configuration file
387      * @return a List containing the commands to launch each instantiator
388      */

389     private int getInstantiatorCount()
390     {
391         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
392         Enumeration JavaDoc e = cf.propertyNames();
393         int count = 0;
394         while (e.hasMoreElements())
395         {
396             String JavaDoc name = (String JavaDoc) e.nextElement();
397             if (name.startsWith(INSTANTIATOR_PROPERTY_PREFIX))
398             {
399                 count++;
400             }
401         }
402         return count;
403     }
404
405     /**
406      * Launches the instantiators defined in the configuration file in the same
407      * process as the Jackass Daemon.
408      */

409     private void startInstantiators()
410     {
411         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
412         Enumeration JavaDoc e = cf.propertyNames();
413         while (e.hasMoreElements())
414         {
415             String JavaDoc name = (String JavaDoc) e.nextElement();
416             if (name.startsWith(INSTANTIATOR_PROPERTY_PREFIX))
417             {
418                 String JavaDoc number = name.substring(name.indexOf(PROPERTY_NAME_SEPARATOR)+1);
419                 String JavaDoc command = cf.getProperty(name);
420                 String JavaDoc[] params = new String JavaDoc[] {number};
421
422                 if (command.startsWith(INPROCESS_STRING))
423                 {
424                     if (LOG.isDebugEnabled()) LOG.debug("Starting instantiator " + number);
425                     String JavaDoc 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     /**
438      * Starts an instantiator in a separate process
439      * @param className the name of the main instantiator class
440      */

441     private void startExternalInstantiator(String JavaDoc className)
442     {
443
444     }
445
446     /**
447      * Creates the POA that will be used to create the proxy references.
448      * @return the created POA
449      * @throws UserException thrown if the orb is unable to create the POA
450      */

451     private POA JavaDoc createProxyPoa() throws UserException JavaDoc
452     {
453         org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[]
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 JavaDoc 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     /**
471      * Creates the POA that will be used to create the context objects which will in turn
472      * be passed to the components at deployment time
473      * @return the created POA
474      * @throws UserException thrown if the orb is unable to create the POA
475      */

476     private POA JavaDoc createContextPoa() throws UserException JavaDoc
477     {
478         org.omg.CORBA.Policy JavaDoc[] policies = new org.omg.CORBA.Policy JavaDoc[1];
479         policies[0] = rootPoa.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);
480
481         POA JavaDoc contextPoa = rootPoa.create_POA(CONTEXT_POA_NAME, null, policies);
482         contextPoa.the_POAManager().activate();
483         return contextPoa;
484     }
485
486 }
487
Popular Tags