1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: RawProtocol.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.protocol.raw;
27   
28   import com.mjh.switchrmi.*;
29   
30   import java.io.IOException;
31   import java.io.InputStream;
32   import java.io.ObjectInputStream;
33   import java.io.ObjectOutputStream;
34   import java.io.OutputStream;
35   
36   import java.lang.reflect.Method;
37   
38   import org.apache.log4j.Logger;
39   
40   public class RawProtocol implements RmiProtocol
41   {
42       private static final Logger log = 
43               Logger.getLogger(RawProtocol.class.getName());
44   
45       public RawProtocol()
46       {
47       }
48   
49       public String getName()
50       {
51           return RmiProtocol.RAWRPC;
52       }
53   
54       public String getMimeType()
55       {
56           return RmiProtocol.RAWRPC_CONTENT_TYPE;
57       }
58   
59       public RmiRequest createRequest(Method method, Object[] args, 
60                                       RmiContext context)
61       {
62           return new RmiRequestImpl(method, args);
63       }
64   
65       public RmiResponse createResponse(RmiRequest request, Object result, 
66                                         RmiContext context)
67       {
68           return new RmiResponseImpl(request, result);
69       }
70   
71       public void writeRequest(RmiRequest request, RmiContext context)
72                         throws Exception
73       {
74           writeObject(request, context);
75       }
76   
77       public RmiResponse readResponse(RmiContext context)
78                                throws Exception
79       {
80           return (context.getTransport().getType().equals(RmiTransport.SYNCHRONOUS))
81                  ? (RmiResponse) readObject(context) : new RmiResponseImpl(null);
82       }
83   
84       public RmiRequest readRequest(RmiContext context)
85                              throws Exception
86       {
87           return (RmiRequest) readObject(context);
88       }
89   
90       public void writeResponse(RmiResponse response, RmiContext context)
91                          throws Exception
92       {
93           if (context.getTransport().getType().equals(RmiTransport.SYNCHRONOUS))
94           {
95               writeObject(response, context);
96           }
97       }
98   
99       private void writeObject(Object object, RmiContext context)
100                        throws Exception
101      {
102          ObjectOutputStream out = 
103                  new ObjectOutputStream(context.getTransport()
104                                                .getOutputStream(context));
105  
106          out.writeObject(object);
107          out.flush();
108      }
109  
110      private Object readObject(RmiContext context)
111                         throws Exception
112      {
113          ObjectInputStream in = 
114                  new ObjectInputStream(context.getTransport()
115                                               .getInputStream(context));
116  
117          return in.readObject();
118      }
119  
120      private void debug(String msg)
121      {
122          if (log.isDebugEnabled())
123          {
124              log.debug(msg);
125          }
126      }
127  }