Coverage Report - com.sourceforge.jpatterns.core.JPEngineImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
JPEngineImpl
70% 
69% 
2,583
 
 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  5
     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  67
     private IJPFactory m_pluginFactory = null;
 42  
 
 43  
     /**
 44  
      * Default public constructor.
 45  
      */
 46  
     public JPEngineImpl()
 47  
     {
 48  67
         super();
 49  67
     }
 50  
 
 51  
     /**
 52  
      * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
 53  
      */
 54  
     public void init(JPConstants.EngineConfigCaregory category)
 55  
     {
 56  77
         InputArgumentUtils.checkObjects(category);
 57  72
         m_category = category;
 58  72
     }
 59  
 
 60  
     /**
 61  
      * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
 62  
      */
 63  
     public JPConstants.EngineConfigCaregory getCategory()
 64  
     {
 65  15
         return m_category;
 66  
     }
 67  
 
 68  
     /**
 69  
      * @see IJPEngine#isInitialized()
 70  
      */
 71  
     public boolean isInitialized()
 72  
     {
 73  302
         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  93
         final CastorSectionType castorObject = getCastorSectionType(factoryName, scope);
 82  73
         if (!(castorObject instanceof Factory))
 83  
         {
 84  5
             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  68
         final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
 89  68
         IJPFactory factory = PropertiesBasedFactory.getInstance().instantiateFactory();
 90  68
         factory.init((Factory) castorObject, bean);
 91  68
         LoggingUtils.debug(LOG, JPEngineImpl.class, "Factory for the parameters factory name [" + factoryName + "], scope [" +
 92  
             scope + "] was onbtained succesfully");
 93  
 
 94  68
         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  12
         final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
 104  12
         final CastorSectionType castorObject = getCastorSectionType(patternName, scope);
 105  
 
 106  
         // 2: retrieve the pattern, make all the necessary validations
 107  3
         final Object patternObject =
 108  0
             getPluginFactory().getImplementation(JPConstants.FrameworkConfigEntities.ITEM_JPATTERNS_PLUGIN,
 109  0
                 ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase(), null);
 110  3
         final String message = "name [" + patternName + "], scope [" + scope + "], configuration type [" +
 111  0
             ReflexionUtils.getBaseName(castorObject.getClass()).toLowerCase() + "].";
 112  3
         if (null == patternObject)
 113  0
         {
 114  0
             logAndThrow("Can not retrive the pattern for the " + message);
 115  0
         }
 116  3
         if (!(patternObject instanceof IJPattern))
 117  
         {
 118  0
             logAndThrow("The pattern with " + message + " is not of the type IJPattern.");
 119  0
         }
 120  0
         // 3: cast to the pattern, init and return the pattern
 121  3
         final IJPattern pattern = (IJPattern) patternObject;
 122  3
         pattern.checkCastorConfig(castorObject);
 123  3
         pattern.init(castorObject, bean);
 124  
 
 125  3
         LoggingUtils.debug(LOG, JPEngineImpl.class, "IJPattern for the parameters factory name [" + patternName + "], scope [" +
 126  
             scope + "] was onbtained succesfully");
 127  
 
 128  3
         return pattern;
 129  
     }
 130  
 
 131  
     /**
 132  
      * @see IJPEngine#getConfig(java.lang.String, java.lang.String)
 133  
      */
 134  0
     public IJPConfig getConfig(final String configName, final String scope)
 135  
     {
 136  0
         // 1: get pattern
 137  12
         final IJPattern pattern = getPattern(configName, scope);
 138  0
         // 2: validate it has the necssary type
 139  3
         if (!(pattern instanceof IJPConfig))
 140  0
         {
 141  0
             logAndThrow("The found IJPattern is not of the IJPConfig type. Config name [" + configName + "], scope [" + scope + "]");
 142  
         }
 143  3
         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  36
      */
 154  34
     protected CastorSectionType getCastorSectionType(final String name, final String scope)
 155  30
     {
 156  99
         checkIsInitialized();
 157  66
         InputArgumentUtils.checkStrings(true, name);
 158  54
         final String actualScope = (null == scope || "".equals(scope)) ? JPConstants.DEFAULT_SCOPE_NAME : scope;
 159  84
         LoggingUtils.debug(LOG, JPEngineImpl.class, "Trying to obtain the pattern using factory name [" + name + "], scope [" +
 160  30
             actualScope + "].");
 161  30
 
 162  54
         final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
 163  56
         CastorSectionType castorObject = bean.getSection(actualScope, name);
 164  54
         if (null == castorObject)
 165  28
         {
 166  6
             logAndThrow("Have not found the configuration for the pattern name [" + name + "], scope [" + actualScope + "].");
 167  
         }
 168  48
         return castorObject;
 169  
     }
 170  
 
 171  
     /**
 172  
      * Get factory is used for the retriving the plugin configurations
 173  
      *
 174  
      * @return the instance of IJPFactory.
 175  0
      */
 176  
     protected IJPFactory getPluginFactory()
 177  0
     {
 178  3
         if (null != m_pluginFactory)
 179  
         {
 180  0
             return m_pluginFactory;
 181  
         }
 182  0
         // obtain the factory we need
 183  3
         m_pluginFactory = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.FRAMEWORK).
 184  0
             getFactory(JPConstants.FrameworkConfigEntities.FACTORY_JPATTERNS_CORE, null);
 185  3
         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  3
         return m_pluginFactory;
 191  
     }
 192  
 
 193  
     /**
 194  
      * @return the {@link JPatternsConfigsBean specific for the pointed category}
 195  56
      */
 196  56
     protected JPatternsConfigsBean getCategoryJPatternsConfigsBean()
 197  
     {
 198  164
         checkIsInitialized();
 199  108
         if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
 200  0
         {
 201  102
             return PropertiesBasedFactory.getInstance().getJPConfigurator().getJPatternsConsumerConfiguration();
 202  0
         }
 203  6
         else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
 204  0
         {
 205  6
             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  
      * Check if the instance of initialized.
 213  96
      */
 214  
     protected void checkIsInitialized()
 215  2
     {
 216  188
         if (!isInitialized())
 217  2
         {
 218  3
             final String message = "The instance of the Engine is not initialized.";
 219  97
             LoggingUtils.error(LOG, JPEngineImpl.class, message);
 220  3
             throw new JPInitializationException(message);
 221  
         }
 222  183
     }
 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  4
      */
 232  4
     protected void logAndThrow(final String message) throws JPConfigException
 233  4
     {
 234  13
         checkIsInitialized();
 235  9
         InputArgumentUtils.checkStrings(true, message);
 236  13
         LoggingUtils.error(LOG, JPEngineImpl.class, message);
 237  9
         if (m_category.equals(JPConstants.EngineConfigCaregory.CONSUMER))
 238  0
         {
 239  9
             throw new JPConsumerConfigException(message);
 240  0
         }
 241  0
         else if (m_category.equals(JPConstants.EngineConfigCaregory.FRAMEWORK))
 242  
         {
 243  0
             throw new JPFrameworkConfigException(message);
 244  0
         }
 245  
         else
 246  
         {
 247  0
             throw new JPConfigException(message);
 248  
         }
 249  
     }
 250  
 }