KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_jms > JmsAdminForJoram


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JmsAdminForJoram.java,v 1.31 2004/08/18 09:08:19 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_jms;
28
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.jms.ConnectionFactory JavaDoc;
33 import javax.jms.Queue JavaDoc;
34 import javax.jms.QueueConnectionFactory JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnectionFactory JavaDoc;
37 import javax.jms.XAConnectionFactory JavaDoc;
38 import javax.jms.XAQueueConnectionFactory JavaDoc;
39 import javax.jms.XATopicConnectionFactory JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42
43 import org.objectweb.common.Env;
44 import org.objectweb.jonas_jms.api.JmsAdministration;
45 import org.objectweb.joram.client.jms.admin.AdminModule;
46 import org.objectweb.joram.client.jms.admin.User;
47 import org.objectweb.joram.client.jms.local.XALocalConnectionFactory;
48 import org.objectweb.joram.client.jms.local.XAQueueLocalConnectionFactory;
49 import org.objectweb.joram.client.jms.local.XATopicLocalConnectionFactory;
50 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory;
51 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
52 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;
53 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory;
54 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory;
55 import org.objectweb.joram.client.jms.tcp.XATopicTcpConnectionFactory;
56 import org.objectweb.util.monolog.api.BasicLevel;
57
58 import fr.dyade.aaa.agent.AgentServer;
59
60 /**
61  * Joram administration. This JORAM specific class allows to administer a JORAM server
62  * when JORAM has been declared as a service.
63  *
64  * @author Philippe Coq
65  * Contributor(s):
66  * Philippe Durieux<p>
67  * Jeff Mesnil: for JORAM 3.x integration<p>
68  * Frederic Maistre for JORAM 4.x integration<p>
69  * 03/05/25 Adriana Danes : JMS resource monitoring
70  *
71  */

