cleanup, no change
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/3c51a773 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/3c51a773 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/3c51a773 Branch: refs/heads/master Commit: 3c51a773ce64be713295157cf77d8fa72379ab91 Parents: 5ce09a7 Author: aadamchik <aadamc...@apache.org> Authored: Sat Nov 22 17:56:11 2014 +0300 Committer: aadamchik <aadamc...@apache.org> Committed: Sat Nov 22 17:58:06 2014 +0300 ---------------------------------------------------------------------- .../configuration/ConfigurationNameMapper.java | 35 +- .../configuration/DataChannelDescriptor.java | 268 ++++--- .../DefaultConfigurationNameMapper.java | 186 +++-- .../XMLDataChannelDescriptorLoader.java | 761 +++++++++---------- 4 files changed, 589 insertions(+), 661 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/3c51a773/cayenne-server/src/main/java/org/apache/cayenne/configuration/ConfigurationNameMapper.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/ConfigurationNameMapper.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/ConfigurationNameMapper.java index 47982fd..2b7dc51 100644 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/ConfigurationNameMapper.java +++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/ConfigurationNameMapper.java @@ -25,24 +25,23 @@ import org.apache.cayenne.resource.Resource; */ public interface ConfigurationNameMapper { - /** - * Returns the name of a configuration resource based on a naming convention for a - * given node type. - */ - String configurationLocation(ConfigurationNode node); + /** + * Returns the name of a configuration resource based on a naming convention + * for a given node type. + */ + String configurationLocation(ConfigurationNode node); - /** - * Returns the name of a configuration resource based on a naming convention for a - * given node type. - */ - String configurationLocation(Class<? extends ConfigurationNode> type, String nodeName); + /** + * Returns the name of a configuration resource based on a naming convention + * for a given node type. + */ + String configurationLocation(Class<? extends ConfigurationNode> type, String nodeName); - /** - * Returns a node name for a given configuration type and a configuration resource. - * This operation is the opposite of the {@link #configurationLocation(Class, String)} - * . May return null if the resource name is not following the expected naming format. - */ - String configurationNodeName( - Class<? extends ConfigurationNode> type, - Resource resource); + /** + * Returns a node name for a given configuration type and a configuration + * resource. This operation is the opposite of the + * {@link #configurationLocation(Class, String)} . May return null if the + * resource name is not following the expected naming format. + */ + String configurationNodeName(Class<? extends ConfigurationNode> type, Resource resource); } http://git-wip-us.apache.org/repos/asf/cayenne/blob/3c51a773/cayenne-server/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptor.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptor.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptor.java index 34b14b8..a64fb51 100644 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptor.java +++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/DataChannelDescriptor.java @@ -36,139 +36,137 @@ import org.apache.cayenne.util.XMLSerializable; * * @since 3.1 */ -public class DataChannelDescriptor implements ConfigurationNode, Serializable, - XMLSerializable { - - protected String name; - protected Map<String, String> properties; - protected Collection<DataMap> dataMaps; - protected Collection<DataNodeDescriptor> nodeDescriptors; - protected Resource configurationSource; - protected String defaultNodeName; - - public DataChannelDescriptor() { - properties = new HashMap<String, String>(); - dataMaps = new ArrayList<DataMap>(5); - nodeDescriptors = new ArrayList<DataNodeDescriptor>(3); - } - - public void encodeAsXML(XMLEncoder encoder) { - - encoder.print("<domain"); - encoder.printProjectVersion(); - encoder.println(">"); - - encoder.indent(1); - boolean breakNeeded = false; - - if (!properties.isEmpty()) { - breakNeeded = true; - - List<String> keys = new ArrayList<String>(properties.keySet()); - Collections.sort(keys); - - for (String key : keys) { - encoder.printProperty(key, properties.get(key)); - } - } - - if (!dataMaps.isEmpty()) { - if (breakNeeded) { - encoder.println(); - } - else { - breakNeeded = true; - } - - List<DataMap> maps = new ArrayList<DataMap>(this.dataMaps); - Collections.sort(maps); - - for (DataMap dataMap : maps) { - - encoder.print("<map"); - encoder.printAttribute("name", dataMap.getName().trim()); - encoder.println("/>"); - } - } - - if (!nodeDescriptors.isEmpty()) { - if (breakNeeded) { - encoder.println(); - } - else { - breakNeeded = true; - } - - List<DataNodeDescriptor> nodes = new ArrayList<DataNodeDescriptor>( - nodeDescriptors); - Collections.sort(nodes); - encoder.print(nodes); - } - - encoder.indent(-1); - encoder.println("</domain>"); - } - - public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) { - return visitor.visitDataChannelDescriptor(this); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Map<String, String> getProperties() { - return properties; - } - - public Collection<DataMap> getDataMaps() { - return dataMaps; - } - - public DataMap getDataMap(String name) { - for (DataMap map : dataMaps) { - if (name.equals(map.getName())) { - return map; - } - } - return null; - } - - public Collection<DataNodeDescriptor> getNodeDescriptors() { - return nodeDescriptors; - } - - public DataNodeDescriptor getNodeDescriptor(String name) { - for (DataNodeDescriptor node : nodeDescriptors) { - if (name.equals(node.getName())) { - return node; - } - } - - return null; - } - - public Resource getConfigurationSource() { - return configurationSource; - } - - public void setConfigurationSource(Resource configurationSource) { - this.configurationSource = configurationSource; - } - - /** - * Returns the name of the DataNode that should be used as the default if a DataMap is - * not explicitly linked to a node. - */ - public String getDefaultNodeName() { - return defaultNodeName; - } - - public void setDefaultNodeName(String defaultDataNodeName) { - this.defaultNodeName = defaultDataNodeName; - } +public class DataChannelDescriptor implements ConfigurationNode, Serializable, XMLSerializable { + + private static final long serialVersionUID = 6567527544207035602L; + + protected String name; + protected Map<String, String> properties; + protected Collection<DataMap> dataMaps; + protected Collection<DataNodeDescriptor> nodeDescriptors; + protected Resource configurationSource; + protected String defaultNodeName; + + public DataChannelDescriptor() { + properties = new HashMap<String, String>(); + dataMaps = new ArrayList<DataMap>(5); + nodeDescriptors = new ArrayList<DataNodeDescriptor>(3); + } + + public void encodeAsXML(XMLEncoder encoder) { + + encoder.print("<domain"); + encoder.printProjectVersion(); + encoder.println(">"); + + encoder.indent(1); + boolean breakNeeded = false; + + if (!properties.isEmpty()) { + breakNeeded = true; + + List<String> keys = new ArrayList<String>(properties.keySet()); + Collections.sort(keys); + + for (String key : keys) { + encoder.printProperty(key, properties.get(key)); + } + } + + if (!dataMaps.isEmpty()) { + if (breakNeeded) { + encoder.println(); + } else { + breakNeeded = true; + } + + List<DataMap> maps = new ArrayList<DataMap>(this.dataMaps); + Collections.sort(maps); + + for (DataMap dataMap : maps) { + + encoder.print("<map"); + encoder.printAttribute("name", dataMap.getName().trim()); + encoder.println("/>"); + } + } + + if (!nodeDescriptors.isEmpty()) { + if (breakNeeded) { + encoder.println(); + } else { + breakNeeded = true; + } + + List<DataNodeDescriptor> nodes = new ArrayList<DataNodeDescriptor>(nodeDescriptors); + Collections.sort(nodes); + encoder.print(nodes); + } + + encoder.indent(-1); + encoder.println("</domain>"); + } + + public <T> T acceptVisitor(ConfigurationNodeVisitor<T> visitor) { + return visitor.visitDataChannelDescriptor(this); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map<String, String> getProperties() { + return properties; + } + + public Collection<DataMap> getDataMaps() { + return dataMaps; + } + + public DataMap getDataMap(String name) { + for (DataMap map : dataMaps) { + if (name.equals(map.getName())) { + return map; + } + } + return null; + } + + public Collection<DataNodeDescriptor> getNodeDescriptors() { + return nodeDescriptors; + } + + public DataNodeDescriptor getNodeDescriptor(String name) { + for (DataNodeDescriptor node : nodeDescriptors) { + if (name.equals(node.getName())) { + return node; + } + } + + return null; + } + + public Resource getConfigurationSource() { + return configurationSource; + } + + public void setConfigurationSource(Resource configurationSource) { + this.configurationSource = configurationSource; + } + + /** + * Returns the name of the DataNode that should be used as the default if a + * DataMap is not explicitly linked to a node. + */ + public String getDefaultNodeName() { + return defaultNodeName; + } + + public void setDefaultNodeName(String defaultDataNodeName) { + this.defaultNodeName = defaultDataNodeName; + } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/3c51a773/cayenne-server/src/main/java/org/apache/cayenne/configuration/DefaultConfigurationNameMapper.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/DefaultConfigurationNameMapper.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/DefaultConfigurationNameMapper.java index ba7dcd4..ffe2868 100644 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/DefaultConfigurationNameMapper.java +++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/DefaultConfigurationNameMapper.java @@ -26,100 +26,94 @@ import org.apache.cayenne.resource.Resource; */ public class DefaultConfigurationNameMapper implements ConfigurationNameMapper { - private static final String CAYENNE_PREFIX = "cayenne-"; - private static final String CAYENNE_SUFFIX = ".xml"; - - private static final String DATA_MAP_SUFFIX = ".map.xml"; - - protected ConfigurationNodeVisitor<String> nameMapper; - - public DefaultConfigurationNameMapper() { - nameMapper = new NameMapper(); - } - - public String configurationLocation(ConfigurationNode node) { - return node.acceptVisitor(nameMapper); - } - - public String configurationLocation( - Class<? extends ConfigurationNode> type, - String name) { - if (DataChannelDescriptor.class.isAssignableFrom(type)) { - return getDataChannelName(name); - } - else if (DataMap.class.isAssignableFrom(type)) { - return getDataMapName(name); - } - - throw new IllegalArgumentException("Unrecognized configuration type: " - + type.getName()); - } - - public String configurationNodeName( - Class<? extends ConfigurationNode> type, - Resource resource) { - - String path = resource.getURL().getPath(); - if (path == null || path.length() == 0) { - return null; - } - - int lastSlash = path.lastIndexOf('/'); - if (lastSlash >= 0) { - - if (lastSlash == path.length() - 1) { - return null; - } - - path = path.substring(lastSlash + 1); - - } - - if (DataChannelDescriptor.class.isAssignableFrom(type)) { - if (!path.startsWith(CAYENNE_PREFIX) || !path.endsWith(CAYENNE_SUFFIX)) { - return null; - } - - return path.substring(CAYENNE_PREFIX.length(), path.length() - - CAYENNE_SUFFIX.length()); - } - else if (DataMap.class.isAssignableFrom(type)) { - if (!path.endsWith(DATA_MAP_SUFFIX)) { - return null; - } - return path.substring(0, path.length() - DATA_MAP_SUFFIX.length()); - } - - throw new IllegalArgumentException("Unrecognized configuration type: " - + type.getName()); - } - - protected String getDataChannelName(String name) { - if (name == null) { - throw new NullPointerException("Null DataChannelDescriptor name"); - } - - return CAYENNE_PREFIX + name + CAYENNE_SUFFIX; - } - - protected String getDataMapName(String name) { - if (name == null) { - throw new NullPointerException("Null DataMap name"); - } - - return name + DATA_MAP_SUFFIX; - } - - final class NameMapper extends BaseConfigurationNodeVisitor<String> { - - @Override - public String visitDataChannelDescriptor(DataChannelDescriptor descriptor) { - return getDataChannelName(descriptor.getName()); - } - - @Override - public String visitDataMap(DataMap dataMap) { - return getDataMapName(dataMap.getName()); - } - } + private static final String CAYENNE_PREFIX = "cayenne-"; + private static final String CAYENNE_SUFFIX = ".xml"; + + private static final String DATA_MAP_SUFFIX = ".map.xml"; + + protected ConfigurationNodeVisitor<String> nameMapper; + + public DefaultConfigurationNameMapper() { + nameMapper = new NameMapper(); + } + + @Override + public String configurationLocation(ConfigurationNode node) { + return node.acceptVisitor(nameMapper); + } + + @Override + public String configurationLocation(Class<? extends ConfigurationNode> type, String name) { + if (DataChannelDescriptor.class.isAssignableFrom(type)) { + return getDataChannelName(name); + } else if (DataMap.class.isAssignableFrom(type)) { + return getDataMapName(name); + } + + throw new IllegalArgumentException("Unrecognized configuration type: " + type.getName()); + } + + @Override + public String configurationNodeName(Class<? extends ConfigurationNode> type, Resource resource) { + + String path = resource.getURL().getPath(); + if (path == null || path.length() == 0) { + return null; + } + + int lastSlash = path.lastIndexOf('/'); + if (lastSlash >= 0) { + + if (lastSlash == path.length() - 1) { + return null; + } + + path = path.substring(lastSlash + 1); + + } + + if (DataChannelDescriptor.class.isAssignableFrom(type)) { + if (!path.startsWith(CAYENNE_PREFIX) || !path.endsWith(CAYENNE_SUFFIX)) { + return null; + } + + return path.substring(CAYENNE_PREFIX.length(), path.length() - CAYENNE_SUFFIX.length()); + } else if (DataMap.class.isAssignableFrom(type)) { + if (!path.endsWith(DATA_MAP_SUFFIX)) { + return null; + } + return path.substring(0, path.length() - DATA_MAP_SUFFIX.length()); + } + + throw new IllegalArgumentException("Unrecognized configuration type: " + type.getName()); + } + + protected String getDataChannelName(String name) { + if (name == null) { + throw new NullPointerException("Null DataChannelDescriptor name"); + } + + return CAYENNE_PREFIX + name + CAYENNE_SUFFIX; + } + + protected String getDataMapName(String name) { + if (name == null) { + throw new NullPointerException("Null DataMap name"); + } + + return name + DATA_MAP_SUFFIX; + } + + final class NameMapper extends BaseConfigurationNodeVisitor<String> { + + @Override + public String visitDataChannelDescriptor(DataChannelDescriptor descriptor) { + return getDataChannelName(descriptor.getName()); + } + + @Override + public String visitDataMap(DataMap dataMap) { + return getDataMapName(dataMap.getName()); + } + } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/3c51a773/cayenne-server/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java ---------------------------------------------------------------------- diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java b/cayenne-server/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java index 7231dfe..420c43c 100644 --- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java +++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoader.java @@ -44,416 +44,353 @@ import org.xml.sax.XMLReader; */ public class XMLDataChannelDescriptorLoader implements DataChannelDescriptorLoader { - private static Log logger = LogFactory.getLog(XMLDataChannelDescriptorLoader.class); - - static final String DOMAIN_TAG = "domain"; - static final String MAP_TAG = "map"; - static final String NODE_TAG = "node"; - static final String PROPERTY_TAG = "property"; - static final String MAP_REF_TAG = "map-ref"; - static final String DATA_SOURCE_TAG = "data-source"; - - /** - * @deprecated the caller should use password resolving strategy instead of resolving - * the password on the spot. For one thing this can be used in the Modeler - * and no password may be available. - */ - @Deprecated - private static String passwordFromURL(URL url) { - InputStream inputStream = null; - String password = null; - - try { - inputStream = url.openStream(); - password = passwordFromInputStream(inputStream); - } - catch (IOException exception) { - // Log the error while trying to open the stream. A null - // password will be returned as a result. - logger.warn(exception); - } - - return password; - } - - /** - * @deprecated the caller should use password resolving strategy instead of resolving - * the password on the spot. For one thing this can be used in the Modeler - * and no password may be available. - */ - @Deprecated - private static String passwordFromInputStream(InputStream inputStream) { - BufferedReader bufferedReader = null; - String password = null; - - try { - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - password = bufferedReader.readLine(); - } - catch (IOException exception) { - logger.warn(exception); - } - finally { - try { - if (bufferedReader != null) { - bufferedReader.close(); - } - } - catch (Exception exception) { - } - - try { - inputStream.close(); - } - catch (IOException exception) { - } - } - - return password; - } - - @Inject - protected DataMapLoader dataMapLoader; - - @Inject - protected ConfigurationNameMapper nameMapper; - - @Inject - protected AdhocObjectFactory objectFactory; - - public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) - throws ConfigurationException { - - if (configurationResource == null) { - throw new NullPointerException("Null configurationResource"); - } - - URL configurationURL = configurationResource.getURL(); - - logger.info("Loading XML configuration resource from " + configurationURL); - - DataChannelDescriptor descriptor = new DataChannelDescriptor(); - descriptor.setConfigurationSource(configurationResource); - descriptor.setName(nameMapper.configurationNodeName( - DataChannelDescriptor.class, - configurationResource)); - - DataChannelHandler rootHandler; - - InputStream in = null; - - try { - in = configurationURL.openStream(); - XMLReader parser = Util.createXmlReader(); - - rootHandler = new DataChannelHandler(descriptor, parser); - parser.setContentHandler(rootHandler); - parser.setErrorHandler(rootHandler); - parser.parse(new InputSource(in)); - } - catch (Exception e) { - throw new ConfigurationException( - "Error loading configuration from %s", - e, - configurationURL); - } - finally { - try { - if (in != null) { - in.close(); - } - } - catch (IOException ioex) { - logger.info("failure closing input stream for " - + configurationURL - + ", ignoring", ioex); - } - } - - // TODO: andrus 03/10/2010 - actually provide load failures here... - return new ConfigurationTree<DataChannelDescriptor>(descriptor, null); - } - - final class DataChannelHandler extends SAXNestedTagHandler { - - private DataChannelDescriptor descriptor; - - DataChannelHandler(DataChannelDescriptor dataChannelDescriptor, XMLReader parser) { - super(parser, null); - this.descriptor = dataChannelDescriptor; - } - - @Override - protected ContentHandler createChildTagHandler( - String namespaceURI, - String localName, - String name, - Attributes attributes) { - - if (localName.equals(DOMAIN_TAG)) { - return new DataChannelChildrenHandler(parser, this); - } - - logger.info(unexpectedTagMessage(localName, DOMAIN_TAG)); - return super.createChildTagHandler(namespaceURI, localName, name, attributes); - } - } - - final class DataChannelChildrenHandler extends SAXNestedTagHandler { - - private DataChannelDescriptor descriptor; - - DataChannelChildrenHandler(XMLReader parser, DataChannelHandler parentHandler) { - super(parser, parentHandler); - this.descriptor = parentHandler.descriptor; - } - - @Override - protected ContentHandler createChildTagHandler( - String namespaceURI, - String localName, - String name, - Attributes attributes) { - - if (localName.equals(PROPERTY_TAG)) { - - String key = attributes.getValue("", "name"); - String value = attributes.getValue("", "value"); - if (key != null && value != null) { - descriptor.getProperties().put(key, value); - } - } - else if (localName.equals(MAP_TAG)) { - - String dataMapName = attributes.getValue("", "name"); - Resource baseResource = descriptor.getConfigurationSource(); - - String dataMapLocation = nameMapper.configurationLocation( - DataMap.class, - dataMapName); - - Resource dataMapResource = baseResource - .getRelativeResource(dataMapLocation); - - logger.info("Loading XML DataMap resource from " + dataMapResource.getURL()); - - DataMap dataMap = dataMapLoader.load(dataMapResource); - dataMap.setName(dataMapName); - dataMap.setLocation(dataMapLocation); - dataMap.setConfigurationSource(dataMapResource); - dataMap.setDataChannelDescriptor(descriptor); - - descriptor.getDataMaps().add(dataMap); - } - else if (localName.equals(NODE_TAG)) { - - String nodeName = attributes.getValue("", "name"); - if (nodeName == null) { - throw new ConfigurationException("Error: <node> without 'name'."); - } - - DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor(); - nodeDescriptor - .setConfigurationSource(descriptor.getConfigurationSource()); - descriptor.getNodeDescriptors().add(nodeDescriptor); - - nodeDescriptor.setName(nodeName); - nodeDescriptor.setAdapterType(attributes.getValue("", "adapter")); - - String parameters = attributes.getValue("", "parameters"); - nodeDescriptor.setParameters(parameters); - - String dataSourceFactory = attributes.getValue("", "factory"); - nodeDescriptor.setDataSourceFactoryType(dataSourceFactory); - nodeDescriptor.setSchemaUpdateStrategyType(attributes.getValue( - "", - "schema-update-strategy")); - nodeDescriptor.setDataChannelDescriptor(descriptor); - - return new DataNodeChildrenHandler(parser, this, nodeDescriptor); - } - - return super.createChildTagHandler(namespaceURI, localName, name, attributes); - } - } - - final class DataNodeChildrenHandler extends SAXNestedTagHandler { - - private DataNodeDescriptor nodeDescriptor; - - DataNodeChildrenHandler(XMLReader parser, SAXNestedTagHandler parentHandler, - DataNodeDescriptor nodeDescriptor) { - super(parser, parentHandler); - this.nodeDescriptor = nodeDescriptor; - } - - @Override - protected ContentHandler createChildTagHandler( - String namespaceURI, - String localName, - String name, - Attributes attributes) { - - if (localName.equals(MAP_REF_TAG)) { - - String mapName = attributes.getValue("", "name"); - nodeDescriptor.getDataMapNames().add(mapName); - } - else if (localName.equals(DATA_SOURCE_TAG)) { - - DataSourceInfo dataSourceDescriptor = new DataSourceInfo(); - nodeDescriptor.setDataSourceDescriptor(dataSourceDescriptor); - return new DataSourceChildrenHandler(parser, this, dataSourceDescriptor); - } - - return super.createChildTagHandler(namespaceURI, localName, name, attributes); - } - } - - class DataSourceChildrenHandler extends SAXNestedTagHandler { - - private DataSourceInfo dataSourceDescriptor; - - DataSourceChildrenHandler(XMLReader parser, - DataNodeChildrenHandler parentHandler, DataSourceInfo dataSourceDescriptor) { - super(parser, parentHandler); - this.dataSourceDescriptor = dataSourceDescriptor; - } - - @Override - protected ContentHandler createChildTagHandler( - String namespaceURI, - String localName, - String name, - Attributes attributes) { - - if (localName.equals("driver")) { - String className = attributes.getValue("", "value"); - dataSourceDescriptor.setJdbcDriver(className); - } - else if (localName.equals("login")) { - - logger.info("loading user name and password."); - - String encoderClass = attributes.getValue("encoderClass"); - - String encoderKey = attributes.getValue("encoderKey"); - if (encoderKey == null) { - encoderKey = attributes.getValue("encoderSalt"); - } - - String password = attributes.getValue("password"); - String passwordLocation = attributes.getValue("passwordLocation"); - String passwordSource = attributes.getValue("passwordSource"); - if (passwordSource == null) { - passwordSource = DataSourceInfo.PASSWORD_LOCATION_MODEL; - } - - String username = attributes.getValue("userName"); - - dataSourceDescriptor.setPasswordEncoderClass(encoderClass); - dataSourceDescriptor.setPasswordEncoderKey(encoderKey); - dataSourceDescriptor.setPasswordLocation(passwordLocation); - dataSourceDescriptor.setPasswordSource(passwordSource); - dataSourceDescriptor.setUserName(username); - - // Replace {} in passwordSource with encoderSalt -- useful for EXECUTABLE - // & URL options - if (encoderKey != null) { - passwordSource = passwordSource.replaceAll("\\{\\}", encoderKey); - } - - String encoderType = dataSourceDescriptor.getPasswordEncoderClass(); - PasswordEncoding passwordEncoder = null; - if (encoderType != null) { - passwordEncoder = objectFactory.newInstance(PasswordEncoding.class, encoderType); - } - - if (passwordLocation != null) { - if (passwordLocation - .equals(DataSourceInfo.PASSWORD_LOCATION_CLASSPATH)) { - - ClassLoader classLoader = Thread - .currentThread() - .getContextClassLoader(); - URL url = classLoader.getResource(username); - if (url != null) { - password = passwordFromURL(url); - } - else { - logger.error("Could not find resource in CLASSPATH: " - + passwordSource); - } - } - else if (passwordLocation - .equals(DataSourceInfo.PASSWORD_LOCATION_URL)) { - try { - password = passwordFromURL(new URL(passwordSource)); - } - catch (MalformedURLException exception) { - logger.warn(exception); - } - } - else if (passwordLocation - .equals(DataSourceInfo.PASSWORD_LOCATION_EXECUTABLE)) { - if (passwordSource != null) { - try { - Process process = Runtime.getRuntime().exec( - passwordSource); - password = passwordFromInputStream(process - .getInputStream()); - process.waitFor(); - } - catch (IOException exception) { - logger.warn(exception); - } - catch (InterruptedException exception) { - logger.warn(exception); - } - } - } - } - - if (password != null && passwordEncoder != null) { - dataSourceDescriptor.setPassword(passwordEncoder.decodePassword( - password, - encoderKey)); - } - } - else if (localName.equals("url")) { - dataSourceDescriptor.setDataSourceUrl(attributes.getValue("value")); - } - else if (localName.equals("connectionPool")) { - String min = attributes.getValue("min"); - if (min != null) { - try { - dataSourceDescriptor.setMinConnections(Integer.parseInt(min)); - } - catch (NumberFormatException nfex) { - logger.info("Non-numeric 'min' attribute", nfex); - throw new ConfigurationException( - "Non-numeric 'min' attribute '%s'", - nfex, - min); - } - } - - String max = attributes.getValue("max"); - if (max != null) { - try { - dataSourceDescriptor.setMaxConnections(Integer.parseInt(max)); - } - catch (NumberFormatException nfex) { - logger.info("Non-numeric 'max' attribute", nfex); - throw new ConfigurationException( - "Non-numeric 'max' attribute '%s'", - nfex, - max); - } - } - } - - return super.createChildTagHandler(namespaceURI, localName, name, attributes); - } - } + private static Log logger = LogFactory.getLog(XMLDataChannelDescriptorLoader.class); + + static final String DOMAIN_TAG = "domain"; + static final String MAP_TAG = "map"; + static final String NODE_TAG = "node"; + static final String PROPERTY_TAG = "property"; + static final String MAP_REF_TAG = "map-ref"; + static final String DATA_SOURCE_TAG = "data-source"; + + /** + * @deprecated the caller should use password resolving strategy instead of + * resolving the password on the spot. For one thing this can be + * used in the Modeler and no password may be available. + */ + @Deprecated + private static String passwordFromURL(URL url) { + InputStream inputStream = null; + String password = null; + + try { + inputStream = url.openStream(); + password = passwordFromInputStream(inputStream); + } catch (IOException exception) { + // Log the error while trying to open the stream. A null + // password will be returned as a result. + logger.warn(exception); + } + + return password; + } + + /** + * @deprecated the caller should use password resolving strategy instead of + * resolving the password on the spot. For one thing this can be + * used in the Modeler and no password may be available. + */ + @Deprecated + private static String passwordFromInputStream(InputStream inputStream) { + BufferedReader bufferedReader = null; + String password = null; + + try { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + password = bufferedReader.readLine(); + } catch (IOException exception) { + logger.warn(exception); + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + } catch (Exception exception) { + } + + try { + inputStream.close(); + } catch (IOException exception) { + } + } + + return password; + } + + @Inject + protected DataMapLoader dataMapLoader; + + @Inject + protected ConfigurationNameMapper nameMapper; + + @Inject + protected AdhocObjectFactory objectFactory; + + @Override + public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException { + + if (configurationResource == null) { + throw new NullPointerException("Null configurationResource"); + } + + URL configurationURL = configurationResource.getURL(); + + logger.info("Loading XML configuration resource from " + configurationURL); + + DataChannelDescriptor descriptor = new DataChannelDescriptor(); + descriptor.setConfigurationSource(configurationResource); + descriptor.setName(nameMapper.configurationNodeName(DataChannelDescriptor.class, configurationResource)); + + DataChannelHandler rootHandler; + + InputStream in = null; + + try { + in = configurationURL.openStream(); + XMLReader parser = Util.createXmlReader(); + + rootHandler = new DataChannelHandler(descriptor, parser); + parser.setContentHandler(rootHandler); + parser.setErrorHandler(rootHandler); + parser.parse(new InputSource(in)); + } catch (Exception e) { + throw new ConfigurationException("Error loading configuration from %s", e, configurationURL); + } finally { + try { + if (in != null) { + in.close(); + } + } catch (IOException ioex) { + logger.info("failure closing input stream for " + configurationURL + ", ignoring", ioex); + } + } + + // TODO: andrus 03/10/2010 - actually provide load failures here... + return new ConfigurationTree<DataChannelDescriptor>(descriptor, null); + } + + final class DataChannelHandler extends SAXNestedTagHandler { + + private DataChannelDescriptor descriptor; + + DataChannelHandler(DataChannelDescriptor dataChannelDescriptor, XMLReader parser) { + super(parser, null); + this.descriptor = dataChannelDescriptor; + } + + @Override + protected ContentHandler createChildTagHandler(String namespaceURI, String localName, String name, + Attributes attributes) { + + if (localName.equals(DOMAIN_TAG)) { + return new DataChannelChildrenHandler(parser, this); + } + + logger.info(unexpectedTagMessage(localName, DOMAIN_TAG)); + return super.createChildTagHandler(namespaceURI, localName, name, attributes); + } + } + + final class DataChannelChildrenHandler extends SAXNestedTagHandler { + + private DataChannelDescriptor descriptor; + + DataChannelChildrenHandler(XMLReader parser, DataChannelHandler parentHandler) { + super(parser, parentHandler); + this.descriptor = parentHandler.descriptor; + } + + @Override + protected ContentHandler createChildTagHandler(String namespaceURI, String localName, String name, + Attributes attributes) { + + if (localName.equals(PROPERTY_TAG)) { + + String key = attributes.getValue("", "name"); + String value = attributes.getValue("", "value"); + if (key != null && value != null) { + descriptor.getProperties().put(key, value); + } + } else if (localName.equals(MAP_TAG)) { + + String dataMapName = attributes.getValue("", "name"); + Resource baseResource = descriptor.getConfigurationSource(); + + String dataMapLocation = nameMapper.configurationLocation(DataMap.class, dataMapName); + + Resource dataMapResource = baseResource.getRelativeResource(dataMapLocation); + + logger.info("Loading XML DataMap resource from " + dataMapResource.getURL()); + + DataMap dataMap = dataMapLoader.load(dataMapResource); + dataMap.setName(dataMapName); + dataMap.setLocation(dataMapLocation); + dataMap.setConfigurationSource(dataMapResource); + dataMap.setDataChannelDescriptor(descriptor); + + descriptor.getDataMaps().add(dataMap); + } else if (localName.equals(NODE_TAG)) { + + String nodeName = attributes.getValue("", "name"); + if (nodeName == null) { + throw new ConfigurationException("Error: <node> without 'name'."); + } + + DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor(); + nodeDescriptor.setConfigurationSource(descriptor.getConfigurationSource()); + descriptor.getNodeDescriptors().add(nodeDescriptor); + + nodeDescriptor.setName(nodeName); + nodeDescriptor.setAdapterType(attributes.getValue("", "adapter")); + + String parameters = attributes.getValue("", "parameters"); + nodeDescriptor.setParameters(parameters); + + String dataSourceFactory = attributes.getValue("", "factory"); + nodeDescriptor.setDataSourceFactoryType(dataSourceFactory); + nodeDescriptor.setSchemaUpdateStrategyType(attributes.getValue("", "schema-update-strategy")); + nodeDescriptor.setDataChannelDescriptor(descriptor); + + return new DataNodeChildrenHandler(parser, this, nodeDescriptor); + } + + return super.createChildTagHandler(namespaceURI, localName, name, attributes); + } + } + + final class DataNodeChildrenHandler extends SAXNestedTagHandler { + + private DataNodeDescriptor nodeDescriptor; + + DataNodeChildrenHandler(XMLReader parser, SAXNestedTagHandler parentHandler, DataNodeDescriptor nodeDescriptor) { + super(parser, parentHandler); + this.nodeDescriptor = nodeDescriptor; + } + + @Override + protected ContentHandler createChildTagHandler(String namespaceURI, String localName, String name, + Attributes attributes) { + + if (localName.equals(MAP_REF_TAG)) { + + String mapName = attributes.getValue("", "name"); + nodeDescriptor.getDataMapNames().add(mapName); + } else if (localName.equals(DATA_SOURCE_TAG)) { + + DataSourceInfo dataSourceDescriptor = new DataSourceInfo(); + nodeDescriptor.setDataSourceDescriptor(dataSourceDescriptor); + return new DataSourceChildrenHandler(parser, this, dataSourceDescriptor); + } + + return super.createChildTagHandler(namespaceURI, localName, name, attributes); + } + } + + class DataSourceChildrenHandler extends SAXNestedTagHandler { + + private DataSourceInfo dataSourceDescriptor; + + DataSourceChildrenHandler(XMLReader parser, DataNodeChildrenHandler parentHandler, + DataSourceInfo dataSourceDescriptor) { + super(parser, parentHandler); + this.dataSourceDescriptor = dataSourceDescriptor; + } + + @Override + protected ContentHandler createChildTagHandler(String namespaceURI, String localName, String name, + Attributes attributes) { + + if (localName.equals("driver")) { + String className = attributes.getValue("", "value"); + dataSourceDescriptor.setJdbcDriver(className); + } else if (localName.equals("login")) { + + logger.info("loading user name and password."); + + String encoderClass = attributes.getValue("encoderClass"); + + String encoderKey = attributes.getValue("encoderKey"); + if (encoderKey == null) { + encoderKey = attributes.getValue("encoderSalt"); + } + + String password = attributes.getValue("password"); + String passwordLocation = attributes.getValue("passwordLocation"); + String passwordSource = attributes.getValue("passwordSource"); + if (passwordSource == null) { + passwordSource = DataSourceInfo.PASSWORD_LOCATION_MODEL; + } + + String username = attributes.getValue("userName"); + + dataSourceDescriptor.setPasswordEncoderClass(encoderClass); + dataSourceDescriptor.setPasswordEncoderKey(encoderKey); + dataSourceDescriptor.setPasswordLocation(passwordLocation); + dataSourceDescriptor.setPasswordSource(passwordSource); + dataSourceDescriptor.setUserName(username); + + // Replace {} in passwordSource with encoderSalt -- useful for + // EXECUTABLE + // & URL options + if (encoderKey != null) { + passwordSource = passwordSource.replaceAll("\\{\\}", encoderKey); + } + + String encoderType = dataSourceDescriptor.getPasswordEncoderClass(); + PasswordEncoding passwordEncoder = null; + if (encoderType != null) { + passwordEncoder = objectFactory.newInstance(PasswordEncoding.class, encoderType); + } + + if (passwordLocation != null) { + if (passwordLocation.equals(DataSourceInfo.PASSWORD_LOCATION_CLASSPATH)) { + + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + URL url = classLoader.getResource(username); + if (url != null) { + password = passwordFromURL(url); + } else { + logger.error("Could not find resource in CLASSPATH: " + passwordSource); + } + } else if (passwordLocation.equals(DataSourceInfo.PASSWORD_LOCATION_URL)) { + try { + password = passwordFromURL(new URL(passwordSource)); + } catch (MalformedURLException exception) { + logger.warn(exception); + } + } else if (passwordLocation.equals(DataSourceInfo.PASSWORD_LOCATION_EXECUTABLE)) { + if (passwordSource != null) { + try { + Process process = Runtime.getRuntime().exec(passwordSource); + password = passwordFromInputStream(process.getInputStream()); + process.waitFor(); + } catch (IOException exception) { + logger.warn(exception); + } catch (InterruptedException exception) { + logger.warn(exception); + } + } + } + } + + if (password != null && passwordEncoder != null) { + dataSourceDescriptor.setPassword(passwordEncoder.decodePassword(password, encoderKey)); + } + } else if (localName.equals("url")) { + dataSourceDescriptor.setDataSourceUrl(attributes.getValue("value")); + } else if (localName.equals("connectionPool")) { + String min = attributes.getValue("min"); + if (min != null) { + try { + dataSourceDescriptor.setMinConnections(Integer.parseInt(min)); + } catch (NumberFormatException nfex) { + logger.info("Non-numeric 'min' attribute", nfex); + throw new ConfigurationException("Non-numeric 'min' attribute '%s'", nfex, min); + } + } + + String max = attributes.getValue("max"); + if (max != null) { + try { + dataSourceDescriptor.setMaxConnections(Integer.parseInt(max)); + } catch (NumberFormatException nfex) { + logger.info("Non-numeric 'max' attribute", nfex); + throw new ConfigurationException("Non-numeric 'max' attribute '%s'", nfex, max); + } + } + } + + return super.createChildTagHandler(namespaceURI, localName, name, attributes); + } + } }