001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)MQSessionPool.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.mq;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.JMSBindingContext;
035: import com.sun.jbi.binding.jms.JMSBindingResources;
036:
037: import java.util.Collection;
038: import java.util.Enumeration;
039: import java.util.Hashtable;
040: import java.util.Iterator;
041:
042: import java.util.logging.Logger;
043:
044: /**
045: * Pool of MQ sessions.
046: *
047: * @author Sun Microsystems Inc.
048: */
049: public final class MQSessionPool implements JMSBindingResources {
050: /**
051: * Table of active sessions.
052: */
053: private Hashtable mLocked;
054: /**
055: * Table of passive sessions.
056: */
057: private Hashtable mUnlocked;
058: /**
059: * Logger object.
060: */
061: private Logger mLogger;
062: /**
063: * i18n.
064: */
065: private StringTranslator mTranslator;
066: /**
067: * Maximum sessions per connection.
068: */
069: private int mMaxSessionsPerConnection = 5;
070: /**
071: * Minimum sessions per connection.
072: */
073: private int mMinSessionsPerConnection = 2;
074:
075: /**
076: * Creates a new MQSessionPool object.
077: */
078: public MQSessionPool() {
079: mLocked = new Hashtable();
080: mUnlocked = new Hashtable();
081: mLogger = JMSBindingContext.getInstance().getLogger();
082: mTranslator = JMSBindingContext.getInstance()
083: .getStringTranslator();
084: }
085:
086: /**
087: * Gets the active session.
088: *
089: * @param con Wrapper to MQ connection.
090: *
091: * @return number of active sessions for this connection.
092: */
093: public int getActiveSessions(MQConnection con) {
094: int count = 0;
095:
096: try {
097: Collection col = mLocked.values();
098: Iterator iter = col.iterator();
099:
100: while (iter.hasNext()) {
101: String fac = (String) iter.next();
102:
103: if (fac.trim().equals(con.getFactory())) {
104: count++;
105: }
106: }
107: } catch (Exception e) {
108: count = -1;
109: }
110:
111: return count;
112: }
113:
114: /**
115: * Gets the number of inactive sessions.
116: *
117: * @param con MQ connectin.
118: *
119: * @return number of inactive sessions.
120: */
121: public int getInActiveSessions(MQConnection con) {
122: int count = 0;
123:
124: try {
125: Collection col = mUnlocked.values();
126: Iterator iter = col.iterator();
127:
128: while (iter.hasNext()) {
129: String fac = (String) iter.next();
130:
131: if (fac.trim().equals(con.getFactory())) {
132: count++;
133: }
134: }
135: } catch (Exception e) {
136: count = -1;
137: }
138:
139: return count;
140: }
141:
142: /**
143: * Sets the max sessions per connection.
144: *
145: * @param max max sessions.
146: */
147: public void setMaxSessionsPerConnection(int max) {
148: mMaxSessionsPerConnection = max;
149: }
150:
151: /**
152: * Sets the minmum sessions per connection.
153: *
154: * @param min min sessions.
155: */
156: public void setMinSessionsPerConnection(int min) {
157: mMinSessionsPerConnection = min;
158: }
159:
160: /**
161: * Gets a session for an endpoint.
162: *
163: * @param eb bean.
164: *
165: * @return Wrapper to JMS session.
166: */
167: public synchronized MQSession getSession(EndpointBean eb) {
168: MQSession session;
169:
170: if (mUnlocked.size() > 0) {
171: Enumeration e = mUnlocked.keys();
172:
173: while (e.hasMoreElements()) {
174: session = (MQSession) e.nextElement();
175:
176: if (validate(session)) {
177: mUnlocked.remove(session);
178: mLocked.put(session, eb.getConnection()
179: .getFactory());
180:
181: return session;
182: } else {
183: mUnlocked.remove(session);
184: session = null;
185: }
186: }
187: }
188:
189: // no new objects available make some
190: if (exhausted(eb.getConnection())) {
191: mLogger.severe("**CONNECTIONS EXHAUSTED**");
192:
193: return null;
194: }
195:
196: session = create(eb);
197:
198: if (session == null) {
199: mLogger.severe(mTranslator.getString(
200: JMS_MQ_CANNOT_GET_SESSION, eb.getConnection()
201: .getFactory()));
202: }
203:
204: return session;
205: }
206:
207: /**
208: * Closes all sessions and cleans up.
209: *
210: * @param con connection for which sessions hahve tobe cleaned.
211: */
212: public void cleanupSessions(MQConnection con) {
213: try {
214: Enumeration col = mUnlocked.keys();
215:
216: while (col.hasMoreElements()) {
217: MQSession fac = (MQSession) col.nextElement();
218:
219: if (fac.getConnection().getFactory().trim().equals(
220: con.getFactory())) {
221: fac.close();
222: mUnlocked.remove(fac);
223: }
224: }
225: } catch (Exception e) {
226: mLogger.severe(e.getMessage());
227: }
228: }
229:
230: /**
231: * Releases session.
232: *
233: * @param session wrapper to JMS session.
234: */
235: public synchronized void releaseSession(MQSession session) {
236: if (session != null) {
237: mLocked.remove(session);
238: mUnlocked
239: .put(session, session.getConnection().getFactory());
240: }
241: }
242:
243: /**
244: * Creates a new JMS session.
245: *
246: * @param eb bean.
247: *
248: * @return wrapper to JMS session.
249: */
250: private MQSession create(EndpointBean eb) {
251: MQSession session = new MQSession();
252:
253: return session;
254: }
255:
256: /**
257: * Checks if sessions for connction are exhausted.
258: *
259: * @param con connection.
260: *
261: * @return true if exhausted.
262: */
263: private boolean exhausted(MQConnection con) {
264: int count = getActiveSessions(con);
265:
266: if (count >= mMaxSessionsPerConnection) {
267: return true;
268: } else {
269: return false;
270: }
271: }
272:
273: /**
274: * Validates a session.
275: *
276: * @param session mq session.
277: *
278: * @return true if valid.
279: */
280: private boolean validate(MQSession session) {
281: return true;
282: }
283: }
|