1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.io.*; 15 import java.net.*; 16 import org.eclipse.osgi.framework.console.CommandInterpreter; 17 import org.eclipse.osgi.framework.console.CommandProvider; 18 import org.eclipse.osgi.util.NLS; 19 import org.osgi.framework.ServiceReference; 20 import org.osgi.util.tracker.ServiceTracker; 21 22 28 public class FrameworkConsole implements Runnable { 29 30 protected BufferedReader in; 31 32 protected PrintWriter out; 33 34 protected final org.osgi.framework.BundleContext context; 35 36 protected final OSGi osgi; 37 38 protected final String [] args; 39 40 protected final CommandProvider osgicp; 41 42 protected final ServiceTracker cptracker; 43 44 45 static final String defaultEncoding = "iso8859-1"; 47 static final String encoding = FrameworkProperties.getProperty("osgi.console.encoding", FrameworkProperties.getProperty("file.encoding", defaultEncoding)); 49 50 protected final boolean useSocketStream; 51 protected boolean disconnect = false; 52 protected final int port; 53 protected ConsoleSocketGetter scsg = null; 54 protected Socket s; 55 boolean blockOnready = FrameworkProperties.getProperty("osgi.dev") != null || FrameworkProperties.getProperty("osgi.console.blockOnReady") != null; volatile boolean shutdown = false; 57 58 65 public FrameworkConsole(OSGi osgi, String [] args) { 66 this(osgi, args, 0, false); 67 } 68 69 76 public FrameworkConsole(OSGi osgi, int port, String [] args) { 77 this(osgi, args, port, true); 78 } 79 80 private FrameworkConsole(OSGi osgi, String [] args, int port, boolean useSocketStream) { 81 this.args = args; 82 this.osgi = osgi; 83 this.useSocketStream = useSocketStream; 84 this.port = port; 85 this.context = osgi.getBundleContext(); 86 87 this.cptracker = new ServiceTracker(context, CommandProvider.class.getName(), null); 89 this.cptracker.open(); 90 91 this.osgicp = new FrameworkCommandProvider(osgi).intialize(); 93 } 94 95 98 private void getDefaultStreams() { 99 InputStream is = new FilterInputStream(System.in) { 100 public void close() throws IOException { 101 } 103 }; 104 in = createBufferedReader(is); 105 106 OutputStream os = new FilterOutputStream(System.out) { 107 public void close() throws IOException { 108 } 110 }; 111 out = createPrintWriter(os); 112 disconnect = false; 113 } 114 115 120 private void getSocketStream() { 121 try { 122 System.out.println(NLS.bind(ConsoleMsg.CONSOLE_LISTENING_ON_PORT, String.valueOf(port))); 123 synchronized (this) { 124 if (scsg == null) 125 scsg = new ConsoleSocketGetter(new ServerSocket(port)); 126 scsg.setAcceptConnections(true); 127 } 128 Socket temp = scsg.getSocket(); 130 if (temp == null) 131 return; 132 synchronized (this) { 133 s = temp; 134 in = createBufferedReader(s.getInputStream()); 135 out = createPrintWriter(s.getOutputStream()); 136 disconnect = false; 137 } 138 } catch (UnknownHostException uhe) { 139 uhe.printStackTrace(); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 145 151 private BufferedReader createBufferedReader(InputStream _in) { 152 BufferedReader reader; 153 try { 154 reader = new BufferedReader(new InputStreamReader(_in, encoding)); 155 } catch (UnsupportedEncodingException uee) { 156 reader = new BufferedReader(new InputStreamReader(_in)); 158 } 159 return reader; 160 } 161 162 168 PrintWriter createPrintWriter(OutputStream _out) { 169 PrintWriter writer; 170 try { 171 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out, encoding)), true); 172 } catch (UnsupportedEncodingException uee) { 173 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(_out)), true); 175 } 176 return writer; 177 } 178 179 183 public PrintWriter getWriter() { 184 return out; 185 } 186 187 191 public BufferedReader getReader() { 192 return in; 193 } 194 195 199 public boolean getUseSocketStream() { 200 return useSocketStream; 201 } 202 203 206 public void run() { 207 getDefaultStreams(); 209 try { 210 console(args); 211 } catch (IOException e) { 212 e.printStackTrace(out); 213 } 214 while (!shutdown) { 215 if (useSocketStream) 216 getSocketStream(); 217 try { 218 console(); 219 } catch (IOException e) { 220 if (!shutdown) 221 e.printStackTrace(out); 222 } 223 } 224 } 225 226 236 public void console(String args[]) throws IOException { 237 if (args != null) { 239 for (int i = 0; i < args.length; i++) { 240 docommand(args[i]); 241 } 242 } 243 } 244 245 253 protected void console() throws IOException { 254 BufferedReader br = in; 256 String consolePrompt = "\r\n" + ConsoleMsg.CONSOLE_PROMPT; while (!disconnected()) { 259 out.print(consolePrompt); 260 out.flush(); 261 262 String cmdline = null; 263 if (blockOnready && !useSocketStream) { 264 try { 266 while (!br.ready()) 267 Thread.sleep(300); 268 cmdline = br.readLine(); 269 } catch (InterruptedException e) { 270 } 272 } else 273 cmdline = br.readLine(); 274 275 if (cmdline != null && !shutdown) 276 docommand(cmdline); 277 } 278 } 279 280 286 protected void docommand(String cmdline) { 287 if (cmdline != null && cmdline.length() > 0) { 288 CommandInterpreter intcp = new FrameworkCommandInterpreter(cmdline, getServices(), this); 289 String command = intcp.nextArgument(); 290 if (command != null) { 291 intcp.execute(command); 292 } 293 } 294 } 295 296 300 public synchronized void disconnect() { 301 if (!disconnect) { 302 disconnect = true; 303 if (useSocketStream) { 305 if (s != null) 306 try { 307 s.close(); 308 } catch (IOException ioe) { 309 } 311 if (out != null) 312 out.close(); 313 if (in != null) 314 try { 315 in.close(); 316 } catch (IOException ioe) { 317 } 319 } 320 } 321 } 322 323 326 private synchronized boolean disconnected() { 327 return disconnect; 328 } 329 330 335 public String getInput() { 336 String input; 337 try { 338 339 input = in.readLine(); 340 System.out.println("<" + input + ">"); } catch (IOException e) { 342 input = ""; } 344 return input; 345 } 346 347 357 public Object [] getServices() { 358 ServiceReference[] serviceRefs = cptracker.getServiceReferences(); 359 Util.dsort(serviceRefs, 0, serviceRefs.length); 360 361 Object [] serviceObjects = new Object [serviceRefs.length]; 362 for (int i = 0; i < serviceRefs.length; i++) 363 serviceObjects[i] = FrameworkConsole.this.context.getService(serviceRefs[i]); 364 return serviceObjects; 365 } 366 367 372 public synchronized void shutdown() { 373 shutdown = true; 374 cptracker.close(); 375 disconnect(); 376 if (scsg != null) 377 try { 378 scsg.shutdown(); 379 } catch (IOException e) { 380 System.err.println(e.getMessage()); 381 } 382 } 383 384 391 class ConsoleSocketGetter implements Runnable { 392 393 394 private final ServerSocket server; 395 396 private Socket socket; 397 398 private boolean acceptConnections = true; 399 400 private final Object lock = new Object (); 401 402 408 ConsoleSocketGetter(ServerSocket server) { 409 this.server = server; 410 Thread t = new Thread (this, "ConsoleSocketGetter"); t.setDaemon(true); 412 t.start(); 413 } 414 415 public void run() { 416 while (!shutdown) { 417 try { 418 socket = server.accept(); 419 if (!acceptConnections) { 420 PrintWriter o = createPrintWriter(socket.getOutputStream()); 421 o.println(ConsoleMsg.CONSOLE_TELNET_CONNECTION_REFUSED); 422 o.println(ConsoleMsg.CONSOLE_TELNET_CURRENTLY_USED); 423 o.println(ConsoleMsg.CONSOLE_TELNET_ONE_CLIENT_ONLY); 424 o.close(); 425 socket.close(); 426 } else { 427 synchronized (lock) { 428 lock.notify(); 429 } 430 } 431 } catch (Exception e) { 432 if (!shutdown) 433 e.printStackTrace(); 434 } 435 436 } 437 } 438 439 444 public Socket getSocket() throws InterruptedException { 445 synchronized (lock) { 447 lock.wait(); } 449 setAcceptConnections(false); 450 return socket; 451 } 452 453 458 public void setAcceptConnections(boolean acceptConnections) { 459 this.acceptConnections = acceptConnections; 460 } 461 462 public void shutdown() throws IOException { 463 server.close(); 464 } 465 } 466 467 } 468 | Popular Tags |