| 1 |
|
package com.sourceforge.jpatterns.patterns; |
| 2 |
|
|
| 3 |
|
import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException; |
| 4 |
|
import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigsBean; |
| 5 |
|
import com.sourceforge.jpatterns.patterns.factory.JPFactoryImpl; |
| 6 |
|
import com.sourceforge.jpatterns.schema.CastorNameScopePriorityType; |
| 7 |
|
import com.sourceforge.jpatterns.schema.CastorSectionType; |
| 8 |
|
import com.sourceforge.jpatterns.schema.Item; |
| 9 |
|
import com.zmicer.utils.InputArgumentUtils; |
| 10 |
|
import com.zmicer.utils.LoggingUtils; |
| 11 |
|
import com.zmicer.utils.ObjectStateUtils; |
| 12 |
|
import com.zmicer.utils.ReflexionUtils; |
| 13 |
|
import org.apache.commons.lang.StringUtils; |
| 14 |
|
import org.apache.log4j.Logger; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
71 |
public abstract class PatternBase implements IJPattern |
| 26 |
|
{ |
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
5 |
final public static Logger LOG = Logger.getLogger(PatternBase.class); |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
71 |
protected CastorSectionType m_castorSection = null; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
71 |
protected JPatternsConfigsBean m_configBean = null; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
public abstract boolean checkCastorConfig(CastorSectionType castorSectionType); |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
public void init(CastorSectionType castorSectionType, JPatternsConfigsBean configBean) |
| 56 |
|
{ |
| 57 |
96 |
InputArgumentUtils.checkObjects(castorSectionType, configBean); |
| 58 |
81 |
if (!(checkCastorConfig(castorSectionType))) |
| 59 |
|
{ |
| 60 |
5 |
final String errorMessage = "Error during initializing the pattern - the provided CastorSectionType object is not of " + |
| 61 |
|
"appropriate type. It's scope [" + castorSectionType.getScope() + "], name [" + castorSectionType.getName() + "]."; |
| 62 |
5 |
LoggingUtils.error(LOG, JPFactoryImpl.class, errorMessage); |
| 63 |
5 |
throw new JPConfigException(errorMessage); |
| 64 |
|
} |
| 65 |
76 |
m_castorSection = castorSectionType; |
| 66 |
76 |
m_configBean = configBean; |
| 67 |
76 |
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
public boolean check() |
| 73 |
|
{ |
| 74 |
446 |
return (null != m_castorSection && null != m_configBean && |
| 75 |
|
!StringUtils.isBlank(m_castorSection.getName()) && !StringUtils.isBlank(m_castorSection.getScope())); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
protected String getActualScope(final String scope, final CastorSectionType section) |
| 88 |
|
{ |
| 89 |
446 |
InputArgumentUtils.checkObjects(section, section.getScope()); |
| 90 |
446 |
return (StringUtils.isNotBlank(scope)) ? scope : section.getScope(); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
protected Item retrieveItem(final Object nameStorage, final String scope) |
| 108 |
|
{ |
| 109 |
476 |
InputArgumentUtils.checkObjects(nameStorage); |
| 110 |
446 |
if (!(nameStorage instanceof String) && !(nameStorage instanceof Class)) |
| 111 |
|
{ |
| 112 |
0 |
logAndThrow("Smth. wrong at the com.sourceforge.jpatterns.patterns.factory.JPFactoryImpl.retrieveItem. Wrong " + |
| 113 |
|
"storage for the name is passed. Check the algorithm"); |
| 114 |
|
} |
| 115 |
446 |
if (nameStorage instanceof String) |
| 116 |
|
{ |
| 117 |
226 |
InputArgumentUtils.checkStrings(true, (String) nameStorage); |
| 118 |
|
} |
| 119 |
446 |
ObjectStateUtils.strongCheck(this); |
| 120 |
446 |
final String baseName = (nameStorage instanceof String) ? (String) nameStorage : ReflexionUtils.getBaseName((Class) nameStorage); |
| 121 |
446 |
String workingScope = getActualScope(scope, m_castorSection); |
| 122 |
|
|
| 123 |
446 |
final CastorNameScopePriorityType castorNameScopePriorityType = |
| 124 |
|
m_configBean.getBusinessItem(m_castorSection.getScope(), m_castorSection.getName(), workingScope, baseName); |
| 125 |
446 |
if (!(castorNameScopePriorityType instanceof Item)) |
| 126 |
|
{ |
| 127 |
45 |
throw new JPConfigException("Factory should contains items of Item type inside."); |
| 128 |
|
} |
| 129 |
401 |
if (StringUtils.isBlank(castorNameScopePriorityType.getName())) |
| 130 |
|
{ |
| 131 |
0 |
throw new JPConfigException("Smth. wrong with the configuration as the name of the CastorNameScopePriorityType type is " + |
| 132 |
|
"required."); |
| 133 |
|
} |
| 134 |
401 |
return (Item) castorNameScopePriorityType; |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
protected void logAndThrow(final String message) throws JPConfigException |
| 147 |
|
{ |
| 148 |
175 |
InputArgumentUtils.checkStrings(true, message); |
| 149 |
175 |
LoggingUtils.error(LOG, JPFactoryImpl.class, message); |
| 150 |
175 |
throw new JPConfigException(message); |
| 151 |
|
} |
| 152 |
|
} |