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