KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > burlap > 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.burlap;
30
31 import com.caucho.burlap.io.BurlapInput;
32 import com.caucho.burlap.io.BurlapOutput;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.ejb.message.MessageServer;
35 import com.caucho.ejb.protocol.Skeleton;
36 import com.caucho.log.Log;
37 import com.caucho.services.message.MessageSender;
38 import com.caucho.util.CharBuffer;
39
40 import javax.jms.Connection JavaDoc;
41 import javax.jms.Destination JavaDoc;
42 import javax.jms.JMSException 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 BURLAP-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   
72   protected MessageProducer JavaDoc _sender;
73
74   MessageSkeleton(MessageServer server)
75     throws JMSException JavaDoc
76   {
77     _server = server;
78     _destination = server.getDestination();
79     _connection = server.getJMSConnection();
80
81     _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
82     _sender = _session.createProducer(server.getDestination());
83   }
84
85   /**
86    * Sets the owning server.
87    */

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

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

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