View Javadoc

1   /**********************************************************************
2    * InJvmProcessor.java
3    * created on 22.05.2007 by netseeker
4    * $Id: InJvmProcessor.java,v 1.2 2007/05/27 22:13:09 netseeker Exp $
5    * $Log: InJvmProcessor.java,v $
6    * Revision 1.2  2007/05/27 22:13:09  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.1  2007/05/22 22:50:46  netseeker
10   * added In-JVM processing mode, fixed a minor bug within the serverside detection of ADAPTER_STRATEGY_DIRECT.
11   *
12   *
13   * ====================================================================
14   *
15   *  Copyright 2005-2006 netseeker aka Michael Manske
16   *
17   *  Licensed under the Apache License, Version 2.0 (the "License");
18   *  you may not use this file except in compliance with the License.
19   *  You may obtain a copy of the License at
20   *
21   *      http://www.apache.org/licenses/LICENSE-2.0
22   *
23   *  Unless required by applicable law or agreed to in writing, software
24   *  distributed under the License is distributed on an "AS IS" BASIS,
25   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   *  See the License for the specific language governing permissions and
27   *  limitations under the License.
28   * ====================================================================
29   *
30   * This file is part of the EJOE framework.
31   * For more information on the author, please see
32   * <http://www.manskes.de/>.
33   *
34   *********************************************************************/
35  package de.netseeker.ejoe.core;
36  
37  import java.io.ByteArrayInputStream;
38  import java.io.ByteArrayOutputStream;
39  
40  import de.netseeker.ejoe.ConnectionHeader;
41  import de.netseeker.ejoe.IServerInfo;
42  import de.netseeker.ejoe.ServerInfo;
43  import de.netseeker.ejoe.adapter.AdapterFactory;
44  import de.netseeker.ejoe.adapter.SerializeAdapter;
45  import de.netseeker.ejoe.io.IOUtil;
46  
47  /***
48   * An In-JVM solution using Piped-Streams to communicate directly with the ServerHandler of a local EJServer.
49   * 
50   * @author netseeker
51   * @since 0.3.9.3
52   */
53  public class InJvmProcessor extends ConnectionReader
54  {
55      /***
56       * Creates a new instance of InJvmProcessor
57       * 
58       * @param receiverInfo server connection header
59       * @param senderInfo client connection header
60       */
61      public InJvmProcessor(IServerInfo receiverInfo, ConnectionHeader senderInfo)
62      {
63          super( null, (ServerInfo) receiverInfo, senderInfo );
64      }
65  
66      /***
67       * Processes a client request directly with the ServerHandler of a local (within the same JVM) EJServer
68       * 
69       * @param param client request
70       * @return the answer returned by the local EJServer instance
71       * @throws Exception
72       */
73      public Object process( Object param ) throws Exception
74      {
75          ByteArrayInputStream in =null;
76          ByteArrayOutputStream out = null;
77          SerializeAdapter adapter = AdapterFactory.createAdapter( getSenderInfo().getAdapterName() );
78          Object request = null;
79          Object result = null;
80  
81          boolean compressed = getSenderInfo().hasCompression() && getReceiverInfo().hasCompression();
82  
83          if ( !getSenderInfo().isDirect() )
84          {
85              try
86              {
87                  out = new ByteArrayOutputStream();                               
88                  IOUtil.adapterSerialize( adapter, out, param, compressed, getSenderInfo().getCompressionLevel() );
89                  in = new ByteArrayInputStream(out.toByteArray());
90                  request = IOUtil.adapterDeserialize( adapter, in, compressed );
91              }
92              finally
93              {
94                  IOUtil.closeQuiet( in );
95                  IOUtil.closeQuiet( out );
96              }
97          }
98          else
99          {
100             request = param;
101         }
102 
103         result = handleObject( request );
104 
105         if ( !getSenderInfo().isDirect() )
106         {
107             try
108             {
109                 out = new ByteArrayOutputStream();
110                 IOUtil.adapterSerialize( adapter, out, result, compressed, getSenderInfo().getCompressionLevel() );
111                 in = new ByteArrayInputStream(out.toByteArray());
112                 result = IOUtil.adapterDeserialize( adapter, in, compressed );
113             }
114             finally
115             {
116                 IOUtil.closeQuiet( in );
117                 IOUtil.closeQuiet( out );
118             }
119         }
120 
121         return result;
122     }
123 }