Package de.netseeker.ejoe.core

Source Code of de.netseeker.ejoe.core.InJvmProcessor

/*********************************************************************
* InJvmProcessor.java
* created on 22.05.2007 by netseeker
* $Id: InJvmProcessor.java,v 1.2 2007/05/27 22:13:09 netseeker Exp $
* $Log: InJvmProcessor.java,v $
* Revision 1.2  2007/05/27 22:13:09  netseeker
* *** empty log message ***
*
* Revision 1.1  2007/05/22 22:50:46  netseeker
* added In-JVM processing mode, fixed a minor bug within the serverside detection of ADAPTER_STRATEGY_DIRECT.
*
*
* ====================================================================
*
*  Copyright 2005-2006 netseeker aka Michael Manske
*
*  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.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import de.netseeker.ejoe.ConnectionHeader;
import de.netseeker.ejoe.IServerInfo;
import de.netseeker.ejoe.ServerInfo;
import de.netseeker.ejoe.adapter.AdapterFactory;
import de.netseeker.ejoe.adapter.SerializeAdapter;
import de.netseeker.ejoe.io.IOUtil;

/**
* An In-JVM solution using Piped-Streams to communicate directly with the ServerHandler of a local EJServer.
*
* @author netseeker
* @since 0.3.9.3
*/
public class InJvmProcessor extends ConnectionReader
{
    /**
     * Creates a new instance of InJvmProcessor
     *
     * @param receiverInfo server connection header
     * @param senderInfo client connection header
     */
    public InJvmProcessor(IServerInfo receiverInfo, ConnectionHeader senderInfo)
    {
        super( null, (ServerInfo) receiverInfo, senderInfo );
    }

    /**
     * Processes a client request directly with the ServerHandler of a local (within the same JVM) EJServer
     *
     * @param param client request
     * @return the answer returned by the local EJServer instance
     * @throws Exception
     */
    public Object process( Object param ) throws Exception
    {
        ByteArrayInputStream in =null;
        ByteArrayOutputStream out = null;
        SerializeAdapter adapter = AdapterFactory.createAdapter( getSenderInfo().getAdapterName() );
        Object request = null;
        Object result = null;

        boolean compressed = getSenderInfo().hasCompression() && getReceiverInfo().hasCompression();

        if ( !getSenderInfo().isDirect() )
        {
            try
            {
                out = new ByteArrayOutputStream();                              
                IOUtil.adapterSerialize( adapter, out, param, compressed, getSenderInfo().getCompressionLevel() );
                in = new ByteArrayInputStream(out.toByteArray());
                request = IOUtil.adapterDeserialize( adapter, in, compressed );
            }
            finally
            {
                IOUtil.closeQuiet( in );
                IOUtil.closeQuiet( out );
            }
        }
        else
        {
            request = param;
        }

        result = handleObject( request );

        if ( !getSenderInfo().isDirect() )
        {
            try
            {
                out = new ByteArrayOutputStream();
                IOUtil.adapterSerialize( adapter, out, result, compressed, getSenderInfo().getCompressionLevel() );
                in = new ByteArrayInputStream(out.toByteArray());
                result = IOUtil.adapterDeserialize( adapter, in, compressed );
            }
            finally
            {
                IOUtil.closeQuiet( in );
                IOUtil.closeQuiet( out );
            }
        }

        return result;
    }
}
TOP

Related Classes of de.netseeker.ejoe.core.InJvmProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.