|
1 |
| package com.sourceforge.jpatterns.patterns; |
|
2 |
| |
|
3 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException; |
|
4 |
| import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigsBean; |
|
5 |
| import com.sourceforge.jpatterns.patterns.factory.JPFactoryImpl; |
|
6 |
| import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType; |
|
7 |
| import com.sourceforge.jpatterns.schema.CastorSectionType; |
|
8 |
| import com.sourceforge.jpatterns.schema.Item; |
|
9 |
| import com.zmicer.utils.InputArgumentUtils; |
|
10 |
| import com.zmicer.utils.LoggingUtils; |
|
11 |
| import com.zmicer.utils.ObjectStateUtils; |
|
12 |
| import com.zmicer.utils.ReflexionUtils; |
|
13 |
| import org.apache.commons.lang.StringUtils; |
|
14 |
| import org.apache.log4j.Logger; |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| public abstract class PatternBase implements IJPattern |
|
26 |
| { |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| final public static Logger LOG = Logger.getLogger(PatternBase.class); |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| protected CastorSectionType m_castorSection = null; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| protected JPatternsConfigsBean m_configBean = null; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| public abstract boolean checkCastorConfig(CastorSectionType castorSectionType); |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
20
| public void init(CastorSectionType castorSectionType, JPatternsConfigsBean configBean)
|
|
56 |
| { |
|
57 |
20
| InputArgumentUtils.checkObjects(castorSectionType, configBean);
|
|
58 |
17
| if (!(checkCastorConfig(castorSectionType)))
|
|
59 |
| { |
|
60 |
1
| final String errorMessage = "Error during initializing the pattern - the provided CastorSectionType object is not of " +
|
|
61 |
| "appropriate type. It's scope [" + castorSectionType.getScope() + "], name [" + castorSectionType.getName() + "]."; |
|
62 |
1
| LoggingUtils.error(LOG, JPFactoryImpl.class, errorMessage);
|
|
63 |
1
| throw new JPConfigException(errorMessage);
|
|
64 |
| } |
|
65 |
16
| m_castorSection = castorSectionType;
|
|
66 |
16
| m_configBean = configBean;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
90
| public boolean check()
|
|
73 |
| { |
|
74 |
90
| return (null != m_castorSection && null != m_configBean &&
|
|
75 |
| !StringUtils.isBlank(m_castorSection.getName()) && !StringUtils.isBlank(m_castorSection.getScope())); |
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
90
| protected String getActualScope(final String scope, final CastorSectionType section)
|
|
88 |
| { |
|
89 |
90
| InputArgumentUtils.checkObjects(section, section.getScope());
|
|
90 |
90
| return (StringUtils.isNotBlank(scope)) ? scope : section.getScope();
|
|
91 |
| } |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
96
| protected Item retrieveItem(final Object nameStorage, final String scope)
|
|
108 |
| { |
|
109 |
96
| InputArgumentUtils.checkObjects(nameStorage);
|
|
110 |
90
| if (!(nameStorage instanceof String) && !(nameStorage instanceof Class))
|
|
111 |
| { |
|
112 |
0
| logAndThrow("Smth. wrong at the com.sourceforge.jpatterns.patterns.factory.JPFactoryImpl.retrieveItem. Wrong " +
|
|
113 |
| "storage for the name is passed. Check the algorithm"); |
|
114 |
| } |
|
115 |
90
| if (nameStorage instanceof String)
|
|
116 |
| { |
|
117 |
46
| InputArgumentUtils.checkStrings(true, (String) nameStorage);
|
|
118 |
| } |
|
119 |
90
| ObjectStateUtils.strongCheck(this);
|
|
120 |
90
| final String baseName = (nameStorage instanceof String) ? (String) nameStorage : ReflexionUtils.getBaseName((Class) nameStorage);
|
|
121 |
90
| String workingScope = getActualScope(scope, m_castorSection);
|
|
122 |
| |
|
123 |
90
| final CastorNameScopePriorityType castorNameScopePriorityType =
|
|
124 |
| m_configBean.getBusinessItem(m_castorSection.getScope(), m_castorSection.getName(), workingScope, baseName); |
|
125 |
90
| if (!(castorNameScopePriorityType instanceof Item))
|
|
126 |
| { |
|
127 |
9
| throw new JPConfigException("Factory should contains items of Item type inside.");
|
|
128 |
| } |
|
129 |
81
| if (StringUtils.isBlank(castorNameScopePriorityType.getName()))
|
|
130 |
| { |
|
131 |
0
| throw new JPConfigException("Smth. wrong with the configuration as the name of the CastorNameScopePriorityType type is " +
|
|
132 |
| "required."); |
|
133 |
| } |
|
134 |
81
| return (Item) castorNameScopePriorityType;
|
|
135 |
| } |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
35
| protected void logAndThrow(final String message) throws JPConfigException
|
|
147 |
| { |
|
148 |
35
| InputArgumentUtils.checkStrings(true, message);
|
|
149 |
35
| LoggingUtils.error(LOG, JPFactoryImpl.class, message);
|
|
150 |
35
| throw new JPConfigException(message);
|
|
151 |
| } |
|
152 |
| } |