001: package org.objectweb.celtix.configuration.impl;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.util.HashMap;
006: import java.util.Map;
007: import java.util.ResourceBundle;
008:
009: import org.objectweb.celtix.common.i18n.BundleUtils;
010: import org.objectweb.celtix.common.i18n.Message;
011: import org.objectweb.celtix.configuration.Configuration;
012: import org.objectweb.celtix.configuration.ConfigurationBuilder;
013: import org.objectweb.celtix.configuration.ConfigurationException;
014: import org.objectweb.celtix.configuration.ConfigurationMetadata;
015: import org.objectweb.celtix.resource.DefaultResourceManager;
016:
017: public class ConfigurationBuilderImpl implements ConfigurationBuilder {
018: protected static final ResourceBundle BUNDLE = BundleUtils
019: .getBundle(ConfigurationBuilderImpl.class);
020:
021: protected Map<String, Map<String, Configuration>> configurations;
022: private Map<String, ConfigurationMetadata> models;
023:
024: public ConfigurationBuilderImpl() {
025: models = new HashMap<String, ConfigurationMetadata>();
026: configurations = new HashMap<String, Map<String, Configuration>>();
027:
028: /*
029: add("config-metadata/bus-config.xml");
030: add("config-metadata/endpoint-config.xml");
031: add("config-metadata/http-client-config.xml");
032: add("config-metadata/http-listener-config.xml");
033: add("config-metadata/http-server-config.xml");
034: add("config-metadata/port-config.xml");
035: add("config-metadata/service-config.xml");
036: */
037: }
038:
039: public Configuration getConfiguration(String namespaceUri, String id) {
040: Map<String, Configuration> instances = configurations
041: .get(namespaceUri);
042: if (null == instances) {
043: if (null == getModel(namespaceUri)) {
044: throw new ConfigurationException(new Message(
045: "UNKNOWN_NAMESPACE_EXC", BUNDLE, namespaceUri));
046: }
047: return null;
048: }
049: return instances.get(id);
050: }
051:
052: public Configuration getConfiguration(String namespaceUri,
053: String id, Configuration parent) {
054: if (parent == null) {
055: return null;
056: }
057:
058: Configuration c = parent.getChild(namespaceUri, id);
059: if (null == c && null == getModel(namespaceUri)) {
060: throw new ConfigurationException(new Message(
061: "UNKNOWN_NAMESPACE_EXC", BUNDLE, namespaceUri));
062: }
063: return c;
064: }
065:
066: public Configuration buildConfiguration(String namespaceUri,
067: String id, Configuration parent) {
068: ConfigurationMetadata model = getModel(namespaceUri);
069: if (null == model) {
070: throw new ConfigurationException(new Message(
071: "UNKNOWN_NAMESPACE_EXC", BUNDLE, namespaceUri));
072: }
073: /*
074: if (parent != null && !isValidChildConfiguration(model, parent)) {
075: throw new ConfigurationException(new Message("INVALID_CHILD_CONFIGURATION",
076: BUNDLE, namespaceUri,
077: parent.getModel().getNamespaceURI()));
078: }
079: */
080: if (parent == null && !isValidTopConfiguration(model, parent)) {
081: throw new ConfigurationException(new Message(
082: "INVALID_TOP_CONFIGURATION", BUNDLE, namespaceUri));
083: }
084:
085: Configuration c = new AbstractConfigurationImpl(model, id,
086: parent);
087: if (null == parent) {
088: Map<String, Configuration> instances = configurations
089: .get(namespaceUri);
090: if (null == instances) {
091: instances = new HashMap<String, Configuration>();
092: configurations.put(namespaceUri, instances);
093: }
094: instances.put(id, c);
095: }
096: return c;
097: }
098:
099: /* The presence of parentNamespaceURI means that the underlying type of
100: * configuration can only be created as a child of a configuration with
101: * namespace 'parentNamespace'. The absence of this attribute means that it
102: * can be be created as a top level configuration object
103: **/
104: protected boolean isValidTopConfiguration(
105: ConfigurationMetadata model, Configuration parent) {
106: String parentNamespaceURI = model.getParentNamespaceURI();
107:
108: if (parentNamespaceURI == null || "".equals(parentNamespaceURI)) {
109: return true;
110: }
111:
112: return false;
113: }
114:
115: /*
116: private boolean isValidChildConfiguration(ConfigurationMetadata model, Configuration parent) {
117: if (parent != null && parent.getModel().getNamespaceURI().equals(model.getParentNamespaceURI())) {
118: return true;
119: }
120:
121: return false;
122: }
123: */
124:
125: public Configuration buildConfiguration(String namespaceUri,
126: String id) {
127: return buildConfiguration(namespaceUri, id, null);
128: }
129:
130: public final void addModel(ConfigurationMetadata model) {
131: models.put(model.getNamespaceURI(), model);
132: }
133:
134: public ConfigurationMetadata getModel(String namespaceUri) {
135: return models.get(namespaceUri);
136: }
137:
138: public void addModel(String resource) {
139:
140: InputStream is = null;
141: if (resource != null) {
142: is = loadResource(resource);
143: if (is == null) {
144: throw new ConfigurationException(new Message(
145: "METADATA_RESOURCE_EXC", BUNDLE, resource));
146: }
147: }
148:
149: ConfigurationMetadata model = null;
150: ConfigurationMetadataBuilder builder = new ConfigurationMetadataBuilder(
151: true);
152: if (null != is) {
153: try {
154: model = builder.build(is);
155: } catch (IOException ex) {
156: throw new ConfigurationException(new Message(
157: "METADATA_RESOURCE_EXC", BUNDLE, resource), ex);
158: }
159: } else {
160: model = new ConfigurationMetadataImpl();
161: }
162:
163: addModel(model);
164: }
165:
166: private InputStream loadResource(String resourceName) {
167: return DefaultResourceManager.instance().getResourceAsStream(
168: resourceName);
169: }
170: }
|