1 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 ; 83 import java.util.Iterator ; 84 import java.util.Map ; 85 import java.util.StringTokenizer ; 86 87 88 94 public class ComponentConfigBridge { 95 98 private static final Logger log = Logger.getLogger(ComponentConfigBridge.class); 99 100 103 public ComponentConfigBridge() { 104 } 105 106 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 path = lc.getPath(changedComponent); 126 if (path == null || path.length() == 0) { 127 currentConfig.updateConfig(newConfiguration); 128 } else { 129 ComponentConfig currentLevel = currentConfig; 130 StringTokenizer stok = new StringTokenizer (path, "."); 131 132 while (stok.hasMoreTokens()) { 135 String 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 currentLevel.updateConfig(newConfiguration); 145 } 146 } 147 } 148 149 150 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 properties = metadata.getProperties(); 168 DefaultConfigBean targetConfig = new DefaultConfigBean(); 169 170 for (Iterator 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 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 215 private void getSimpleProperties(ExpressoComponent sourceComponent, 216 ComponentMetadata metadata, ComponentConfig targetConfig) 217 throws ConfigurationException { 218 Map properties = metadata.getProperties(); 219 220 for (Iterator i = properties.keySet().iterator(); i.hasNext();) { 221 Property property = (Property) i.next(); 222 String 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 propertyName = property.getName(); 228 229 try { 230 Object propertyValue = PropertyUtils.getProperty(sourceComponent, 231 propertyName); 232 String stringValue = ConvertUtils.convert(propertyValue); 233 targetConfig.setProperty(propertyName, stringValue); 234 } catch (IllegalAccessException 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 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 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 propertyName = property.getName(); 252 Map allProperties = mappedProperty.getValues(); 253 254 try { 255 for (Iterator j = allProperties.keySet().iterator(); 256 j.hasNext();) { 257 String oneKey = (String ) j.next(); 258 Object propertyValue = PropertyUtils.getMappedProperty(sourceComponent, 259 propertyName, oneKey); 260 String stringValue = ConvertUtils.convert(propertyValue); 261 targetConfig.setMappedProperty(propertyName, 262 oneKey, stringValue); 263 } 264 } catch (IllegalAccessException 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 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 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 propertyName = property.getName(); 282 Map allProperties = indexedProperty.getValues(); 283 284 try { 285 for (Iterator j = allProperties.keySet().iterator(); 286 j.hasNext();) { 287 Integer oneKey = (Integer ) j.next(); 288 Object propertyValue = PropertyUtils.getIndexedProperty(sourceComponent, 289 propertyName, oneKey.intValue()); 290 String stringValue = ConvertUtils.convert(propertyValue); 291 targetConfig.setIndexedProperty(propertyName, 292 oneKey.intValue(), stringValue); 293 } 294 } catch (IllegalAccessException 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 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 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 |