72 public class JmsAdminForJoram implements JmsAdministration {
73
74     private static String JavaDoc QUEUE_CONN_FACT_NAME="JQCF";
75     private static String JavaDoc TOPIC_CONN_FACT_NAME="JTCF";
76     private static String JavaDoc CONN_FACT_NAME="JCF";
77     private boolean collocatedmom = false;
78     private InitialContext JavaDoc ictx = null;
79     private Vector JavaDoc namelist = new Vector JavaDoc();
80     private XAConnectionFactory JavaDoc xacf = null;
81     private XATopicConnectionFactory JavaDoc xatcf = null;
82     private XAQueueConnectionFactory JavaDoc xaqcf = null;
83     private ConnectionFactory JavaDoc jcf = null;
84     private TopicConnectionFactory JavaDoc jtcf = null;
85     private QueueConnectionFactory JavaDoc jqcf = null;
86     private org.objectweb.joram.client.jms.admin.User user = null;
87     private org.objectweb.joram.client.jms.admin.User jonasUser = null;
88     private String JavaDoc host = null;
89     private int port;
90
91     /**
92      * default constructor.
93      * This class will be created with newInstance().
94      */

95     public JmsAdminForJoram() {
96     }
97
98     // ------------------------------------------------------------
99
// private methods
100
// ------------------------------------------------------------
101

102     /**
103      * start the MOM in the same JVM than the current
104      *
105      * Start the A3 AgentServer with the default configuration
106      * in transient mode and with the a3servers.xml file located into $JONAS_BASE/conf
107      * what it is made here is equivalent to
108      * java -Djonas.base=$JONAS_BASE -DTransaction=fr.dyade.aaa.util.NullTransaction
109      * -Dfr.dyade.aaa.agent.A3CONF_DIR=$JONAS_ROOT/config
110      * fr.dyade.aaa.agent.AgentServer 0 ./s0&
111      */

112     private void startMOM() throws Exception JavaDoc {
113         TraceJms.logger.log(BasicLevel.DEBUG,"");
114
115         try {
116             // -DTransaction=fr.dyade.aaa.util.NullTransaction
117
String JavaDoc TRANSACTION_PROP = "Transaction";
118             String JavaDoc s1 = System.getProperty(TRANSACTION_PROP);
119             if (s1 == null) {
120                 System.setProperty(TRANSACTION_PROP, "fr.dyade.aaa.util.NullTransaction");
121             }
122
123             // -Dfr.dyade.aaa.agent.A3CONF_DIR=$JONAS_BASE/conf
124
String JavaDoc A3CONF_DIR_PROP = "fr.dyade.aaa.agent.A3CONF_DIR";
125             String JavaDoc a3config_dir = System.getProperty(A3CONF_DIR_PROP);
126             if (a3config_dir == null) {
127                 System.setProperty(A3CONF_DIR_PROP, System.getProperty("jonas.base")
128                                    + System.getProperty("file.separator") + "conf");
129             }
130
131             if(TraceJms.isDebug())
132                 TraceJms.logger.log(BasicLevel.DEBUG,"a3config_dir= "+a3config_dir);
133             // java fr.dyade.aaa.agent.AgentServer 0 s0
134
// 0 identifies wich server to run among those described in config file.
135
// s0 is a directory used for persistence.
136
short serverId = 0;
137             AgentServer.init(serverId,"s0", TraceJms.getLoggerFactory());
138             AgentServer.start();
139
140             // wait 10 millisec for the MOM to start ???
141
Thread.sleep(10);
142         
143             // Get host and port that must be used to start Admin
144
// mainly to get the port number set in a3servers.xml
145
short sid = AgentServer.getServerId(); // should be 0
146
host = AgentServer.getHostname(sid); // should be localhost
147
port = 16010;
148             try {
149                 String JavaDoc s = AgentServer.getServiceArgs(sid, "org.objectweb.joram.mom.proxies.tcp.TcpProxyService");
150                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
151                 port = Integer.parseInt(st.nextToken());
152             } catch (NumberFormatException JavaDoc exc) {
153                 TraceJms.logger.log(BasicLevel.ERROR,"exception: "+exc);
154             } catch (Exception JavaDoc e) {
155                 TraceJms.logger.log(BasicLevel.ERROR,"MOM exception:"+e);
156                 throw e;
157             }
158             TraceJms.logger.log(BasicLevel.INFO, "starting MOM on host " + host + ", port " + port);
159
160         } catch (Exception JavaDoc ex) {
161             TraceJms.logger.log(BasicLevel.ERROR,"exception: "+ex);
162             throw ex;
163         }
164     }
165     
166     // ------------------------------------------------------------
167
// JmsAdministration implementation
168
// ------------------------------------------------------------
169

170     /**
171      * Jms Administrator is created with newInstance().
172      * initialization is done later with this method.
173      * The MOM will be started if collocated.
174      * This method should create an XATopicConnectionFactory and
175      * an XAQueueConnectionFactory
176      * @param boolean true for if the MOM in run in the current JVM
177      * @param String url connexion that must be used if not collocated
178      */

179     public void start(boolean collocated, String JavaDoc url) throws Exception JavaDoc {
180
181         // Start the MOM in this JVM if collocated.
182
if (collocated) {
183             collocatedmom = true;
184             startMOM();
185         }
186
187         // Create an InitialContext
188
ictx = new InitialContext JavaDoc();
189
190         // Start jms admin
191
if (host != null) {
192             // collocated: use url got from the MOM
193
TraceJms.logger.log(BasicLevel.INFO, "starting JmsAdmin with host " + host + ", port " + port);
194             AdminModule.collocatedConnect("root", "root");
195         } else if (url != null && url.length() > 0) {
196             // not collocated: use url from jonas.properties
197
int indexOfHost = url.indexOf("//") + 2;
198             int indexOfPort = url.indexOf(":", indexOfHost) + 1;
199             host = url.substring(indexOfHost, indexOfPort - 1);
200             port = Integer.parseInt(url.substring(indexOfPort));
201             TraceJms.logger.log(BasicLevel.WARN, "starting JmsAdmin with host " + host + ", port " + port);
202             AdminModule.connect(host, port, "root", "root", 100);
203         } else {
204             // use default url
205
TraceJms.logger.log(BasicLevel.WARN, "starting JmsAdmin with default url");
206             AdminModule.connect("root", "root", 100);
207         }
208
209         user = User.create("anonymous", "anonymous");
210         jonasUser = User.create("jonas", "jonas");
211
212         // Create connection factories that will be used by the EJB components.
213
if (collocated) {
214             xacf = XALocalConnectionFactory.create();
215             xatcf = XATopicLocalConnectionFactory.create();
216             xaqcf = XAQueueLocalConnectionFactory.create();
217         }
218         else {
219             xacf = XATcpConnectionFactory.create(host, port);
220             xatcf = XATopicTcpConnectionFactory.create(host, port);
221             xaqcf = XAQueueTcpConnectionFactory.create(host, port);
222         }
223         
224
225         // Create connection factories that will be used by a pure JMS Client
226
String JavaDoc name = CONN_FACT_NAME;
227         try {
228             jcf = TcpConnectionFactory.create(host, port);
229             ictx.rebind(name, jcf);
230             namelist.addElement(name);
231         } catch (NamingException JavaDoc e) {
232             TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name);
233         }
234
235         name = QUEUE_CONN_FACT_NAME;
236         try {
237             jqcf = QueueTcpConnectionFactory.create(host, port);
238             ictx.rebind(name, jqcf);
239             namelist.addElement(name);
240         } catch (NamingException JavaDoc e) {
241             TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name);
242         }
243
244         name = TOPIC_CONN_FACT_NAME;
245         try {
246             jtcf = TopicTcpConnectionFactory.create(host, port);
247             ictx.rebind(name, jtcf);
248             namelist.addElement(name);
249         } catch (NamingException JavaDoc e) {
250             TraceJms.logger.log(BasicLevel.ERROR,"cannot register "+name);
251         }
252     }
253
254     /**
255      * Stop the Jms Administrator
256      */

