KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > ServerStartupDaemonImpl


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.imr;
22
23 /**
24  * This class is used to start servers on (from the view of the repository)
25  * remote hosts. It has a thread for forwarding output of started servers.
26  *
27  * @author Nicolas Noffke
28  *
29  * $Id: ServerStartupDaemonImpl.java,v 1.14 2004/05/06 12:39:59 nicolas Exp $
30  *
31  */

32
33 import org.jacorb.util.threadpool.*;
34
35 import java.lang.*;
36 import java.net.*;
37 import java.io.*;
38
39 import org.omg.PortableServer.*;
40
41 import org.apache.avalon.framework.logger.Logger;
42 import org.apache.avalon.framework.configuration.*;
43
44 public class ServerStartupDaemonImpl
45     extends org.jacorb.imr.ServerStartupDaemonPOA
46 {
47     private org.omg.CORBA.ORB JavaDoc orb = null;
48     private static final String JavaDoc out_prefix = ">> ";
49
50     private ThreadPool stdout_pool = null;
51     private ThreadPool stderr_pool = null;
52
53     private Logger logger;
54
55     /**
56      * The constructor. It registers this daemon at the repository.
57      *
58      * @exception Exception any exception that is thrown inside is propagated upwards.
59      */

60     public ServerStartupDaemonImpl(org.omg.CORBA.ORB JavaDoc orb)
61     {
62         this.orb = orb;
63     }
64
65     public void configure(Configuration myConfiguration)
66         throws ConfigurationException
67     {
68         this.logger = ((org.jacorb.config.Configuration) myConfiguration).getNamedLogger("jacorb.imr");
69
70         try
71         {
72             Registration _registration = null;
73
74             _registration =
75                 RegistrationHelper.narrow( orb.resolve_initial_references("ImplementationRepository"));
76             if( _registration == null )
77                 throw new ConfigurationException("ImR not found");
78
79             POA poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
80             poa.the_POAManager().activate();
81
82             ServerStartupDaemon ssd =
83                 ServerStartupDaemonHelper.narrow(poa.servant_to_reference(this));
84
85             HostInfo _me = new HostInfo(InetAddress.getLocalHost().getHostName(),
86                                         ssd,
87                                         orb.object_to_string(ssd));
88
89             _registration.register_host(_me);
90         }
91         catch (Exception JavaDoc e)
92         {
93             throw new ConfigurationException("Caught Exception", e);
94         }
95
96         stdout_pool = new ThreadPool( new OutputForwarderFactory( new InputStreamSelector(){
97                 public InputStream getInputStream( Process JavaDoc p )
98                 {
99                     return p.getInputStream();
100                 }
101             }),
102                                       100, //max threads
103
10 );//max idle threads
104

105         stderr_pool = new ThreadPool( new OutputForwarderFactory( new InputStreamSelector(){
106                 public InputStream getInputStream( Process JavaDoc p )
107                 {
108                     return p.getErrorStream();
109                 }
110             }),
111                                       100, //max threads
112
10 );//max idle threads
113

114     }
115
116     /**
117      * NOT IMPLEMENTED, but currently used for "pinging" purposes.
118      * @return 0 always
119      */

120
121     public int get_system_load()
122     {
123         // Dummy method, not supported yet.
124
return 0;
125     }
126
127     /**
128      * This method starts a server on this host as specified by 'command'.
129      *
130      * @param command The server startup command, i.e. the servers class name and
131      * parameters for its main method. The interpreter is inserted automatically.
132      *
133      * @exception org.jacorb.imr.ServerStartupDaemonPackage.ServerStartupFailed
134      * Runtime.exec() failed to execute the command.
135      */

136
137     public void start_server(String JavaDoc command)
138         throws ServerStartupFailed
139     {
140         try
141         {
142             if (this.logger.isDebugEnabled())
143             {
144                 this.logger.debug("Starting: " + command);
145             }
146
147             Process JavaDoc _server = Runtime.getRuntime().exec( command );
148
149             stdout_pool.putJob(_server);
150             stderr_pool.putJob(_server);
151         }
152         catch (Exception JavaDoc _e)
153         {
154             this.logger.debug("Caught Exception", _e);
155             throw new ServerStartupFailed( _e.toString() );
156         }
157     }
158
159     /**
160      * main method. Creates a new ServerStartupDaemonImpl instance and runs the orb.
161      **/

162     public static void main( String JavaDoc[] args )
163     {
164         try
165         {
166             org.omg.CORBA.ORB JavaDoc orb = org.omg.CORBA.ORB.init( args, null );
167             ServerStartupDaemonImpl _ssd = new ServerStartupDaemonImpl(orb);
168             _ssd.configure(((org.jacorb.orb.ORB) orb).getConfiguration());
169
170             orb.run();
171         }
172         catch( Exception JavaDoc _e )
173         {
174             _e.printStackTrace();
175             System.exit(1);
176         }
177
178         System.exit(0);
179     }
180
181     /**
182      * Inner class used to forward output of servers, since that would be
183      * invisible otherwise.
184      */

185     private class OutputForwarder
186         implements Consumer
187     {
188         /**
189          * prefix to help distinguish between output of a
190          * started server and output of this SSD
191          */

192         private InputStreamSelector selector = null;
193
194         public OutputForwarder( InputStreamSelector selector )
195         {
196             this.selector = selector;
197         }
198
199         public void doWork( Object JavaDoc job )
200         {
201             Process JavaDoc p = (Process JavaDoc) job;
202
203             BufferedReader _in = new BufferedReader(new InputStreamReader(selector.getInputStream( p )));
204             String JavaDoc _line = null;
205
206             try
207             {
208                 // If we get null from readLine() we assume that the process
209
// has exited. Unfortunately there is no exception thrown
210
// when trying to read from a dead processes output stream.
211
while((_line = _in.readLine()) != null)
212                 {
213                     System.out.println(out_prefix + _line);
214                 }
215
216                 _in.close();
217             }
218             catch( Exception JavaDoc _e )
219             {
220                 logger.debug("Caught Exception", _e);
221             }
222
223             logger.debug("A server process exited");
224         }
225     }//OutputForwarder
226

227     private interface InputStreamSelector
228     {
229         public InputStream getInputStream( Process JavaDoc p );
230     }
231
232     private class OutputForwarderFactory
233         implements ConsumerFactory
234     {
235         private InputStreamSelector selector = null;
236
237         public OutputForwarderFactory( InputStreamSelector selector )
238         {
239             this.selector = selector;
240         }
241
242         public Consumer create()
243         {
244             return new OutputForwarder( selector );
245         }
246     }
247 } // ServerStartupDaemonImpl
248
Popular Tags