001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.kernel.management;
066:
067: import com.jcorporate.expresso.kernel.ComponentLifecycle;
068: import com.jcorporate.expresso.kernel.ExpressoComponent;
069: import com.jcorporate.expresso.kernel.digester.ComponentConfig;
070: import com.jcorporate.expresso.kernel.digester.ExpressoServicesConfig;
071: import com.jcorporate.expresso.kernel.exception.ConfigurationException;
072: import com.jcorporate.expresso.kernel.internal.DefaultConfigBean;
073: import com.jcorporate.expresso.kernel.metadata.ComponentMetadata;
074: import com.jcorporate.expresso.kernel.metadata.IndexedProperty;
075: import com.jcorporate.expresso.kernel.metadata.MappedProperty;
076: import com.jcorporate.expresso.kernel.metadata.Property;
077: import com.jcorporate.expresso.kernel.util.LocatorUtils;
078: import org.apache.commons.beanutils.ConvertUtils;
079: import org.apache.commons.beanutils.PropertyUtils;
080: import org.apache.log4j.Logger;
081:
082: import java.lang.reflect.InvocationTargetException;
083: import java.util.Iterator;
084: import java.util.Map;
085: import java.util.StringTokenizer;
086:
087: /**
088: * Provides getting and setting of configuration information for individual components
089: *
090: * @author Michael Rimov
091: * @version $Revision: 1.4 $ on $Date: 2004/11/17 20:48:17 $
092: */
093: public class ComponentConfigBridge {
094: /**
095: * The logger.
096: */
097: private static final Logger log = Logger
098: .getLogger(ComponentConfigBridge.class);
099:
100: /**
101: * Creates a new ComponentConfigBridge object.
102: */
103: public ComponentConfigBridge() {
104: }
105:
106: /**
107: * Updates the system configuration to that of the component that has
108: * been changed
109: *
110: * @param changedComponent The changed component to be updated
111: * @param allServicesConfiguration The Complete Expresso Services Config
112: * usually obtained from the RootContainerInterface
113: * @param newConfiguration the new Configuration of the component. This can
114: * be obtained from this objects getConfiguration() method
115: * @throws ConfigurationException upon error.
116: */
117: public void updateSystemConfiguration(
118: ExpressoComponent changedComponent,
119: ExpressoServicesConfig allServicesConfiguration,
120: ComponentConfig newConfiguration)
121: throws ConfigurationException {
122:
123: synchronized (ComponentConfigBridge.class) {
124: ComponentConfig currentConfig = allServicesConfiguration
125: .getRootConfig();
126:
127: LocatorUtils lc = new LocatorUtils(changedComponent);
128: String path = lc.getPath(changedComponent);
129: if (path == null || path.length() == 0) {
130: currentConfig.updateConfig(newConfiguration);
131: } else {
132: ComponentConfig currentLevel = currentConfig;
133: StringTokenizer stok = new StringTokenizer(path, ".");
134:
135: //Iterate through the child components until we run out of items, at
136: //which point, we're done.
137: while (stok.hasMoreTokens()) {
138: String subComponent = stok.nextToken();
139: currentLevel = currentLevel
140: .getChildComponent(subComponent);
141: if (currentLevel == null) {
142: throw new ConfigurationException(
143: "Unable to find subcomponent: "
144: + subComponent + " for path: "
145: + path);
146: }
147: }
148:
149: //By this time we're where we want to be
150: currentLevel.updateConfig(newConfiguration);
151: }
152: }
153: }
154:
155: /**
156: * Resets the configuration for a component. It is intended for this
157: * function to be used at Runtime. It locks the entire
158: * ComponentConfigBridge class so that only one component configuration by
159: * one administrator can take place at a time.
160: *
161: * @param targetComponent the Component to configure
162: * @param newConfiguration the new configuration information to set for the
163: * component
164: * @throws ConfigurationException if there is an error configuring the
165: * component.
166: */
167: public void setConfiguration(ExpressoComponent targetComponent,
168: ComponentConfig newConfiguration)
169: throws ConfigurationException {
170: synchronized (ComponentConfigBridge.class) {
171: if (targetComponent instanceof ComponentLifecycle) {
172: ComponentMetadata metadata = targetComponent
173: .getMetaData();
174: Map properties = metadata.getProperties();
175: DefaultConfigBean targetConfig = new DefaultConfigBean();
176:
177: for (Iterator j = properties.values().iterator(); j
178: .hasNext();) {
179: Property p = (Property) j.next();
180: p.createConfigBean(targetConfig, newConfiguration,
181: metadata);
182: }
183:
184: ((ComponentLifecycle) targetComponent)
185: .reconfigure(targetConfig);
186: }
187: }
188: }
189:
190: /**
191: * Given a component, retrieve the component config object for this
192: * component. It is intended for this function to be used at Runtime (as
193: * opposed to startup time). It locks the entire ComponentConfigBridge
194: * class so that only one component configuration by one administrator can
195: * take place at a time.
196: * <p/>
197: * <p/>
198: * This function only retrieves the configuration of the target
199: * configuration, not its children.
200: * </p>
201: *
202: * @param sourceComponent the Component to retrieve the configuration for.
203: * @return a filled out ComponentConfig object for the given target
204: * component
205: * @throws ConfigurationException upon error
206: */
207: public ComponentConfig getConfiguration(
208: ExpressoComponent sourceComponent)
209: throws ConfigurationException {
210: synchronized (ComponentConfigBridge.class) {
211: ComponentMetadata metadata = sourceComponent.getMetaData();
212: ComponentConfig newConfig = new ComponentConfig();
213:
214: return null;
215: }
216: }
217:
218: /**
219: * Put the simple properties in the expresso component into the target
220: * configuration converting them to strings as appropriate.
221: *
222: * @param sourceComponent the component to retrieve data from
223: * @param metadata the metadata for the component
224: * @param targetConfig the configuration for the component
225: */
226: private void getSimpleProperties(ExpressoComponent sourceComponent,
227: ComponentMetadata metadata, ComponentConfig targetConfig)
228: throws ConfigurationException {
229: Map properties = metadata.getProperties();
230:
231: for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
232: Property property = (Property) i.next();
233: String access = property.getAccess();
234:
235: if ("readwrite".equalsIgnoreCase(access)
236: || "rw".equalsIgnoreCase(access)) {
237: if (property instanceof com.jcorporate.expresso.kernel.metadata.SimpleProperty) {
238: String propertyName = property.getName();
239:
240: try {
241: Object propertyValue = PropertyUtils
242: .getProperty(sourceComponent,
243: propertyName);
244: String stringValue = ConvertUtils
245: .convert(propertyValue);
246: targetConfig.setProperty(propertyName,
247: stringValue);
248: } catch (IllegalAccessException ex) {
249: log.error("Error getting simple property ", ex);
250: throw new ConfigurationException(
251: "Property "
252: + propertyName
253: + " specified in metadata was not accessible. Must be 'public'",
254: ex);
255: } catch (InvocationTargetException ex) {
256: log.error("Error getting simple property ", ex);
257: throw new ConfigurationException(
258: "Unable to get property specified in metadata: "
259: + propertyName, ex);
260: } catch (NoSuchMethodException ex) {
261: log.error("Error getting simple property ", ex);
262: throw new ConfigurationException(
263: "Getter method for property "
264: + propertyName
265: + " specified in metadata does not exist",
266: ex);
267: }
268: } else if (property instanceof com.jcorporate.expresso.kernel.metadata.MappedProperty) {
269: MappedProperty mappedProperty = (MappedProperty) property;
270: String propertyName = property.getName();
271: Map allProperties = mappedProperty.getValues();
272:
273: try {
274: for (Iterator j = allProperties.keySet()
275: .iterator(); j.hasNext();) {
276: String oneKey = (String) j.next();
277: Object propertyValue = PropertyUtils
278: .getMappedProperty(sourceComponent,
279: propertyName, oneKey);
280: String stringValue = ConvertUtils
281: .convert(propertyValue);
282: targetConfig.setMappedProperty(
283: propertyName, oneKey, stringValue);
284: }
285: } catch (IllegalAccessException ex) {
286: log.error("Error getting simple property ", ex);
287: throw new ConfigurationException(
288: "Property "
289: + propertyName
290: + " specified in metadata was not accessible. Must be 'public'",
291: ex);
292: } catch (InvocationTargetException ex) {
293: log.error("Error getting simple property ", ex);
294: throw new ConfigurationException(
295: "Unable to get property specified in metadata: "
296: + propertyName, ex);
297: } catch (NoSuchMethodException ex) {
298: log.error("Error getting simple property ", ex);
299: throw new ConfigurationException(
300: "Getter method for property "
301: + propertyName
302: + " specified in metadata does not exist",
303: ex);
304: }
305: } else if (property instanceof com.jcorporate.expresso.kernel.metadata.IndexedProperty) {
306: IndexedProperty indexedProperty = (IndexedProperty) property;
307: String propertyName = property.getName();
308: Map allProperties = indexedProperty.getValues();
309:
310: try {
311: for (Iterator j = allProperties.keySet()
312: .iterator(); j.hasNext();) {
313: Integer oneKey = (Integer) j.next();
314: Object propertyValue = PropertyUtils
315: .getIndexedProperty(
316: sourceComponent,
317: propertyName, oneKey
318: .intValue());
319: String stringValue = ConvertUtils
320: .convert(propertyValue);
321: targetConfig.setIndexedProperty(
322: propertyName, oneKey.intValue(),
323: stringValue);
324: }
325: } catch (IllegalAccessException ex) {
326: log.error("Error getting simple property ", ex);
327: throw new ConfigurationException(
328: "Property "
329: + propertyName
330: + " specified in metadata was not accessible. Must be 'public'",
331: ex);
332: } catch (InvocationTargetException ex) {
333: log.error("Error getting simple property ", ex);
334: throw new ConfigurationException(
335: "Unable to get property specified in metadata: "
336: + propertyName, ex);
337: } catch (NoSuchMethodException ex) {
338: log.error("Error getting simple property ", ex);
339: throw new ConfigurationException(
340: "Getter method for property "
341: + propertyName
342: + " specified in metadata does not exist",
343: ex);
344: }
345: }
346: }
347: }
348: }
349: }
|