KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > Runtime


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.corba.runtime;
31
32 /**
33  ** <p>The <tt>Runtime</tt> class provides the bootstrapping feature for a CORBA object implemented
34  ** using the system component based framework.</p>
35  **/

36 public class Runtime
37 {
38     static private String JavaDoc _class_name = "Runtime";
39     static private FactoryFinderService _ff_service;
40     static private RegistrationService _reg_service;
41     static private MainThread _main_thread;
42     // NOTE: stores the main orb instance
43
static private org.omg.CORBA.ORB JavaDoc _orb;
44
45     static {
46         // create FactoryFinder service
47
_ff_service = new FactoryFinderServiceImpl();
48         // complete configuration
49
_ff_service.system_configuration_complete(new SystemConfigurationImpl());
50
51         // create Registration service
52
_reg_service = new RegistrationServiceImpl();
53         // complete configuration
54
_reg_service.system_configuration_complete(new SystemConfigurationImpl());
55
56         // register ORB service factory
57
String JavaDoc entrypt = "org.objectweb.corba.runtime.ORBFactoryImpl.create_factory";
58         _ff_service.register_service_factory(ORBService.SERVICE_ID, null, entrypt);
59
60         // add debug PI to the ORB service factory
61
ORBFactory fact = (ORBFactory)getFFService().find_service_factory(ORBService.SERVICE_ID);
62         fact.add_default_initializer("org.objectweb.corba.runtime.DebugORBInitializerImpl");
63
64         // register POA service factory
65
entrypt = "org.objectweb.corba.runtime.POAFactoryImpl.create_factory";
66         _ff_service.register_service_factory(POAService.SERVICE_ID, null, entrypt);
67
68         // create new main thread
69
_main_thread = new SingleORBWorker();
70         _main_thread.startThread();
71
72         _orb = null;
73     }
74
75     //
76
// public operations
77
//
78

79     static public FactoryFinderService
80     getFFService()
81     {
82         return _ff_service;
83     }
84
85     static public RegistrationService
86     getRegistrationService()
87     {
88         return _reg_service;
89     }
90
91     static public MainThread
92     getMainThread()
93     {
94         return _main_thread;
95     }
96
97     static public org.omg.CORBA.ORB JavaDoc
98     getORB()
99     {
100         return _orb;
101     }
102
103     static public Servant
104     loadServant(RuntimeConfiguration config)
105     {
106         final String JavaDoc opname = "loadServant";
107
108         // register default factories for services
109
String JavaDoc[] sids = config.service_ids;
110         String JavaDoc[] entrypts = config.service_entrypts;
111
112         for (int i=0;i<sids.length;i++) {
113             _ff_service.register_service_factory(sids[i], null, entrypts[i]);
114         }
115
116         // add archive (JAR) location to the class loader path
117
String JavaDoc archloc = config.archive_location;
118
119         // check location
120
if (archloc!=null) {
121             TheClassLoader.addResource(archloc);
122         }
123
124         // create new instance
125
String JavaDoc entrypt = config.archive_entrypoint;
126         Object JavaDoc obj = TheClassLoader.newInstance(entrypt);
127
128         // check creation
129
if (obj==null) {
130             // NOTE: TODO: should be reported as an exception
131
final String JavaDoc msg = "FAILED (servant not created)";
132             TheLogger.error(_class_name, opname, msg);
133         }
134
135         // check that the instance is a Servant object
136
Servant servant = null;
137         try {
138             servant = (Servant)obj;
139         } catch (ClassCastException JavaDoc ex) {
140             // NOTE: TODO: should be reported as an exception
141
final String JavaDoc msg = "FAILED (not a servant)";
142             TheLogger.error(_class_name, opname, msg);
143         }
144
145         // create system services
146
// create ORB service instance
147
ORBService orbs = null;
148         SystemFactory orbfact = _ff_service.find_service_factory(ORBService.SERVICE_ID);
149
150         // create config
151
ORBConfiguration orbcfg = new ORBConfigurationImpl(config.orb_initializers,
152                                                            config.value_factories,
153                                                            config.initial_references,
154                                                            config.stringified_initial_references);
155
156         orbs = (ORBService)orbfact.create_system_component(orbcfg);
157
158         // set as ORB singleton
159
try {
160             ORBServiceImpl orbimpl = (ORBServiceImpl)orbs;
161             _orb = orbimpl.getORB();
162         } catch (ClassCastException JavaDoc ex) {
163             // ignore
164
}
165
166         // create POA service instance
167
// NOTE: TODO: this POA is used to create the servant ref and shoud therefore be
168
// configurable externally like the ORB
169
POAService poas = null;
170         SystemFactory poafact = _ff_service.find_service_factory(POAService.SERVICE_ID);
171
172         // create config
173
// params: no name, no pols, orbs
174
POAConfiguration poacfg = new POAConfigurationImpl(null, null, orbs);
175         poas = (POAService)poafact.create_system_component(poacfg);
176
177         // complete configuration
178
ServantConfiguration scfg = new ServantConfigurationImpl(orbs, poas);
179         servant.system_configuration_complete(scfg);
180
181         // create reference and register it
182
// use runtime.id as oid
183
byte[] oid = System.getProperty("runtime.id").getBytes();
184         org.omg.CORBA.Object JavaDoc ref = poas.create_ref_with_id(oid, servant.type_id());
185
186         // activate servant
187
poas.activate_servant_with_id(servant, oid);
188
189         // try to store in local file and in ns
190
RegistrationService regs = _reg_service;
191         // check if a IOR file is set
192
String JavaDoc iorfile = System.getProperty("runtime.iorfile");
193         if (iorfile!=null) {
194             // obtain file registration scheme
195
FileRegistrationScheme filescheme = (FileRegistrationScheme)regs.get_scheme(FileRegistrationScheme.SCHEME_ID);
196             filescheme.write_ior(iorfile, ref, orbs);
197         }
198
199         // obtain ns name from properties (always set)
200
String JavaDoc insname = System.getProperty("runtime.id");
201         INSRegistrationScheme insscheme = (INSRegistrationScheme)regs.get_scheme(INSRegistrationScheme.SCHEME_ID);
202         insscheme.bind(insname, ref, orbs);
203
204         return servant;
205     }
206
207     static public void
208     loadMain(RuntimeConfiguration config)
209     {
210         // add archive (JAR) location to the class loader path
211
String JavaDoc archloc = config.archive_location;
212
213         // check location
214
if (archloc!=null) {
215             TheClassLoader.addResource(archloc);
216         }
217
218         // create ORB service instance
219
ORBService orbs = null;
220         SystemFactory orbfact = _ff_service.find_service_factory(ORBService.SERVICE_ID);
221
222         // create config
223
ORBConfiguration orbcfg = new ORBConfigurationImpl(config.orb_initializers,
224                                                            config.value_factories,
225                                                            config.initial_references,
226                                                            config.stringified_initial_references);
227
228         orbs = (ORBService)orbfact.create_system_component(orbcfg);
229
230         // NOTE: the expected signature is :
231
// static public java.lang.Runnable
232
// <op_name>(ORBService orbs);
233
//
234
String JavaDoc entrypt = config.archive_entrypoint;
235         Class JavaDoc[] argc = { ORBService.class };
236         Object JavaDoc[] argv = { orbs };
237
238         Runnable JavaDoc run = null;
239         try {
240             run = (Runnable JavaDoc)TheClassLoader.newInstance(entrypt, argc, argv);
241         } catch (ClassCastException JavaDoc ex) {
242             // NOTE: TODO: should be reported as an exception
243
final String JavaDoc opname = "loadMain";
244             final String JavaDoc msg = "FAILED (not a runnable)";
245             TheLogger.error(_class_name, opname, msg);
246         }
247
248         run.run();
249     }
250 }
251
Popular Tags