Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server;
 
 
 
 public class LocalConnector extends AbstractConnector
 {
     private static final Logger LOG = Log.getLogger(LocalConnector.class);
     private final BlockingQueue<Request_requests = new LinkedBlockingQueue<Request>();
     
     public LocalConnector()
     {
         setMaxIdleTime(30000);
     }
 
     public Object getConnection()
     {
         return this;
     }
 
     public String getResponses(String requeststhrows Exception
     {
         return getResponses(requestsfalse);
     }
 
     public String getResponses(String requestsboolean keepOpenthrows Exception
     {
         ByteArrayBuffer result = getResponses(new ByteArrayBuffer(requests.), keepOpen);
         return result==null?null:result.toString(.);
     }
 
     public ByteArrayBuffer getResponses(ByteArrayBuffer requestsBufferboolean keepOpenthrows Exception
     {
         CountDownLatch latch = new CountDownLatch(1);
         Request request = new Request(requestsBufferkeepOpenlatch);
         .add(request);
         latch.await(getMaxIdleTime(),.);
         return request.getResponsesBuffer();
     }
 
     @Override
     protected void accept(int acceptorIDthrows IOExceptionInterruptedException
     {
         Request request = .take();
         getThreadPool().dispatch(request);
     }
 
     public void open() throws IOException
     {
     }
 
     public void close() throws IOException
     {
     }
 
     public int getLocalPort()
     {
         return -1;
     }
 
     public void executeRequest(String rawRequestthrows IOException
     {
         Request request = new Request(new ByteArrayBuffer(rawRequest"UTF-8"), truenull);
         .add(request);
     }
 
     private class Request implements Runnable
     {
         private final ByteArrayBuffer _requestsBuffer;
         private final boolean _keepOpen;
         private final CountDownLatch _latch;
        private volatile ByteArrayBuffer _responsesBuffer;
        private Request(ByteArrayBuffer requestsBufferboolean keepOpenCountDownLatch latch)
        {
             = requestsBuffer;
             = keepOpen;
             = latch;
        }
        public void run()
        {
            try
            {
                ByteArrayEndPoint endPoint = new ByteArrayEndPoint(.asArray(), 1024)
                {
                    @Override
                    public void setConnection(Connection connection)
                    {
                        if (getConnection()!=null && connection!=getConnection())
                            connectionUpgraded(getConnection(),connection);
                        super.setConnection(connection);
                    }
                };
                endPoint.setGrowOutput(true);
                AbstractHttpConnection connection = new BlockingHttpConnection(LocalConnector.thisendPointgetServer());
                endPoint.setConnection(connection);
                connectionOpened(connection);
                boolean leaveOpen = ;
                try
                {
                    while (endPoint.getIn().length() > 0 && endPoint.isOpen())
                    {
                        while (true)
                        {
                            final Connection con = endPoint.getConnection();
                            final Connection next = con.handle();
                            if (next!=con)
                            {  
                                endPoint.setConnection(next);
                                continue;
                            }
                            break;
                        }
                    }
                }
                catch (IOException x)
                {
                    .debug(x);
                    leaveOpen = false;
                }
                catch (Exception x)
                {
                    .warn(x);
                    leaveOpen = false;
                }
                finally
                {
                    if (!leaveOpen)
                        connectionClosed(connection);
                     = endPoint.getOut();
                }
            }
            finally
            {
                if ( != null)
                    .countDown();
            }
        }
        public ByteArrayBuffer getResponsesBuffer()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X