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.binding.file;
012:
013: import com.sun.jbi.binding.file.FileBindingContext;
014: import com.sun.jbi.binding.file.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.LinkedList;
021: import java.util.List;
022: import java.util.Set;
023: import java.util.logging.Logger;
024:
025: import com.sun.jbi.binding.file.util.ConfigData;
026: import javax.xml.namespace.QName;
027:
028: /**
029: * This is a registry which maintains the servicename, endpoint name across
030: * the bean object. This is an in-memory representation of all endpoints
031: * deployed to file binding.
032: *
033: * @author Sun Microsystems, Inc.
034: */
035: public final class DeploymentRegistry implements FileBindingResources {
036: /**
037: * Singleton reference.
038: */
039: private static DeploymentRegistry sMe;
040:
041: /**
042: * List of all registered channels.
043: */
044: private HashMap mDeployments;
045:
046: /**
047: * Logger object
048: */
049: private Logger mLog;
050:
051: /**
052: * Helper for i18n
053: */
054: private StringTranslator mTranslator;
055:
056: /**
057: * Private constructor.
058: */
059: private DeploymentRegistry() {
060: mDeployments = new HashMap();
061: mLog = FileBindingContext.getInstance().getLogger();
062: mTranslator = new StringTranslator();
063: }
064:
065: /**
066: * Used to grab a reference of this object.
067: *
068: * @return an initialized DeploymentRegistry reference
069: */
070: public static synchronized DeploymentRegistry getInstance() {
071: if (sMe == null) {
072: sMe = new DeploymentRegistry();
073: }
074:
075: return sMe;
076: }
077:
078: /**
079: * Returns all the deployments to file binding.
080: *
081: * @return an array of Deployment Ids.
082: */
083: public synchronized String[] getAllDeployments() {
084: List deps = new ArrayList();
085: Collection col = mDeployments.values();
086: String[] deployments = null;
087:
088: try {
089: Iterator iter = col.iterator();
090:
091: while (iter.hasNext()) {
092: EndpointBean eb = (EndpointBean) iter.next();
093: String depid = eb.getDeploymentId();
094:
095: if (!deps.contains(depid)) {
096: deps.add(depid);
097: }
098: }
099:
100: deployments = new String[deps.size()];
101:
102: Iterator depiter = deps.iterator();
103: int i = 0;
104:
105: while (depiter.hasNext()) {
106: deployments[i] = (String) depiter.next();
107: i++;
108: }
109: } catch (Exception e) {
110: e.printStackTrace();
111: }
112:
113: return deployments;
114: }
115:
116: /**
117: * Util method to find if a service has been deployed.
118: *
119: * @param serviceendpoint serviceendpoint information.
120: *
121: * @return true if deployed, false otherwise.
122: */
123: public boolean isDeployed(String serviceendpoint) {
124: if (mDeployments.get(serviceendpoint) == null) {
125: return false;
126: }
127:
128: return true;
129: }
130:
131: /**
132: * Gets the status of the su id.
133: *
134: * @param suId SU Id
135: *
136: * @return true if deployed , false otherwise.
137: */
138: public boolean getDeploymentStatus(String suId) {
139: Set keyset = mDeployments.keySet();
140: Iterator iter = keyset.iterator();
141:
142: while (iter.hasNext()) {
143: String ser = (String) iter.next();
144: EndpointBean eb = (EndpointBean) mDeployments.get(ser);
145:
146: if (eb.getDeploymentId().trim().equals(suId)) {
147: return true;
148: }
149: }
150:
151: return false;
152: }
153:
154: /**
155: * Gets the enpoint list associated with the SU id.
156: *
157: * @param suId SU ID.
158: *
159: * @return list of endpoint Beans.
160: */
161: public List getEndpoints(String suId) {
162: Set keyset = mDeployments.keySet();
163: Iterator iter = keyset.iterator();
164: List eplist = new LinkedList();
165:
166: while (iter.hasNext()) {
167: String ser = (String) iter.next();
168: EndpointBean eb = (EndpointBean) mDeployments.get(ser);
169:
170: if (eb.getDeploymentId().trim().equals(suId)) {
171: eplist.add(eb);
172: }
173: }
174:
175: return eplist;
176: }
177:
178: /**
179: * Removes all the entries from the registry.
180: */
181: public void clearRegistry() {
182: if (mDeployments == null) {
183: return;
184: }
185:
186: mDeployments = new HashMap();
187: }
188:
189: /**
190: * Removes an endpoint from the registry.
191: *
192: * @param serviceendpoint service:endpoint combination.
193: */
194: public synchronized void deregisterEndpoint(String serviceendpoint) {
195: mLog.info(mTranslator.getString(FBC_DEREGISTER_ENDPOINT,
196: serviceendpoint));
197:
198: EndpointBean eb = null;
199:
200: try {
201: eb = (EndpointBean) mDeployments.remove(serviceendpoint);
202: } catch (Exception e) {
203: ;
204: }
205:
206: if (eb == null) {
207: mLog.severe(mTranslator.getString(
208: FBC_DEREGISTER_ENDPOINT_FAILED, serviceendpoint));
209: }
210: }
211:
212: /**
213: * Used to find a registered endpoint which matches the specified service.
214: *
215: * @param serviceendpoint the service to match against
216: *
217: * @return the appropriate endpoint bean, or no such endpoint exists.
218: */
219: public EndpointBean findEndpoint(String serviceendpoint) {
220: EndpointBean eb;
221: eb = (EndpointBean) mDeployments.get(serviceendpoint);
222:
223: return eb;
224: }
225:
226: /**
227: * Used to find a registered endpoint which matches the specified interface.
228: *
229: * @param interface name the service to match against
230: *
231: * @return the appropriate endpoint bean, or no such endpoint exists.
232: */
233: public EndpointBean findEndpointByInterface(String interfacename) {
234: Set keyset = mDeployments.keySet();
235: Iterator iter = keyset.iterator();
236:
237: while (iter.hasNext()) {
238: String ser = (String) iter.next();
239: EndpointBean eb = (EndpointBean) mDeployments.get(ser);
240: QName in = new QName(eb
241: .getValue(ConfigData.INTERFACE_NAMESPACE), eb
242: .getValue(ConfigData.INTERFACE_LOCALNAME));
243: if ((in.toString().trim().equals(interfacename))
244: && (eb.getRole() == ConfigData.CONSUMER)) {
245: return eb;
246: }
247: }
248: return null;
249: }
250:
251: /**
252: * List all endpoints currently present in the registry.
253: *
254: * @return an iterator over all endpoints in the registry.
255: */
256: public Iterator listAllEndpoints() {
257: if (mDeployments == null) {
258: return null;
259: }
260:
261: Set tmp = mDeployments.keySet();
262:
263: if (tmp == null) {
264: return null;
265: }
266:
267: return tmp.iterator();
268: }
269:
270: /**
271: * Registers an endpoint with the registry. Duplicates are allowed and no
272: * validation is performed at this point.
273: *
274: * @param serviceendpoint the endpoint to register
275: * @param endpoint endpoint bean object.
276: */
277: public synchronized void registerEndpoint(String serviceendpoint,
278: EndpointBean endpoint) {
279: mLog.info(mTranslator.getString(FBC_REGISTER_ENDPOINT,
280: serviceendpoint));
281: mDeployments.put(serviceendpoint, endpoint);
282: }
283: }
|