View Javadoc

1   package org.apache.torque.generator.configuration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Provides InputStreams to read a configuration of a unit of generation from a
39   * jar file.
40   */
41  public class JarConfigurationProvider implements ConfigurationProvider
42  {
43      /** The logger. */
44      private static Log log
45              = LogFactory.getLog(JarConfigurationProvider.class);
46  
47      /**
48       * The paths needed to interact with the enclosing project, not null.
49       */
50      private ProjectPaths projectPaths;
51  
52      /**
53       * The internal directory structure of the generator configuration files,
54       * not null.
55       */
56      private TorqueGeneratorPaths configurationPaths;
57  
58      /**
59       * The jar file from which the configuration should be read, not null.
60       */
61      private JarFile jarFile;
62  
63      /**
64       * Constructor.
65       * @param projectPaths the paths needed to interact with the enclosing
66       *         project, not null.
67       * @param configurationPaths The internal directory structure of the
68       *         generator configuration files, not null.
69       *
70       * @throws NullPointerException if projectPaths or configurationPaths
71       *          are null.
72       * @throws ConfigurationException if the jar file can not be accessed.
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      * Extracts the outlet configuration files from a jar file.
201      * @param jarFile the jar file to process, not null.
202      * @param outletConfigurationDirectory the name of the directory
203      *        which contains the outlet configuration files. Cannot be
204      *        a composite path like parent/child.
205      * @return a set with the names of all outlet configuration files
206      *         contained in the jar file.
207      * @throws NullPointerException if jarFile
208      *         or outletConfigurationDirectory is null
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                 // file is in a subdirectory of outletConfigurationSubdir,
244                 // we only consider files directly in
245                 // outletConfigurationSubdir
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 }