| 1 |
|
package com.sourceforge.jpatterns.core; |
| 2 |
|
|
| 3 |
|
import com.sourceforge.jpatterns.core.configuration.IPropertiesManager; |
| 4 |
|
import com.sourceforge.jpatterns.core.configuration.PropertiesBasedFactory; |
| 5 |
|
import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException; |
| 6 |
|
import com.zmicer.utils.InputArgumentUtils; |
| 7 |
|
import com.zmicer.utils.LoggingUtils; |
| 8 |
|
import org.apache.log4j.Logger; |
| 9 |
|
|
| 10 |
|
import java.io.Serializable; |
| 11 |
|
import java.util.HashMap; |
| 12 |
|
import java.util.Map; |
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
public final class JPEngineFactory implements Serializable |
| 23 |
|
{ |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
3 |
final public static Logger LOG = Logger.getLogger(JPEngineFactory.class); |
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
5 |
private static JPEngineFactory INSTANCE = null; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
2 |
|
| 40 |
3 |
private Map<JPConstants.EngineConfigCaregory, IJPEngine> m_engines = new HashMap<JPConstants.EngineConfigCaregory, IJPEngine>(); |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private JPEngineFactory() |
| 49 |
|
{ |
| 50 |
3 |
super(); |
| 51 |
|
|
| 52 |
5 |
init(); |
| 53 |
3 |
} |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
public void init() |
| 59 |
|
{ |
| 60 |
6 |
final IJPEngine engine1 = instantiateEngine(); |
| 61 |
6 |
engine1.init(JPConstants.EngineConfigCaregory.CONSUMER); |
| 62 |
8 |
m_engines.put(JPConstants.EngineConfigCaregory.CONSUMER, engine1); |
| 63 |
|
|
| 64 |
8 |
final IJPEngine engine2 = instantiateEngine(); |
| 65 |
8 |
engine2.init(JPConstants.EngineConfigCaregory.FRAMEWORK); |
| 66 |
6 |
m_engines.put(JPConstants.EngineConfigCaregory.FRAMEWORK, engine2); |
| 67 |
6 |
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
4 |
protected IJPEngine instantiateEngine() |
| 73 |
4 |
{ |
| 74 |
16 |
final IPropertiesManager propsManager = PropertiesBasedFactory.getInstance().getPropertiesManager(); |
| 75 |
12 |
if (null == propsManager) |
| 76 |
4 |
{ |
| 77 |
4 |
throw new JPInitializationException("IPropertiesManager can not be null."); |
| 78 |
4 |
} |
| 79 |
16 |
final Object objectEngine = propsManager.getBundledObject(IJPEngine.class); |
| 80 |
12 |
if (null == objectEngine) |
| 81 |
|
{ |
| 82 |
0 |
throw new JPInitializationException("Can not instantiate the IJPEngine implementation."); |
| 83 |
|
} |
| 84 |
12 |
if (!(objectEngine instanceof IJPEngine)) |
| 85 |
|
{ |
| 86 |
8 |
throw new JPInitializationException("Incorrect implemetation of the IJPEngine interface is set at the framework properties."); |
| 87 |
8 |
} |
| 88 |
12 |
return (IJPEngine) objectEngine; |
| 89 |
0 |
} |
| 90 |
|
|
| 91 |
8 |
|
| 92 |
8 |
|
| 93 |
|
|
| 94 |
0 |
public synchronized static JPEngineFactory getInstance() |
| 95 |
|
{ |
| 96 |
77 |
if (null != INSTANCE) |
| 97 |
|
{ |
| 98 |
66 |
return INSTANCE; |
| 99 |
|
} |
| 100 |
8 |
try |
| 101 |
|
{ |
| 102 |
3 |
INSTANCE = new JPEngineFactory(); |
| 103 |
3 |
return INSTANCE; |
| 104 |
|
} |
| 105 |
0 |
catch (Throwable ex) |
| 106 |
|
{ |
| 107 |
0 |
LoggingUtils.logException(LOG, ex, null, JPEngineFactory.class); |
| 108 |
42 |
throw new JPInitializationException("Cannot instantiate JPEngineFactory: " +ex.getMessage()); |
| 109 |
|
} |
| 110 |
40 |
} |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
2 |
|
| 115 |
2 |
|
| 116 |
|
|
| 117 |
0 |
|
| 118 |
|
|
| 119 |
0 |
public static IJPEngine getJPEngine(JPConstants.EngineConfigCaregory category) |
| 120 |
0 |
{ |
| 121 |
33 |
InputArgumentUtils.checkObjects(category); |
| 122 |
33 |
if (null == getInstance().m_engines.get(category)) |
| 123 |
|
{ |
| 124 |
0 |
logAndThrow("Can not obtain the IJPEngine of the category [" + category.toString() + "]"); |
| 125 |
|
} |
| 126 |
33 |
return getInstance().m_engines.get(category); |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
20 |
|
| 134 |
20 |
|
| 135 |
|
|
| 136 |
0 |
protected static void logAndThrow(final String message) throws JPInitializationException |
| 137 |
|
{ |
| 138 |
20 |
InputArgumentUtils.checkStrings(true, message); |
| 139 |
0 |
LoggingUtils.error(LOG, JPEngineFactory.class, message); |
| 140 |
0 |
throw new JPInitializationException(message); |
| 141 |
|
} |
| 142 |
|
} |