1
2
3
4
5
6
7
8
9
10
11 package com.legstar.host.server;
12
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Executors;
15
16 import javax.naming.InitialContext;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import com.legstar.config.PoolingEngineConfig;
22 import com.legstar.pool.manager.ConnectionPoolManager;
23 import com.legstar.work.invoke.InvokeWorkFactory;
24 import com.legstar.work.manager.WorkManagerImpl;
25
26 import commonj.work.WorkManager;
27
28
29
30
31
32 public final class EngineHolder {
33
34
35 private static Engine sEngine;
36
37
38 private static ExecutorService sExecutor;
39
40
41 private static WorkManager sWorkManager;
42
43
44 private static ConnectionPoolManager sPoolManager;
45
46
47 private static PoolingEngineConfig sConfig;
48
49
50 private static final Log LOG = LogFactory.getLog(EngineHolder.class);
51
52
53 private EngineHolder() {
54 }
55
56
57
58
59
60
61
62 public static void preInit(final PoolingEngineConfig config)
63 throws EngineConfigurationException {
64 sConfig = config;
65 sPoolManager = new ConnectionPoolManager(sConfig.getHostEndpoints());
66 initializeWorkManager();
67 }
68
69
70
71
72
73
74 public static void init() throws EngineStartupException {
75 LOG.debug("Starting engine.");
76 sEngine = new Engine(sConfig.getMaxRequests(),
77 sWorkManager,
78 sPoolManager,
79 new InvokeWorkFactory(),
80 sConfig.getTakeTimeout());
81 try {
82 sWorkManager.schedule(sEngine, new EngineListener());
83 } catch (IllegalArgumentException e) {
84 throw new EngineStartupException(e);
85 }
86 }
87
88
89 public static void stop() {
90 if (sEngine != null) {
91 sEngine.shutDown();
92 sEngine = null;
93 }
94 if (sPoolManager != null) {
95 sPoolManager.shutDown();
96 sPoolManager = null;
97 }
98 if (sExecutor != null) {
99 sExecutor.shutdownNow();
100 sExecutor = null;
101 }
102 sWorkManager = null;
103 }
104
105
106
107
108
109 public static Engine getEngine() throws EngineNotStartedException {
110 if (sEngine == null) {
111 throw new EngineNotStartedException(
112 "The host access engine is not running.");
113 }
114 if (sEngine.isShuttingDown()) {
115 throw new EngineNotStartedException(
116 "The host access engine is shutting down.");
117 }
118 return sEngine;
119 }
120
121
122
123
124
125
126
127 private static void initializeWorkManager() {
128 LOG.debug("Initializing Work Manager.");
129 String workMgrLocation = sConfig.getWorkManagerJNDILocation();
130 if (workMgrLocation != null && workMgrLocation.length() > 0) {
131 try {
132 InitialContext ic = new InitialContext();
133 sWorkManager = (WorkManager) ic.lookup(workMgrLocation);
134 } catch (Exception e) {
135 sWorkManager = null;
136 }
137 } else {
138 sWorkManager = null;
139 }
140
141 if (sWorkManager == null) {
142 int threadPoolSize = sConfig.getThreadPoolSize();
143 sExecutor = Executors.newFixedThreadPool(threadPoolSize);
144 sWorkManager = new WorkManagerImpl(sExecutor);
145 }
146 }
147
148 }