KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > ServerActivatorImpl


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.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>.</p>
36  **/

37 public class ServerActivatorImpl
38 extends org.omg.CORBA.LocalObject JavaDoc
39 implements Servant, org.omg.Components.Deployment.ServerActivatorOperations
40 {
41     // server activator
42
static private String JavaDoc _class_name = "ServerActivatorImpl";
43     static final private String JavaDoc _type_id = org.omg.Components.Deployment.ServerActivatorHelper.id();
44     private ORBService _orb_service;
45     private org.omg.CosNaming.NamingContext JavaDoc _local_ns;
46     private java.util.ArrayList JavaDoc _cs_refs; // (list of ::Components::Deployment::ComponentServer)
47
private int _cs_count;
48
49     // default constructor
50
public
51     ServerActivatorImpl()
52     {
53         // server activator
54
_orb_service = null;
55         _local_ns = null;
56         _cs_refs = new java.util.ArrayList JavaDoc();
57         _cs_count = 0;
58     }
59
60     //
61
// Entry point
62
//
63

64     static public Servant
65     create_servant()
66     {
67         return new ServerActivatorImpl();
68     }
69
70     //
71
// internal operations
72
//
73

74     //
75
// IDL:org/objectweb/corba/runtime/SystemComponent:1.0
76
//
77

78     final public void
79     system_configuration_complete(SystemConfiguration cfg)
80     {
81         // obtain services
82
ServantConfiguration scfg = (ServantConfiguration)cfg;
83         _orb_service = scfg.orb_service();
84
85         // obtain ref of local ns (should be registered with LocalNameService name)
86
org.omg.CORBA.Object JavaDoc obj = null;
87         obj = _orb_service.resolve_initial_references("LocalNameService");
88
89         // check if found
90
if (obj==null) {
91             // NOTE: should not happen
92
final String JavaDoc opname = "system_configuration_complete";
93             final String JavaDoc msg = "FAILED (local NS not found)";
94             TheLogger.error(_class_name, opname, msg);
95             return ;
96         }
97
98         _local_ns = org.omg.CosNaming.NamingContextHelper.narrow(obj);
99
100         // register ConfigValue factory
101
String JavaDoc typeid = org.omg.Components.ConfigValueHelper.id();
102         org.omg.CORBA.portable.ValueFactory JavaDoc fact = ConfigValueFactoryImpl.create_factory();
103         _orb_service.register_valuefactory(new ValueFactoryImpl(typeid, fact));
104     }
105
106     final public void
107     destroy()
108     {
109         // destroy all created CS
110
org.omg.Components.Deployment.ComponentServer[] refs = get_component_servers();
111         for (int i=0;i<refs.length;i++) {
112             try {
113                 refs[i].remove();
114             } catch (org.omg.Components.RemoveFailure ex) {
115                 // ignore
116
final String JavaDoc opname = "destroy";
117                 TheLogger.debug(_class_name, opname, "IGNORE", ex);
118                 return ;
119             }
120         }
121
122         // clear list
123
_cs_refs.clear();
124
125         // NOTE: TODO: should stop the process
126
}
127
128     //
129
// IDL:org/objectweb/corba/runtime/Servant:1.0
130
//
131

132     final public String JavaDoc
133     type_id()
134     {
135         return _type_id;
136     }
137
138     final public org.omg.PortableServer.Servant JavaDoc
139     as_native_servant()
140     {
141         return new org.omg.Components.Deployment.ServerActivatorPOATie(this);
142     }
143
144     //
145
// IDL:omg.org/Components/Deployment/ServerActivator:1.0
146
//
147

148     final public org.omg.Components.Deployment.ComponentServer
149     create_component_server(org.omg.Components.ConfigValue[] config)
150     throws org.omg.Components.CreateFailure,
151            org.omg.Components.Deployment.InvalidConfiguration
152     {
153         // NOTE: TODO: rework this code
154
// - launch a thread which reads the subprocesses stdout/stderr to avoid deadlocks (see java doc)
155
// - use the cs_start scripts directly rather than the launcher
156

157         final String JavaDoc opname = "create_component_server";
158         String JavaDoc msg = null;
159         // NOTE: start a new process
160
String JavaDoc optionstr = System.getProperty("os.option.str");
161         String JavaDoc libdir = System.getProperty("ECM.install.lib.dir");
162         String JavaDoc xmldir = System.getProperty("ECM.install.launcher.dir");
163
164         // use fake insname
165
String JavaDoc insname = "ComponentServer"+_cs_count++;
166         String JavaDoc targetxml = xmldir+java.io.File.separator+"corba_runtime.xml";
167
168         // build args
169
String JavaDoc ns_ior = _orb_service.object_to_string(_local_ns);
170         org.omg.CORBA.Object JavaDoc obj = _orb_service.resolve_initial_references("ExtComponentInstallation");
171         String JavaDoc extci_ior = _orb_service.object_to_string(obj);
172
173         String JavaDoc[] args = {
174             "-Darchive.location="+libdir+java.io.File.separator+"ccm_runtime_cs.jar",
175             "-Darchive.entrypoint=org.objectweb.ccm.runtime.ComponentServerImpl.create_servant",
176             "-Druntime.id="+insname,
177             "-ArchiveLocation "+libdir+java.io.File.separator+"ccm_runtime_cs.jar",
178             "-ArchiveEntrypoint org.objectweb.ccm.runtime.ComponentServerImpl.create_servant",
179             "-RuntimeId "+insname,
180             "-ORBInitRef NameService="+ns_ior,
181             "-ORBInitRef ExtComponentInstallation="+extci_ior
182         };
183
184         // start a launcher
185
org.objectweb.util.launcher.Launcher launcher = null;
186         launcher = new org.objectweb.util.launcher.Launcher(targetxml, "corba_runtime", args);
187
188         // get process to wait for message on stderr
189
msg = "launcher started, wait on stderr/stdout";
190         TheLogger.debug(_class_name, opname, msg);
191         java.lang.Process JavaDoc csprocess = launcher.getProcess();
192         java.io.InputStream JavaDoc err = csprocess.getErrorStream();
193         java.io.InputStream JavaDoc out = csprocess.getInputStream();
194         java.io.BufferedInputStream JavaDoc berr = new java.io.BufferedInputStream JavaDoc(err);
195         java.io.BufferedInputStream JavaDoc bout = new java.io.BufferedInputStream JavaDoc(out);
196         try {
197             int errnum = 0;
198             int outnum = 0;
199             while (((errnum=berr.available())<=0) &&
200                    ((outnum=bout.available())<=0)) {
201                 // if no byte available, then CS is not fully started, so wait 500 ms
202
Thread.currentThread().sleep(500);
203             }
204
205             // read and log
206
if (errnum>0) {
207                 byte[] line = new byte[errnum];
208                 berr.read(line, 0, errnum);
209
210                 msg = "(STDERR)"+new String JavaDoc(line);
211                 TheLogger.debug(_class_name, opname, msg);
212             }
213
214             if (outnum>0) {
215                 byte[] line = new byte[outnum];
216                 bout.read(line, 0, outnum);
217
218                 msg = "(STDOUT)"+new String JavaDoc(line);
219                 TheLogger.debug(_class_name, opname, msg);
220             }
221
222             // start a reader thread to avoid deadlock on subprocess outputs (see javadoc)
223
new SAConsoleReader(berr,bout).start();
224         } catch (java.lang.Exception JavaDoc ex) {
225             // ignore
226
TheLogger.debug(_class_name, opname, "IGNORE", ex);
227         }
228
229         // obtain ref from local NS
230
RegistrationService regs = org.objectweb.corba.runtime.Runtime.getRegistrationService();
231         INSRegistrationScheme insscheme = (INSRegistrationScheme)regs.get_scheme(INSRegistrationScheme.SCHEME_ID);
232         obj = insscheme.lookup_in_ns(insname, _local_ns);
233
234         // check if found
235
if (obj==null) {
236             msg = "FAILED ("+insname+" not found in local NS)";
237             TheLogger.error(_class_name, opname, msg);
238         }
239
240         org.omg.Components.Deployment.ComponentServer cs = org.omg.Components.Deployment.ComponentServerHelper.narrow(obj);
241
242         // store
243
_cs_refs.add(cs);
244
245         
246         return cs;
247     }
248
249     final public void
250     remove_component_server(org.omg.Components.Deployment.ComponentServer server)
251     throws org.omg.Components.RemoveFailure
252     {
253         // remove from storage
254
org.omg.Components.Deployment.ComponentServer[] refs = get_component_servers();
255         int i=0;
256         for (;i<refs.length;i++) {
257             if (server._is_equivalent(refs[i])) {
258                 _cs_refs.remove(i);
259                 // NOTE: only call remove on the server if it was found to prevent a removal
260
// of a server which is not owned by this SA
261
server.remove();
262
263                 // NOTE: TODO: should stop the process
264

265                 break;
266             }
267         }
268
269         // if it wasn't found, report removal failure
270
if (i==refs.length) {
271             throw new org.omg.Components.RemoveFailure();
272         }
273     }
274
275     final public org.omg.Components.Deployment.ComponentServer[]
276     get_component_servers()
277     {
278         return (org.omg.Components.Deployment.ComponentServer[])_cs_refs.toArray(new org.omg.Components.Deployment.ComponentServer[0]);
279     }
280 }
281
282 //
283
//
284
//
285
class SAConsoleReader
286 extends java.lang.Thread JavaDoc
287 {
288     // reader
289
static private String JavaDoc _class_name = "SAConsoleReader";
290     private java.io.BufferedInputStream JavaDoc _err;
291     private java.io.BufferedInputStream JavaDoc _out;
292     private boolean _running;
293
294     // default constructor
295
public
296     SAConsoleReader(java.io.BufferedInputStream JavaDoc out,
297                     java.io.BufferedInputStream JavaDoc err)
298     {
299         // reader
300
_err = err;
301         _out = out;
302         _running = false;
303     }
304
305     //
306
// public operations
307
//
308

309     final public void
310     stopReader()
311     {
312         _running = false;
313     }
314
315     //
316
// java.lang.Thread
317
//
318

319     final public void
320     run()
321     {
322         final String JavaDoc opname = "run";
323         _running = true;
324
325         while (_running) {
326             try {
327                 int errnum = 0;
328                 int outnum = 0;
329                 while (((errnum=berr.available())<=0) &&
330                        ((outnum=bout.available())<=0)) {
331                     // if no byte available, wait 100 ms
332
Thread.currentThread().sleep(100);
333                 }
334
335                 // read and log
336
if (errnum>0) {
337                     byte[] line = new byte[errnum];
338                     berr.read(line, 0, errnum);
339
340                     msg = "(STDERR)"+new String JavaDoc(line);
341                     TheLogger.debug(_class_name, opname, msg);
342                 }
343
344                 if (outnum>0) {
345                     byte[] line = new byte[outnum];
346                     bout.read(line, 0, outnum);
347
348                     msg = "(STDOUT)"+new String JavaDoc(line);
349                     TheLogger.debug(_class_name, opname, msg);
350                 }
351
352             } catch (java.lang.Exception JavaDoc ex) {
353                 // ignore
354
TheLogger.debug(_class_name, opname, "IGNORE", ex);
355             }
356         }
357     }
358 }
359
Popular Tags