1 package com.sourceforge.jpatterns.core.configuration.model;
2
3 import com.sourceforge.jpatterns.core.JPConstants;
4 import com.sourceforge.jpatterns.core.configuration.PropertiesBasedFactory;
5 import com.sourceforge.jpatterns.core.configuration.PropertiesProvider;
6 import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException;
7 import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException;
8 import com.sourceforge.jpatterns.schema.CastorConfigType;
9 import com.sourceforge.jpatterns.schema.CastorFactoryType;
10 import com.sourceforge.jpatterns.schema.CastorGroupType;
11 import com.sourceforge.jpatterns.schema.CastorGroupTypeItem;
12 import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType;
13 import com.sourceforge.jpatterns.schema.CastorSectionType;
14 import com.sourceforge.jpatterns.schema.Item;
15 import com.sourceforge.jpatterns.schema.JPatternsConfig;
16 import com.sourceforge.jpatterns.utils.CastorUtils;
17 import com.zmicer.utils.InputArgumentUtils;
18 import com.zmicer.utils.ObjectStateUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25
26
27
28
29
30
31
32
33 public class JPatternsConfigBeansBuilderImpl implements IJPatternsConfigBeansBuilder
34 {
35
36
37
38 final public static Logger LOG = Logger.getLogger(JPatternsConfigBeansBuilderImpl.class);
39
40
41
42
43 private static String OverridingDepth;
44
45
46
47
48
49 private static boolean OverrideNotDependingOnPriority;
50
51
52
53
54 {
55 PropertiesProvider propertiesProvider =
56 PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), false);
57 final String depth = propertiesProvider.getStringProperty(
58 com.sourceforge.jpatterns.core.configuration.PropertiesProvider.JPProperties.XML_CONFIG_OVERRIDING_DEPTH);
59 setOverridingDepth((null == depth) ? PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString() : depth);
60 final Boolean property = propertiesProvider.getBooleanProperty(com.sourceforge.jpatterns.core.configuration.
61 PropertiesProvider.JPProperties.XML_CONFIG_CUSTOM_OVERRIDES_NOT_DEPENDING_ON_PRIORITY);
62 setOverrideNotDependingOnPriority((null != property) && property);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public JPatternsConfigBean build(final JPatternsConfig config)
97 {
98
99 InputArgumentUtils.checkObjects(config);
100
101 JPatternsConfigBean result = new JPatternsConfigBean();
102
103 result.setDefaultScope(config.getDefaultScope());
104 result.setCastorConfig(config);
105
106
107 CastorUtils.validateAndNormalizeScopesPriorities(config);
108 fill(result, CastorUtils.extractCastorSectionTypeObjects(config));
109
110 return result;
111 }
112
113
114
115
116 public JPatternsConfigsBean build(final List<JPatternsConfigBaseBean> beans)
117 {
118 InputArgumentUtils.checkObjects(beans);
119 final JPatternsConfigsBean result = new JPatternsConfigsBean();
120
121 final List<CastorSectionType> sections = new ArrayList<CastorSectionType>();
122 for (JPatternsConfigBaseBean bean : beans)
123 {
124 ObjectStateUtils.strongCheck(bean);
125 sections.addAll(bean.getListOfSectionItems());
126 }
127 fill(result, sections);
128
129 return result;
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144 protected void fill(final JPatternsConfigBaseBean initialBaseBean, final List<CastorSectionType> sections)
145 {
146 InputArgumentUtils.checkObjects(initialBaseBean, sections);
147 ObjectStateUtils.strongCheck(initialBaseBean);
148
149
150 for (final CastorSectionType section : sections)
151 {
152 if (null == section.getScope() && null == section.getName())
153 {
154 throw new JPConfigException("The scope and name of CastorSectionType can not be null.");
155 }
156 final CastorSectionType alreadySection = initialBaseBean.getSection(section.getScope(), section.getName());
157
158 if (null == alreadySection ||
159 (!section.getScope().equals(alreadySection.getScope()) || !section.getName().equals(alreadySection.getName())))
160 {
161 initialBaseBean.setSection(section.getScope(), section);
162 }
163 else
164 {
165 initialBaseBean.setSection(section.getScope(),
166 (CastorSectionType) choiceOrMergeCastorNameScopePriorityTypes(section, alreadySection));
167 }
168 }
169
170
171
172 Map<String, Map<String, CastorSectionType>> sectionItems = initialBaseBean.getSectionItems();
173 for (String sectionScope : sectionItems.keySet())
174 {
175 final Map<String, CastorSectionType> sectionsMap = sectionItems.get(sectionScope);
176 for (String sectionName : sectionsMap.keySet())
177 {
178 final CastorSectionType section = sectionsMap.get(sectionName);
179 final List<CastorNameScopePriorityType> items = CastorUtils.extractCastorNameScopePriorityTypeObjects(section);
180 for (CastorNameScopePriorityType item : items)
181 {
182 if (null == item.getScope() && null == section.getName())
183 {
184 throw new JPConfigException("The scope and name of Item can not be null.");
185 }
186 if (StringUtils.isBlank(section.getScope()))
187 {
188 throw new JPInitializationException("Section should have scope set.");
189 }
190 final CastorNameScopePriorityType alreadyItem =
191 initialBaseBean.getBusinessItem(section.getScope(), sectionName, item.getScope(), item.getName());
192
193 if (null == alreadyItem)
194 {
195 initialBaseBean.setBusinessItem(section.getScope(), sectionName, item);
196 }
197 else
198 {
199 initialBaseBean.setBusinessItem(section.getScope(), sectionName,
200 choiceOrMergeCastorNameScopePriorityTypes(item, alreadyItem));
201 }
202 }
203 }
204 }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218 protected CastorNameScopePriorityType choiceOrMergeCastorNameScopePriorityTypes(final CastorNameScopePriorityType castor1,
219 final CastorNameScopePriorityType castor2)
220 {
221 InputArgumentUtils.checkObjects(castor1, castor2);
222 InputArgumentUtils.checkStrings(true, castor1.getScope(), castor1.getName(), castor1.getPriority());
223 InputArgumentUtils.checkStrings(true, castor2.getScope(), castor2.getName(), castor2.getPriority());
224 if (!castor1.getScope().equals(castor2.getScope()) || !castor1.getName().equals(castor2.getName()))
225 {
226 throw new IllegalArgumentException("The provided object should have the identical pathes to be checked / compared.");
227 }
228 if (!castor1.getClass().equals(castor2.getClass()))
229 {
230 throw new IllegalArgumentException("The classes of the provided objects should be identical.");
231 }
232
233 if (castor1.getPriority().equals(castor2.getPriority()))
234 {
235
236 if (!(castor1 instanceof CastorSectionType))
237 {
238 throw new JPConfigException("The elements with identical pathes can not be merged - scope [" +
239 castor1.getScope() + "], name [" + castor1.getName() + "], priority [" + castor1.getPriority() + "]; class name " +
240 "is [" + castor1.getClass().getName() + "]");
241 }
242
243 else
244 {
245
246 if (OverridingDepth.equals(
247 com.sourceforge.jpatterns.core.configuration.PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString()))
248 {
249 throw new JPConfigException("The elements with identical pathes can not be merged - scope [" +
250 castor1.getScope() + "], name [" + castor1.getName() + "], priority [" + castor1.getPriority() + "]; class " +
251 "name is [" + castor1.getClass().getName() + "]");
252 }
253
254
255 else if (OverridingDepth.equals(
256 com.sourceforge.jpatterns.core.configuration.PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_ITEM.toString()))
257 {
258 final List<CastorNameScopePriorityType> items =
259 CastorUtils.extractCastorNameScopePriorityTypeObjects((CastorSectionType) castor2);
260 for (CastorNameScopePriorityType item : items)
261 {
262
263
264
265 if (castor1 instanceof CastorConfigType)
266 {
267 if (!(item instanceof Item))
268 {
269 throw new JPConfigException("The CastorNameScopePriorityType item should be of Item type in the " +
270 "case castor section object is of the type CastorConfigType");
271 }
272 ((CastorConfigType) castor1).addItem((Item) item);
273 }
274 else if (castor1 instanceof CastorFactoryType)
275 {
276 if (!(item instanceof Item))
277 {
278 throw new JPConfigException("The CastorNameScopePriorityType item should be of Item type in the " +
279 "case castor section object is of the type CastorFactoryType");
280 }
281 ((CastorFactoryType) castor1).addItem((Item) item);
282 }
283 else if (castor1 instanceof CastorGroupType)
284 {
285 final CastorGroupTypeItem groupItem = CastorUtils.constructGroupItem(item);
286 ((CastorGroupType) castor1).addCastorGroupTypeItem(groupItem);
287 }
288 }
289 return castor1;
290 }
291 }
292 }
293 else
294 {
295 int firstPriority = CastorUtils.getPriority(castor1);
296 int secondPriority = CastorUtils.getPriority(castor2);
297 boolean firstPrioritized = false;
298 boolean secondPrioritized = false;
299 if (castor1.getPriority().startsWith(JPConstants.PRIORITIZED_PRIOTITY_PREFIX))
300 {
301 firstPrioritized = true;
302 }
303 if (castor2.getPriority().startsWith(JPConstants.PRIORITIZED_PRIOTITY_PREFIX))
304 {
305 secondPrioritized = true;
306 }
307
308 if (firstPrioritized && secondPrioritized || (!firstPrioritized && !secondPrioritized))
309 {
310 return (firstPriority > secondPriority) ? castor1 : castor2;
311 }
312
313 else if (firstPrioritized)
314 {
315 if (OverrideNotDependingOnPriority)
316 {
317 return castor1;
318 }
319 else
320 {
321 return (firstPriority > secondPriority) ? castor1 : castor2;
322 }
323 }
324
325 else
326 {
327 if (OverrideNotDependingOnPriority)
328 {
329 return castor2;
330 }
331 else
332 {
333 return (firstPriority > secondPriority) ? castor1 : castor2;
334 }
335 }
336 }
337 return null;
338 }
339
340
341
342
343
344
345 protected static void setOverridingDepth(final String depth)
346 {
347 InputArgumentUtils.checkStrings(true, depth);
348 OverridingDepth = depth;
349 }
350
351
352
353
354 protected static String getOverridingDepth()
355 {
356 return OverridingDepth;
357 }
358
359
360
361
362
363
364 protected static void setOverrideNotDependingOnPriority(final boolean value)
365 {
366 OverrideNotDependingOnPriority = value;
367 }
368
369
370
371
372 protected static boolean getOverrideNotDependingOnPriority()
373 {
374 return OverrideNotDependingOnPriority;
375 }
376 }