1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 }