KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > local > LocalConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 2006 ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): ScalAgent Distributed Technologies
21  * Contributor(s):
22  */

23 package org.objectweb.joram.client.jms.local;
24
25 import java.util.Timer JavaDoc;
26
27 import javax.jms.JMSException JavaDoc;
28
29 import org.objectweb.joram.client.jms.connection.RequestChannel;
30 import org.objectweb.joram.mom.proxies.StandardConnectionContext;
31 import org.objectweb.joram.shared.client.AbstractJmsReply;
32 import org.objectweb.joram.shared.client.AbstractJmsRequest;
33 import org.objectweb.joram.shared.client.ProducerMessages;
34 import org.objectweb.joram.mom.notifications.GetProxyIdNot;
35 import org.objectweb.joram.mom.proxies.ConnectionManager;
36 import org.objectweb.joram.mom.proxies.FlowControl;
37 import org.objectweb.joram.mom.proxies.MultiCnxSync;
38 import org.objectweb.joram.mom.proxies.OpenConnectionNot;
39 import org.objectweb.joram.mom.proxies.RequestNot;
40
41 import fr.dyade.aaa.agent.AgentId;
42 import fr.dyade.aaa.agent.AgentServer;
43 import fr.dyade.aaa.agent.Channel;
44
45 import org.objectweb.joram.shared.JoramTracing;
46 import org.objectweb.util.monolog.api.BasicLevel;
47
48 public class LocalConnection implements RequestChannel {
49   
50   private String JavaDoc userName;
51   
52   private String JavaDoc password;
53
54   private AgentId proxyId;
55
56   private StandardConnectionContext ctx;
57
58   public LocalConnection(String JavaDoc userName,
59                          String JavaDoc password) throws JMSException JavaDoc {
60     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
61       JoramTracing.dbgProxy.log(BasicLevel.DEBUG,
62                                 "LocalConnection.<init>(" + userName +
63                                 ',' + password + ')');
64
65     this.userName = userName;
66     this.password = password;
67   }
68   
69   public void setTimer(Timer JavaDoc timer) {
70     // Use of timer is useless
71
}
72   
73   public void connect() throws Exception JavaDoc {
74     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
75       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "LocalConnection.connect()");
76
77     if (AgentServer.getStatus() != AgentServer.Status.STARTED) {
78       if ((AgentServer.getStatus() != AgentServer.Status.INITIALIZED) &&
79           (AgentServer.getStatus() != AgentServer.Status.STOPPED)) {
80         if (JoramTracing.dbgProxy.isLoggable(BasicLevel.ERROR))
81           JoramTracing.dbgProxy.log(BasicLevel.ERROR,
82                                     "LocalConnection.connect(), server is not initialized: " + AgentServer.getStatusInfo() + '.');
83
84         throw new Exception JavaDoc();
85       }
86       
87       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.WARN))
88         JoramTracing.dbgProxy.log(BasicLevel.WARN,
89                                   "LocalConnection.connect(), server is not started: " + AgentServer.getStatusInfo() + '.');
90     }
91
92     GetProxyIdNot gpin = new GetProxyIdNot(userName, password, null);
93     try {
94       gpin.invoke(new AgentId(AgentServer.getServerId(),
95                               AgentServer.getServerId(),
96                               AgentId.JoramAdminStamp));
97       proxyId = gpin.getProxyId();
98     } catch (Exception JavaDoc exc) {
99       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
100         JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "", exc);
101       throw new JMSException JavaDoc(exc.getMessage());
102     }
103
104     OpenConnectionNot ocn = new OpenConnectionNot(false, 0);
105     try {
106       ocn.invoke(proxyId);
107       ctx = (StandardConnectionContext)ocn.getConnectionContext();
108     } catch (Exception JavaDoc exc) {
109       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
110         JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "", exc);
111       JMSException JavaDoc jmse = new JMSException JavaDoc(exc.getMessage());
112       jmse.setLinkedException(exc);
113       throw jmse;
114     }
115   }
116
117   public void send(AbstractJmsRequest request)
118     throws Exception JavaDoc {
119     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
120       JoramTracing.dbgClient.log( BasicLevel.DEBUG,
121                                   "LocalConnection.send(" + request + ')');
122
123     ConnectionManager.sendToProxy(proxyId, ctx.getKey(), request, request);
124   }
125
126   public AbstractJmsReply receive() throws Exception JavaDoc {
127     AbstractJmsReply reply = (AbstractJmsReply)ctx.getQueue().get();
128     ctx.getQueue().pop();
129
130     return reply;
131   }
132
133   public void close() {
134     // Nothing to do
135
}
136 }
137
Popular Tags