KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > management > ComponentConfigBridge


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.kernel.management;
66
67 import com.jcorporate.expresso.kernel.ComponentLifecycle;
68 import com.jcorporate.expresso.kernel.ExpressoComponent;
69 import com.jcorporate.expresso.kernel.digester.ComponentConfig;
70 import com.jcorporate.expresso.kernel.digester.ExpressoServicesConfig;
71 import com.jcorporate.expresso.kernel.exception.ConfigurationException;
72 import com.jcorporate.expresso.kernel.internal.DefaultConfigBean;
73 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata;
74 import com.jcorporate.expresso.kernel.metadata.IndexedProperty;
75 import com.jcorporate.expresso.kernel.metadata.MappedProperty;
76 import com.jcorporate.expresso.kernel.metadata.Property;
77 import com.jcorporate.expresso.kernel.util.LocatorUtils;
78 import org.apache.commons.beanutils.ConvertUtils;
79 import org.apache.commons.beanutils.PropertyUtils;
80 import org.apache.log4j.Logger;
81
82 import java.lang.reflect.InvocationTargetException JavaDoc;
83 import java.util.Iterator JavaDoc;
84 import java.util.Map JavaDoc;
85 import java.util.StringTokenizer JavaDoc;
86
87
88 /**
89  * Provides getting and setting of configuration information for individual components
90  *
91  * @author Michael Rimov
92  * @version $Revision: 1.4 $ on $Date: 2004/11/17 20:48:17 $
93  */

