KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > soap > SoapProxyService


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

25 package org.objectweb.joram.mom.proxies.soap;
26
27 import java.util.*;
28
29 import org.objectweb.joram.shared.client.*;
30 import org.objectweb.joram.shared.excepts.*;
31 import org.objectweb.joram.mom.proxies.*;
32 import org.objectweb.joram.mom.notifications.*;
33
34 import fr.dyade.aaa.agent.AgentId;
35 import fr.dyade.aaa.agent.AgentServer;
36 import fr.dyade.aaa.agent.Channel;
37 import fr.dyade.aaa.util.Queue;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40 import org.objectweb.joram.shared.JoramTracing;
41
42 /**
43  * The <code>SoapProxyService</code> class implements the SOAP service
44  * accessed by the JORAM clients using a <code>SoapConnection</code> for
45  * connecting to the MOM.
46  * <p>
47  */

48 public class SoapProxyService {
49
50   private Hashtable connections;
51
52   /**
53    * Service method: called by the SOAP client for instanciating the SOAP
54    * service and starting the embedded JORAM server.
55    *
56    * @param serverId Identifier of the embedded server.
57    * @param serverName Name of the embedded server.
58    *
59    * @exception Exception If the embedded server could not start.
60    */

61   public void start(int serverId, String JavaDoc serverName) throws Exception JavaDoc {
62     String JavaDoc[] args = {"" + serverId, serverName};
63     AgentServer.init(args);
64     AgentServer.start();
65
66     connections = new Hashtable();
67
68     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
69       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "SoapProxyService started.");
70   }
71
72   /**
73    * Service method: returns the identifier of a given user connection,
74    * or -1 if it is not a valid user of the SOAP proxy.
75    *
76    * @param userName User's name.
77    * @param userPassword User's password.
78    * @param heartBeat
79    * @return connection identifier
80    * @exception Exception If the proxy is not deployed.
81    */

82   public int setConnection(String JavaDoc userName,
83                            String JavaDoc userPassword,
84                            int heartBeat) throws Exception JavaDoc {
85     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
86       JoramTracing.dbgProxy.log(
87         BasicLevel.DEBUG, "SoapProxyService.setConnection(" +
88         userName + ',' +
89         userPassword + ',' +
90         heartBeat + ')');
91
92     GetProxyIdNot gpin = new GetProxyIdNot(userName, userPassword, null);
93     AgentId proxyId;
94     gpin.invoke(new AgentId(AgentServer.getServerId(),
95                             AgentServer.getServerId(),
96                             AgentId.JoramAdminStamp));
97     proxyId = gpin.getProxyId();
98     
99     OpenConnectionNot ocn = new OpenConnectionNot(false, heartBeat);
100     ocn.invoke(proxyId);
101
102     StandardConnectionContext cc =
103       (StandardConnectionContext) ocn.getConnectionContext();
104     ProxyConnectionContext pcc =
105       new ProxyConnectionContext(proxyId, (Queue)cc.getQueue());
106     connections.put(new ConnectionKey(userName, cc.getKey()), pcc);
107     
108     return cc.getKey();
109   }
110   
111   /**
112    * Service method: passes a hashtable containing an
113    * <code>AbstractJmsRequest</code> client request or MOM messages to the
114    * proxy.
115    *
116    * @param cnxId The sending connection.
117    * @param h Hashtable containing a request or MOM messages.
118    *
119    * @exception Exception If the connection has been closed.
120    */

121   public void send(String JavaDoc name, int cnxId, java.util.Hashtable JavaDoc h) throws Exception JavaDoc {
122     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
123       JoramTracing.dbgProxy.log(
124         BasicLevel.DEBUG, "SoapProxyService.send(" +
125         name + ',' + cnxId + ',' + h + ')');
126
127     AbstractJmsRequest request = (AbstractJmsRequest) AbstractJmsMessage.soapDecode(h);
128     
129     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
130       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "--- " + this
131                               + " passes request " + request + " with id "
132                               + request.getRequestId() + " to proxy's cnx "
133                               + cnxId);
134
135     ProxyConnectionContext ctx =
136       (ProxyConnectionContext) connections.get(new ConnectionKey(name, cnxId));
137     if (ctx == null) {
138       throw new StateException("Connection " + name + ':' + cnxId + " closed.");
139     } else {
140       ConnectionManager.sendToProxy(ctx.proxyId, cnxId, request, request);
141     }
142   }
143
144   /**
145    * Service method: returns a Hashtable containing an
146    * <code>AbstractJmsReply</code> reply or MOM messages destinated to a
147    * given connection context.
148    *
149    * @param cnxId The identifier of the requesting connection.
150    *
151    * @exception Exception If the connection has been closed.
152    */

153   public java.util.Hashtable JavaDoc getReply(String JavaDoc name, int cnxId) throws Exception JavaDoc
154   {
155     ConnectionKey ckey = new ConnectionKey(name, cnxId);
156     ProxyConnectionContext ctx =
157       (ProxyConnectionContext) connections.get(ckey);
158     if (ctx == null) {
159       throw new StateException("Connection " + name + ':' + cnxId + " closed.");
160     } else {
161       Object JavaDoc obj = ctx.replyQueue.get();
162       if (obj instanceof Exception JavaDoc) {
163         connections.remove(ckey);
164         throw (Exception JavaDoc)obj;
165       } else {
166         AbstractJmsReply reply = (AbstractJmsReply) obj;
167         ctx.replyQueue.pop();
168         if (reply instanceof CnxCloseReply) {
169           connections.remove(ckey);
170         }
171         return reply.soapCode();
172       }
173     }
174   }
175
176   static class ConnectionKey {
177     private String JavaDoc userName;
178     private int key;
179
180     public ConnectionKey(String JavaDoc userName, int key) {
181       this.userName = userName;
182       this.key = key;
183     }
184
185     public int hashCode() {
186       return userName.hashCode() + key;
187     }
188
189     public boolean equals(Object JavaDoc obj) {
190       if (obj instanceof ConnectionKey) {
191         ConnectionKey ck = (ConnectionKey)obj;
192         return (ck.userName.equals(userName) &&
193                 ck.key == key);
194       }
195       return false;
196     }
197   }
198
199   static class ProxyConnectionContext {
200     AgentId proxyId;
201     Queue replyQueue;
202
203     ProxyConnectionContext(AgentId proxyId,
204                       Queue replyQueue) {
205       this.proxyId = proxyId;
206       this.replyQueue = replyQueue;
207     }
208   }
209 }
210
Popular Tags