/* ====================================================================
 * Copyright 2005-2006 JRemoting Committers
 * Portions copyright 2001 - 2004 Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.codehaus.jremoting.server.transports;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutorService;

import org.codehaus.jremoting.requests.Request;
import org.codehaus.jremoting.responses.Response;
import org.codehaus.jremoting.server.ServerMonitor;

/**
 * Class ServerObjectStreamDriver
 *
 * @author Paul Hammant
 * @version $Revision: 1.2 $
 */
public class ServerObjectStreamDriver extends AbstractServerStreamDriver {

    /**
     * The Object Input Stream
     */
    private ObjectInputStream objectInputStream;

    /**
     * The Object Output Stream
     */
    private ObjectOutputStream objectOutputStream;

    /**
     * Constructor ServerObjectStreamDriver
     */
    public ServerObjectStreamDriver(ServerMonitor serverMonitor, ClassLoader facadesClassLoader, InputStream inputStream,
                                    OutputStream outputStream, Object connectionDetails) {
        super(serverMonitor, inputStream, outputStream, facadesClassLoader, connectionDetails);

    }

    /**
     * Write a response, and wait for a request
     *
     * @param response The response to send
     * @return The new request
     * @throws IOException            In an IO Exception
     * @throws ClassNotFoundException If a class not found during deserialization.
     */
    public synchronized Request writeResponseAndGetRequest(Response response) throws IOException, ClassNotFoundException {

        if (response != null) {
            writeResponse(response);
        }

        return readRequest();
    }

    /**
     * Write a rpely.
     *
     * @param response The response to write
     * @throws IOException If and IO Exception
     */
    private void writeResponse(Response response) throws IOException {

        objectOutputStream.writeObject(response);
        objectOutputStream.flush();

        objectOutputStream.reset();
    }

    public void close() {
        try {
            objectInputStream.close();
        } catch (IOException e) {
        }
        try {
            objectOutputStream.close();
        } catch (IOException e) {
        }
        super.close();
    }

    public void initialize() throws IOException {
        objectInputStream = new ObjectInputStream(new BufferedInputStream(getInputStream()));
        objectOutputStream = new ObjectOutputStream(getOutputStream());
    }

    /**
     * Read a request
     *
     * @return The request
     * @throws IOException            If an IO Exception
     * @throws ClassNotFoundException If a class not found during deserialization.
     */
    private Request readRequest() throws IOException, ClassNotFoundException {
        Request request = (Request) objectInputStream.readObject();
        return request;
    }
}

@@ -1171,16 +1171,81 @@
 Monitor;
+%0Aimport org.codehaus.jremoting.util.ClassLoaderObjectInputStream;
 %0A%0A/**%0A *
@@ -1416,55 +1416,8 @@
  %7B%0A%0A
-    /**%0A     * The Object Input Stream%0A     */%0A
     
@@ -1466,56 +1466,8 @@
 m;%0A%0A
-    /**%0A     * The Object Output Stream%0A     */%0A
     
@@ -1518,68 +1518,8 @@
 m;%0A%0A
-    /**%0A     * Constructor ServerObjectStreamDriver%0A     */%0A
     
@@ -1827,302 +1827,15 @@
 s);%0A
-%0A
     %7D%0A%0A
-    /**%0A     * Write a response, and wait for a request%0A     *%0A     * @param response The response to send%0A     * @return The new request%0A     * @throws IOException            In an IO Exception%0A     * @throws ClassNotFoundException If a class not found during deserialization.%0A     */%0A
     
@@ -1945,33 +1945,32 @@
 oundException %7B%0A
-%0A
         if (resp
@@ -2028,25 +2028,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -2073,145 +2073,8 @@
  %7D%0A%0A
-    /**%0A     * Write a rpely.%0A     *%0A     * @param response The response to write%0A     * @throws IOException If and IO Exception%0A     */%0A
     
@@ -2140,17 +2140,16 @@
 ption %7B%0A
-%0A
         
@@ -2226,17 +2226,16 @@
 lush();%0A
-%0A
         
@@ -2598,24 +2598,35 @@
 tream = new 
+ClassLoader
 ObjectInputS
@@ -2631,16 +2631,41 @@
 tStream(
+getFacadesClassLoader(), 
 new Buff
@@ -2783,220 +2783,8 @@
  %7D%0A%0A
-    /**%0A     * Read a request%0A     *%0A     * @return The request%0A     * @throws IOException            If an IO Exception%0A     * @throws ClassNotFoundException If a class not found during deserialization.%0A     */%0A
     
@@ -2870,25 +2870,14 @@
     
-Request request =
+return
  (Re
@@ -2919,32 +2919,8 @@
 ();%0A
-        return request;%0A
     

move classloader into server driver