1 21 package org.jacorb.imr; 22 23 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 orb = null; 48 private static final String out_prefix = ">> "; 49 50 private ThreadPool stdout_pool = null; 51 private ThreadPool stderr_pool = null; 52 53 private Logger logger; 54 55 60 public ServerStartupDaemonImpl(org.omg.CORBA.ORB 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 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 p ) 98 { 99 return p.getInputStream(); 100 } 101 }), 102 100, 10 ); 105 stderr_pool = new ThreadPool( new OutputForwarderFactory( new InputStreamSelector(){ 106 public InputStream getInputStream( Process p ) 107 { 108 return p.getErrorStream(); 109 } 110 }), 111 100, 10 ); 114 } 115 116 120 121 public int get_system_load() 122 { 123 return 0; 125 } 126 127 136 137 public void start_server(String command) 138 throws ServerStartupFailed 139 { 140 try 141 { 142 if (this.logger.isDebugEnabled()) 143 { 144 this.logger.debug("Starting: " + command); 145 } 146 147 Process _server = Runtime.getRuntime().exec( command ); 148 149 stdout_pool.putJob(_server); 150 stderr_pool.putJob(_server); 151 } 152 catch (Exception _e) 153 { 154 this.logger.debug("Caught Exception", _e); 155 throw new ServerStartupFailed( _e.toString() ); 156 } 157 } 158 159 162 public static void main( String [] args ) 163 { 164 try 165 { 166 org.omg.CORBA.ORB 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 _e ) 173 { 174 _e.printStackTrace(); 175 System.exit(1); 176 } 177 178 System.exit(0); 179 } 180 181 185 private class OutputForwarder 186 implements Consumer 187 { 188 192 private InputStreamSelector selector = null; 193 194 public OutputForwarder( InputStreamSelector selector ) 195 { 196 this.selector = selector; 197 } 198 199 public void doWork( Object job ) 200 { 201 Process p = (Process ) job; 202 203 BufferedReader _in = new BufferedReader(new InputStreamReader(selector.getInputStream( p ))); 204 String _line = null; 205 206 try 207 { 208 while((_line = _in.readLine()) != null) 212 { 213 System.out.println(out_prefix + _line); 214 } 215 216 _in.close(); 217 } 218 catch( Exception _e ) 219 { 220 logger.debug("Caught Exception", _e); 221 } 222 223 logger.debug("A server process exited"); 224 } 225 } 227 private interface InputStreamSelector 228 { 229 public InputStream getInputStream( Process 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 } | Popular Tags |