Clover coverage report - Maven Clover report
Coverage timestamp: Mon Sep 3 2007 08:24:07 EEST
file stats: LOC: 250   Methods: 12
NCLOC: 159   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JPEngineImpl.java 65.4% 84.4% 100% 81.4%
coverage coverage
 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  15 public JPEngineImpl()
 47    {
 48  15 super();
 49    }
 50   
 51    /**
 52    * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
 53    */
 54  17 public void init(JPConstants.EngineConfigCaregory category)
 55    {
 56  17 InputArgumentUtils.checkObjects(category);
 57  16 m_category = category;
 58    }
 59   
 60    /**
 61    * @see IJPEngine#init(com.sourceforge.jpatterns.core.JPConstants.EngineConfigCaregory)
 62    */
 63  3 public JPConstants.EngineConfigCaregory getCategory()
 64    {
 65  3 return m_category;
 66    }
 67   
 68    /**
 69    * @see IJPEngine#isInitialized()
 70    */
 71  66 public boolean isInitialized()
 72    {
 73  66 return (m_category != null);
 74    }
 75   
 76    /**
 77    * @see IJPEngine#getFactory(java.lang.String,java.lang.String)
 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    * @see com.sourceforge.jpatterns.core.IJPEngine#getPattern(String,String)
 99    */
 100  4 public IJPattern getPattern(final String patternName, final String scope)
 101    {
 102    // 1: get the necessary object to proceed later with
 103  4 final JPatternsConfigsBean bean = getCategoryJPatternsConfigsBean();
 104  4 final CastorSectionType castorObject = getCastorSectionType(patternName, scope);
 105   
 106    // 2: retrieve the pattern, make all the necessary validations
 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    // 3: cast to the pattern, init and return the pattern
 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    * @see IJPEngine#getConfig(java.lang.String, java.lang.String)
 133    */
 134  4 public IJPConfig getConfig(final String configName, final String scope)
 135    {
 136    // 1: get pattern
 137  4 final IJPattern pattern = getPattern(configName, scope);
 138    // 2: validate it has the necssary type
 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    * 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  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    * Get factory is used for the retriving the plugin configurations
 173    *
 174    * @return the instance of IJPFactory.
 175    */
 176  1 protected IJPFactory getPluginFactory()
 177    {
 178  1 if (null != m_pluginFactory)
 179    {
 180  0 return m_pluginFactory;
 181    }
 182    // obtain the factory we need
 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    * @return the {@link JPatternsConfigsBean specific for the pointed category}
 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    * Check if the instance of initialized.
 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    * 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  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    }