KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > ha > local > HALocalConnection


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - 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  */

22 package org.objectweb.joram.client.jms.ha.local;
23
24 import java.util.*;
25
26 import javax.jms.*;
27
28 import org.objectweb.joram.client.jms.*;
29 import org.objectweb.joram.client.jms.Connection;
30 import org.objectweb.joram.client.jms.local.*;
31 import org.objectweb.joram.shared.client.*;
32 import org.objectweb.joram.mom.proxies.*;
33 import org.objectweb.joram.mom.notifications.*;
34 import org.objectweb.joram.client.jms.connection.RequestChannel;
35
36 import fr.dyade.aaa.agent.*;
37
38 import org.objectweb.joram.shared.JoramTracing;
39 import org.objectweb.util.monolog.api.BasicLevel;
40
41 public class HALocalConnection
42     implements RequestChannel {
43   
44   public final static int NONE = 0;
45   public final static int INIT = 1;
46   public final static int RUN = 2;
47
48   private static Object JavaDoc lock = new Object JavaDoc();
49
50   private static int status;
51   
52   public static void init(String JavaDoc args, boolean firstTime)
53     throws Exception JavaDoc {
54     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
55       JoramTracing.dbgProxy.log(
56         BasicLevel.DEBUG,
57         "HALocalConnection.init(" +
58         args + ',' + firstTime + ')');
59
60     synchronized (lock) {
61       status = INIT;
62       lock.notifyAll();
63     }
64   }
65
66   public static void waitForStart() {
67     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
68       JoramTracing.dbgProxy.log(
69         BasicLevel.DEBUG,
70         "HALocalConnection.waitForStart()");
71     synchronized (lock) {
72       while (status == NONE) {
73     try {
74       lock.wait();
75     } catch (InterruptedException JavaDoc exc) {}
76       }
77      
78       if (status == INIT) {
79         // Clean the proxies
80
GetProxyIdListNot gpin = new GetProxyIdListNot();
81         AgentId[] proxyIds;
82         try {
83           gpin.invoke(new AgentId(AgentServer.getServerId(),
84                                   AgentServer.getServerId(),
85                                   AgentId.JoramAdminStamp));
86           proxyIds = gpin.getIds();
87           ResetCollocatedConnectionsNot rccn =
88           new ResetCollocatedConnectionsNot();
89           for (int i = 0; i < proxyIds.length; i++) {
90             Channel.sendTo(proxyIds[i], rccn);
91           }
92           status = RUN;
93         } catch (Exception JavaDoc exc) {
94           JoramTracing.dbgClient.log(
95             BasicLevel.ERROR, "", exc);
96           throw new Error JavaDoc(exc.toString());
97         }
98       }
99     }
100   }
101
102   private String JavaDoc userName;
103   
104   private String JavaDoc password;
105   
106   private LocalConnection localConnection;
107
108   public HALocalConnection(
109     String JavaDoc userName2, String JavaDoc password2) throws JMSException {
110     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
111       JoramTracing.dbgProxy.log(
112         BasicLevel.DEBUG,
113         "HALocalConnection.<init>(" +
114         userName + ',' + password + ')');
115     waitForStart();
116     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
117       JoramTracing.dbgProxy.log(
118         BasicLevel.DEBUG,
119         " -> create the local connection");
120     userName = userName2;
121     password = password2;
122   }
123   
124   public void setTimer(Timer timer) {
125     // No timer is useful
126
}
127   
128   public void connect() throws Exception JavaDoc {
129     localConnection = new LocalConnection(userName, password);
130     localConnection.connect();
131   }
132   
133   public void send(AbstractJmsRequest request)
134     throws Exception JavaDoc {
135     localConnection.send(request);
136   }
137
138   public AbstractJmsReply receive()
139     throws Exception JavaDoc {
140     return localConnection.receive();
141   }
142   
143   public void close() {
144     localConnection.close();
145   }
146 }
147
Popular Tags