257     public void stop() {
258         if ( Env.isJAVA4() ){
259             // workaround against bug in SUN JVM JDK1.3 on Linux
260
try {
261                 AdminModule.disconnect();
262             } catch (Exception JavaDoc ex) {
263                 TraceJms.logger.log(BasicLevel.ERROR, "problem for closing admin object " + ex);
264             }
265             if (collocatedmom) {
266                 AgentServer.stop();
267                 if(TraceJms.isDebug())
268                     TraceJms.logger.log(BasicLevel.DEBUG, "agentServer stopped");
269             }
270         }
271         // clean up JNDI
272
try {
273             ictx.unbind(CONN_FACT_NAME);
274             ictx.unbind(QUEUE_CONN_FACT_NAME);
275             ictx.unbind(TOPIC_CONN_FACT_NAME);
276             if(TraceJms.isDebug())
277                 TraceJms.logger.log(BasicLevel.DEBUG,"unbind connection factories");
278         } catch(Exception JavaDoc ex) {
279             TraceJms.logger.log(BasicLevel.ERROR,"cannot unbind connection factories");
280         }
281     }
282
283
284     /**
285      * Get the XAConnectionFactory
286      */

287     public XAConnectionFactory JavaDoc getXAConnectionFactory() {
288         return xacf;
289     }
290
291     /**
292      * Get the XATopicConnectionFactory
293      */

294     public XATopicConnectionFactory JavaDoc getXATopicConnectionFactory() {
295         return xatcf;
296     }
297
298     /**
299      * Get the XAQueueConnectionFactory
300      */

301     public XAQueueConnectionFactory JavaDoc getXAQueueConnectionFactory() {
302         return xaqcf;
303     }
304
305
306     /**
307      * Create a Queue and bind it to the registry
308      */

309     public Queue JavaDoc createQueue(String JavaDoc name) throws Exception JavaDoc {
310         TraceJms.logger.log(BasicLevel.DEBUG,"");
311         org.objectweb.joram.client.jms.Queue queue = null;
312         try {
313             queue = (org.objectweb.joram.client.jms.Queue) ictx.lookup(name);
314         } catch (Exception JavaDoc e) {
315             queue = org.objectweb.joram.client.jms.Queue.create(name);
316             ictx.rebind(name, queue);
317         }
318         queue.setWriter(user);
319         queue.setReader(user);
320         queue.setWriter(jonasUser);
321         queue.setReader(jonasUser);
322         return queue;
323     }
324
325     /**
326      * Create a Topic and bind it to the registry
327      */

328     public Topic JavaDoc createTopic(String JavaDoc name) throws Exception JavaDoc {
329         TraceJms.logger.log(BasicLevel.DEBUG,"");
330         org.objectweb.joram.client.jms.Topic topic = null;
331         try {
332             topic = (org.objectweb.joram.client.jms.Topic) ictx.lookup(name);
333         } catch (Exception JavaDoc e) {
334             topic = org.objectweb.joram.client.jms.Topic.create(name);
335             ictx.rebind(name, topic);
336         }
337         topic.setWriter(user);
338         topic.setReader(user);
339         topic.setWriter(jonasUser);
340         topic.setReader(jonasUser);
341         return topic;
342     }
343
344     /**
345      * Delete a destination.
346      */

347     public void deleteDestination(String JavaDoc name) throws Exception JavaDoc {
348         TraceJms.logger.log(BasicLevel.DEBUG,"");
349         try {
350             org.objectweb.joram.client.jms.Destination dest = (org.objectweb.joram.client.jms.Destination) ictx.lookup(name);
351             dest.delete();
352         } catch (Exception JavaDoc e) {
353         }
354     }
355   
356     /**
357      * Get number of pending messages on a queue
358      * @param queue the monitored queue
359      * @exception Exception if could not get info (queue not valid or connection failed or monitoring request failed)
360      */

361     public int getPendingMessages(javax.jms.Queue JavaDoc queue) throws Exception JavaDoc {
362         return ((org.objectweb.joram.client.jms.Queue) queue).getPendingMessages();
363     }
364
365     /**
366      * Get number of pending requests on a queue
367      * @param queue the monitored queue
368      * @exception Exception if could not get info (queue not valid or connection failed or monitoring request failed)
369      */

370     public int getPendingRequests(javax.jms.Queue JavaDoc queue) throws Exception JavaDoc {
371         return ((org.objectweb.joram.client.jms.Queue) queue).getPendingRequests();
372     }
373
374     /**
375      * Get number of subscriptions on a topic
376      * @param topic the monitored topic
377      * @exception Exception if could not get info (topic not valid or connection failed or monitoring request failed)
378      */

379     public int getSubscriptions(javax.jms.Topic JavaDoc topic) throws Exception JavaDoc {
380         return ((org.objectweb.joram.client.jms.Topic) topic).getSubscriptions();
381     }
382 }
383
Popular Tags