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: * @(#)SimpleRegistryImpl.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.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.JMSBindingContext;
035: import com.sun.jbi.binding.jms.JMSBindingResources;
036: import com.sun.jbi.binding.jms.OperationBean;
037: import com.sun.jbi.binding.jms.JMSConstants;
038: import com.sun.jbi.binding.jms.config.ConfigConstants;
039: import com.sun.jbi.binding.jms.util.UtilBase;
040:
041: import java.io.BufferedReader;
042: import java.io.BufferedWriter;
043: import java.io.File;
044: import java.io.FileNotFoundException;
045: import java.io.FileReader;
046: import java.io.FileWriter;
047: import java.io.IOException;
048: import java.io.Reader;
049: import java.io.Writer;
050:
051: import java.util.ArrayList;
052: import java.util.Collection;
053: import java.util.HashMap;
054: import java.util.Iterator;
055: import java.util.List;
056: import java.util.Map;
057: import java.util.Properties;
058: import java.util.Set;
059: import java.util.StringTokenizer;
060:
061: import java.util.logging.Logger;
062:
063: import javax.jbi.JBIException;
064:
065: import javax.xml.namespace.QName;
066:
067: /**
068: * This is a very simple registry implementation.
069: *
070: * @author Sun Microsystems. Inc.
071: */
072: public class SimpleRegistryImpl extends
073: com.sun.jbi.binding.jms.util.UtilBase implements
074: RegistryImplementor, JMSBindingResources {
075: /**
076: * Seperator used to seperate fields in the serialized registry file.
077: */
078: private static final String FIELD_SEPERATOR = ", ";
079:
080: /**
081: * Seperator used to seperate fields in a registry key.
082: */
083: private static final char KEY_FIELD_SEPERATOR = '#';
084:
085: /**
086: * Character used to comment lines in the serialized registry file.
087: */
088: private static final char COMMENT_CHAR = '#';
089:
090: /**
091: * Default number of tokens present a for given registry fiel entry. The
092: * default entry identifies a service with no namespace.
093: */
094: private static final int DEFAULT_TOKEN_COUNT = 10;
095:
096: /**
097: * Null string that will be stored in registry if a value is not present.
098: */
099: private static final String NULL_VALUE = "NULL";
100:
101: /**
102: * Logger.
103: */
104: private Logger mLogger;
105:
106: /**
107: * Internal handle to the inbound registry.
108: */
109: private Map mRegistry;
110:
111: /**
112: * Internal handle to the deployment properties.
113: */
114: private Properties mDeploymentProperties;
115:
116: /**
117: * Deployment file.
118: */
119: private String mDeploymentFile;
120:
121: /**
122: * Translator.
123: */
124: private StringTranslator mStringTranslator;
125:
126: /**
127: * Creates a new instance of SimpleRegistryImpl.
128: */
129: public SimpleRegistryImpl() {
130: mRegistry = new HashMap();
131: mLogger = JMSBindingContext.getInstance().getLogger();
132: mStringTranslator = JMSBindingContext.getInstance()
133: .getStringTranslator();
134: }
135:
136: /**
137: * Returns all keys.
138: *
139: * @return iterator to keys.
140: */
141: public Iterator getAllKeys() {
142: return mRegistry.keySet().iterator();
143: }
144:
145: /**
146: * Gets all values.
147: *
148: * @return collection of values.
149: */
150: public Collection getAllValues() {
151: Collection allvalues = new ArrayList();
152:
153: Object value = null;
154:
155: //Get the Inbound registry endpoints and unregister them
156: Iterator keyIter = getAllKeys();
157:
158: while (keyIter.hasNext()) {
159: //Get the registry key
160: String regkey = (String) keyIter.next();
161:
162: //Get the endpoint for this key
163: value = getValue(regkey);
164:
165: if (value == null) {
166: continue;
167: }
168:
169: allvalues.add(value);
170: }
171:
172: //end while
173: return allvalues;
174: }
175:
176: /**
177: * Returns the keys.
178: *
179: * @return iterator to keys.
180: */
181: public Iterator getKeys() {
182: Iterator keyIter = null;
183: keyIter = mRegistry.keySet().iterator();
184:
185: return keyIter;
186: }
187:
188: /**
189: * Get the directory which has the schema definitions for the endpoint.
190: *
191: * @return the Directory which has the schema directory.
192: */
193: public File getSchemaDir() {
194: StringBuffer schemaPath = new StringBuffer();
195:
196: // Construct deployment file name
197: schemaPath.append(JMSBindingContext.getInstance().getContext()
198: .getInstallRoot());
199: schemaPath.append(File.separator);
200: schemaPath.append(mDeploymentProperties
201: .getProperty(JMSConstants.REGISTRY_FILE_LOCATION));
202:
203: // Read the file contents
204: return new java.io.File(schemaPath.toString());
205: }
206:
207: /**
208: * Returns value from registry.
209: *
210: * @param registryKey key.
211: *
212: * @return object corresponding to key.
213: */
214: public Object getValue(String registryKey) {
215: Object value;
216: value = mRegistry.get(registryKey);
217:
218: return value;
219: }
220:
221: /**
222: * Clears the registry.
223: */
224: public void clearRegistry() {
225: mRegistry.clear();
226: }
227:
228: /**
229: * Checks if registry contains the key.
230: *
231: * @param registryKey key.
232: *
233: * @return true if registry contains key.
234: */
235: public boolean containsKey(String registryKey) {
236: boolean isDeployed = false;
237: isDeployed = mRegistry.containsKey(registryKey);
238:
239: return isDeployed;
240: }
241:
242: /**
243: * Initializes the registry.
244: *
245: * @param props properties.
246: *
247: * @throws JBIException jbi exception.
248: */
249: public void init(java.util.Properties props) throws JBIException {
250: mDeploymentProperties = props;
251:
252: String deploymentFileName = props
253: .getProperty(JMSConstants.REGISTRY_FILE_NAME);
254: String deploymentFileLocation = props
255: .getProperty(JMSConstants.REGISTRY_FILE_LOCATION);
256: StringBuffer deploymentFileBuffer = null;
257:
258: try {
259: deploymentFileBuffer = new StringBuffer();
260:
261: // Construct deployment file name
262: deploymentFileBuffer.append(JMSBindingContext.getInstance()
263: .getContext().getInstallRoot());
264: deploymentFileBuffer.append(File.separator);
265: deploymentFileBuffer.append(deploymentFileLocation);
266: deploymentFileBuffer.append(File.separator);
267: deploymentFileBuffer.append(deploymentFileName);
268: mLogger.fine(mStringTranslator.getString(JMS_REG_FILE_NAME,
269: deploymentFileBuffer.toString()));
270: mDeploymentFile = deploymentFileBuffer.toString();
271: BufferedReader reader = new BufferedReader(new FileReader(
272: mDeploymentFile.toString()));
273: String serializedDeployment = reader.readLine();
274: while (serializedDeployment != null) {
275: if (serializedDeployment.charAt(0) != COMMENT_CHAR) {
276: EndpointBean endpoint = deserializeEndpoint(serializedDeployment);
277: registerKey(endpoint.getUniqueName(), endpoint);
278: }
279:
280: serializedDeployment = reader.readLine();
281: }
282: } catch (FileNotFoundException fe) {
283: mLogger.severe(mStringTranslator.getString(
284: JMS_REG_FILE_NOT_FOUND, deploymentFileBuffer
285: .toString()));
286: } catch (Exception exception) {
287: mLogger.severe(mStringTranslator.getString(
288: JMS_ERROR_LOADING_DEPLOYMENTS, exception
289: .getMessage()));
290: exception.printStackTrace();
291: throw new JBIException(exception.getMessage());
292: }
293: }
294:
295: /**
296: * Registers the key.
297: * @param key key.
298: * @param value value.
299: *
300: * @return key.
301: */
302: public synchronized String registerKey(String key, Object value) {
303: mRegistry.put(key, value);
304:
305: return key;
306: }
307:
308: /**
309: * Removes deployment from the deployment registry.
310: *
311: * @param registryKey key.
312: */
313: public synchronized void removeKey(String registryKey) {
314: mRegistry.remove(registryKey);
315: }
316:
317: /**
318: * Following method serializes/persists All Endpoints/Deployments in the
319: * registry.dat file (default for now).
320: *
321: * @throws JBIException serialization exception.
322: */
323: public void serialize() throws JBIException {
324: try {
325: BufferedWriter writer = new BufferedWriter(new FileWriter(
326: getRegFileName()));
327: addRegFileHeader(writer);
328:
329: // Loop through all the deployments
330: Collection alldepl = getAllValues();
331:
332: Iterator iter = alldepl.iterator();
333: EndpointBean ep = null;
334:
335: while (iter.hasNext()) {
336: //Get the Endpoint Object
337: ep = (EndpointBean) iter.next();
338:
339: if (ep != null) {
340: // tokenize each endpoint
341: String tokEp = tokenizeEndpoint(ep);
342:
343: //Write this to registry file
344: writer.write(tokEp);
345: writer.newLine();
346: writer.flush();
347: }
348: }
349: writer.close();
350: } catch (IOException ioe) {
351: mLogger.severe(mStringTranslator.getString(
352: JMS_ERROR_LOADING_DEPLOYMENTS, ioe.getMessage()));
353: throw new JBIException(ioe.getMessage());
354: } catch (Exception ex) {
355: throw new JBIException(ex.getMessage());
356: }
357: }
358:
359: /**
360: * Get the registry file name.
361: *
362: * @return registry file name.
363: */
364: private String getRegFileName() {
365: return mDeploymentFile;
366: }
367:
368: /**
369: * Sets the role.
370: *
371: * @param eb endpoint bean.
372: */
373: private void setRole(EndpointBean eb) {
374: String role = (String) eb
375: .getValue(ConfigConstants.ENDPOINT_TYPE);
376:
377: if (role == null) {
378: eb.setRole(ConfigConstants.CONSUMER);
379: } else {
380: if (role.trim().equalsIgnoreCase(
381: ConfigConstants.PROVIDER_STRING)) {
382: eb.setRole(ConfigConstants.PROVIDER);
383: } else {
384: eb.setRole(ConfigConstants.CONSUMER);
385: }
386: }
387: }
388:
389: /**
390: * Following method adds the header lines for the registry file This header
391: * explains the registry format.
392: *
393: * @param writer file to write.
394: *
395: * @throws IOException exception while writing inot file.
396: */
397: private void addRegFileHeader(BufferedWriter writer)
398: throws IOException {
399: writer.write("#Format of the registry file");
400: writer.newLine();
401: writer
402: .write("#deploymentId, service_namespace, service_name, endpoint_name, endpoint_type,"
403: + "destination_name, destination_style, durability, connection-factory-name,"
404: + "connection-user-name, connection-password, message_selector, "
405: + "time_to_live, reply_to, ( operation_namespace, "
406: + "operation_name, mepname, input_type, output_type) ");
407: writer.newLine();
408: }
409:
410: /**
411: * Decorates.
412: *
413: * @param s string to decorate.
414: *
415: * @return decorated string.
416: */
417: private String decorate(String s) {
418: if (s == null) {
419: return NULL_VALUE;
420: } else if (s.trim().equals("")) {
421: return NULL_VALUE;
422: }
423:
424: return s;
425: }
426:
427: /**
428: * This is a specific function which has to be customized according to the
429: * object that is serialized and deserialized.
430: *
431: * @param serializedString string to deserialize.
432: *
433: * @return endpoint bean.
434: */
435: private EndpointBean deserializeEndpoint(String serializedString) {
436: EndpointBean endpoint = null;
437: StringTokenizer deploymentTokenizer = new StringTokenizer(
438: serializedString, FIELD_SEPERATOR);
439:
440: QName serviceQName = null;
441: mLogger.fine("Tokens : " + deploymentTokenizer.countTokens());
442:
443: String deploymentId = deploymentTokenizer.nextToken().trim();
444: mLogger.fine("Deployment id: " + deploymentId);
445:
446: String serviceNamespace = evaluate(deploymentTokenizer
447: .nextToken().trim());
448: mLogger.fine("Service Name Space : " + serviceNamespace);
449:
450: String serviceName = deploymentTokenizer.nextToken().trim();
451: mLogger.fine("Service name : " + serviceName);
452:
453: serviceQName = new QName(serviceNamespace, serviceName);
454:
455: String endpointName = deploymentTokenizer.nextToken().trim();
456: mLogger.fine("Endpoint : " + endpointName);
457:
458: String endpointType = deploymentTokenizer.nextToken().trim();
459: mLogger.fine("Endpoint type : " + endpointType);
460:
461: String destname = deploymentTokenizer.nextToken().trim();
462: mLogger.fine("Destination Name :" + destname);
463:
464: String deststyle = deploymentTokenizer.nextToken().trim();
465: mLogger.fine("Destination style : " + deststyle);
466:
467: String confactory = deploymentTokenizer.nextToken().trim();
468: mLogger.fine("Connection factory : " + confactory);
469:
470: String conuser = deploymentTokenizer.nextToken().trim();
471: mLogger.fine("Connection user name : " + conuser);
472:
473: String conpassword = deploymentTokenizer.nextToken().trim();
474: mLogger.fine("Connection user password: " + conpassword);
475:
476: String durability = evaluate(deploymentTokenizer.nextToken()
477: .trim());
478: mLogger.fine("Durability = " + durability);
479:
480: String msgsel = evaluate(deploymentTokenizer.nextToken().trim());
481: mLogger.fine("Selector = " + msgsel);
482:
483: String timetolive = evaluate(deploymentTokenizer.nextToken()
484: .trim());
485: mLogger.fine("time to live = " + timetolive);
486:
487: String replyto = evaluate(deploymentTokenizer.nextToken()
488: .trim());
489: mLogger.fine("Reply to = " + replyto);
490:
491: endpoint = new EndpointBean();
492: endpoint.setValue(ConfigConstants.SERVICE_NAMESPACE,
493: serviceNamespace);
494: endpoint.setValue(ConfigConstants.SERVICENAME, serviceName);
495: endpoint.setValue(ConfigConstants.ENDPOINTNAME, endpointName);
496: endpoint.setValue(ConfigConstants.ENDPOINT_TYPE, endpointType);
497: endpoint.setValue(ConfigConstants.DESTINATION_NAME, destname);
498: endpoint.setValue(ConfigConstants.DESTINATION_STYLE, deststyle);
499: endpoint.setValue(ConfigConstants.CONNECTION_FACTORY,
500: confactory);
501: endpoint.setValue(ConfigConstants.DURABILITY, durability);
502: endpoint.setValue(ConfigConstants.MESSAGE_SELECTOR, msgsel);
503: endpoint.setValue(ConfigConstants.TIME_TO_LIVE, timetolive);
504: endpoint.setValue(ConfigConstants.REPLY_TO, replyto);
505: endpoint.setValue(ConfigConstants.CONNECTION_USER_ID, conuser);
506: endpoint.setValue(ConfigConstants.CONNECTION_PASSWORD,
507: conpassword);
508: endpoint.setDeploymentId(deploymentId);
509:
510: setRole(endpoint);
511:
512: while (deploymentTokenizer.hasMoreTokens()) {
513: String operationNamespace = deploymentTokenizer.nextToken()
514: .trim();
515: mLogger
516: .fine("Operation name space = "
517: + operationNamespace);
518:
519: String operationName = deploymentTokenizer.nextToken()
520: .trim();
521: mLogger.fine("Operation name = " + operationName);
522:
523: String pattern = deploymentTokenizer.nextToken().trim();
524: mLogger.fine("pattern = " + pattern);
525:
526: String input = evaluate(deploymentTokenizer.nextToken()
527: .trim());
528: mLogger.fine("input = " + input);
529:
530: String output = evaluate(deploymentTokenizer.nextToken()
531: .trim());
532: mLogger.fine("output = " + output);
533:
534: endpoint.addOperation(operationNamespace, operationName,
535: pattern, input, output);
536: }
537:
538: return endpoint;
539: }
540:
541: /**
542: * Evaluator.
543: *
544: * @param e string to trim.
545: *
546: * @return string.
547: */
548: private String evaluate(String e) {
549: if (e.equals(NULL_VALUE)) {
550: return "";
551: }
552:
553: return e;
554: }
555:
556: /**
557: * Following method tokenizes an endpoint (Comma seperated).
558: *
559: * @param endpt - Endpoint that needs to be tokenzied
560: *
561: * @return tokenized string.
562: */
563: private String tokenizeEndpoint(EndpointBean endpt) {
564: //Construct the string that needs to be persisted.
565: //This string is consists of comma (FIELD_SEPARATOR)
566: //seperated attributes for an endpoint (service name, endpoint name etc.)
567: StringBuffer epBuff = new StringBuffer();
568:
569: epBuff.append(endpt.getDeploymentId());
570: epBuff.append(FIELD_SEPERATOR);
571:
572: //Service namespace
573: epBuff
574: .append(decorate(endpt
575: .getValue(ConfigConstants.SERVICE_NAMESPACE
576: .toString())));
577: epBuff.append(FIELD_SEPERATOR);
578:
579: //Service Name
580: epBuff.append(endpt.getValue(ConfigConstants.SERVICENAME)
581: .toString());
582: epBuff.append(FIELD_SEPERATOR);
583:
584: //Endpoint Name
585: epBuff.append(endpt.getValue(ConfigConstants.ENDPOINTNAME));
586: epBuff.append(FIELD_SEPERATOR);
587:
588: //Endpoint role
589: epBuff.append(endpt.getValue(ConfigConstants.ENDPOINT_TYPE));
590: epBuff.append(FIELD_SEPERATOR);
591:
592: //Endpoint Address
593: epBuff.append(endpt.getValue(ConfigConstants.DESTINATION_NAME));
594: epBuff.append(FIELD_SEPERATOR);
595:
596: //Endpoint Address
597: epBuff
598: .append(endpt
599: .getValue(ConfigConstants.DESTINATION_STYLE));
600: epBuff.append(FIELD_SEPERATOR);
601:
602: //Endpoint Address
603: epBuff.append(endpt
604: .getValue(ConfigConstants.CONNECTION_FACTORY));
605: epBuff.append(FIELD_SEPERATOR);
606:
607: epBuff.append(endpt
608: .getValue(ConfigConstants.CONNECTION_USER_ID));
609: epBuff.append(FIELD_SEPERATOR);
610:
611: epBuff.append(endpt
612: .getValue(ConfigConstants.CONNECTION_PASSWORD));
613: epBuff.append(FIELD_SEPERATOR);
614:
615: //Endpoint Address
616: epBuff.append(decorate(endpt
617: .getValue(ConfigConstants.DURABILITY)));
618: epBuff.append(FIELD_SEPERATOR);
619:
620: //Endpoint Address
621: epBuff.append(decorate(endpt
622: .getValue(ConfigConstants.MESSAGE_SELECTOR)));
623: epBuff.append(FIELD_SEPERATOR);
624:
625: //Endpoint Address
626: epBuff.append(decorate(endpt
627: .getValue(ConfigConstants.TIME_TO_LIVE)));
628: epBuff.append(FIELD_SEPERATOR);
629:
630: //Endpoint Address
631: epBuff
632: .append(decorate(endpt
633: .getValue(ConfigConstants.REPLY_TO)));
634:
635: // epBuff.append(FIELD_SEPERATOR);
636: Iterator opsIter = endpt.getAllOperations().iterator();
637:
638: while (opsIter.hasNext()) {
639: OperationBean op = (OperationBean) opsIter.next();
640:
641: epBuff.append(FIELD_SEPERATOR);
642:
643: //Operation Namespace
644: epBuff.append(op.getNamespace());
645: epBuff.append(FIELD_SEPERATOR);
646:
647: //Operation Name
648: epBuff.append(op.getName());
649: epBuff.append(FIELD_SEPERATOR);
650:
651: epBuff.append(op.getMep());
652: epBuff.append(FIELD_SEPERATOR);
653:
654: epBuff.append(decorate(op.getInputType()));
655: epBuff.append(FIELD_SEPERATOR);
656:
657: epBuff.append(decorate(op.getOutputType()));
658: epBuff.append(FIELD_SEPERATOR);
659: }
660:
661: //end while
662: return epBuff.toString();
663: }
664: }
|