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: * @(#)MessageRegistry.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.sequencing;
030:
031: import com.sun.jbi.engine.sequencing.framework.Servicelist;
032: import com.sun.jbi.engine.sequencing.util.StringTranslator;
033:
034: import java.util.HashMap;
035: import java.util.Iterator;
036: import java.util.Set;
037: import java.util.Vector;
038: import java.util.logging.Logger;
039:
040: /**
041: * This is a registry which maintains the exchnage Id and the servicelist
042: * runtime object so that responses can be correlated.
043: *
044: * @author Sun Microsystems, Inc.
045: */
046: public final class MessageRegistry implements SequencingEngineResources {
047: /**
048: * Singleton reference.
049: */
050: private static MessageRegistry sMe;
051:
052: /**
053: * Timeout.
054: */
055: private static final int TIMEDOUT_MESSAGE_COUNT = 10000;
056:
057: /**
058: * List of all registered exchanges.
059: */
060: private HashMap mExchanges;
061:
062: /**
063: * Logger object
064: */
065: private Logger mLog;
066:
067: /**
068: * Translator object for internationalization.
069: */
070: private StringTranslator mTranslator;
071:
072: /**
073: * List of exchanges that timedout.
074: */
075: private Vector mTimeOutExchanges;
076:
077: /**
078: * Private constructor.
079: */
080: private MessageRegistry() {
081: mExchanges = new HashMap();
082: mTimeOutExchanges = new Vector();
083: mLog = SequencingEngineContext.getInstance().getLogger();
084: mTranslator = new StringTranslator();
085: }
086:
087: public void dump() {
088: mLog.info("**** PRINTING MSG REGISTRY ****** ");
089: mLog.info(mExchanges.keySet().toString());
090: mLog.info("***** PRINTING TIMED OUT REGISTRY **** ");
091: mLog.info(mTimeOutExchanges.toString());
092: }
093:
094: /**
095: * Used to grab a reference of this object.
096: *
097: * @return an initialized MessageRegistry reference
098: */
099: public static synchronized MessageRegistry getInstance() {
100: if (sMe == null) {
101: sMe = new MessageRegistry();
102: }
103:
104: return sMe;
105: }
106:
107: /**
108: * Chesks if there any active message exchanges.
109: *
110: * @return true if no exchanges are active.
111: */
112: public boolean isEmpty() {
113: return mExchanges.isEmpty();
114: }
115:
116: /**
117: * Retrieves the servicelist runtime object corresponding to each exchange
118: * ID.
119: *
120: * @param exchangeId message xchnage
121: *
122: * @return servicelist bean object
123: */
124: public Servicelist getServicelist(String exchangeId) {
125: Servicelist l;
126: l = (Servicelist) mExchanges.get(exchangeId);
127:
128: return l;
129: }
130:
131: /**
132: * Checks if timed out.
133: *
134: * @param exc exchangeid.
135: *
136: * @return true if timed out.
137: */
138: public synchronized boolean isTimedOut(String exc) {
139: if (mTimeOutExchanges.contains(exc)) {
140: return true;
141: }
142:
143: return false;
144: }
145:
146: /**
147: * Removes all the entries from the registry.
148: */
149: public void clearRegistry() {
150: if (mExchanges == null) {
151: return;
152: }
153:
154: mExchanges = new HashMap();
155: }
156:
157: /**
158: * Removes an exchange from the registry.
159: *
160: * @param exchangeId exchange id
161: */
162: public synchronized void deregisterExchange(String exchangeId) {
163: mLog.fine(mTranslator.getString(SEQ_DEREGISTER_EXCHANGE,
164: exchangeId));
165:
166: Servicelist l = (Servicelist) mExchanges.remove(exchangeId);
167:
168: if (l == null) {
169: mLog.warning(mTranslator.getString(
170: SEQ_DEREGISTER_EXCHANGE_FAILED, exchangeId));
171: }
172: }
173:
174: /**
175: * Removes an exchange from the registry.
176: *
177: * @param exchangeId exchange id
178: */
179: public synchronized void deregisterTimedOutExchange(
180: String exchangeId) {
181: mLog.fine(mTranslator.getString(SEQ_DEREGISTER_EXCHANGE,
182: exchangeId));
183:
184: if (!mTimeOutExchanges.remove(exchangeId)) {
185: mLog.warning(mTranslator.getString(
186: SEQ_DEREGISTER_EXCHANGE_FAILED, exchangeId));
187: }
188: }
189:
190: /**
191: * List all the active exchanges.
192: *
193: * @return iterator
194: */
195: public Iterator listActiveExchanges() {
196: if (mExchanges == null) {
197: return null;
198: }
199:
200: Set tmp = mExchanges.keySet();
201:
202: if (tmp == null) {
203: return null;
204: }
205:
206: return tmp.iterator();
207: }
208:
209: /**
210: * Registers a message Exchange.
211: *
212: * @param exchnageId exchnage id
213: * @param list servicelist
214: */
215: public synchronized void registerExchange(String exchnageId,
216: Servicelist list) {
217: mLog.info(mTranslator.getString(SEQ_REGISTER_EXCHANGE,
218: exchnageId));
219: mExchanges.put(exchnageId, list);
220: }
221:
222: /**
223: * Registers a timedout message Exchange.
224: *
225: * @param exchnageId exchnage id
226: */
227: public synchronized void registerTimedOutExchange(String exchnageId) {
228: mLog.info(mTranslator.getString(SEQ_REGISTER_EXCHANGE,
229: exchnageId));
230:
231: try {
232: if (mTimeOutExchanges.size() >= TIMEDOUT_MESSAGE_COUNT) {
233: mTimeOutExchanges.removeElementAt(0);
234: }
235:
236: mTimeOutExchanges.add(exchnageId);
237: } catch (Exception e) {
238: e.printStackTrace();
239: }
240: }
241: }
|