View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2010 LegSem.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the GNU Lesser Public License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7    * 
8    * Contributors:
9    *     LegSem - initial API and implementation
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   * This class holds a reference to a singleton Engine. Any client who needs
30   * access to the engine will do so thru the engine holder.
31   */
32  public final class EngineHolder {
33  
34      /** The singleton engine instance. */
35      private static Engine sEngine;
36  
37      /** A thread pool to be used with default work manager implementation. */
38      private static ExecutorService sExecutor;
39  
40      /** WorkManager implementation for asynchronous work. */
41      private static WorkManager sWorkManager;
42  
43      /** Host connections pool manager. */
44      private static ConnectionPoolManager sPoolManager;
45  
46      /** The current configuration settings. */
47      private static PoolingEngineConfig sConfig;
48  
49      /** Logger. */
50      private static final Log LOG = LogFactory.getLog(EngineHolder.class);
51  
52      /** An Engine Holder cannot be instantiated. */
53      private EngineHolder() {
54      }
55  
56      /**
57       * Create the engine environment described in a configuration.
58       * 
59       * @param config the complete configuration hierarchy
60       * @throws EngineConfigurationException if configuration is invalid
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       * Create the single instance of an engine.
71       * 
72       * @throws EngineStartupException if engine fails to start
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      /** Shutdown the engine. */
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      * @return the Engine singleton.
107      * @throws EngineNotStartedException if engine is unavailable
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      * This method initializes the work manager used by the engine. We will
123      * first attempt to lookup the work manager from the JNDI location
124      * specified in the engine config file. If not specified, or unable to load,
125      * we will use the default work manager.
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 }