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.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.apache.torque.generator.configuration.controller.Loglevel;
27
28 /**
29 * The configuration class keeps the complete configuration needed for the
30 * generation process. It is supplied with UnitDescriptors describing the
31 * available units of generation, and can then read the configuration.
32 * After the configuration is read, no new units of generation can be added.
33 */
34 public class Configuration
35 {
36 /**
37 * All known units of generation.
38 */
39 private List<UnitDescriptor> unitDescriptors
40 = new ArrayList<UnitDescriptor>();
41
42 /**
43 * The configurations for all known units of generation.
44 */
45 private List<UnitConfiguration> unitConfigurations;
46
47 /**
48 * The available configuration handlers.
49 */
50 private ConfigurationHandlers configurationHandlers
51 = new ConfigurationHandlers();
52
53 /**
54 * Whether the configuration has already been read.
55 */
56 private boolean read = false;
57
58 /**
59 * Adds a unit of generation to the configuration.
60 *
61 * @param unitDescriptor Describes the unit of generation to add,
62 * not null.
63 *
64 * @throws NullPointerException if unitDescriptor is null.
65 * @throws IllegalStateException if the configuration has already been
66 * read.
67 */
68 public void addUnit(UnitDescriptor unitDescriptor)
69 {
70 if (unitDescriptor == null)
71 {
72 throw new NullPointerException("unitDescriptor must not be null.");
73 }
74 if (read)
75 {
76 throw new IllegalStateException(
77 "Configuration has already been read.");
78 }
79 unitDescriptors.add(unitDescriptor);
80 }
81
82
83 /**
84 * Adds several units of generation to the configuration.
85 *
86 * @param unitDescriptors Describes the units of generation to add,
87 * not null.
88 *
89 * @throws NullPointerException if unitDescriptors is null or the list
90 * contains a null entry.
91 * @throws IllegalStateException if the configuration has already been
92 * read.
93 */
94 public void addUnits(List<UnitDescriptor> unitDescriptors)
95 {
96 for (UnitDescriptor unitDescriptor : unitDescriptors)
97 {
98 addUnit(unitDescriptor);
99 }
100 }
101
102 /**
103 * Reads the configuration.
104 *
105 * @throws ConfigurationException if an error occurs during reading the
106 * configuration.
107 */
108 public void read() throws ConfigurationException
109 {
110 if (read)
111 {
112 throw new IllegalStateException(
113 "Configuration has already been read.");
114 }
115 unitConfigurations = new ArrayList<UnitConfiguration>();
116 Loglevel oldLoglevel = Loglevel.getCurrentLoglevel();
117 for (UnitDescriptor unitDescriptor : unitDescriptors)
118 {
119 if (unitDescriptor.getLoglevel() != null)
120 {
121 unitDescriptor.getLoglevel().apply();
122 }
123 else
124 {
125 oldLoglevel.apply();
126 }
127 UnitConfigurationReader configurationReader
128 = new UnitConfigurationReader();
129
130 UnitConfiguration unitConfiguration
131 = configurationReader.read(
132 unitDescriptor,
133 configurationHandlers);
134 if (unitDescriptor.getLoglevel() != null)
135 {
136 unitConfiguration.setLoglevel(unitDescriptor.getLoglevel());
137 }
138 unitConfigurations.add(unitConfiguration);
139 }
140 read = true;
141 }
142
143 /**
144 * Returns the list of UnitConfigurations.
145 *
146 * @return the list of unit configurations, never null.
147 *
148 * @throws IllegalStateException if the configuration was not yet read.
149 */
150 public List<UnitConfiguration> getUnitConfigurations()
151 {
152 if (!read)
153 {
154 throw new IllegalStateException("Configuration was not yet read");
155 }
156 return Collections.unmodifiableList(unitConfigurations);
157 }
158
159 /**
160 * Returns the available configuration handlers.
161 *
162 * @return the available configuration handlers, not null.
163 */
164 public ConfigurationHandlers getConfigurationHandlers()
165 {
166 return configurationHandlers;
167 }
168
169 /**
170 * Sets the available configuration handlers.
171 *
172 * @param configurationHandlers the available configuration handlers,
173 * not null.
174 */
175 public void setConfigurationHandlers(
176 ConfigurationHandlers configurationHandlers)
177 {
178 if (configurationHandlers == null)
179 {
180 throw new NullPointerException(
181 "configurationHandlers must not be null");
182 }
183 this.configurationHandlers = configurationHandlers;
184 }
185 }