1 package org.objectweb.celtix.configuration.impl; 2 3 4 5 import java.lang.reflect.InvocationTargetException ; 6 import java.lang.reflect.Method ; 7 import java.util.List ; 8 import java.util.ResourceBundle ; 9 import java.util.Vector ; 10 import java.util.logging.Level ; 11 import java.util.logging.Logger ; 12 13 import javax.xml.namespace.QName ; 14 15 import org.objectweb.celtix.common.i18n.BundleUtils; 16 import org.objectweb.celtix.common.i18n.Message; 17 import org.objectweb.celtix.common.logging.LogUtils; 18 import org.objectweb.celtix.configuration.Configuration; 19 import org.objectweb.celtix.configuration.ConfigurationException; 20 import org.objectweb.celtix.configuration.ConfigurationItemMetadata; 21 import org.objectweb.celtix.configuration.ConfigurationMetadata; 22 import org.objectweb.celtix.configuration.ConfigurationProvider; 23 import org.objectweb.celtix.configuration.Configurator; 24 25 public class AbstractConfigurationImpl implements Configuration { 26 27 private static final Logger LOG = LogUtils.getL7dLogger(AbstractConfigurationImpl.class); 28 private static final ResourceBundle BUNDLE = BundleUtils.getBundle(AbstractConfigurationImpl.class); 29 private Configurator configurator; 30 private ConfigurationMetadata model; 31 private String id; 32 private List <ConfigurationProvider> providers; 33 34 public AbstractConfigurationImpl(ConfigurationMetadata m, String instanceId, Configuration parent) { 35 model = m; 36 id = instanceId; 37 configurator = new ConfiguratorImpl(this, parent instanceof AbstractConfigurationImpl 38 ? (AbstractConfigurationImpl)parent 39 : null); 40 41 providers = new Vector <ConfigurationProvider>(); 42 43 46 DefaultConfigurationProviderFactory factory = DefaultConfigurationProviderFactory.getInstance(); 47 ConfigurationProvider defaultProvider = factory.createDefaultProvider(this); 48 49 if (null != defaultProvider) { 50 providers.add(defaultProvider); 51 } 52 } 53 54 public Object getId() { 55 return id; 56 } 57 58 public Configuration getParent() { 59 if (null != configurator.getHook()) { 60 return configurator.getHook().getConfiguration(); 61 } 62 return null; 63 } 64 65 public Configuration getChild(String namespaceURI, Object childId) { 66 for (Configurator c : configurator.getClients()) { 67 if (namespaceURI.equals(c.getConfiguration().getModel().getNamespaceURI()) 68 && childId.equals(c.getConfiguration().getId())) { 69 return c.getConfiguration(); 70 } 71 } 72 if (LOG.isLoggable(Level.FINE)) { 73 LOG.fine("Could not find child configuration with id: " + childId); 74 } 75 return null; 76 } 77 78 public ConfigurationMetadata getModel() { 79 return model; 80 } 81 82 public List <ConfigurationProvider> getProviders() { 83 return providers; 84 } 85 86 public void setProviders(List <ConfigurationProvider> p) { 87 providers = p; 88 } 89 90 public <T> T getObject(Class <T> cls, String name) { 91 Object obj = getObject(name); 92 return cls.cast(obj); 93 } 94 95 public Object getObject(String name) { 96 97 ConfigurationItemMetadata definition = model.getDefinition(name); 98 if (null == definition) { 99 throw new ConfigurationException(new Message("ITEM_NOT_DEFINED_EXC", BUNDLE, name)); 100 } 101 102 Configuration holder = this; 103 while (null != holder) { 104 Object obj = getLocal(holder, name); 105 if (null != obj) { 106 return obj; 107 } 108 holder = holder.getParent(); 109 } 110 return definition.getDefaultValue(); 111 } 112 113 117 public boolean setObject(String name, Object value) { 118 ConfigurationItemMetadata definition = model.getDefinition(name); 119 if (null == definition) { 120 throw new ConfigurationException(new Message("ITEM_NOT_DEFINED_EXC", BUNDLE, name)); 121 } 122 123 Object defaultValue = definition.getDefaultValue(); 125 if (defaultValue != null && !defaultValue.getClass().isAssignableFrom(value.getClass())) { 126 QName type = model.getDefinition(name).getType(); 127 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 128 } 129 130 135 boolean accepted = false; 137 138 for (ConfigurationProvider provider : providers) { 139 if (provider.setObject(name, value)) { 140 accepted = true; 141 break; 142 } 143 } 144 145 if (accepted) { 146 reconfigure(name); 147 } 148 return accepted; 149 } 150 151 public boolean save() { 152 boolean accepted = false; 153 154 156 for (ConfigurationProvider provider : providers) { 157 if (provider.save()) { 158 accepted = true; 159 break; 160 } 161 } 162 return accepted; 163 } 164 165 public boolean getBoolean(String name) { 166 Object obj = getObject(name); 167 if (null == obj) { 168 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 169 } 170 try { 171 return ((Boolean )obj).booleanValue(); 172 } catch (ClassCastException ex) { 173 QName type = model.getDefinition(name).getType(); 174 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 175 } 176 } 177 178 public boolean setBoolean(String name, boolean value) { 179 return setObject(name, Boolean.valueOf(value)); 180 } 181 182 public short getShort(String name) { 183 Object obj = getObject(name); 184 if (null == obj) { 185 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 186 } 187 try { 188 return ((Short )obj).shortValue(); 189 } catch (ClassCastException ex) { 190 QName type = model.getDefinition(name).getType(); 191 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 192 } 193 } 194 195 public boolean setShort(String name, short value) { 196 return setObject(name, new Short (value)); 197 } 198 199 public int getInt(String name) { 200 Object obj = getObject(name); 201 if (null == obj) { 202 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 203 } 204 try { 205 return ((Integer )obj).intValue(); 206 } catch (ClassCastException ex) { 207 QName type = model.getDefinition(name).getType(); 208 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 209 } 210 } 211 212 public boolean setInt(String name, int value) { 213 return setObject(name, new Integer (value)); 214 } 215 216 public float getFloat(String name) { 217 Object obj = getObject(name); 218 if (null == obj) { 219 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 220 } 221 try { 222 return ((Float )obj).floatValue(); 223 } catch (ClassCastException ex) { 224 QName type = model.getDefinition(name).getType(); 225 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 226 } 227 } 228 229 public boolean setFloat(String name, float value) { 230 return setObject(name, new Float (value)); 231 } 232 233 public double getDouble(String name) { 234 Object obj = getObject(name); 235 if (null == obj) { 236 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 237 } 238 try { 239 return ((Double )obj).doubleValue(); 240 } catch (ClassCastException ex) { 241 QName type = model.getDefinition(name).getType(); 242 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 243 } 244 } 245 246 public boolean setDouble(String name, double value) { 247 return setObject(name, new Double (value)); 248 } 249 250 public long getLong(String name) { 251 Object obj = getObject(name); 252 if (null == obj) { 253 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 254 } 255 try { 256 return ((Long )obj).longValue(); 257 } catch (ClassCastException ex) { 258 QName type = model.getDefinition(name).getType(); 259 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 260 } 261 } 262 263 public boolean setLong(String name, long value) { 264 return setObject(name, new Long (value)); 265 } 266 267 public String getString(String name) { 268 Object obj = getObject(name); 269 if (null == obj) { 270 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 271 } 272 if (!(obj instanceof String )) { 273 QName type = model.getDefinition(name).getType(); 274 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 275 } 276 return (String )obj; 277 } 278 279 public boolean setString(String name, String value) { 280 return setObject(name, (String )value); 281 } 282 283 @SuppressWarnings ("unchecked") 284 public List <String > getStringList(String name) { 285 Object obj = getObject(name); 286 if (null == obj) { 287 throw new ConfigurationException(new Message("ITEM_NO_VALUE_EXC", BUNDLE, name)); 288 } 289 try { 290 Method method = obj.getClass().getMethod("getItem", new Class [0]); 291 obj = method.invoke(obj, new Object [0]); 292 293 return (List <String >)obj; 294 } catch (ClassCastException ex) { 295 QName type = model.getDefinition(name).getType(); 296 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 297 } catch (NoSuchMethodException e) { 298 QName type = model.getDefinition(name).getType(); 299 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 300 } catch (IllegalAccessException e) { 301 QName type = model.getDefinition(name).getType(); 302 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 303 } catch (InvocationTargetException e) { 304 QName type = model.getDefinition(name).getType(); 305 throw new ConfigurationException(new Message("ITEM_TYPE_MISMATCH_EXC", BUNDLE, name, type)); 306 } 307 } 308 309 protected Object getLocal(Configuration c, String name) { 310 if (null == providers) { 311 return null; 312 } 313 Object obj; 314 for (ConfigurationProvider provider : providers) { 315 obj = provider.getObject(name); 316 if (null != obj) { 317 return obj; 318 } 319 } 320 return null; 321 } 322 323 public final Configurator getConfigurator() { 324 return configurator; 325 } 326 327 public void reconfigure(String name) { 328 } 330 } 331 | Popular Tags |