001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.integration.dna;
027:
028: import org.jicarilla.container.Resolver;
029: import org.jicarilla.collections.NodeUtil;
030: import org.jcontainer.dna.impl.ContainerUtil;
031: import org.jcontainer.dna.impl.ConsoleLogger;
032: import org.jcontainer.dna.impl.ConfigurationUtil;
033: import org.jcontainer.dna.impl.DefaultParameters;
034: import org.jcontainer.dna.Logger;
035: import org.jcontainer.dna.ResourceLocator;
036: import org.jcontainer.dna.Configurable;
037: import org.jcontainer.dna.Parameterizable;
038: import org.jcontainer.dna.Configuration;
039: import org.jcontainer.dna.Parameters;
040: import org.jcontainer.dna.ConfigurationException;
041: import org.xml.sax.InputSource;
042:
043: import java.io.Reader;
044: import java.io.StringReader;
045: import java.util.Properties;
046: import java.util.Iterator;
047:
048: import com.thoughtworks.xstream.XStream;
049:
050: /**
051: *
052: *
053: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
054: * @version $Id: DnaUtil.java,v 1.1 2004/04/03 12:09:21 lsimons Exp $
055: */
056: public class DnaUtil {
057: public final static String CONFIGURATION_POSTFIX = "Configuration";
058:
059: public final static ConsoleLogger FallbackLogger = new ConsoleLogger(
060: ConsoleLogger.LEVEL_WARN);
061:
062: public static void startDnaLifecycle(final Object instance,
063: final Resolver container) throws InstantiationException {
064: try {
065: ContainerUtil.enableLogging(instance, getLogger(container,
066: instance.getClass().getName()));
067: ContainerUtil.compose(instance,
068: getResourceLocator(container));
069:
070: if (instance instanceof Configurable
071: || instance instanceof Parameterizable) {
072: final Configuration configuration = getConfiguration(
073: container, instance.getClass().getName());
074: ContainerUtil.configure(instance, configuration);
075:
076: if (instance instanceof Parameterizable) {
077: Parameters parameters = configurationToParameters(configuration);
078: ContainerUtil.parameterize(instance, parameters);
079: }
080: }
081: ContainerUtil.initialize(instance);
082: } catch (Exception e) {
083: final InstantiationException ex = new InstantiationException();
084: ex.initCause(e);
085: throw ex;
086: }
087: }
088:
089: protected static Logger getLogger(final Resolver container,
090: final String fallbackLoggerName) {
091: Logger logger = null;
092:
093: logger = (Logger) container.get(Logger.class);
094:
095: if (logger == null)
096: logger = FallbackLogger.getChildLogger(fallbackLoggerName);
097:
098: return logger;
099: }
100:
101: protected static ResourceLocator getResourceLocator(
102: final Resolver container) {
103: return new JicarillaBasedDnaResourceLocator(container);
104: }
105:
106: public static Configuration getConfiguration(
107: final Resolver container, final String className)
108: throws Exception {
109: final Object obj = container.get(className
110: + CONFIGURATION_POSTFIX);
111:
112: if (obj == null)
113: throw new ConfigurationException(
114: "No configuration can be found for '" + className
115: + "'", null, null);
116:
117: if (obj instanceof Configuration)
118: return (Configuration) obj;
119:
120: return beanToConfiguration(obj);
121: }
122:
123: protected static Configuration beanToConfiguration(final Object obj)
124: throws Exception {
125: final XStream xstream = new XStream();
126: final String configString = xstream.toXML(obj);
127:
128: final Reader reader = new StringReader(configString);
129: final InputSource source = new InputSource(reader);
130:
131: final Configuration configuration = ConfigurationUtil
132: .buildFromXML(source);
133:
134: return configuration;
135: }
136:
137: protected static Parameters configurationToParameters(
138: final Configuration configuration) {
139: final Properties properties = NodeUtil
140: .toProperties(NodeUtil.fromElement(ConfigurationUtil
141: .toElement(configuration)));
142: final DefaultParameters parameters = new DefaultParameters();
143: Iterator it = properties.keySet().iterator();
144: while (it.hasNext()) {
145: Object key = it.next();
146: if (key == null)
147: continue;
148: String keyString = key.toString();
149: parameters.setParameter(keyString, properties
150: .getProperty(keyString));
151: }
152: parameters.makeReadOnly();
153: return parameters;
154: }
155: }
|