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: * @(#)Config.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.config;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.JMSBindingContext;
034: import com.sun.jbi.binding.jms.JMSBindingResources;
035: import com.sun.jbi.binding.jms.JMSConstants;
036:
037: import com.sun.jbi.binding.jms.util.UtilBase;
038:
039: import com.sun.jbi.common.ConfigFileValidator;
040:
041: import java.io.File;
042:
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.Map;
047: import java.util.Properties;
048:
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: import javax.jbi.JBIException;
053:
054: import org.w3c.dom.Document;
055:
056: /**
057: * This object defines the JMS binding's runtime configuration information. The
058: * runtime is setup when the component is installed and is physically stored
059: * in ${COMPONENT_INSTALL_ROOT)/jms_config.xml file. This object uses the
060: * XMLReader implementation to read configuration data and initialize itself.
061: * The object is created when the binding component is initialized. If changes
062: * are manually made to ${COMPONENT_INSTALL_ROOT)/jms_config.xml file, the
063: * component needs to be shutdown and restarted for the changes to take
064: * affect.
065: *
066: * @author Sun Microsystems Inc.
067: */
068: public class Config extends UtilBase implements JMSBindingResources {
069: /**
070: * Defines the association between the level string names and
071: * java.util.logging.Levels. The currently supported levels are
072: *
073: * <UL>
074: * <li>
075: * SEVERE
076: * </li>
077: * <li>
078: * WARNING
079: * </li>
080: * <li>
081: * INFO
082: * </li>
083: * <li>
084: * CONFIG
085: * </li>
086: * <li>
087: * FINE
088: * </li>
089: * <li>
090: * FINER
091: * </li>
092: * <li>
093: * FINEST
094: * </li>
095: * <li>
096: * OFF
097: * </li>
098: * <li>
099: * ALL
100: * </li>
101: * </ul>.
102: */
103: private static Map sLevelCache;
104:
105: /**
106: * Defines the association between the implementation names and
107: * corresponding implementation. The currently supported implementations
108: * are
109: *
110: * <UL>
111: * <li>
112: * SimpleImplementation
113: * </li>
114: * </ul>.
115: */
116: private static Map sDeploymentImplCache;
117:
118: /**
119: * Validator for the XML file.
120: */
121: private ConfigFileValidator mValidator;
122:
123: /**
124: * Contains the application log level.
125: */
126: private Level mLogLevel;
127:
128: /**
129: * The Logger.
130: */
131: private Logger mLogger;
132:
133: /**
134: * Contains the properties configured for the deployment persistance
135: * mechanism.
136: */
137: private Properties mDeploymentProperties;
138:
139: /**
140: * Contains the configuration file name. This file name should not be
141: * relative and should contain the absolute path.
142: */
143: private String mConfigFileName;
144:
145: /**
146: * Contains the Deployment persistance implmentation class name.
147: */
148: private String mDeploymentImplClass;
149:
150: /**
151: * Lookup value for initial context.
152: */
153: private String mInitialContextFactory;
154:
155: /**
156: * Provider URL.
157: */
158: private String mProviderUrl;
159:
160: /**
161: * Password for the MQ connection.
162: */
163: private String mReceiverConnectionPassword;
164:
165: /**
166: * MQ connection user id.
167: */
168: private String mReceiverConnectionUser;
169:
170: /**
171: * Desination name.
172: */
173: private String mReceiverDestination;
174:
175: /**
176: * Receiver factory.
177: */
178: private String mReceiverFactory;
179:
180: /**
181: * Password.
182: */
183: private String mSecurityCredentials;
184:
185: /**
186: * Security level.
187: */
188: private String mSecurityLevel;
189:
190: /**
191: * User name.
192: */
193: private String mSecurityPrincipal;
194:
195: /**
196: * Local handle to StringTranslator implementation.
197: */
198: private StringTranslator mStringTranslator;
199:
200: /**
201: * Maximum number of threads.
202: */
203: private int mMaxThreads;
204:
205: /**
206: * Minimum number of threads.
207: */
208: private int mMinThreads;
209:
210: /**
211: * Stop timeout value.
212: */
213: private long mStopTimeout;
214:
215: {
216: sLevelCache = new HashMap();
217: sLevelCache.put("SEVERE", Level.SEVERE);
218: sLevelCache.put("WARNING", Level.WARNING);
219: sLevelCache.put("INFO", Level.INFO);
220: sLevelCache.put("CONFIG", Level.CONFIG);
221: sLevelCache.put("FINE", Level.FINE);
222: sLevelCache.put("FINER", Level.FINER);
223: sLevelCache.put("FINEST", Level.FINEST);
224: sLevelCache.put("OFF", Level.OFF);
225: sLevelCache.put("ALL", Level.ALL);
226: sDeploymentImplCache = new HashMap();
227: sDeploymentImplCache.put("SimpleImplementation",
228: "com.sun.jbi.binding.jms.deploy.RegistryCacheImpl");
229: sDeploymentImplCache.put("DEFAULT",
230: "com.sun.jbi.binding.jms.deploy.RegistryCacheImpl");
231: }
232:
233: /**
234: * Creates a new instance of Config.
235: *
236: * @param configFileName configuration file name. This file name should not
237: * be relative and should contain the absolute path.
238: */
239: public Config(String configFileName) {
240: mConfigFileName = configFileName;
241: mDeploymentProperties = new Properties();
242: mStringTranslator = JMSBindingContext.getInstance()
243: .getStringTranslator();
244: mLogger = JMSBindingContext.getInstance().getLogger();
245:
246: String schema = JMSBindingContext.getInstance().getContext()
247: .getInstallRoot()
248: + ConfigConstants.JMS_CONFIG_FILE_SCHEMA;
249: mValidator = new ConfigFileValidator(schema, mConfigFileName);
250: }
251:
252: /**
253: * Gets the configured deployment implementation class name. This value is
254: * extracted from the element
255: * <pre> <code>
256: * <jmsbc:jms-binding>
257: * <deployment>
258: * <impl>
259: * </impl>
260: * </deployment>
261: * </jmsbc:jms-binding>
262: * </code> </pre>
263: * present in the configuration file. Supported runtime deployment
264: * implementations
265: *
266: * <UL>
267: * <li>
268: * SimpleImplementation
269: * </li>
270: * </ul>.
271: *
272: *
273: * @return class name.
274: */
275: public String getDeploymentImplClass() {
276: return mDeploymentImplClass;
277: }
278:
279: /**
280: * Gets the properties configured for the deployment implementation. The
281: * values are extracted from the elements
282: * <pre> <code>
283: * <jmsbc:jms-binding>
284: * <deployment>
285: * <impl>
286: * <param>
287: * <paramname/>
288: * <paramvalue/>
289: * </param>*
290: * </impl>
291: * </deployment>
292: * </jmsbc:jms-binding>
293: * </code> </pre>.
294: *
295: * @return configured properties.
296: */
297: public Properties getDeploymentProperties() {
298: return mDeploymentProperties;
299: }
300:
301: /**
302: * Get the lookup for initial context factory.
303: *
304: * @return the String.
305: */
306: public String getInitialContextFactory() {
307: return mInitialContextFactory;
308: }
309:
310: /**
311: * Gets the configured application log level. This value is extracted from
312: * the element
313: * <pre> <code>
314: * <jmsbc:jms-binding>
315: * <loglevel/>
316: * </jmsbc:jms-binding>
317: * </code> </pre>
318: * present in the configuration file. The supported loglevel values are
319: *
320: * <UL>
321: * <li>
322: * SEVERE
323: * </li>
324: * <li>
325: * WARNING
326: * </li>
327: * <li>
328: * INFO
329: * </li>
330: * <li>
331: * CONFIG
332: * </li>
333: * <li>
334: * FINE
335: * </li>
336: * <li>
337: * FINER
338: * </li>
339: * <li>
340: * FINEST
341: * </li>
342: * <li>
343: * OFF
344: * </li>
345: * <li>
346: * ALL
347: * </li>
348: * </ul>.
349: *
350: *
351: * @return log level.
352: */
353: public Level getLogLevel() {
354: return mLogLevel;
355: }
356:
357: /**
358: * Gets the configured maximum thread count. This value is extracted from
359: * the element
360: * <pre> <code>
361: * <jmsbc:jms-binding>
362: * <threadpool>
363: * <maxthreads>
364: * </maxthreads>
365: * </threadpool>
366: * </jmsbc:jms-binding>
367: * </code> </pre>
368: * present in the configuration file.
369: *
370: * @return maximum thread count.
371: */
372: public int getMaxThreadCount() {
373: return mMaxThreads;
374: }
375:
376: /**
377: * Gets the configured minimum thread count. This value is extracted from
378: * the element
379: * <pre> <code>
380: * <jmsbc:jms-binding>
381: * <threadpool>
382: * <minthreads>
383: * </minthreads>
384: * </threadpool>
385: * </jmsbc:jms-binding>
386: * </code> </pre>
387: * present in the configuration file.
388: *
389: * @return minimum thread count.
390: */
391: public int getMinThreadCount() {
392: return mMinThreads;
393: }
394:
395: /**
396: * Gets the provider url.
397: *
398: * @return the provider url.
399: */
400: public String getProviderUrl() {
401: return mProviderUrl;
402: }
403:
404: /**
405: * Returns the receiver connection password.
406: *
407: * @return receiver connection password.
408: */
409: public String getReceiverConnectionPassword() {
410: return mReceiverConnectionPassword;
411: }
412:
413: /**
414: * Returns the receiver connection user.
415: *
416: * @return receiver connection user name.
417: */
418: public String getReceiverConnectionUser() {
419: return mReceiverConnectionUser;
420: }
421:
422: /**
423: * Returns the receiver destination.
424: *
425: * @return receiver destination.
426: */
427: public String getReceiverDestination() {
428: return mReceiverDestination;
429: }
430:
431: /**
432: * Returns the receiver factory.
433: *
434: * @return receiver factory.
435: */
436: public String getReceiverFactory() {
437: return mReceiverFactory;
438: }
439:
440: /**
441: * Get the SecurityConfiguration.
442: *
443: * @return the String.
444: */
445: public String getSecurityCredentials() {
446: return mSecurityCredentials;
447: }
448:
449: /**
450: * Get the Security level.
451: *
452: * @return the String.
453: */
454: public String getSecurityLevel() {
455: return mSecurityLevel;
456: }
457:
458: /**
459: * Get the SecurityConfiguration.
460: *
461: * @return the SecurityConfig instance.
462: */
463: public String getSecurityPrincipal() {
464: return mSecurityPrincipal;
465: }
466:
467: /**
468: * Reads data from the configuration object and initializes itself.
469: *
470: * @param componentId is the ComponentId obtained from the JBI Context.
471: *
472: * @throws JBIException - when the object cannot be initialized.
473: */
474: public void loadConfiguration(String componentId)
475: throws JBIException {
476: clear();
477:
478: mValidator.setValidating();
479: mValidator.validate();
480:
481: Document d = mValidator.getDocument();
482:
483: if ((!mValidator.isValid()) || (d == null)) {
484: mLogger.severe(mStringTranslator.getString(
485: JMS_INVALID_INSTALL_CONFIG_FILE, mConfigFileName));
486: setError(mStringTranslator.getString(
487: JMS_INVALID_CONFIG_FILE, mConfigFileName)
488: + " " + mValidator.getError());
489:
490: return;
491: }
492:
493: XMLReader reader = new XMLReader();
494: reader.init(d);
495: mLogLevel = getLevel(reader
496: .getConfigurationValue("/jmsbc:jms-binding/log-level"));
497:
498: ConfigElement configElement = reader
499: .getConfigurationElement("/jmsbc:jms-binding/thread-pool");
500:
501: mMinThreads = Integer.parseInt(configElement
502: .getProperty("min-threads"));
503: mMaxThreads = Integer.parseInt(configElement
504: .getProperty("max-threads"));
505:
506: String deploymentImpl = reader
507: .getConfigurationValue("/jmsbc:jms-binding/deployment/impl-class");
508: mDeploymentImplClass = (String) sDeploymentImplCache
509: .get(deploymentImpl);
510:
511: if (mDeploymentImplClass == null) {
512: // using default implementation
513: mDeploymentImplClass = (String) sDeploymentImplCache
514: .get("DEFAULT");
515: }
516:
517: Collection deploymentPropertyList = reader
518: .getConfigurationElementList("/jmsbc:jms-binding/deployment/param");
519:
520: for (Iterator iter = deploymentPropertyList.iterator(); iter
521: .hasNext();) {
522: ConfigElement propertyElement = (ConfigElement) iter.next();
523: mDeploymentProperties.setProperty(propertyElement
524: .getProperty("param-name"), propertyElement
525: .getProperty("param-value"));
526: }
527:
528: validateProperties();
529:
530: if (!isValid()) {
531: setError(mStringTranslator
532: .getString(JMS_INVALID_DEPLOYMENT_PROPERTIES));
533:
534: return;
535: }
536:
537: mDeploymentImplClass = (String) sDeploymentImplCache
538: .get("DEFAULT");
539: mInitialContextFactory = reader
540: .getConfigurationValue("/jmsbc:jms-binding/initial-context-factory");
541: mProviderUrl = reader
542: .getConfigurationValue("/jmsbc:jms-binding/provider-url");
543:
544: mSecurityPrincipal = reader
545: .getConfigurationValue("/jmsbc:jms-binding/security-principal");
546: mSecurityCredentials = reader
547: .getConfigurationValue("/jmsbc:jms-binding/security-credentials");
548: mSecurityLevel = reader
549: .getConfigurationValue("/jmsbc:jms-binding/security-level");
550:
551: try {
552: mStopTimeout = Long
553: .parseLong(reader
554: .getConfigurationValue("/jmsbc:jms-binding/stop-timeout"));
555: } catch (NumberFormatException ne) {
556: mStopTimeout = JMSConstants.DEFAULT_STOP_TIMEOUT;
557: }
558:
559: mReceiverDestination = reader
560: .getConfigurationValue("/jmsbc:jms-binding/temporary-receiver/destination-name");
561: mReceiverFactory = reader
562: .getConfigurationValue("/jmsbc:jms-binding/temporary-receiver/connection-factory");
563:
564: mReceiverConnectionUser = reader
565: .getConfigurationValue("/jmsbc:jms-binding/temporary-receiver/connection-user-name");
566:
567: mReceiverConnectionPassword = reader
568: .getConfigurationValue("/jmsbc:jms-binding/temporary-receiver/connection-password");
569:
570: printDetails();
571: }
572:
573: /**
574: * Prints the configuration details.
575: */
576: public void printDetails() {
577: mLogger.fine("Log level " + mLogLevel.getName());
578: mLogger.fine("Min threads " + new Integer(mMinThreads));
579: mLogger.fine("Max threads " + new Integer(mMaxThreads));
580: mLogger.fine("Deployment Class " + mDeploymentImplClass);
581: mLogger
582: .fine("Deploy props "
583: + mDeploymentProperties.toString());
584: mLogger.fine("Initial context factory "
585: + mInitialContextFactory);
586: mLogger.fine("Provider URL " + mProviderUrl);
587: mLogger.fine("Receiver destination " + mReceiverDestination);
588: mLogger
589: .fine("Receiver destination Factory "
590: + mReceiverFactory);
591: mLogger.fine("Receiver connection user "
592: + mReceiverConnectionUser);
593: mLogger.fine("Receiver connection password"
594: + mReceiverConnectionPassword);
595: }
596:
597: /**
598: * Clears the configuration object details.
599: */
600: protected void clearAll() {
601: mLogLevel = JMSConstants.DEFAULT_LOG_LEVEL;
602: mDeploymentProperties.clear();
603: mDeploymentImplClass = null;
604: mMinThreads = JMSConstants.DEFAULT_MIN_THREADS;
605: mMaxThreads = JMSConstants.DEFAULT_MAX_THREADS;
606: }
607:
608: /**
609: * Gets the java.util.logging.Level associated with the configured string
610: * name. If no level exists for the provided level name, it returns the
611: * default level.
612: *
613: * @param levelName - string identifying the level.
614: *
615: * @return associated level; default level if the levelName is incorrect.
616: */
617: private Level getLevel(String levelName) {
618: Level logLevel;
619:
620: if (levelName == null) {
621: mLogger.warning(mStringTranslator.getString(
622: JMS_INVALID_LOG_LEVEL, mLogLevel));
623: logLevel = JMSConstants.DEFAULT_LOG_LEVEL;
624: } else {
625: logLevel = (Level) sLevelCache.get(levelName);
626:
627: if (logLevel == null) {
628: mLogger.warning(mStringTranslator.getString(
629: JMS_INVALID_LOG_LEVEL, mLogLevel));
630: logLevel = JMSConstants.DEFAULT_LOG_LEVEL;
631: }
632: }
633:
634: return logLevel;
635: }
636:
637: /**
638: * Validates all the properties.
639: */
640: private void validateProperties() {
641: String filename = mDeploymentProperties
642: .getProperty(JMSConstants.REGISTRY_FILE_NAME);
643: String filelocation = mDeploymentProperties
644: .getProperty(JMSConstants.REGISTRY_FILE_LOCATION);
645:
646: if ((filename == null) || (filename.trim().equals(""))) {
647: setError(mStringTranslator.getString(
648: JMS_REG_FILE_NOT_FOUND, filename));
649:
650: return;
651: }
652:
653: if ((filelocation == null) || (filelocation.trim().equals(""))) {
654: setError(mStringTranslator.getString(
655: JMS_REG_FILE_NOT_FOUND, filelocation));
656:
657: return;
658: }
659:
660: String installroot = JMSBindingContext.getInstance()
661: .getContext().getInstallRoot();
662:
663: String filepath = installroot + File.separator + filelocation
664: + File.separator + filename;
665:
666: File f = new File(filepath);
667:
668: if (f == null) {
669: setError(mStringTranslator.getString(
670: JMS_REG_FILE_NOT_FOUND, filepath));
671:
672: return;
673: }
674:
675: if (!f.exists()) {
676: setError(mStringTranslator.getString(
677: JMS_REG_FILE_NOT_FOUND, filepath));
678:
679: return;
680: }
681:
682: if (!f.canWrite()) {
683: setError(mStringTranslator.getString(
684: JMS_REG_FILE_NOT_WRITABLE, filepath));
685: }
686:
687: if (!f.canRead()) {
688: setError(mStringTranslator.getString(
689: JMS_REG_FILE_NOT_READABLE, filepath));
690: }
691: }
692: }
|