1 package org.apache.torque.generator.configuration.outlet;
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.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.torque.generator.configuration.ConfigurationException;
28 import org.apache.torque.generator.configuration.mergepoint.MergepointMapping;
29 import org.apache.torque.generator.outlet.Outlet;
30 import org.apache.torque.generator.qname.QualifiedName;
31
32 /**
33 * Administers the available Outlets.
34 */
35 public class OutletConfiguration
36 {
37 /** A map containing all outlets, keyed by their name. */
38 private Map<QualifiedName, Outlet> outlets
39 = new HashMap<QualifiedName, Outlet>();
40
41 /** A map containing all isolated mergepoint mappings,
42 * keyed by their name. */
43 private Map<String, MergepointMapping> mergepointMappings
44 = new HashMap<String, MergepointMapping>();
45
46 /**
47 * Whether the mergepoint mappings have been resolved and added to
48 * the respective outlets.
49 */
50 private boolean mergepointMappingsResolved = false;
51
52 /**
53 * Creates a OutletConfiguration containing a list of Outlets.
54 *
55 * @param outlets the list of outlets, not null.
56 *
57 * @throws ConfigurationException if outlets contains
58 * two outlets with the same name.
59 * @throws NullPointerException if outlets is null.
60 */
61 public OutletConfiguration(Collection<Outlet> outlets,
62 Collection<MergepointMapping> mergepointMappings)
63 throws ConfigurationException
64 {
65 if (outlets == null)
66 {
67 throw new NullPointerException("outlets must not be null");
68 }
69 if (mergepointMappings == null)
70 {
71 throw new NullPointerException(
72 "mergepointMappings must not be null");
73 }
74 for (Outlet outlet : outlets)
75 {
76 addOutlet(outlet);
77 }
78 for (MergepointMapping mergepointMapping : mergepointMappings)
79 {
80 this.mergepointMappings.put(
81 mergepointMapping.getName(),
82 mergepointMapping);
83 }
84 }
85
86 /**
87 * Resolves the isolated mergepoint mappings and adds them to
88 * the relevant outlets.
89 * @throws ConfigurationException
90 */
91 public void resolveMergepointMappings() throws ConfigurationException
92 {
93 for (Map.Entry<String, MergepointMapping> entry
94 : mergepointMappings.entrySet())
95 {
96 String name = entry.getKey();
97 QualifiedName qualifiedMergepointName
98 = new QualifiedName(name);
99 if (qualifiedMergepointName.getNamespace().isRoot())
100 {
101 // outlet name is missing
102 throw new ConfigurationException(
103 "The isolated mergepoint mapping with the name "
104 + name
105 + " needs to be qualified with the outlet name");
106 }
107 QualifiedName outletName = new QualifiedName(
108 qualifiedMergepointName.getNamespace().toString());
109 Outlet outlet = outlets.get(outletName);
110 if (outlet == null)
111 {
112 throw new ConfigurationException(
113 "No outlet with name "
114 + outletName
115 + "exists (required by the isolated mergepoint mapping"
116 + " with the name "
117 + name
118 + ")");
119 }
120 MergepointMapping originalMergepointMapping = entry.getValue();
121 MergepointMapping resolvedMergepointMapping
122 = new MergepointMapping(
123 qualifiedMergepointName.getName(),
124 originalMergepointMapping.getActions());
125 outlet.setMergepointMapping(resolvedMergepointMapping);
126 }
127 mergepointMappingsResolved = true;
128 mergepointMappings.clear();
129 }
130
131 /**
132 * Returns a map containing all the configured outlets, keyed by their
133 * name.
134 *
135 * @return all outlets, not null.
136 *
137 * @throws IllegalStateException if the mergepoint mappings have not
138 * yet been resolved.
139 */
140 public Map<QualifiedName, Outlet> getOutlets()
141 {
142 if (!mergepointMappingsResolved)
143 {
144 throw new IllegalStateException(
145 "Mergepoint mappings must be resoved first");
146 }
147 return Collections.unmodifiableMap(outlets);
148 }
149
150 /**
151 * Returns the outlet with the name <code>name</code>.
152 *
153 * @param name the name of the outlet to be returned.
154 *
155 * @return The outlet with the given name, or null if it does not
156 * exist.
157 *
158 * @throws IllegalStateException if the mergepoint mappings have not
159 * yet been resolved.
160 */
161 public Outlet getOutlet(QualifiedName name)
162 {
163 if (!mergepointMappingsResolved)
164 {
165 throw new IllegalStateException(
166 "Mergepoint mappings must be resoved first");
167 }
168 return outlets.get(name);
169 }
170
171 /**
172 * Adds a outlet.
173 *
174 * @param outlet the outlet to be added, not null.
175 *
176 * @throws ConfigurationException if a outlet with the outlet's name
177 * already exists in the configuration.
178 * @throws NullPointerException if outlet is null.
179 */
180 public void addOutlet(Outlet outlet)
181 throws ConfigurationException
182 {
183 if (outlet == null)
184 {
185 throw new NullPointerException("outlet must not be null");
186 }
187 Outlet existingOutlet = outlets.get(outlet.getName());
188 if (existingOutlet != null)
189 {
190 throw new ConfigurationException("Trying to add the outlet "
191 + outlet.getName()
192 + " and class "
193 + outlet.getClass().getName()
194 + " : A outlet with the same name "
195 + " already exists, it has the class "
196 + existingOutlet.getClass().getName());
197 }
198 outlets.put(outlet.getName(), outlet);
199 }
200
201 /**
202 * Creates a String view of this object for debuggung purposes.
203 *
204 * @return a String view of this object, never null.
205 *
206 * @see java.lang.Object#toString()
207 */
208 @Override
209 public String toString()
210 {
211 StringBuffer result = new StringBuffer("(outlets=[")
212 .append(outlets)
213 .append(")");
214 return result.toString();
215 }
216
217 /**
218 * Returns whether a outlet for the given name exists.
219 * Name and namespace must match exactly.
220 *
221 * @param qualifiedName the name of the outlet.
222 *
223 * @return true if a outlet with the name exists, false otherwise.
224 */
225 public boolean outletExists(QualifiedName qualifiedName)
226 {
227 return outlets.containsKey(qualifiedName);
228 }
229 }