1 package org.apache.torque.generator.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.torque.generator.configuration.paths.ProjectPaths;
35 import org.apache.torque.generator.configuration.paths.TorqueGeneratorPaths;
36
37
38
39
40
41 public class JarConfigurationProvider implements ConfigurationProvider
42 {
43
44 private static Log log
45 = LogFactory.getLog(JarConfigurationProvider.class);
46
47
48
49
50 private ProjectPaths projectPaths;
51
52
53
54
55
56 private TorqueGeneratorPaths configurationPaths;
57
58
59
60
61 private JarFile jarFile;
62
63
64
65
66
67
68
69
70
71
72
73
74 public JarConfigurationProvider(
75 ProjectPaths projectPaths,
76 TorqueGeneratorPaths configurationPaths)
77 throws ConfigurationException
78 {
79 if (projectPaths == null)
80 {
81 throw new NullPointerException("projectPaths is null");
82 }
83 if (configurationPaths == null)
84 {
85 throw new NullPointerException("configurationPaths is null");
86 }
87 this.projectPaths = projectPaths;
88 this.configurationPaths = configurationPaths;
89
90 try
91 {
92 jarFile = new JarFile(projectPaths.getConfigurationPath());
93 }
94 catch (IOException e)
95 {
96 log.error("Could not open jar File "
97 + projectPaths.getConfigurationPath()
98 .getAbsolutePath());
99 throw new ConfigurationException(e);
100 }
101 }
102
103 public InputStream getControlConfigurationInputStream()
104 throws ConfigurationException
105 {
106 return getInputStream(
107 configurationPaths.getControlConfigurationFile(),
108 configurationPaths.getConfigurationDirectory(),
109 "configuration");
110 }
111
112
113 public String getControlConfigurationLocation()
114 throws ConfigurationException
115 {
116 return projectPaths.getConfigurationPath() + ":"
117 + configurationPaths.getConfigurationDirectory() + "/"
118 + configurationPaths.getControlConfigurationFile();
119 }
120
121 public InputStream getTemplateInputStream(String name)
122 throws ConfigurationException
123 {
124 return getInputStream(
125 name,
126 configurationPaths.getTemplateDirectory(),
127 "template");
128 }
129
130 public InputStream getOutletConfigurationInputStream(String name)
131 throws ConfigurationException
132 {
133 return getInputStream(
134 name,
135 configurationPaths.getOutletDirectory(),
136 "outlet configuration");
137 }
138
139 public InputStream getResourceInputStream(String name)
140 throws ConfigurationException
141 {
142 return getInputStream(
143 name,
144 configurationPaths.getResourceDirectory(),
145 "resource");
146 }
147
148 public InputStream getOptionsInputStream(String name)
149 throws ConfigurationException
150 {
151 return getInputStream(
152 name,
153 configurationPaths.getConfigurationDirectory(),
154 "option");
155 }
156
157 private InputStream getInputStream(
158 String name,
159 String directory,
160 String description)
161 throws ConfigurationException
162 {
163 String fileName = directory + "/" + name;
164
165 InputStream inputStream = null;
166 try
167 {
168 JarEntry jarEntry = jarFile.getJarEntry(fileName);
169 inputStream = jarFile.getInputStream(jarEntry);
170 }
171 catch (IOException e)
172 {
173 throw new ConfigurationException(
174 "Could not read " + description + " file "
175 + fileName
176 + " in jar file "
177 + projectPaths.getConfigurationPath(),
178 e);
179 }
180 BufferedInputStream bis = new BufferedInputStream(inputStream);
181 if (log.isDebugEnabled())
182 {
183 log.debug("Reading " + description + " file: '"
184 + projectPaths.getConfigurationPath()
185 + "' in jar file "
186 + projectPaths.getConfigurationPath());
187 }
188 return bis;
189 }
190
191 public Collection<String> getOutletConfigurationNames()
192 throws ConfigurationException
193 {
194 return getOutletConfigurationNames(
195 jarFile,
196 configurationPaths.getOutletDirectory());
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210 static Collection<String> getOutletConfigurationNames(
211 JarFile jarFile,
212 String outletConfigurationDirectory)
213 {
214 if (log.isDebugEnabled())
215 {
216 log.debug("Analyzing jar file " + jarFile.getName()
217 + " seeking Directory " + outletConfigurationDirectory);
218 }
219
220 List<String> result = new ArrayList<String>();
221
222 Enumeration<JarEntry> entries = jarFile.entries();
223 while (entries.hasMoreElements())
224 {
225 JarEntry jarEntry = entries.nextElement();
226 if (jarEntry.isDirectory())
227 {
228 continue;
229 }
230 String rawName = jarEntry.getName();
231 if (!rawName.startsWith(outletConfigurationDirectory))
232 {
233 continue;
234 }
235 String name = rawName.substring(rawName.lastIndexOf('/') + 1);
236
237 int expectedRawNameLength
238 = outletConfigurationDirectory.length()
239 + name.length()
240 + 1;
241 if (rawName.length() != expectedRawNameLength)
242 {
243
244
245
246 continue;
247 }
248 result.add(name);
249 }
250 if (log.isDebugEnabled())
251 {
252 log.debug("Found the following outlet configuration files "
253 + result);
254 }
255 return result;
256 }
257 }