1 package org.apache.torque.generator.configuration.outlet;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35 public class OutletConfiguration
36 {
37
38 private Map<QualifiedName, Outlet> outlets
39 = new HashMap<QualifiedName, Outlet>();
40
41
42
43 private Map<String, MergepointMapping> mergepointMappings
44 = new HashMap<String, MergepointMapping>();
45
46
47
48
49
50 private boolean mergepointMappingsResolved = false;
51
52
53
54
55
56
57
58
59
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
88
89
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
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
133
134
135
136
137
138
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
152
153
154
155
156
157
158
159
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
173
174
175
176
177
178
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
203
204
205
206
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
219
220
221
222
223
224
225 public boolean outletExists(QualifiedName qualifiedName)
226 {
227 return outlets.containsKey(qualifiedName);
228 }
229 }