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: * @(#)EndpointRegistry.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms.deploy;
030:
031: import com.sun.jbi.binding.jms.EndpointBean;
032: import com.sun.jbi.binding.jms.JMSBindingContext;
033:
034: import com.sun.jbi.binding.jms.config.ConfigConstants;
035: import com.sun.jbi.binding.jms.config.Config;
036:
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.Iterator;
040: import java.util.List;
041:
042: import java.util.logging.Logger;
043:
044: import javax.jbi.JBIException;
045:
046: import javax.jbi.servicedesc.ServiceEndpoint;
047:
048: import javax.xml.namespace.QName;
049:
050: /**
051: * Simple Deployment Registry implementation. The Registry is currently loaded
052: * from a scaffolded registry data file. This will be improved later.
053: *
054: * @author Sun Microsystems Inc.
055: */
056: public class EndpointRegistry {
057: /**
058: * Internal handle to the logger object.
059: */
060: private Logger mLogger;
061:
062: /**
063: * Internal handle to the registry implementation.
064: */
065: private RegistryImplementor mRegistryImplementor;
066:
067: /**
068: * Creates a new instance of EndpointRegistry.
069: */
070: public EndpointRegistry() {
071: mLogger = JMSBindingContext.getInstance().getLogger();
072: }
073:
074: /**
075: * Returns an iterator for endpoint names.
076: *
077: * @return iterator of endpoint names.
078: */
079: public Iterator getAllEndpointNames() {
080: return mRegistryImplementor.getAllKeys();
081: }
082:
083: /**
084: * Returns all the endpoints.
085: *
086: * @return collection of endpoints.
087: */
088: public Collection getAllEndpoints() {
089: return mRegistryImplementor.getAllValues();
090: }
091:
092: /**
093: * Gets the inbound deployment for a given service URL.
094: *
095: * @param key - service address.
096: *
097: * @return deployed endpoint.
098: *
099: * @throws JBIException when the endpoint could not be deployed.
100: */
101: public EndpointBean getEndpoint(Object key) throws JBIException {
102: String regkey = getRegistryKey(key);
103:
104: return (EndpointBean) mRegistryImplementor.getValue(regkey);
105: }
106:
107: /**
108: * Returns the endpoints corresponding to a deployment.
109: *
110: * @param asid SU id.
111: *
112: * @return collection of endpoints.
113: */
114: public Collection getEndpoints(String asid) {
115: Collection allvalues = new ArrayList();
116:
117: EndpointBean eb = null;
118:
119: //Get the Inbound registry endpoints and unregister them
120: Iterator keyIter = getAllEndpointNames();
121:
122: try {
123: while (keyIter.hasNext()) {
124: //Get the registry key
125: String regkey = (String) keyIter.next();
126:
127: //Get the endpoint for this key
128: eb = (EndpointBean) getEndpoint(regkey);
129:
130: if (eb == null) {
131: continue;
132: }
133:
134: if (eb.getDeploymentId().trim().equals(asid)) {
135: allvalues.add(eb);
136: }
137: }
138: } catch (Exception e) {
139: allvalues = null;
140: }
141:
142: //end while
143: return allvalues;
144: }
145:
146: /**
147: * Clears all deployments.
148: */
149: public void clearEndpoints() {
150: if (mRegistryImplementor != null) {
151: mRegistryImplementor.clearRegistry();
152: }
153: }
154:
155: /**
156: * Indicates if the current inbound deployment exists.
157: *
158: * @param key - registry key.
159: *
160: * @return true if a deployment exists for this address;false otherwise.
161: */
162: public boolean containsEndpoint(Object key) {
163: String registryKey = getRegistryKey(key);
164:
165: return mRegistryImplementor.containsKey(registryKey);
166: }
167:
168: /**
169: * Initializes the deployment registry.
170: *
171: * @throws JBIException jbi exception.
172: */
173: public void init() throws JBIException {
174: // Load it from a file for maximum flexibility for vertical slice
175: loadDeployments();
176: }
177:
178: /**
179: * Loads deployemnts from the deployment file. this is a hack and will be
180: * gone.
181: *
182: * @throws JBIException jbi exception.
183: */
184: public void loadDeployments() throws JBIException {
185: try {
186: Config config = JMSBindingContext.getInstance().getConfig();
187: String implementationClassName = config
188: .getDeploymentImplClass();
189: ClassLoader bindingClassLoader = this .getClass()
190: .getClassLoader();
191: mLogger.fine("Loading " + implementationClassName
192: + " using the thread context class loader");
193:
194: Class implementationClass = bindingClassLoader
195: .loadClass(implementationClassName);
196: mLogger.fine("Creating a new instance of "
197: + implementationClassName);
198: mRegistryImplementor = (RegistryImplementor) implementationClass
199: .newInstance();
200: mRegistryImplementor.init(config.getDeploymentProperties());
201: } catch (Exception exception) {
202: exception.printStackTrace();
203:
204: JBIException jbiException = new JBIException(exception
205: .getMessage());
206: jbiException.initCause(exception);
207: throw jbiException;
208: } catch (Throwable t) {
209: t.printStackTrace();
210: }
211: }
212:
213: /**
214: * Persists the deployments.
215: *
216: * @throws JBIException jbi exception.
217: */
218: public void persist() throws JBIException {
219: mRegistryImplementor.serialize();
220: }
221:
222: /**
223: * Adds a new inbound deployment to the deployment registry.
224: *
225: * @param endpoint to be deployed.
226: *
227: * @return registered key.
228: */
229: public String registerEndpoint(EndpointBean endpoint) {
230: String key = endpoint.getUniqueName();
231:
232: return mRegistryImplementor.registerKey(key, endpoint);
233: }
234:
235: /**
236: * Removes the endpoint from the registry.
237: *
238: * @param endpoint endpoint to be removed.
239: */
240:
241: public void deregisterEndpoint(String endpoint) {
242: mRegistryImplementor.removeKey(endpoint);
243: }
244:
245: /**
246: * Removes the inbound deployment from the deployment registry.
247: *
248: * @param key to be undeployed.
249: *
250: * @throws JBIException when the deployment could not be undeployed.
251: */
252: public void removeEndpoint(Object key) throws JBIException {
253: String regkey = getRegistryKey(key);
254: mRegistryImplementor.removeKey(regkey);
255: }
256:
257: /**
258: * Returns a registry key.
259: *
260: * @param key registry key.
261: *
262: * @return string value of the key.
263: */
264: private String getRegistryKey(Object key) {
265: String regkey = null;
266:
267: if (key == null) {
268: return regkey;
269: }
270:
271: try {
272: if (key instanceof ServiceEndpoint) {
273: ServiceEndpoint epref = (ServiceEndpoint) key;
274: if (epref.getEndpointName() != null) {
275: regkey = epref.getServiceName().toString()
276: + epref.getEndpointName();
277: } else {
278: regkey = epref.getServiceName().toString();
279: }
280: } else if (key instanceof EndpointBean) {
281: EndpointBean eb = (EndpointBean) key;
282: regkey = eb.getUniqueName();
283: } else if (key instanceof String) {
284: regkey = (String) key;
285: }
286: } catch (Exception e) {
287: ;
288: }
289:
290: return regkey;
291: }
292:
293: /**
294: * Returns all the deployments.
295: *
296: * @return String [] array of deployments.
297: *
298: */
299: public String[] getAllDeployments() {
300:
301: List deps = new ArrayList();
302: Collection col = getAllEndpoints();
303: Iterator iter = col.iterator();
304: while (iter.hasNext()) {
305: EndpointBean eb = (EndpointBean) iter.next();
306: String depid = eb.getDeploymentId();
307: if (!deps.contains(depid)) {
308: deps.add(depid);
309: }
310: }
311: String[] deployments = new String[deps.size()];
312: Iterator depiter = deps.iterator();
313: int i = 0;
314: while (depiter.hasNext()) {
315: deployments[i] = (String) depiter.next();
316: i++;
317: }
318: return deployments;
319: }
320:
321: /**
322: * Used to find a registered endpoint which matches the specified interface.
323: *
324: * @param interfacename the interface to match against.
325: *
326: * @return the appropriate endpoint bean, or no such endpoint exists.
327: */
328: public EndpointBean findEndpointByInterface(String interfacename) {
329: //Set keyset =
330: Iterator iter = mRegistryImplementor.getAllKeys();
331:
332: while (iter.hasNext()) {
333: String ser = (String) iter.next();
334: EndpointBean eb = (EndpointBean) mRegistryImplementor
335: .getValue(ser);
336: QName in = new QName(eb
337: .getValue(ConfigConstants.INTERFACE_NAMESPACE), eb
338: .getValue(ConfigConstants.INTERFACE_LOCALNAME));
339: if ((in.toString().trim().equals(interfacename))
340: && (eb.getRole() == ConfigConstants.CONSUMER)) {
341: return eb;
342: }
343: }
344: return null;
345: }
346:
347: /**
348: * Used to check if the q or topic is already deployed.
349: *
350: * @param eb bean.
351: *
352: * @return true or false.
353: */
354: public boolean containsDestination(Object eb) {
355: EndpointBean bean = (EndpointBean) eb;
356: Iterator iter = mRegistryImplementor.getAllKeys();
357: while (iter.hasNext()) {
358: String ser = (String) iter.next();
359: EndpointBean epbean = (EndpointBean) mRegistryImplementor
360: .getValue(ser);
361: if ((epbean.getValue(ConfigConstants.DESTINATION_NAME)
362: .equals(bean
363: .getValue(ConfigConstants.DESTINATION_NAME)))
364: && (epbean.getStyle() == bean.getStyle())) {
365: return true;
366: }
367: }
368: return false;
369: }
370:
371: }
|