private boolean accept() {
   InputStream in = null;
   OutputStream out = null;
   Socket socket = null;
   Channel channel = null;
   try {
     try {
       socket = this.serverSocket.accept();
     } catch (IOException ex) {
       return false;
     } // socket closed
     in = socket.getInputStream();
     out = socket.getOutputStream();
     ContextRunner runner =
         new ContextRunner(channel = new Channel(getChannelName(), in, out, socket), logger);
     if (threadPool != null) {
       threadPool.start(runner);
     } else {
       Thread t = new Util.Thread(runner, "JavaBridgeContextRunner(" + contextName + ")");
       t.start();
     }
   } catch (SecurityException t) {
     if (channel != null) channel.shutdown();
     ContextFactory.destroyAll();
     Util.printStackTrace(t);
     return false;
   } catch (Throwable t) {
     if (channel != null) channel.shutdown();
     Util.printStackTrace(t);
   }
   return true;
 }
  /** Release the continuation */
  public void release() {
    if (continuation != null) {
      try {
        continuation.release();
        ctx.releaseManaged();
      } catch (InterruptedException e) {
        return;
      }
      ctx = null;

      continuation = null;
      script = null;
      scriptClosure = null;

      try {
        getContext().getWriter().flush();
      } catch (Exception e) {
        Util.printStackTrace(e);
      }
      try {
        getContext().getErrorWriter().flush();
      } catch (Exception e) {
        Util.printStackTrace(e);
      }
    }
  }
  protected Continuation getContinuation(Reader reader, ScriptContext context) throws IOException {
    HeaderParser headerParser =
        HeaderParser.DEFAULT_HEADER_PARSER; // ignore encoding, we pass everything directly
    IPhpScriptContext phpScriptContext = (IPhpScriptContext) context;
    updateGlobalEnvironment(context);
    OutputStream out = ((PhpScriptWriter) (context.getWriter())).getOutputStream();
    OutputStream err = ((PhpScriptWriter) (context.getErrorWriter())).getOutputStream();

    /*
     * encode according to content-type charset
     */
    if (out instanceof WriterOutputStream)
      headerParser = new SimpleHeaderParser((WriterOutputStream) out);

    Continuation kont =
        phpScriptContext.createContinuation(
            reader,
            env,
            out,
            err,
            headerParser,
            resultProxy = new ResultProxy(this),
            Util.getLogger(),
            isCompiled);

    phpScriptContext.setContinuation(kont);
    phpScriptContext.startContinuation();
    return kont;
  }
  /** Destroy the server */
  public void destroy() {
    closeAllSockets();

    if (serverSocket != null) {
      try {
        serverSocket.close();
      } catch (IOException e) {
        Util.printStackTrace(e);
      }
      serverSocket = null;
    }
  }
 /** {@inheritDoc} */
 public CompiledScript compile(String script) throws ScriptException {
   Reader reader = new StringReader(script);
   try {
     return compile(reader);
   } finally {
     try {
       reader.close();
     } catch (IOException e) {
       Util.printStackTrace(e);
     }
   }
 }
 /**
  * Create a new ContextServer using the ThreadPool.
  *
  * @param threadPool Obtain runnables from this pool. If null, new threads will be created.
  */
 public SocketContextServer(AppThreadPool threadPool, boolean promiscuous, String contextName) {
   this.threadPool = threadPool;
   this.contextName = contextName;
   try {
     serverSocket = JavaBridge.bind(promiscuous ? "INET:0" : "INET_LOCAL:0");
     SecurityManager sec = System.getSecurityManager();
     if (sec != null) sec.checkAccept("127.0.0.1", Integer.parseInt(serverSocket.getSocketName()));
     Thread t =
         new Util.Thread(
             this, "JavaBridgeSocketContextServer(" + serverSocket.getSocketName() + ")");
     t.start();
   } catch (Throwable t) {
     Util.warn("Local communication channel not available.");
     Util.printStackTrace(t);
     if (serverSocket != null)
       try {
         serverSocket.close();
       } catch (IOException e) {
       }
     serverSocket = null;
   }
 }
  /** Create a new context ID and a environment map which we send to the client. */
  protected void setNewContextFactory() {
    env = (Map) getProcessEnvironment().clone();

    addNewContextFactory();

    // short path S1: no PUT request
    ContextServer contextServer = getContextServer();
    AbstractChannelName channelName = contextServer.getChannelName(ctx);
    if (channelName != null) {
      env.put("X_JAVABRIDGE_REDIRECT", channelName.getName());
      ctx.getBridge();
      contextServer.start(channelName, Util.getLogger());
    }

    setStandardEnvironmentValues(env);
  }