KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > hessian > MessageSkeleton


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.hessian;
30
31 import com.caucho.ejb.AbstractServer;
32 import com.caucho.ejb.message.MessageServer;
33 import com.caucho.ejb.protocol.Skeleton;
34 import com.caucho.hessian.io.HessianInput;
35 import com.caucho.hessian.io.HessianOutput;
36 import com.caucho.hessian.io.HessianSerializerInput;
37 import com.caucho.log.Log;
38 import com.caucho.services.message.MessageSender;
39 import com.caucho.util.CharBuffer;
40
41 import javax.jms.Connection JavaDoc;
42 import javax.jms.Destination JavaDoc;
43 import javax.jms.Message JavaDoc;
44 import javax.jms.MessageProducer JavaDoc;
45 import javax.jms.Session JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.io.Serializable JavaDoc;
50 import java.util.HashMap JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * Base class for any bean skeleton capable of handling an HESSIAN-RPC request.
56  *
57  * <p/>Once selected, the calling servlet will dispatch the request through
58  * the <code>_service</code> call. After parsing the request headers,
59  * <code>_service</code> calls the generated entry <code>_execute</code>
60  * to execute the request.
61  */

62 public class MessageSkeleton extends Skeleton {
63   protected static Logger JavaDoc log = Log.open(MessageSkeleton.class);
64
65   protected MessageServer _server;
66   protected Connection JavaDoc _connection;
67
68   protected Destination JavaDoc _destination;
69
70   protected Session JavaDoc _session;
71   protected MessageProducer JavaDoc _producer;
72
73   MessageSkeleton(MessageServer server)
74     throws javax.jms.JMSException JavaDoc
75   {
76     _server = server;
77     _destination = server.getDestination();
78     _connection = server.getJMSConnection();
79     _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
80     _producer = _session.createProducer(_destination);
81   }
82
83   /**
84    * Sets the owning server.
85    */

86   void _setServer(AbstractServer server)
87   {
88     _server = (MessageServer) server;
89   }
90
91   /**
92    * Sets the underlying object manipulated by the skeleton.
93    */

94   protected void _setObject(Object JavaDoc obj)
95   {
96   }
97
98   /**
99    * Services the request.
100    *
101    * <pre>
102    * send(HashMap header, Object data)
103    * </pre>
104    *
105    * @param rawIs the raw input stream from the servlet request
106    * @param rawOs the raw output stream to the servlet response
107    */

108   public void _service(InputStream JavaDoc rawIs, OutputStream JavaDoc rawOs)
109     throws Exception JavaDoc
110   {
111     HessianInput in = new HessianSerializerInput(rawIs);
112     HessianOutput out = new HessianWriter(rawOs);
113
114     try {
115       in.startCall();
116       
117       String JavaDoc method = in.getMethod();
118
119       if (! "send".equals(method))
120         throw new IOException JavaDoc("no such method: " + method);
121       
122       HashMap JavaDoc headers = (HashMap JavaDoc) in.readObject();
123       Object JavaDoc data = in.readObject();
124
125       in.completeCall();
126
127       if (_destination instanceof MessageSender) {
128         ((MessageSender) _destination).send(headers, data);
129       }
130       else {
131         Message JavaDoc message = null;
132       
133         if (data instanceof String JavaDoc) {
134           message = _session.createTextMessage((String JavaDoc) data);
135         }
136         else
137           message = _session.createObjectMessage((Serializable JavaDoc) data);
138
139     _producer.send(message);
140       }
141
142       out.startReply();
143       out.writeNull();
144       out.completeReply();
145     } catch (Exception JavaDoc e) {
146       log.log(Level.FINE, e.toString(), e);
147
148       out.startReply();
149       out.writeFault("SystemFault", String.valueOf(e), e);
150       out.completeReply();
151     }
152   }
153
154   protected void _execute(CharBuffer method,
155                           HessianInput in,
156                           HessianOutput out)
157     throws Exception JavaDoc
158   {
159   }
160 }
161
162
163
Popular Tags