94 public class ComponentConfigBridge {
95     /**
96      * The logger.
97      */

98     private static final Logger log = Logger.getLogger(ComponentConfigBridge.class);
99
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(ExpressoComponent changedComponent,
118                                           ExpressoServicesConfig allServicesConfiguration,
119                                           ComponentConfig newConfiguration) throws ConfigurationException {
120
121         synchronized (ComponentConfigBridge.class) {
122             ComponentConfig currentConfig = allServicesConfiguration.getRootConfig();
123
124             LocatorUtils lc = new LocatorUtils(changedComponent);
125             String JavaDoc path = lc.getPath(changedComponent);
126             if (path == null || path.length() == 0) {
127                 currentConfig.updateConfig(newConfiguration);
128             } else {
129                 ComponentConfig currentLevel = currentConfig;
130                 StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(path, ".");
131
132                 //Iterate through the child components until we run out of items, at
133
//which point, we're done.
134
while (stok.hasMoreTokens()) {
135                     String JavaDoc subComponent = stok.nextToken();
136                     currentLevel = currentLevel.getChildComponent(subComponent);
137                     if (currentLevel == null) {
138                         throw new ConfigurationException("Unable to find subcomponent: "
139                                 + subComponent + " for path: " + path);
140                     }
141                 }
142
143                 //By this time we're where we want to be
144
currentLevel.updateConfig(newConfiguration);
145             }
146         }
147     }
148
149
150     /**
151      * Resets the configuration for a component. It is intended for this
152      * function to be used at Runtime. It locks the entire
153      * ComponentConfigBridge class so that only one component configuration by
154      * one administrator can take place at a time.
155      *
156      * @param targetComponent the Component to configure
157      * @param newConfiguration the new configuration information to set for the
158      * component
159      * @throws ConfigurationException if there is an error configuring the
160      * component.
161      */

162     public void setConfiguration(ExpressoComponent targetComponent,
163                                  ComponentConfig newConfiguration) throws ConfigurationException {
164         synchronized (ComponentConfigBridge.class) {
165             if (targetComponent instanceof ComponentLifecycle) {
166                 ComponentMetadata metadata = targetComponent.getMetaData();
167                 Map JavaDoc properties = metadata.getProperties();
168                 DefaultConfigBean targetConfig = new DefaultConfigBean();
169
170                 for (Iterator JavaDoc j = properties.values().iterator(); j.hasNext();) {
171                     Property p = (Property) j.next();
172                     p.createConfigBean(targetConfig, newConfiguration, metadata);
173                 }
174
175                 ((ComponentLifecycle) targetComponent).reconfigure(targetConfig);
176             }
177         }
178     }
179
180     /**
181      * Given a component, retrieve the component config object for this
182      * component. It is intended for this function to be used at Runtime (as
183      * opposed to startup time). It locks the entire ComponentConfigBridge
184      * class so that only one component configuration by one administrator can
185      * take place at a time.
186      * <p/>
187      * <p/>
188      * This function only retrieves the configuration of the target
189      * configuration, not its children.
190      * </p>
191      *
192      * @param sourceComponent the Component to retrieve the configuration for.
193      * @return a filled out ComponentConfig object for the given target
194      * component
195      * @throws ConfigurationException upon error
196      */

197     public ComponentConfig getConfiguration(ExpressoComponent sourceComponent)
198             throws ConfigurationException {
199         synchronized (ComponentConfigBridge.class) {
200             ComponentMetadata metadata = sourceComponent.getMetaData();
201             ComponentConfig newConfig = new ComponentConfig();
202
203             return null;
204         }
205     }
206
207     /**
208      * Put the simple properties in the expresso component into the target
209      * configuration converting them to strings as appropriate.
210      *
211      * @param sourceComponent the component to retrieve data from
212      * @param metadata the metadata for the component
213      * @param targetConfig the configuration for the component
214      */

215     private void getSimpleProperties(ExpressoComponent sourceComponent,
216                                      ComponentMetadata metadata, ComponentConfig targetConfig)
217             throws ConfigurationException {
218         Map JavaDoc properties = metadata.getProperties();
219
220         for (Iterator JavaDoc i = properties.keySet().iterator(); i.hasNext();) {
221             Property property = (Property) i.next();
222             String JavaDoc access = property.getAccess();
223
224             if ("readwrite".equalsIgnoreCase(access) ||
225                     "rw".equalsIgnoreCase(access)) {
226                 if (property instanceof com.jcorporate.expresso.kernel.metadata.SimpleProperty) {
227                     String JavaDoc propertyName = property.getName();
228
229                     try {
230                         Object JavaDoc propertyValue = PropertyUtils.getProperty(sourceComponent,
231                                 propertyName);
232                         String JavaDoc stringValue = ConvertUtils.convert(propertyValue);
233                         targetConfig.setProperty(propertyName, stringValue);
234                     } catch (IllegalAccessException JavaDoc ex) {
235                         log.error("Error getting simple property ", ex);
236                         throw new ConfigurationException("Property " +
237                                 propertyName +
238                                 " specified in metadata was not accessible. Must be 'public'",
239                                 ex);
240                     } catch (InvocationTargetException JavaDoc ex) {
241                         log.error("Error getting simple property ", ex);
242                         throw new ConfigurationException("Unable to get property specified in metadata: " +
243                                 propertyName, ex);
244                     } catch (NoSuchMethodException JavaDoc ex) {
245                         log.error("Error getting simple property ", ex);
246                         throw new ConfigurationException("Getter method for property " + propertyName +
247                                 " specified in metadata does not exist", ex);
248                     }
249                 } else if (property instanceof com.jcorporate.expresso.kernel.metadata.MappedProperty) {
250                     MappedProperty mappedProperty = (MappedProperty) property;
251                     String JavaDoc propertyName = property.getName();
252                     Map JavaDoc allProperties = mappedProperty.getValues();
253
254                     try {
255                         for (Iterator JavaDoc j = allProperties.keySet().iterator();
256                              j.hasNext();) {
257                             String JavaDoc oneKey = (String JavaDoc) j.next();
258                             Object JavaDoc propertyValue = PropertyUtils.getMappedProperty(sourceComponent,
259                                     propertyName, oneKey);
260                             String JavaDoc stringValue = ConvertUtils.convert(propertyValue);
261                             targetConfig.setMappedProperty(propertyName,
262                                     oneKey, stringValue);
263                         }
264                     } catch (IllegalAccessException JavaDoc ex) {
265                         log.error("Error getting simple property ", ex);
266                         throw new ConfigurationException("Property " +
267                                 propertyName +
268                                 " specified in metadata was not accessible. Must be 'public'",
269                                 ex);
270                     } catch (InvocationTargetException JavaDoc ex) {
271                         log.error("Error getting simple property ", ex);
272                         throw new ConfigurationException("Unable to get property specified in metadata: " +
273                                 propertyName, ex);
274                     } catch (NoSuchMethodException JavaDoc ex) {
275                         log.error("Error getting simple property ", ex);
276                         throw new ConfigurationException("Getter method for property " + propertyName +
277                                 " specified in metadata does not exist", ex);
278                     }
279                 } else if (property instanceof com.jcorporate.expresso.kernel.metadata.IndexedProperty) {
280                     IndexedProperty indexedProperty = (IndexedProperty) property;
281                     String JavaDoc propertyName = property.getName();
282                     Map JavaDoc allProperties = indexedProperty.getValues();
283
284                     try {
285                         for (Iterator JavaDoc j = allProperties.keySet().iterator();
286                              j.hasNext();) {
287                             Integer JavaDoc oneKey = (Integer JavaDoc) j.next();
288                             Object JavaDoc propertyValue = PropertyUtils.getIndexedProperty(sourceComponent,
289                                     propertyName, oneKey.intValue());
290                             String JavaDoc stringValue = ConvertUtils.convert(propertyValue);
291                             targetConfig.setIndexedProperty(propertyName,
292                                     oneKey.intValue(), stringValue);
293                         }
294                     } catch (IllegalAccessException JavaDoc ex) {
295                         log.error("Error getting simple property ", ex);
296                         throw new ConfigurationException("Property " +
297                                 propertyName +
298                                 " specified in metadata was not accessible. Must be 'public'",
299                                 ex);
300                     } catch (InvocationTargetException JavaDoc ex) {
301                         log.error("Error getting simple property ", ex);
302                         throw new ConfigurationException("Unable to get property specified in metadata: " +
303                                 propertyName, ex);
304                     } catch (NoSuchMethodException JavaDoc ex) {
305                         log.error("Error getting simple property ", ex);
306                         throw new ConfigurationException("Getter method for property " + propertyName +
307                                 " specified in metadata does not exist", ex);
308                     }
309                 }
310             }
311         }
312     }
313 }
314
Popular Tags