1 8 9 package mx4j.tools.naming; 10 11 import java.io.BufferedInputStream ; 12 import java.io.File ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InterruptedIOException ; 16 import java.security.AccessController ; 17 import java.security.PrivilegedAction ; 18 19 import mx4j.log.Log; 20 import mx4j.log.Logger; 21 22 29 public class CosNamingService implements CosNamingServiceMBean 30 { 31 private int m_port; 32 private volatile boolean m_running; 33 private Process m_process; 34 private InputStreamConsumer m_output; 35 private InputStreamConsumer m_error; 36 private volatile Exception exception; 37 38 41 public CosNamingService() 42 { 43 this(900); 44 } 45 46 49 public CosNamingService(int port) 50 { 51 m_port = port; 52 } 53 54 59 public void setPort(int port) 60 { 61 m_port = port; 62 } 63 64 69 public int getPort() 70 { 71 return m_port; 72 } 73 74 79 public boolean isRunning() 80 { 81 return m_running; 82 } 83 84 90 public synchronized void start() throws Exception 91 { 92 if (isRunning()) return; 93 94 final Logger logger = getLogger(); 95 96 Thread thread = new Thread (new Runnable () 98 { 99 public void run() 100 { 101 String home = getJavaHomeBin(); 102 103 String command = (home == null ? "" : home) + "tnameserv -ORBInitialPort " + getPort(); 104 try 105 { 106 m_process = Runtime.getRuntime().exec(command); 107 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Process created: " + m_process); 108 } 109 catch (IOException x) 110 { 111 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Could not create process", x); 112 exception = x; 113 return; 114 } 115 116 m_output = new InputStreamConsumer(m_process.getInputStream()); 117 m_error = new InputStreamConsumer(m_process.getErrorStream()); 118 m_output.start(); 119 m_error.start(); 120 121 m_running = true; 122 123 try 124 { 125 int result = m_process.waitFor(); 127 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Exit value for tnameserv is: " + result); 128 129 if (isRunning()) 132 { 133 stop(); 134 if (logger.isEnabledFor(Logger.INFO)) logger.info("Unexpected death of tnameserv process (maybe the port " + getPort() + " is already in use)"); 135 exception = new IOException ("Unexpected death of tnameserv process " + m_process); 136 } 137 } 138 catch (InterruptedException x) 139 { 140 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Process tnameserv has been interrupted", x); 141 stop(); 142 } 143 } 144 }, "CosNamingService Thread"); 145 146 thread.setDaemon(true); 147 thread.start(); 148 149 Thread.sleep(500); 151 152 try 155 { 156 m_process.exitValue(); 157 throw new IOException ("Unexpected death of tnameserv process " + m_process); 159 } 160 catch (IllegalThreadStateException ignored) 161 { 162 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Process tnameserv is alive"); 164 } 165 166 if (!isRunning() && exception != null) throw exception; 168 169 if (logger.isEnabledFor(Logger.TRACE)) logger.trace("CosNamingService started"); 170 } 171 172 private String getJavaHomeBin() 173 { 174 String home = (String )AccessController.doPrivileged(new PrivilegedAction () 175 { 176 public Object run() 177 { 178 return System.getProperty("java.home"); 179 } 180 }); 181 if (home != null && !home.endsWith(File.separator)) home += File.separator; 182 if (home != null) home += "bin" + File.separator; 183 return home; 184 } 185 186 191 public synchronized void stop() 192 { 193 if (!isRunning()) return; 194 195 m_running = false; 196 m_output.interrupt(); 197 m_error.interrupt(); 198 m_process.destroy(); 199 } 200 201 private Logger getLogger() 202 { 203 return Log.getLogger(getClass().getName()); 204 } 205 206 private class InputStreamConsumer extends Thread 207 { 208 private final InputStream m_stream; 209 private final byte[] m_buffer = new byte[128]; 210 211 public InputStreamConsumer(InputStream stream) 212 { 213 super("Stream Consumer Thread"); 214 m_stream = new BufferedInputStream (stream); 215 setDaemon(true); 216 } 217 218 public void run() 219 { 220 Logger logger = getLogger(); 221 while (!isInterrupted()) 222 { 223 try 224 { 225 int read = -1; 226 while ((read = m_stream.read(m_buffer)) >= 0) 227 { 228 if (logger.isEnabledFor(Logger.INFO)) logger.info(new String (m_buffer, 0, read)); 229 } 230 } 231 catch (InterruptedIOException x) 232 { 233 Thread.currentThread().interrupt(); 234 break; 235 } 236 catch (IOException x) 237 { 238 if (logger.isEnabledFor(Logger.INFO)) logger.info("Error while consuming process stream", x); 239 break; 240 } 241 } 242 } 243 } 244 } 245 | Popular Tags |