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