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.File;
24  import java.io.FileInputStream;
25  import java.io.FileNotFoundException;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.List;
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 the configuration from a directory.
39   */
40  public class DirectoryConfigurationProvider implements ConfigurationProvider
41  {
42      /** The logger. */
43      private static Log log
44              = LogFactory.getLog(DirectoryConfigurationProvider.class);
45  
46      /**
47       * The paths needed to interact with the enclosing project, not null.
48       */
49      private ProjectPaths projectPaths;
50  
51      /**
52       * The internal directory structure of the Torque generator configuration
53       * files, not null.
54       */
55      private TorqueGeneratorPaths configurationPaths;
56  
57      /**
58       * Constructor.
59       *
60       * @param projectPaths the paths needed to interact with the enclosing
61       *        project, not null.
62       * @param configurationPaths The internal directory structure of the
63       *        generator files, not null.
64       *
65       * @throws NullPointerException if projectPaths or configurationPaths
66       *         are null.
67       */
68      public DirectoryConfigurationProvider(
69              ProjectPaths projectPaths,
70              TorqueGeneratorPaths configurationPaths)
71      {
72          if (projectPaths == null)
73          {
74              throw new NullPointerException("projectPaths is null");
75          }
76          if (configurationPaths == null)
77          {
78              throw new NullPointerException("configurationPaths is null");
79          }
80          this.projectPaths = projectPaths;
81          this.configurationPaths = configurationPaths;
82      }
83  
84      public InputStream getControlConfigurationInputStream()
85              throws ConfigurationException
86      {
87          return getInputStream(
88                  configurationPaths.getControlConfigurationFile(),
89                  configurationPaths.getConfigurationDirectory(),
90                  "control file");
91      }
92  
93      public String getControlConfigurationLocation()
94              throws ConfigurationException
95      {
96          return getFile(
97                  configurationPaths.getControlConfigurationFile(),
98                  configurationPaths.getConfigurationDirectory(),
99                  "control file").getAbsolutePath();
100     }
101 
102     public InputStream getTemplateInputStream(String name)
103             throws ConfigurationException
104     {
105         return getInputStream(
106                 name,
107                 configurationPaths.getTemplateDirectory(),
108                 "template");
109     }
110 
111     public InputStream getOutletConfigurationInputStream(String name)
112             throws ConfigurationException
113     {
114         return getInputStream(
115                 name,
116                 configurationPaths.getOutletDirectory(),
117                 "outlet configuration");
118     }
119 
120     public InputStream getResourceInputStream(String name)
121             throws ConfigurationException
122     {
123         return getInputStream(
124                 name,
125                 configurationPaths.getResourceDirectory(),
126                 "resource");
127     }
128 
129     public InputStream getOptionsInputStream(String name)
130         throws ConfigurationException
131     {
132         return getInputStream(
133                 name,
134                 configurationPaths.getConfigurationDirectory(),
135                 "option");
136     }
137 
138     private File getFile(
139                 String name,
140                 String directory,
141                 String description)
142             throws ConfigurationException
143     {
144         File file = null;
145         try
146         {
147             File configDir =  new File(
148                     projectPaths.getConfigurationPath(),
149                     directory);
150 
151             file = new File(configDir, name);
152             // use canonical file to resolve . and .. directories
153             file = file.getCanonicalFile();
154         }
155         catch (IOException e)
156         {
157             throw new ConfigurationException("Canonical name for "
158                     + description + file
159                     + " could not be calculated",
160                 e);
161         }
162         return file;
163     }
164 
165     private InputStream getInputStream(
166                 String name,
167                 String directory,
168                 String description)
169             throws ConfigurationException
170     {
171         File file = getFile(name, directory, description);
172 
173         InputStream inputStream;
174         try
175         {
176             inputStream = new FileInputStream(file);
177         }
178         catch (FileNotFoundException e)
179         {
180             throw new ConfigurationException(description + " file "
181                     + file.getAbsolutePath()
182                     + " not found",
183                 e);
184         }
185         BufferedInputStream bis = new BufferedInputStream(inputStream);
186         if (log.isDebugEnabled())
187         {
188             log.debug("Reading " + description + " file: '"
189                     + file.getAbsolutePath() + "'");
190         }
191         return bis;
192     }
193 
194     public Collection<String> getOutletConfigurationNames()
195         throws ConfigurationException
196     {
197         File outletConfigDir =  new File(
198                 projectPaths.getConfigurationPath(),
199                 configurationPaths.getOutletDirectory());
200 
201         List<String> result = new ArrayList<String>();
202         if (!outletConfigDir.isDirectory())
203         {
204             throw new ConfigurationException(
205                     "OutletsConfigDirectory "
206                         + outletConfigDir.getAbsolutePath()
207                         + "must be a directory");
208         }
209 
210         File[] sourceFiles = outletConfigDir.listFiles();
211         for (int fileNr = 0; fileNr < sourceFiles.length; ++fileNr)
212         {
213             if (!sourceFiles[fileNr].isDirectory()
214                     && sourceFiles[fileNr].getPath().endsWith("xml"))
215             {
216                 String name = sourceFiles[fileNr].getName();
217                 result.add(name);
218             }
219         }
220         return result;
221     }
222 }