KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > ConnectionManager


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2004 - ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - France-Telecom R&D
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  * Initial developer(s): ScalAgent Distributed Technologies
22  */

23 package org.objectweb.joram.mom.proxies;
24
25 import fr.dyade.aaa.agent.*;
26
27 import org.objectweb.joram.shared.JoramTracing;
28 import org.objectweb.joram.mom.dest.*;
29 import org.objectweb.joram.mom.proxies.AdminNotification;
30 import org.objectweb.joram.shared.client.AbstractJmsRequest;
31 import org.objectweb.joram.shared.client.JmsRequestGroup;
32 import org.objectweb.joram.shared.client.ProducerMessages;
33
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.util.*;
37
38 /**
39  * A <code>ConnectionManager</code> is started as a service in each
40  * MOM agent server for allowing connections with external clients.
41  */

42 public class ConnectionManager {
43   
44   public static final String JavaDoc MULTI_CNX_SYNC =
45     "org.objectweb.joram.mom.proxies.ConnectionManager.multiCnxSync";
46   
47   public static final String JavaDoc MULTI_CNX_SYNC_DELAY =
48     "org.objectweb.joram.mom.proxies.ConnectionManager.multiCnxSyncDelay";
49   
50   private static boolean multiCnxSync = Boolean.getBoolean(MULTI_CNX_SYNC);
51   
52   private static long multiThreadSyncDelay =
53     Long.getLong(MULTI_CNX_SYNC_DELAY, 1).longValue();
54   
55   public static final void sendToProxy(AgentId proxyId, int cnxKey,
56       AbstractJmsRequest req, Object JavaDoc msg) {
57     RequestNot rn = new RequestNot(cnxKey, msg);
58     if (multiCnxSync
59         && (req instanceof ProducerMessages ||
60             req instanceof JmsRequestGroup)) {
61       MultiCnxSync mcs = ConnectionManager.getMultiCnxSync(proxyId);
62       mcs.send(rn);
63     } else {
64       Channel.sendTo(proxyId, rn);
65     }
66     if (req instanceof ProducerMessages) {
67       FlowControl.flowControl();
68     }
69   }
70   
71   public static final long getMultiThreadSyncDelay() {
72     return multiThreadSyncDelay;
73   }
74   
75   private static Hashtable multiCnxSyncTable = new Hashtable();
76   
77   public static MultiCnxSync getMultiCnxSync(AgentId proxyId) {
78     synchronized (multiCnxSyncTable) {
79       MultiCnxSync mcs = (MultiCnxSync) multiCnxSyncTable.get(proxyId);
80       if (mcs == null) {
81         mcs = new MultiCnxSync(proxyId);
82         multiCnxSyncTable.put(proxyId, mcs);
83       }
84       return mcs;
85     }
86   }
87   
88   /**
89    * Limit of incoming messages flow (msgs/s) requested if any, default
90    * value is -1 (no limitation).
91    * This value can be adjusted by setting <tt>ConnectionManager.inFlow</tt>
92    * property. This property can be fixed either from <code>java</code>
93    * launching command, or in <code>a3servers.xml</code> configuration file.
94    */

95   public static int inFlow = -1;
96
97   /**
98    * Timer provided by the connection manager.
99    */

100   private static fr.dyade.aaa.util.Timer timer;
101
102   /**
103    * Returns the timer provided by the connection manager.
104    */

105   public final static fr.dyade.aaa.util.Timer getTimer() {
106     if (timer == null) {
107       timer = new fr.dyade.aaa.util.Timer();
108     }
109     return timer;
110   }
111
112   /**
113    * Initializes the connection manager as a service.
114    * Creates and deploys the aministration topic, the connection manager
115    * agent and if requested the administration user proxy.
116    *
117    * @param args name and password of the administrator (optional).
118    * @param firstTime <code>true</code> when the agent server starts.
119    * @exception Exception Thrown when processing the String argument
120    * or in case of a problem when deploying the ConnectionFactory.
121    */

122   public static void init(String JavaDoc args, boolean firstTime)
123     throws Exception JavaDoc {
124     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
125       JoramTracing.dbgProxy.log(
126         BasicLevel.DEBUG,
127         "ConnectionManager.init(" + args + ',' + firstTime + ')');
128
129     if (! firstTime) return;
130
131     AdminTopic adminTopic = new AdminTopic();
132     adminTopic.deploy();
133
134     inFlow = Integer.getInteger("ConnectionManager.inFlow", inFlow).intValue();
135
136     if (args != null) {
137       String JavaDoc initialAdminName = null;
138       String JavaDoc initialAdminPass = null;
139       StringTokenizer st = new StringTokenizer(args);
140
141       if (st.countTokens() >= 2) {
142         initialAdminName = st.nextToken();
143         initialAdminPass = st.nextToken();
144       }
145       
146       // AF: deprecated, will be deleted.
147
if (st.hasMoreTokens()) {
148         try {
149           inFlow = Integer.parseInt(st.nextToken());
150         } catch (Exception JavaDoc exc) {
151           inFlow = -1;
152         }
153       }
154
155       if (initialAdminName != null && initialAdminPass != null) {
156         UserAgent userAgent = new UserAgent(AgentId.JoramAdminPxStamp);
157         userAgent.deploy();
158
159         AdminNotification adminNot =
160           new AdminNotification(
161             userAgent.getId(),
162             initialAdminName,
163             initialAdminPass);
164
165         Channel.sendTo(adminTopic.getId(), adminNot);
166       }
167     }
168   }
169
170   /**
171    * Stops the <code>ConnectionManager</code> service.
172    */

173   public static void stopService() {
174     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
175       JoramTracing.dbgProxy.log(
176         BasicLevel.DEBUG,
177         "ConnectionManager.stop()");
178     if (timer != null)
179       timer.cancel();
180   }
181 }
182
Popular Tags