View Javadoc
1   package org.andromda.andromdapp;
2   
3   import java.io.InputStream;
4   import java.net.URL;
5   import java.util.ArrayList;
6   import java.util.LinkedHashMap;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Properties;
10  import org.andromda.core.common.ResourceUtils;
11  import org.apache.commons.io.IOUtils;
12  
13  /**
14   * Represents the configuration of an AndroMDAppType.
15   *
16   * @author Chad Brandon
17   * @see AndroMDAppType
18   */
19  public class Configuration
20  {
21      /**
22       * Stores any properties defined in this configuration.
23       */
24      private final Properties properties = new Properties();
25  
26      /**
27       * Adds a property with the name and value to the current properties
28       * map.
29       *
30       * @param name the name of the property to add.
31       * @param value the value of the property.
32       */
33      public void addProperty(
34          final String name,
35          final String value)
36      {
37          this.properties.put(
38              name,
39              value);
40      }
41  
42      /**
43       * Stores any locations to property files.
44       */
45      private final List<String> locations = new ArrayList<String>();
46  
47      /**
48       * Adds a location to this configuration.
49       *
50       * @param location the path of the location.
51       */
52      public void addLocation(final String location)
53      {
54          this.locations.add(location);
55      }
56  
57      /**
58       * The patterns to use for the locations
59       */
60      private static final String[] LOCATION_PATTERNS = new String[] {"**/*.properties"};
61  
62      /**
63       * Retrieves all properties including all those found in the given locations.
64       *
65       * @return the map containing all properties
66       */
67      public Map<String,String> getAllProperties()
68      {
69          final Map<String, String> allProperties = new LinkedHashMap<String, String>();
70          for (final String location : this.locations)
71          {
72              final List<String> resources =
73                      ResourceUtils.getDirectoryContents(
74                              ResourceUtils.toURL(location),
75                              true,
76                              LOCATION_PATTERNS);
77              if (resources != null)
78              {
79                  for (final String path : resources)
80                  {
81                      final URL resource = ResourceUtils.toURL(path);
82                      final Properties properties = new Properties();
83                      InputStream stream = null;
84                      try
85                      {
86                          stream = resource.openStream();
87                          properties.load(stream);
88                          addProperties(allProperties, properties);
89                      }
90                      catch (final Exception ignore)
91                      {
92                          // - ignore
93                      }
94                      finally
95                      {
96                          IOUtils.closeQuietly(stream);
97                      }
98                  }
99              }
100         }
101         addProperties(allProperties, this.properties);
102         return allProperties;
103     }
104 
105     /**
106      * @param target
107      * @param properties
108      */
109     protected void addProperties(final Map<String, String> target, final Properties properties)
110     {
111         for (final String propertyName : properties.stringPropertyNames())
112         {
113             target.put(propertyName, properties.getProperty(propertyName));
114         }
115     }
116 
117     /**
118      * Stores whether or not the application should be overwritten if it previously existed.
119      */
120     private boolean overwrite;
121 
122     /**
123      * Whether or not the application should be overwritten if it already exits.
124      *
125      * @return true/false
126      */
127     public boolean isOverwrite()
128     {
129         return this.overwrite;
130     }
131     /**
132      * Sets whether or not the application should be overwritten if it previously existed.
133      *
134      * @param overwrite true/false
135      */
136     public void setOverwrite(final boolean overwrite)
137     {
138         this.overwrite = overwrite;
139     }
140 }