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 public JPEngineImpl()
47 {
48 super();
49 }
50
51
52
53
54 public void init(JPConstants.EngineConfigCaregory category)
55 {
56 InputArgumentUtils.checkObjects(category);
57 m_category = category;
58 }
59
60
61
62
63 public JPConstants.EngineConfigCaregory getCategory()
64 {
65 return m_category;
66 }
67
68
69
70
71 public boolean isInitialized()
72 {
73 return (m_category != null);
74 }
75
76
77
78
79 public IJPFactory getFactory(final String factoryName, final String scope)
80 {
81 final CastorSectionType castorObject = getCastorSectionType(factoryName, scope);
82 if (!(castorObject instanceof Factory))
83 {
84 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 final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
89 IJPFactory factory = PropertiesBasedFactory.getInstance().instantiateFactory();
90 factory.init((Factory) castorObject, bean);
91 LoggingUtils.debug(LOG, JPEngineImpl.class, "Factory for the parameters factory name [" + factoryName + "], scope [" +
92 scope + "] was onbtained succesfully");
93
94 return factory;
95 }
96
97
98
99
100 public IJPattern getPattern(final String patternName, final String scope)
101 {
102
103 final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
104 final CastorSectionType castorObject = getCastorSectionType(patternName, scope);
105
106
107 final Object patternObject =
108 getPluginFactory().getImplementation(JPConstants.FrameworkConfigEntities.ITEM_JPATTERNS_PLUGIN,
109 ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase(), null);
110 final String message = "name [" + patternName + "], scope [" + scope + "], configuration type [" +
111 ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase() + "].";
112 if (null == patternObject)
113 {
114 logAndThrow("Can not retrive the pattern for the " + message);
115 }
116 if (!(patternObject instanceof IJPattern))
117 {
118 logAndThrow("The pattern with " + message + " is not of the type IJPattern.");
119 }
120
121 final IJPattern pattern = (IJPattern) patternObject;
122 pattern.checkCastorConfig(castorObject);
123 pattern.init(castorObject, bean);
124
125 LoggingUtils.debug(LOG, JPEngineImpl.class, "IJPattern for the parameters factory name [" + patternName + "], scope [" +
126 scope + "] was onbtained succesfully");
127
128 return pattern;
129 }
130
131
132
133
134 public IJPConfig getConfig(final String configName, final String scope)
135 {
136
137 final IJPattern pattern = getPattern(configName, scope);
138
139 if (!(pattern instanceof IJPConfig))
140 {
141 logAndThrow("The found IJPattern is not of the IJPConfig type. Config name [" + configName + "], scope [" + scope + "]");
142 }
143 return (IJPConfig) pattern;
144 }
145
146
147
148
149
150
151
152
153
154 protected CastorSectionType getCastorSectionType(final String name, final String scope)
155 {
156 checkIsInitialized();
157 InputArgumentUtils.checkStrings(true, name);
158 final String actualScope = (null == scope || "".equals(scope)) ? JPConstants.DEFAULT_SCOPE_NAME : scope;
159 LoggingUtils.debug(LOG, JPEngineImpl.class, "Trying to obtain the pattern using factory name [" + name + "], scope [" +
160 actualScope + "].");
161
162 final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
163 CastorSectionType castorObject = bean.getSection(actualScope, name);
164 if (null == castorObject)
165 {
166 logAndThrow("Have not found the configuration for the pattern name [" + name + "], scope [" + actualScope + "].");
167 }
168 return castorObject;
169 }
170
171
172
173
174
175
176 protected IJPFactory getPluginFactory()
177 {
178 if (null != m_pluginFactory)
179 {
180 return m_pluginFactory;
181 }
182
183 m_pluginFactory = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.FRAMEWORK).
184 getFactory(JPConstants.FrameworkConfigEntities.FACTORY_JPATTERNS_CORE, null);
185 if (null == m_pluginFactory)
186 {
187 logAndThrow("Can not instantiate plugin factory. The factory name we use [" + JPConstants.EngineConfigCaregory.FRAMEWORK + "], " +
188 "default scope");
189 }
190 return m_pluginFactory;
191 }
192
193
194
195
196 protected JPatternsConfigsBean getCategoryJPatternsConfigsBean()
197 {
198 checkIsInitialized();
199 if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
200 {
201 return PropertiesBasedFactory.getInstance().getJPConfigurator().getJPatternsConsumerConfiguration();
202 }
203 else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
204 {
205 return PropertiesBasedFactory.getInstance().getJPConfigurator().getJPatternsFrameworkConfiguration();
206 }
207 logAndThrow("Can'not obtain the JPatternsConfigsBean to be used");
208 return null;
209 }
210
211
212
213
214 protected void checkIsInitialized()
215 {
216 if (!isInitialized())
217 {
218 final String message = "The instance of the Engine is not initialized.";
219 LoggingUtils.error(LOG, JPEngineImpl.class, message);
220 throw new JPInitializationException(message);
221 }
222 }
223
224
225
226
227
228
229
230
231
232 protected void logAndThrow(final String message) throws JPConfigException
233 {
234 checkIsInitialized();
235 InputArgumentUtils.checkStrings(true, message);
236 LoggingUtils.error(LOG, JPEngineImpl.class, message);
237 if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
238 {
239 throw new JPConsumerConfigException(message);
240 }
241 else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
242 {
243 throw new JPFrameworkConfigException(message);
244 }
245 else
246 {
247 throw new JPConfigException(message);
248 }
249 }
250 }