001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * DeploymentRegistry.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL.
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license terms.
009: *
010: */
011: package com.sun.jbi.engine.sequencing;
012:
013: import com.sun.jbi.engine.sequencing.servicelist.ServicelistBean;
014: import com.sun.jbi.engine.sequencing.util.StringTranslator;
015:
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Set;
022: import java.util.logging.Logger;
023:
024: /**
025: * This is a registry which maintains the list of deployed services and the
026: * bean object.
027: *
028: * @author Sun Microsystems, Inc.
029: */
030: public final class DeploymentRegistry implements
031: SequencingEngineResources {
032: /**
033: * Singleton reference.
034: */
035: private static DeploymentRegistry sMe;
036:
037: /**
038: * List of all registered channels.
039: */
040: private HashMap mDeployments;
041:
042: /**
043: * Logger object
044: */
045: private Logger mLog;
046:
047: /**
048: * Translator object for internationalization.
049: */
050: private StringTranslator mTranslator;
051:
052: /**
053: * Private constructor.
054: */
055: private DeploymentRegistry() {
056: mDeployments = new HashMap();
057: mLog = SequencingEngineContext.getInstance().getLogger();
058: mTranslator = new StringTranslator();
059: }
060:
061: /**
062: * Used to grab a reference of this object.
063: *
064: * @return an initialized ChannelRegistry reference
065: */
066: public static synchronized DeploymentRegistry getInstance() {
067: if (sMe == null) {
068: sMe = new DeploymentRegistry();
069: }
070:
071: return sMe;
072: }
073:
074: /**
075: * Returns all the deployments to SE.
076: *
077: * @return Array of deployment Ids.
078: */
079: public synchronized String[] getAllDeployments() {
080: List deps = new ArrayList();
081: Collection col = mDeployments.values();
082: Iterator iter = col.iterator();
083:
084: while (iter.hasNext()) {
085: ServicelistBean eb = (ServicelistBean) iter.next();
086: String depid = eb.getDeploymentId();
087: deps.add(depid);
088: }
089:
090: String[] deployments = new String[deps.size()];
091: Iterator depiter = deps.iterator();
092: int i = 0;
093:
094: while (depiter.hasNext()) {
095: deployments[i] = (String) depiter.next();
096: i++;
097: }
098:
099: return deployments;
100: }
101:
102: /**
103: * Util method to find if a service has been deployed.
104: *
105: * @param servicename Service name
106: *
107: * @return true if deployed, false otherwise.
108: */
109: public boolean isDeployed(String servicename) {
110: if (mDeployments.get(servicename) == null) {
111: return false;
112: }
113:
114: return true;
115: }
116:
117: /**
118: * Gets the status of the asa id.
119: *
120: * @param asId sub-assembly id
121: *
122: * @return true if deployed , false otherwise.
123: */
124: public boolean getDeploymentStatus(String asId) {
125: Set keyset = mDeployments.keySet();
126: Iterator iter = keyset.iterator();
127:
128: while (iter.hasNext()) {
129: String ser = (String) iter.next();
130: ServicelistBean eb = (ServicelistBean) mDeployments
131: .get(ser);
132:
133: if (eb.getDeploymentId().equals(asId)) {
134: return true;
135: }
136: }
137:
138: return false;
139: }
140:
141: /**
142: * Gets the servicelist bean associated with the ASA id.
143: *
144: * @param asId ASA ID.
145: *
146: * @return list of serviclist bean.
147: */
148: public ServicelistBean getService(String asId) {
149: Set keyset = mDeployments.keySet();
150: Iterator iter = keyset.iterator();
151:
152: while (iter.hasNext()) {
153: String ser = (String) iter.next();
154: ServicelistBean sb = (ServicelistBean) mDeployments
155: .get(ser);
156:
157: if (sb.getDeploymentId().trim().equals(asId)) {
158: return sb;
159: }
160: }
161:
162: return null;
163: }
164:
165: /**
166: * Removes all the entries from the registry.
167: */
168: public void clearRegistry() {
169: if (mDeployments == null) {
170: return;
171: }
172:
173: mDeployments = new HashMap();
174: }
175:
176: /**
177: * Removes a service from the registry.
178: *
179: * @param service service name
180: */
181: public synchronized void deregisterService(String service) {
182: mLog.info(mTranslator
183: .getString(SEQ_DEREGISTER_SERVICE, service));
184:
185: ServicelistBean eb = (ServicelistBean) mDeployments
186: .remove(service);
187:
188: if (eb == null) {
189: mLog.severe(mTranslator.getString(
190: SEQ_DEREGISTER_SERVICE_FAILED, service));
191: }
192: }
193:
194: /**
195: * Gets the Service list associated with the service name .
196: *
197: * @param servicename service name
198: *
199: * @return servicelist bean
200: */
201: public ServicelistBean findService(String servicename) {
202: ServicelistBean sb;
203: sb = (ServicelistBean) mDeployments.get(servicename);
204:
205: return sb;
206: }
207:
208: /**
209: * List all endpoints currently present in the registry.
210: *
211: * @return an iterator over all endpoints in the registry.
212: */
213: public Iterator listAllServices() {
214: if (mDeployments == null) {
215: return null;
216: }
217:
218: Set tmp = mDeployments.keySet();
219:
220: if (tmp == null) {
221: return null;
222: }
223:
224: return tmp.iterator();
225: }
226:
227: /**
228: * Registers an endpoint with the registry. Duplicates are allowed and no
229: * validation is performed at this point.
230: *
231: * @param service the endpoint to register
232: * @param listbean list bean
233: */
234: public synchronized void registerService(String service,
235: ServicelistBean listbean) {
236: mLog.info(mTranslator.getString(SEQ_REGISTER_SERVICE, service));
237: mDeployments.put(service, listbean);
238: }
239: }
|