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.File;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.torque.generator.configuration.controller.Loglevel;
30  import org.apache.torque.generator.configuration.controller.Output;
31  import org.apache.torque.generator.configuration.outlet.OutletConfiguration;
32  import org.apache.torque.generator.configuration.source.EntityReferences;
33  import org.apache.torque.generator.option.Options;
34  import org.apache.torque.generator.source.SourceProvider;
35  
36  /**
37   * Contains all information to run a generation unit.
38   * Provides state checking, i.e. getters can only be called
39   * after all setters has been called.
40   */
41  public class UnitConfiguration
42  {
43      /** The list of all output activities of the generation unit. */
44      private List<Output> outputList = new ArrayList<Output>();
45  
46      /** All options for the generation unit. */
47      private Options options;
48  
49      /** Overrides the source provider defined in the control file. */
50      private SourceProvider overrideSourceProvider;
51  
52      /** Whether the overrideSourceProviderInitialized was initialized. */
53      private boolean overrideSourceProviderInitialized = false;
54  
55      /** The configuration for the available outlets. */
56      private OutletConfiguration outletConfiguration;
57  
58      /** The entity references for resolving schema files for XML sources. */
59      private EntityReferences entityReferences;
60  
61      /** The configuration handlers which are used to read this configuration. */
62      private ConfigurationHandlers configurationHandlers;
63  
64      /**
65       * The base output directories for the generated files,
66       * keyed by the output directory key.
67       */
68      private Map<String, File> outputDirectoryMap = new HashMap<String, File>();
69  
70      /**
71       * A directory where the Torque generator can store internal files
72       */
73      private File workDirectory;
74  
75      /** The Loglevel for generation. */
76      private Loglevel loglevel = Loglevel.INFO;
77  
78      /**
79       * The output encoding if no specific encoding has been set in the output;
80       * null for the default platform encoding.
81       */
82      private String defaultOutputEncoding = null;
83  
84      /**
85       * Returns the configuration of the outlets in this generation unit.
86       *
87       * @return the outlet configuration, not null.
88       *
89       * @throws IllegalStateException if outletConfiguration was not set.
90       */
91      public OutletConfiguration getOutletConfiguration()
92      {
93          if (outletConfiguration == null)
94          {
95              throw new IllegalStateException(
96                      "outletConfiguration is not initialized");
97          }
98          return outletConfiguration;
99      }
100 
101     /**
102      * Sets the outlet configuration of the associated configuration unit.
103      *
104      * @param outletConfiguration the outlet configuration, not null.
105      *
106      * @throws NullPointerException if outletConfiguration is null.
107      */
108     public void setOutletConfiguration(
109             OutletConfiguration outletConfiguration)
110     {
111         if (outletConfiguration == null)
112         {
113             throw new NullPointerException(
114                     "outletConfiguration cannot be null");
115         }
116         this.outletConfiguration = outletConfiguration;
117     }
118 
119     /**
120      * Returns the options of the associated configuration unit.
121      *
122      * @return the options, not null.
123      *
124      * @throws IllegalStateException if options were not yet set.
125      */
126     public Options getOptions()
127     {
128         if (options == null)
129         {
130             throw new IllegalStateException(
131                     "options are not initialized");
132         }
133         return options;
134     }
135 
136     /**
137      * Sets the options of the associated configuration unit.
138      *
139      * @param options the options, not null.
140      *
141      * @throws NullPointerException if options is null.
142      */
143     public void setOptions(Options options)
144     {
145         if (options == null)
146         {
147             throw new NullPointerException(
148                     "options cannot be null");
149         }
150         this.options = options;
151     }
152 
153     /**
154      * Returns the output directory for a given output directory key.
155      *
156      * @param outputDirKey the key which output directory should be returned.
157      *        Null represents the default output directory and is always
158      *        mapped to a non-null value.
159      *
160      * @return the output directory for the key, not null.
161      *
162      * @throws IllegalStateException if the default output directory
163      *         was not yet set.
164      */
165     public File getOutputDirectory(String outputDirKey)
166     {
167         File result = outputDirectoryMap.get(outputDirKey);
168         if (result == null)
169         {
170             throw new IllegalStateException(
171                     "The output directory for the key " + outputDirKey
172                     + "is not set");
173         }
174         return result;
175     }
176 
177     /**
178      * Returns the output directory map which contains the mapping
179      * from output directory key to output directory.
180      *
181      * @return the immutable output directory map, not null, contains at least
182      *         a mapping for the key null.
183      *
184      * @throws IllegalStateException if no mapping is contained
185      *         for the key null.
186      */
187     public Map<String, File> getOutputDirectoryMap()
188     {
189         if (outputDirectoryMap.get(null) == null)
190         {
191             throw new IllegalStateException(
192                     "outputDirectoryMap does not contain a mapping"
193                         + " for the key null");
194         }
195         return Collections.unmodifiableMap(outputDirectoryMap);
196     }
197 
198     /**
199      * Sets the output directory map which contains the mapping
200      * from output directory key to output directory.
201      *
202      * @param outputDirectoryMap the new output directory map,
203      *        must contain at least a mapping for the key null.
204      *
205      * @throws NullPointerException if outputDirectoryMap is null.
206      * @throws IllegalStateException if the target directory was not yet set.
207      */
208     public void setOutputDirectoryMap(Map<String, File> outputDirectoryMap)
209     {
210         if (outputDirectoryMap == null)
211         {
212             throw new NullPointerException(
213                     "outputDirectoryMap may not be null");
214         }
215         if (outputDirectoryMap.get(null) == null)
216         {
217             throw new IllegalArgumentException(
218                     "outputDirectoryMap(null) may not be null");
219         }
220         this.outputDirectoryMap.clear();
221         this.outputDirectoryMap.putAll(outputDirectoryMap);
222     }
223 
224     /**
225      * Returns the directory where the generator can store internal files.
226      *
227      * @return the directory where the generator can store internal files,
228      *         not null.
229      *
230      * @throws IllegalStateException if the target directory was not yet set.
231      */
232     public File getWorkDirectory()
233     {
234         if (workDirectory == null)
235         {
236             throw new IllegalStateException(
237                     "workDirectory is not initialized");
238         }
239         return workDirectory;
240     }
241 
242     /**
243      * Sets the directory where the generator can store internal files.
244      *
245      * @param workDirectory the work directory, not null.
246      *
247      * @throws NullPointerException if workDirectory is null.
248      */
249     public void setWorkDirectory(File workDirectory)
250     {
251         if (workDirectory == null)
252         {
253             throw new NullPointerException("workDirectory cannot be null");
254         }
255         this.workDirectory = workDirectory;
256     }
257 
258     /**
259      * Sets the output activities of the associated configuration unit.
260      *
261      * @param outputList the output activities, not null.
262      *
263      * @throws NullPointerException if outputFiles is null.
264      */
265     public void setOutputList(List<Output> outputList)
266     {
267         if (outputList == null)
268         {
269             throw new NullPointerException(
270                     "outputList cannot be null");
271         }
272         this.outputList = outputList;
273     }
274 
275     /**
276      * Returns the list of output definitions of the associated configuration
277      * unit.
278      *
279      * @return the output definitions, not null.
280      *
281      * @throws IllegalStateException if the output definitions were not yet set.
282      */
283     public List<Output> getOutputList()
284     {
285         if (outputList == null)
286         {
287             throw new IllegalStateException(
288                     "outputFiles are not initialized");
289         }
290         return Collections.unmodifiableList(outputList);
291     }
292 
293     /**
294      * Returns the Loglevel during generation.
295      *
296      * @return the Loglevel, not null.
297      *
298      * @throws IllegalStateException if the loglevel is not yet set.
299      */
300     public Loglevel getLoglevel()
301     {
302         if (loglevel == null)
303         {
304             throw new IllegalStateException(
305                     "loglevel is not initialized");
306         }
307         return loglevel;
308     }
309 
310     /**
311      * Sets the Loglevel during generation.
312      *
313      * @param loglevel the Loglevel, not null.
314      *
315      * @throws NullPointerException if loglevel is set to null.
316      */
317     public void setLoglevel(Loglevel loglevel)
318     {
319         if (loglevel == null)
320         {
321             throw new NullPointerException("loglevel is null");
322         }
323         this.loglevel = loglevel;
324     }
325 
326     /**
327      * Returns the configuration handlers used to parse the configuration
328      * of this generation unit.
329      *
330      * @return the configuration handlers, not null.
331      *
332      * @throws IllegalStateException if configurationHandlers was not set.
333      */
334     public ConfigurationHandlers getConfigurationHandlers()
335     {
336         if (configurationHandlers == null)
337         {
338             throw new IllegalStateException(
339                     "configurationHandlers is not initialized");
340         }
341         return configurationHandlers;
342     }
343 
344     /**
345      * Sets the configuration handlers used to parse the configuration
346      * of this generation unit.
347      *
348      * @param configurationHandlers the configuration handlers, not null.
349      *
350      * @throws NullPointerException if configurationHandlers is null.
351      */
352     public void setConfigurationHandlers(
353             ConfigurationHandlers configurationHandlers)
354     {
355         if (configurationHandlers == null)
356         {
357             throw new NullPointerException(
358                     "configurationHandlers cannot be null");
359         }
360         this.configurationHandlers = configurationHandlers;
361     }
362 
363     /**
364      * Returns the source provider which overrides the source provider defined
365      * in the control file.
366      *
367      * @return the overriding source provider, or null to use the
368      *         source provider defined in the control file.
369      *
370      * @throws NullPointerException if overrideSourceFileset was not yet set.
371      */
372     public SourceProvider getOverrideSourceProvider()
373     {
374         if (!overrideSourceProviderInitialized)
375         {
376             throw new IllegalStateException(
377                     "overrideSourceProvider is not initialized");
378         }
379         return overrideSourceProvider;
380     }
381 
382     /**
383      * Sets the source provider which overrides the source provider defined
384      * in the control file.
385      *
386      * @param overrideSourceProvider the override source provider, or null
387      *        to use the source provider defined in the control file.
388      */
389     public void setOverrideSourceProvider(
390             SourceProvider overrideSourceProvider)
391     {
392         this.overrideSourceProvider = overrideSourceProvider;
393         overrideSourceProviderInitialized = true;
394     }
395 
396     /**
397      * Returns the entityReferences of the associated configuration unit.
398      *
399      * @return the entityReferences, not null.
400      *
401      * @throws IllegalStateException if entityReferences were not yet set.
402      */
403     public EntityReferences getEntityReferences()
404     {
405         if (entityReferences == null)
406         {
407             throw new IllegalStateException(
408                     "entityReferences are not initialized");
409         }
410         return entityReferences;
411     }
412 
413     /**
414      * Sets the entityReferences of the associated configuration unit.
415      *
416      * @param options the entityReferences, not null.
417      *
418      * @throws NullPointerException if entityReferences is null.
419      */
420     public void setEntityReferences(EntityReferences entityReferences)
421     {
422         if (entityReferences == null)
423         {
424             throw new NullPointerException(
425                     "entityReferences cannot be null");
426         }
427         this.entityReferences = entityReferences;
428     }
429 
430     /**
431      * Returns the output encoding if no specific encoding has been set
432      * in the output.
433      *
434      * @return the default output encoding, null for the default
435      *         platform encoding.
436      */
437     public String getDefaultOutputEncoding()
438     {
439         return defaultOutputEncoding;
440     }
441 
442     /**
443      * Sets the output encoding if no specific encoding has been set
444      * in the output.
445      *
446      * @param defaultOutputEncoding the default output encoding,
447      *        null for the default platform encoding.
448      */
449     public void setDefaultOutputEncoding(String defaultOutputEncoding)
450     {
451         this.defaultOutputEncoding = defaultOutputEncoding;
452     }
453 
454     /**
455      * Checks whether the unit configuration is fully initialized.
456      *
457      * @return true if the unit configuration is fully initialized,
458      *          false otherwise.
459      */
460     public boolean isInit()
461     {
462         if (outputList == null
463             || options == null
464             || outletConfiguration == null
465             || outputDirectoryMap.get(null) == null
466             || loglevel == null
467             || configurationHandlers == null
468             || entityReferences == null
469             || !overrideSourceProviderInitialized)
470         {
471             return false;
472         }
473         return true;
474     }
475 
476     @Override
477     public String toString()
478     {
479         StringBuffer result = new StringBuffer();
480         result.append("(outputFiles=")
481                 .append(outputList.toString())
482                 .append(", options=")
483                 .append(options)
484                 .append(", outletConfiguration=")
485                 .append(outletConfiguration)
486                 .append(", outputDirectoryMap=")
487                 .append(outputDirectoryMap)
488                 .append(", loglevel=")
489                 .append(loglevel)
490                 .append(")");
491         return result.toString();
492     }
493 }