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: * @(#)MQConnection.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.JMSBindingContext;
034: import com.sun.jbi.binding.jms.JMSBindingResources;
035:
036: import com.sun.jbi.binding.jms.util.UtilBase;
037:
038: import java.util.logging.Logger;
039:
040: import javax.jms.Connection;
041: import javax.jms.JMSException;
042:
043: /**
044: * MQ connection.
045: *
046: * @author Sun Microsystems Inc.
047: */
048: public final class MQConnection extends UtilBase implements
049: JMSBindingResources {
050: /**
051: * JMS connection.
052: */
053: private javax.jms.Connection mConnection;
054: /**
055: * Logger.
056: */
057: private Logger mLogger;
058: /**
059: * Factory.
060: */
061: private String mFactory;
062: /**
063: * Style.
064: */
065: private int mStyle;
066: /**
067: * Usage count.
068: */
069: private int mUsageCount = 0;
070: /**
071: * String translator.
072: */
073: private StringTranslator mStringTranslator;
074:
075: /**
076: * Creates a new MQConnection object.
077: *
078: * @param fac factory name.
079: * @param con connection.
080: * @param style style.
081: */
082: public MQConnection(String fac, javax.jms.Connection con, int style) {
083: mFactory = fac;
084: mConnection = con;
085: mStyle = style;
086: mLogger = JMSBindingContext.getInstance().getLogger();
087: mStringTranslator = JMSBindingContext.getInstance()
088: .getStringTranslator();
089: }
090:
091: /**
092: * Gets the connection.
093: *
094: * @return JMS connection.
095: */
096: public javax.jms.Connection getConnection() {
097: return mConnection;
098: }
099:
100: /**
101: * Get factory.
102: *
103: * @return factory.
104: */
105: public String getFactory() {
106: return mFactory;
107: }
108:
109: /**
110: * Gets the style.
111: *
112: * @return style.
113: */
114: public int getStyle() {
115: return mStyle;
116: }
117:
118: /**
119: * Closes the connection.
120: */
121: public synchronized void close() {
122: clear();
123:
124: if (mUsageCount > 0) {
125: mLogger.warning(mStringTranslator.getString(
126: JMS_MQ_CANNOT_CLOSE_FACTORY, mFactory));
127:
128: return;
129: }
130:
131: try {
132: mConnection.close();
133: } catch (JMSException je) {
134: je.printStackTrace();
135: setError(mStringTranslator.getString(
136: JMS_MQ_CANNOT_CLOSE_FACTORY, mFactory));
137: setError(je.getMessage());
138: }
139: }
140:
141: /**
142: * Decrements the connection usage.
143: */
144: public synchronized void decrementUsage() {
145: mUsageCount--;
146: }
147:
148: /**
149: * Increments the usage.
150: */
151: public synchronized void incrementUsage() {
152: mUsageCount++;
153: }
154:
155: /**
156: * Starts the connection.
157: *
158: * @return return code.
159: */
160: public synchronized int start() {
161: clear();
162:
163: try {
164: mConnection.start();
165: } catch (Exception e) {
166: setError(mStringTranslator.getString(
167: JMS_MQ_CANNOT_START_CONNECTION, mConnection));
168: setError(e.getMessage());
169:
170: return MQCodes.FAILURE;
171: }
172:
173: return MQCodes.SUCCESS;
174: }
175:
176: /**
177: * Stops the connection.
178: */
179: public synchronized void stop() {
180: clear();
181:
182: if (mUsageCount > 0) {
183: mLogger.severe(mStringTranslator.getString(
184: JMS_MQ_CANNOT_CLOSE_FACTORY, mFactory));
185:
186: return;
187: }
188:
189: try {
190: mConnection.stop();
191: } catch (JMSException je) {
192: setError(mStringTranslator.getString(
193: JMS_MQ_CANNOT_CLOSE_FACTORY, mFactory));
194: setError(je.getMessage());
195: je.printStackTrace();
196: }
197: }
198: }
|