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
15
16
17
18
19 public class Configuration
20 {
21
22
23
24 private final Properties properties = new Properties();
25
26
27
28
29
30
31
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
44
45 private final List<String> locations = new ArrayList<String>();
46
47
48
49
50
51
52 public void addLocation(final String location)
53 {
54 this.locations.add(location);
55 }
56
57
58
59
60 private static final String[] LOCATION_PATTERNS = new String[] {"**/*.properties"};
61
62
63
64
65
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
93 }
94 finally
95 {
96 IOUtils.closeQuietly(stream);
97 }
98 }
99 }
100 }
101 addProperties(allProperties, this.properties);
102 return allProperties;
103 }
104
105
106
107
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
119
120 private boolean overwrite;
121
122
123
124
125
126
127 public boolean isOverwrite()
128 {
129 return this.overwrite;
130 }
131
132
133
134
135
136 public void setOverwrite(final boolean overwrite)
137 {
138 this.overwrite = overwrite;
139 }
140 }