View Javadoc

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   * The concrete, JPatterns "default" implementation.
21   * <p/>
22   * $Author:: zmicer             $<br/>
23   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
24   * $Date:: 2007-08-28 21:37:07 #$<br/>
25   */
26  public class JPEngineImpl implements IJPEngine
27  {
28      /**
29       * Logger instance.
30       */
31      final public static Logger LOG = Logger.getLogger(JPConstants.LoggingCategory.OBTAIN_PATTERNS.toString());
32  
33      /**
34       * Category to be set.
35       */
36      private JPConstants.EngineConfigCaregory m_category;
37  
38      /**
39       * The instance of the factory would be used for the plugins
40       */
41      private IJPFactory m_pluginFactory = null;
42  
43      /**
44       * Default public constructor.
45       */
46      public JPEngineImpl()
47      {
48          super();
49      }
50  
51      /**
52       * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
53       */
54      public void init(JPConstants.EngineConfigCaregory category)
55      {
56          InputArgumentUtils.checkObjects(category);
57          m_category = category;
58      }
59  
60      /**
61       * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
62       */
63      public JPConstants.EngineConfigCaregory getCategory()
64      {
65          return m_category;
66      }
67  
68      /**
69       * @see IJPEngine#isInitialized()
70       */
71      public boolean isInitialized()
72      {
73          return (m_category != null);
74      }
75  
76      /**
77       * @see IJPEngine#getFactory(java.lang.String,java.lang.String)
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       * @see com.sourceforge.jpatterns.core.IJPEngine#getPattern(String,String)
99       */
100     public IJPattern getPattern(final String patternName, final String scope)
101     {
102         // 1: get the necessary object to proceed later with
103         final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
104         final CastorSectionType castorObject = getCastorSectionType(patternName, scope);
105 
106         // 2: retrieve the pattern, make all the necessary validations
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         // 3: cast to the pattern, init and return the pattern
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      * @see IJPEngine#getConfig(java.lang.String, java.lang.String)
133      */
134     public IJPConfig getConfig(final String configName, final String scope)
135     {
136         // 1: get pattern
137         final IJPattern pattern = getPattern(configName, scope);
138         // 2: validate it has the necssary type
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      * Get the castor section type using the provided input data
148      *
149      * @param name  the name of the section to be retrived, Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
150      * @param scope the name of the scope to be retrived, Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
151      *
152      * @return the castor section type to be used.
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      * Get factory is used for the retriving the plugin configurations
173      *
174      * @return the instance of IJPFactory.
175      */
176     protected IJPFactory getPluginFactory()
177     {
178         if (null != m_pluginFactory)
179         {
180             return m_pluginFactory;
181         }
182         // obtain the factory we need
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      * @return the {@link JPatternsConfigsBean specific for the pointed category}
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      * Check if the instance of initialized.
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      * Log the message and then throw it.
226      *
227      * @param message the not emopty message to be thrown and logged
228      *
229      * @throws com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException
230      *          always, using message
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 }