001: package org.andromda.andromdapp;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005:
006: import java.net.URL;
007:
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.LinkedHashMap;
011: import java.util.List;
012: import java.util.Map;
013: import java.util.Properties;
014:
015: import org.andromda.core.common.ResourceUtils;
016:
017: /**
018: * Represents the configuration of an AndroMDAppType.
019: *
020: * @author Chad Brandon
021: * @see AndroMDAppType
022: */
023: public class Configuration {
024: /**
025: * Stores any properties defined in this configuration.
026: */
027: private final Map properties = new LinkedHashMap();
028:
029: /**
030: * Adds a property with the name and value to the current properties
031: * map.
032: *
033: * @param name the name of the property to add.
034: * @param value the value of the property.
035: */
036: public void addProperty(final String name, final String value) {
037: this .properties.put(name, value);
038: }
039:
040: /**
041: * Stores any locations to property files.
042: */
043: private final List locations = new ArrayList();
044:
045: /**
046: * Adds a location to this configuration.
047: *
048: * @param location the path of the location.
049: */
050: public void addLocation(final String location) {
051: this .locations.add(location);
052: }
053:
054: /**
055: * The patterns to use for the locations
056: */
057: private static final String[] LOCATION_PATTERNS = new String[] { "**/*.properties" };
058:
059: /**
060: * Retrieves all properties including all those found in the given locations.
061: *
062: * @return the map containing all properties
063: */
064: public Map getAllProperties() {
065: final Map allProperties = new LinkedHashMap();
066: for (final Iterator iterator = this .locations.iterator(); iterator
067: .hasNext();) {
068: final String location = (String) iterator.next();
069: final List resources = ResourceUtils.getDirectoryContents(
070: ResourceUtils.toURL(location), true,
071: LOCATION_PATTERNS);
072: if (resources != null) {
073: for (final Iterator resourceIterator = resources
074: .iterator(); resourceIterator.hasNext();) {
075: final String path = (String) resourceIterator
076: .next();
077: final URL resource = ResourceUtils.toURL(path);
078: final Properties properties = new Properties();
079: InputStream stream = null;
080: try {
081: stream = resource.openStream();
082: properties.load(stream);
083: allProperties.putAll(properties);
084: } catch (final Exception exception) {
085: // - ignore
086: } finally {
087: try {
088: stream.close();
089: stream = null;
090: } catch (IOException exception) {
091: // - ignore
092: }
093: }
094: }
095: }
096: }
097: allProperties.putAll(this .properties);
098: return allProperties;
099: }
100:
101: /**
102: * Stores whether or not the application should be overwritten if it previously existed.
103: */
104: private boolean ovewrite;
105:
106: /**
107: * Whether or not the application should be overwritten if it already exits.
108: *
109: * @return true/false
110: */
111: public boolean isOverwrite() {
112: return this .ovewrite;
113: }
114:
115: /**
116: * Sets whether or not the application should be overwritten if it previously existed.
117: *
118: * @param overwrite true/false
119: */
120: public void setOverwrite(final boolean overwrite) {
121: this.ovewrite = overwrite;
122: }
123: }
|