1 /*
2 * @(#)SocketLinkServer.java
3 *
4 * JFoxMQ the open source JMS MOM.
5 *
6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software
7 * is licensed under LGPL license.
8 *
9 * For more information, please visit: http://www.huihoo.org
10 */
11
12 package org.huihoo.jfox.ms.crb.socket;
13
14 import java.io.BufferedInputStream;
15 import java.io.BufferedOutputStream;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.net.InetSocketAddress;
20 import java.net.Socket;
21
22 import javax.jms.Destination;
23 import javax.jms.Message;
24 import javax.jms.Queue;
25 import javax.jms.TemporaryQueue;
26 import javax.jms.TemporaryTopic;
27 import javax.jms.Topic;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30
31 import org.huihoo.jfox.jndi.JNDIProperties;
32 import org.huihoo.jfox.ms.crb.GenConnectionFactoryImpl;
33 import org.huihoo.jfox.ms.crb.LinkServer;
34
35 /***
36 * Defines the methods that can be called by a client on the server.
37 *
38 * @author Liang.Zhao (zlushangnwpu@sina.com)
39 * @version @created July 23th 2003
40 */
41 public class SocketLinkServer
42 implements LinkServer, java.io.Serializable, java.lang.Cloneable {
43
44 private InetSocketAddress serverSocketAddress;
45 private Socket socket = null;
46 private ObjectInputStream in = null; // inputStream used by Socket
47 private ObjectOutputStream out = null; // OutputStream used by Socket
48 private boolean status = false;
49
50 public SocketLinkServer(InetSocketAddress address) {
51 serverSocketAddress = address;
52 }
53
54 public void createConnection() throws Exception {
55 socket = new Socket();
56 socket.connect(serverSocketAddress);
57 in =
58 new ObjectInputStream(
59 new BufferedInputStream(socket.getInputStream()));
60 out =
61 new ObjectOutputStream(
62 new BufferedOutputStream(socket.getOutputStream()));
63 //out.writeInt(SocketLinkConstants.ISREADY);
64 //waitForAnswer();
65 }
66
67 public void rleaseSource() {
68 try {
69 in.close();
70 out.close();
71 socket.close();
72 } catch (IOException e) {
73 System.out.println(e.toString());
74 }
75 }
76
77 /***
78 * @return Object
79 * @exception Exception
80 */
81 private Object waitForAnswer() throws Exception {
82 out.reset();
83 out.flush();
84 int result = in.readByte();
85 if (result == SocketLinkConstants.SUCCESS) {
86 return null;
87 }
88 if (result == SocketLinkConstants.SUCCESS_OBJECT) {
89 return in.readObject();
90 } else {
91 Exception e = (Exception)in.readObject();
92 throw e;
93 }
94 }
95
96 /***
97 * @return LinkServer
98 * @exception Exception
99 */
100 public LinkServer cloneLinkServer() throws Exception {
101 return (LinkServer)clone();
102 }
103
104 /***
105 * @return Object
106 * @exception CloneNotSupportedException
107 */
108 public Object clone() throws CloneNotSupportedException {
109 return super.clone();
110 }
111
112 /***
113 * Get a clientID from the server.
114 *
115 * @return an internally generated clientID.
116 * @exception Exception
117 */
118 public String getID() throws Exception {
119 if (status == false) {
120 createConnection();
121 status = true;
122 }
123 out.writeByte(SocketLinkConstants.GET_ID);
124 return (String)waitForAnswer();
125 }
126
127 /***
128 * Gets the TemporaryTopic
129 *
130 * @param
131 * @return TemporaryTopic
132 * @exception Exception
133 */
134 public TemporaryTopic createTemporaryTopic() throws Exception {
135 out.writeByte(SocketLinkConstants.CRE_TEMPORARY_TOPIC);
136 return (TemporaryTopic)waitForAnswer();
137 }
138
139 /***
140 * Gets the TemporaryQueue
141 *
142 * @param
143 * @return TemporaryQueue
144 * @exception Exception
145 */
146 public TemporaryQueue createTemporaryQueue() throws Exception {
147 out.writeByte(SocketLinkConstants.CRE_TEMPORARY_TOPIC);
148 return (TemporaryQueue)waitForAnswer();
149 }
150
151 /***
152 * The client close the connection.
153 *
154 * @param
155 * @exception Exception
156 */
157 public void closeConnection() throws Exception {
158 out.writeByte(SocketLinkConstants.CONNECTION_CLOSING);
159 waitForAnswer();
160 }
161
162 /***
163 * Check if clientID is a valid ID
164 *
165 * @param String
166 * ID
167 * @exception Exception
168 */
169 public void checkID(String ID) throws Exception {
170 out.writeByte(SocketLinkConstants.CHECK_ID);
171 out.writeObject(ID);
172 waitForAnswer();
173 }
174
175 /***
176 * Send a message to the destination specifyed in the message.
177 *
178 * @param Message
179 * @exception Exception
180 */
181 public void sendMessage(Message message) throws Exception {
182 out.writeByte(SocketLinkConstants.SEND_MESSAGE);
183 out.writeObject(message);
184 waitForAnswer();
185 }
186
187 /***
188 * Create a queue identity
189 *
190 * @param String
191 * destination
192 * @return Queue
193 * @exception Exception
194 */
195 public Queue createQueue(String destination) throws Exception {
196 out.writeByte(SocketLinkConstants.CREATE_QUEUE);
197 out.writeObject(destination);
198 return (Queue)waitForAnswer();
199 }
200
201 /***
202 * Create a topic identity
203 *
204 * @param destination
205 * @return Topic
206 * @exception Exception
207 */
208 public Topic createTopic(String destination) throws Exception {
209 out.writeByte(SocketLinkConstants.CREATE_TOPIC);
210 out.writeObject(destination);
211 return (Topic)waitForAnswer();
212
213 }
214
215 /***
216 * Delete a temporary destination from server
217 *
218 * @param Destination
219 * destination
220 * @exception Exception
221 */
222 public void deleteTemporaryDestination(Destination destination)
223 throws Exception {
224 out.writeByte(SocketLinkConstants.DELETE_TEMPORARY_DESTINATION);
225 out.writeObject(destination);
226 waitForAnswer();
227
228 }
229
230 /***
231 * Commit a transaction
232 *
233 * @param TransactionRequest
234 * t
235 * @exception Exception
236 */
237 //public void CommitTransaction(TransactionRequest t)
238 // throws Exception;
239
240 /***
241 * Send a acknowledge to server
242 *
243 * @param AcknowledgementRequest
244 * ack
245 * @exception Exception
246 */
247 //public void sendAcknowledge(AcknowledgementRequest ack)
248 // throws Exception;
249
250 /***
251 * Browse message on given destination
252 *
253 * @param Destination
254 * destination
255 * @param String
256 * selector
257 * @return Message[]
258 * @exception Exception
259 */
260 public Message[] browse(Destination destination, String selector)
261 throws Exception {
262 out.writeByte(SocketLinkConstants.BROWSE);
263 out.writeObject(destination);
264 out.writeObject(selector);
265 return (Message[])waitForAnswer();
266
267 }
268
269 /***
270 * Receive a message synchronously.
271 *
272 * @param Destination
273 * destination
274 * @param long
275 * wait
276 * @return Message
277 * @exception Exception
278 */
279 public Message receiveMessage(Destination destination, long wait)
280 throws Exception {
281 out.writeByte(SocketLinkConstants.RECEIVE);
282 out.writeObject(destination);
283 out.writeLong(wait);
284 return (Message)waitForAnswer();
285 }
286
287 /***
288 * Remove a subcriber.
289 *
290 * @param
291 * @exception Exception
292 * Description of Exception
293 */
294 //public void removeSubscriber()
295 // throws Exception
296
297 /***
298 * Authenticate the user.
299 *
300 * @param String
301 * userName
302 * @param String
303 * password
304 * @return boolean
305 * @exception Exception
306 */
307 public boolean authenticate(String userName, String password)
308 throws Exception {
309 out.writeByte(SocketLinkConstants.RECEIVE);
310 out.writeObject(password);
311 try {
312 waitForAnswer();
313 } catch (Exception e) {
314 return false;
315 }
316 return true;
317
318 }
319
320 /***
321 * @param
322 * @exception java.lang.Exception
323 * The exception description.
324 */
325 //void addSubscriber()
326 // throws java.lang.Exception;
327
328 /***
329 * only for test
330 *
331 */
332
333 public static void main(String[] args) {
334 try {
335 GenConnectionFactoryImpl gcf = null;
336 System.setProperty(
337 "java.naming.factory.initial",
338 JNDIProperties.INITIAL_CONTEXT_FACTORY);
339 System.setProperty(
340 "java.naming.factory.url.pkgs",
341 "org.huihoo.jfox.jndi");
342 System.setProperty(
343 "java.naming.provider.url",
344 JNDIProperties.PROVIDER_URL);
345
346 // Get an InitialContext
347 Context ctx = new InitialContext();
348 //gcf = (GenConnectionFactoryImpl)ctx.lookup("ConnectionFactory");
349 Object ref = ctx.lookup("ConnectionFactory");
350 gcf =
351 (
352 GenConnectionFactoryImpl)javax
353 .rmi
354 .PortableRemoteObject
355 .narrow(
356 ref,
357 GenConnectionFactoryImpl.class);
358 LinkServer link = gcf.getLinkServer();
359 //if(link instanceof
360 // RMILinkServer_Stub)System.out.println("stub");
361 if (link != null)
362 System.out.println("link");
363
364 // boolean result=link.authenticate("test","test");
365 //if(result==true)System.out.print(result);
366 String s = link.getID();
367 if (s != null)
368 System.out.print(s);
369 link.closeConnection();
370 ((SocketLinkServer)link).rleaseSource();
371 } catch (Exception e) {
372 System.out.println(e.toString());
373 }
374
375 }
376
377 }
This page was automatically generated by Maven