KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ve > luz > ica > jackass > instantiator > InstantiatorServer


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.instantiator;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import org.omg.CORBA.UserException JavaDoc;
15 import org.omg.PortableServer.POAHelper JavaDoc;
16 import org.omg.CORBA.Object JavaDoc;
17
18 import ve.luz.ica.jackass.deploy.daemon.NodeDeployer;
19 import ve.luz.ica.jackass.deploy.daemon.NodeDeployerHelper;
20 import ve.luz.ica.jackass.util.ConfigurationManager;
21
22 /**
23  * Instantiator is the main class.
24  * It initializes the orb and creates factories for each component type and registers them
25  * with the Jackass daemon and calls orb.run() to wait for requests
26  * @author Carlos Arévalo
27  */

28 public class InstantiatorServer
29 {
30     private static final Log LOG = LogFactory.getLog(InstantiatorServer.class);
31
32     private static final String JavaDoc NODE_DEPLOYER_REF_FILE = "node_deployer.ior";
33     private static final String JavaDoc ROOT_POA = "RootPOA";
34     //private static final String INTERCEPTOR_VALUE = "ve.luz.ica.jackass.client.Initializer";
35
//private static final String INTERCEPTOR_PROPERTY = "org.omg.PortableInterceptor.ORBInitializerClass.jackass_init";
36

37     private static final String JavaDoc INSTANTIATOR_ORB_PROPERTIES = "instantiator-orb.properties";
38     private static final String JavaDoc ERROR_READING_INSTANTIATOR_ORB_PROPERTIES = "Error reading instantiator-orb.properties";
39
40     private org.omg.CORBA.ORB JavaDoc orb = null;
41     private org.omg.PortableServer.POA JavaDoc poa = null;
42
43     /**
44      * The application entry point.
45      * Creates an Instantiator object, initializes it and invokes the run method
46      * @param args the array of command line parameters
47      */

48     public static void main(String JavaDoc[] args)
49     {
50         try
51         {
52             InstantiatorServer instantiatorServer = new InstantiatorServer();
53             instantiatorServer.init(args);
54             instantiatorServer.run();
55         }
56         catch (Exception JavaDoc e)
57         {
58             LOG.fatal("Error at system startup", e);
59         }
60     }
61
62     /**
63      * Initializes the orb and the poa for the factories and calls createFactory.
64      * @param args the command line arguments to be passed on to the orb
65      * @throws Exception if there is an error during the initialization process
66      */

67     public void init(String JavaDoc[] args) throws Exception JavaDoc
68     {
69         Properties JavaDoc cf = ConfigurationManager.getConfigFile();
70
71         Properties JavaDoc props = new Properties JavaDoc();
72         try
73         {
74             InputStream JavaDoc in = ClassLoader.getSystemResourceAsStream(INSTANTIATOR_ORB_PROPERTIES);
75             props.load(in);
76         }
77         catch (Exception JavaDoc e)
78         {
79             if (LOG.isErrorEnabled()) LOG.error(ERROR_READING_INSTANTIATOR_ORB_PROPERTIES, e);
80             throw new IOException JavaDoc(ERROR_READING_INSTANTIATOR_ORB_PROPERTIES + e.getMessage());
81         }
82         
83         if (LOG.isDebugEnabled())
84         {
85             LOG.debug(props.getProperty("OAPort"));
86             LOG.debug(props.getProperty("jacorb.implname"));
87         }
88
89         orb = org.omg.CORBA.ORB.init(args, props);
90         Object JavaDoc obj = orb.resolve_initial_references(ROOT_POA);
91         if (LOG.isDebugEnabled()) LOG.debug(obj);
92         
93         poa = POAHelper.narrow(obj);
94         poa.the_POAManager().activate();
95         this.createInstantiator();
96     }
97
98     /**
99      * Invokes orb.run to wait for requests
100      */

101     public void run()
102     {
103         LOG.info("Instantiator ready");
104         orb.run();
105     }
106
107     /**
108      * Creates the factories for each component type and registers them with the local NodeDeployer.
109      * It creates factories for each component type and registers them with the Jackass daemon
110      * @throws UserException if there is a Corba related error during the creation process.
111      */

112     public void createInstantiator() throws UserException JavaDoc
113     {
114         try
115         {
116             // create the instantiator
117
JInstantiatorImpl instantiatorImpl = new JInstantiatorImpl(orb, poa);
118             org.omg.CORBA.Object JavaDoc obj = poa.servant_to_reference(instantiatorImpl);
119             JInstantiator instantiator = JInstantiatorHelper.narrow(obj);
120
121             // find the deamon
122
//java.io.FileInputStream file = new java.io.FileInputStream( "daemon.ior" );
123
java.io.FileInputStream JavaDoc file = new java.io.FileInputStream JavaDoc(NODE_DEPLOYER_REF_FILE);
124             java.io.BufferedReader JavaDoc myInput = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(file));
125             String JavaDoc stringTarget = myInput.readLine();
126             org.omg.CORBA.Object JavaDoc dobj = orb.string_to_object(stringTarget);
127             NodeDeployer nodeDeployer = NodeDeployerHelper.narrow(dobj);
128
129             if (LOG.isDebugEnabled())
130             {
131                 LOG.debug("Node deployer reference resolved");
132             }
133
134             // register the instantiator with the Jackass daemon
135
nodeDeployer.addInstantiator(instantiator);
136
137             if (LOG.isDebugEnabled())
138             {
139                 LOG.debug("Instantiator registered");
140             }
141         }
142         catch (java.io.IOException JavaDoc ex)
143         {
144             LOG.error("Unable to resolve the daemon reference");
145         }
146
147     }
148 }
149
Popular Tags