|
1 |
| package com.sourceforge.jpatterns.core; |
|
2 |
| |
|
3 |
| import com.sourceforge.jpatterns.core.configuration.PropertiesBasedFactory; |
|
4 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException; |
|
5 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPConsumerConfigException; |
|
6 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPFrameworkConfigException; |
|
7 |
| import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException; |
|
8 |
| import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigsBean; |
|
9 |
| import com.sourceforge.jpatterns.patterns.IJPattern; |
|
10 |
| import com.sourceforge.jpatterns.patterns.config.IJPConfig; |
|
11 |
| import com.sourceforge.jpatterns.patterns.factory.IJPFactory; |
|
12 |
| import com.sourceforge.jpatterns.schema.CastorSectionType; |
|
13 |
| import com.sourceforge.jpatterns.schema.Factory; |
|
14 |
| import com.zmicer.utils.InputArgumentUtils; |
|
15 |
| import com.zmicer.utils.LoggingUtils; |
|
16 |
| import com.zmicer.utils.ReflexionUtils; |
|
17 |
| import org.apache.log4j.Logger; |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public class JPEngineImpl implements IJPEngine |
|
27 |
| { |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| final public static Logger LOG = Logger.getLogger(JPConstants.LoggingCategory.OBTAIN_PATTERNS.toString()); |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| private JPConstants.EngineConfigCaregory m_category; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| private IJPFactory m_pluginFactory = null; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
15
| public JPEngineImpl()
|
|
47 |
| { |
|
48 |
15
| super();
|
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
17
| public void init(JPConstants.EngineConfigCaregory category)
|
|
55 |
| { |
|
56 |
17
| InputArgumentUtils.checkObjects(category);
|
|
57 |
16
| m_category = category;
|
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
3
| public JPConstants.EngineConfigCaregory getCategory()
|
|
64 |
| { |
|
65 |
3
| return m_category;
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
66
| public boolean isInitialized()
|
|
72 |
| { |
|
73 |
66
| return (m_category != null);
|
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
19
| public IJPFactory getFactory(final String factoryName, final String scope)
|
|
80 |
| { |
|
81 |
19
| final CastorSectionType castorObject = getCastorSectionType(factoryName, scope);
|
|
82 |
15
| if (!(castorObject instanceof Factory))
|
|
83 |
| { |
|
84 |
1
| logAndThrow("The CastorSectionType found using the provided name and scope is not of the type " +
|
|
85 |
| "of Factory - please check the JPatterns configuration. The actual class name is [" + |
|
86 |
| castorObject.getClass().getName() + "]"); |
|
87 |
| } |
|
88 |
14
| final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
|
|
89 |
14
| IJPFactory factory = PropertiesBasedFactory.getInstance().instantiateFactory();
|
|
90 |
14
| factory.init((Factory) castorObject, bean);
|
|
91 |
14
| LoggingUtils.debug(LOG, JPEngineImpl.class, "Factory for the parameters factory name [" + factoryName + "], scope [" +
|
|
92 |
| scope + "] was onbtained succesfully"); |
|
93 |
| |
|
94 |
14
| return factory;
|
|
95 |
| } |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
4
| public IJPattern getPattern(final String patternName, final String scope)
|
|
101 |
| { |
|
102 |
| |
|
103 |
4
| final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
|
|
104 |
4
| final CastorSectionType castorObject = getCastorSectionType(patternName, scope);
|
|
105 |
| |
|
106 |
| |
|
107 |
1
| final Object patternObject =
|
|
108 |
| getPluginFactory().getImplementation(JPConstants.FrameworkConfigEntities.ITEM_JPATTERNS_PLUGIN, |
|
109 |
| ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase(), null); |
|
110 |
1
| final String message = "name [" + patternName + "], scope [" + scope + "], configuration type [" +
|
|
111 |
| ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase() + "]."; |
|
112 |
1
| if (null == patternObject)
|
|
113 |
| { |
|
114 |
0
| logAndThrow("Can not retrive the pattern for the " + message);
|
|
115 |
| } |
|
116 |
1
| if (!(patternObject instanceof IJPattern))
|
|
117 |
| { |
|
118 |
0
| logAndThrow("The pattern with " + message + " is not of the type IJPattern.");
|
|
119 |
| } |
|
120 |
| |
|
121 |
1
| final IJPattern pattern = (IJPattern) patternObject;
|
|
122 |
1
| pattern.checkCastorConfig(castorObject);
|
|
123 |
1
| pattern.init(castorObject, bean);
|
|
124 |
| |
|
125 |
1
| LoggingUtils.debug(LOG, JPEngineImpl.class, "IJPattern for the parameters factory name [" + patternName + "], scope [" +
|
|
126 |
| scope + "] was onbtained succesfully"); |
|
127 |
| |
|
128 |
1
| return pattern;
|
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
4
| public IJPConfig getConfig(final String configName, final String scope)
|
|
135 |
| { |
|
136 |
| |
|
137 |
4
| final IJPattern pattern = getPattern(configName, scope);
|
|
138 |
| |
|
139 |
1
| if (!(pattern instanceof IJPConfig))
|
|
140 |
| { |
|
141 |
0
| logAndThrow("The found IJPattern is not of the IJPConfig type. Config name [" + configName + "], scope [" + scope + "]");
|
|
142 |
| } |
|
143 |
1
| return (IJPConfig) pattern;
|
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
23
| protected CastorSectionType getCastorSectionType(final String name, final String scope)
|
|
155 |
| { |
|
156 |
23
| checkIsInitialized();
|
|
157 |
22
| InputArgumentUtils.checkStrings(true, name);
|
|
158 |
18
| final String actualScope = (null == scope || "".equals(scope)) ? JPConstants.DEFAULT_SCOPE_NAME : scope;
|
|
159 |
18
| LoggingUtils.debug(LOG, JPEngineImpl.class, "Trying to obtain the pattern using factory name [" + name + "], scope [" +
|
|
160 |
| actualScope + "]."); |
|
161 |
| |
|
162 |
18
| final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
|
|
163 |
18
| CastorSectionType castorObject = bean.getSection(actualScope, name);
|
|
164 |
18
| if (null == castorObject)
|
|
165 |
| { |
|
166 |
2
| logAndThrow("Have not found the configuration for the pattern name [" + name + "], scope [" + actualScope + "].");
|
|
167 |
| } |
|
168 |
16
| return castorObject;
|
|
169 |
| } |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
1
| protected IJPFactory getPluginFactory()
|
|
177 |
| { |
|
178 |
1
| if (null != m_pluginFactory)
|
|
179 |
| { |
|
180 |
0
| return m_pluginFactory;
|
|
181 |
| } |
|
182 |
| |
|
183 |
1
| m_pluginFactory = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.FRAMEWORK).
|
|
184 |
| getFactory(JPConstants.FrameworkConfigEntities.FACTORY_JPATTERNS_CORE, null); |
|
185 |
1
| if (null == m_pluginFactory)
|
|
186 |
| { |
|
187 |
0
| logAndThrow("Can not instantiate plugin factory. The factory name we use [" + JPConstants.EngineConfigCaregory.FRAMEWORK + "], " +
|
|
188 |
| "default scope"); |
|
189 |
| } |
|
190 |
1
| return m_pluginFactory;
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
36
| protected JPatternsConfigsBean getCategoryJPatternsConfigsBean()
|
|
197 |
| { |
|
198 |
36
| checkIsInitialized();
|
|
199 |
36
| if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
|
|
200 |
| { |
|
201 |
34
| return PropertiesBasedFactory.getInstance().getJPConfigurator().getJPatternsConsumerConfiguration();
|
|
202 |
| } |
|
203 |
2
| else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
|
|
204 |
| { |
|
205 |
2
| return PropertiesBasedFactory.getInstance().getJPConfigurator().getJPatternsFrameworkConfiguration();
|
|
206 |
| } |
|
207 |
0
| logAndThrow("Can'not obtain the JPatternsConfigsBean to be used");
|
|
208 |
0
| return null;
|
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
62
| protected void checkIsInitialized()
|
|
215 |
| { |
|
216 |
62
| if (!isInitialized())
|
|
217 |
| { |
|
218 |
1
| final String message = "The instance of the Engine is not initialized.";
|
|
219 |
1
| LoggingUtils.error(LOG, JPEngineImpl.class, message);
|
|
220 |
1
| throw new JPInitializationException(message);
|
|
221 |
| } |
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
3
| protected void logAndThrow(final String message) throws JPConfigException
|
|
233 |
| { |
|
234 |
3
| checkIsInitialized();
|
|
235 |
3
| InputArgumentUtils.checkStrings(true, message);
|
|
236 |
3
| LoggingUtils.error(LOG, JPEngineImpl.class, message);
|
|
237 |
3
| if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
|
|
238 |
| { |
|
239 |
3
| throw new JPConsumerConfigException(message);
|
|
240 |
| } |
|
241 |
0
| else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
|
|
242 |
| { |
|
243 |
0
| throw new JPFrameworkConfigException(message);
|
|
244 |
| } |
|
245 |
| else |
|
246 |
| { |
|
247 |
0
| throw new JPConfigException(message);
|
|
248 |
| } |
|
249 |
| } |
|
250 |
| } |