View Javadoc

1   package org.jmage;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.log4j.PropertyConfigurator;
5   import org.jmage.dispatcher.RequestDispatcher;
6   import org.jmage.encoder.EncoderManager;
7   import org.jmage.filterchain.FilterChainManager;
8   import org.jmage.pool.Worker;
9   import org.jmage.pool.WorkerException;
10  import org.jmage.pool.WorkerPool;
11  import org.jmage.pool.WorkerPoolImpl;
12  import org.jmage.resource.ResourceManager;
13  
14  import java.util.Map;
15  import java.util.Properties;
16  
17  /***
18   * ApplicationContext
19   */
20  public abstract class ApplicationContext {
21      protected static ApplicationContext applicationContext = null;
22  
23      protected WorkerPool resourceManagerPool = null;
24      protected WorkerPool filterChainManagerPool = null;
25      protected WorkerPool encoderManagerPool = null;
26      protected WorkerPool requestDispatcherPool = null;
27      protected Properties properties = null;
28  
29      //log4j init
30      protected static PropertyConfigurator log4j = new PropertyConfigurator();
31      protected static Logger log = Logger.getLogger(ApplicationContext.class.getName());
32  
33      private static final String DISPATCHER_OBTAINED = " obtained RequestDispatcher from applicationContext: ";
34      private static final String RESOURCEMANAGER_OBTAINED = " obtained ResourceManager from applicationContext: ";
35      private static final String FILTERCHAINMANAGER_OBTAINED = " obtained FilterChainManager from applicationContext: ";
36      private static final String ENCODERMANAGER_OBTAINED = " obtained EncoderManager from applicationContext: ";
37      private static final String DISPATCHER_RELEASED = " released RequestDispatcher: ";
38      private static final String RESOURCEMANAGER_RELEASED = " released ResourceManager: ";
39      private static final String FILTERCHAINMANAGER_RELEASED = " released FilterChainManager: ";
40      private static final String ENCODERMANAGER_RELEASED = " released EncoderManager: ";
41      private static final String CONFIGURATION_ERROR = "jmage configuration error, cause: ";
42  
43      /***
44       * Use getContext instead.
45       */
46      protected ApplicationContext() {
47          properties = new Properties();
48          requestDispatcherPool = new WorkerPoolImpl();
49          resourceManagerPool = new WorkerPoolImpl();
50          filterChainManagerPool = new WorkerPoolImpl();
51          encoderManagerPool = new WorkerPoolImpl();
52      }
53  
54      public static ApplicationContext getContext() {
55          if (applicationContext == null) {
56              applicationContext = XmlApplicationContext.getContext(null);
57              applicationContext = SystemApplicationContext.getContext(applicationContext);
58          }
59          return applicationContext;
60      }
61  
62      public void addRequestDispatcher(RequestDispatcher requestDispatcher) throws ConfigurationException {
63          try {
64              Worker worker = new Worker();
65              worker.setObject(requestDispatcher);
66              this.requestDispatcherPool.addWorker(worker);
67          } catch (WorkerException e) {
68              throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
69          }
70      }
71  
72      public synchronized RequestDispatcher obtainRequestDispatcher() throws ConfigurationException {
73          try {
74              RequestDispatcher dispatcher = (RequestDispatcher) requestDispatcherPool.hireWorker().getObject();
75              if (log.isDebugEnabled()) log.debug(DISPATCHER_OBTAINED + dispatcher);
76              return dispatcher;
77          } catch (WorkerException e) {
78              throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
79          }
80      }
81  
82      public synchronized void releaseRequestDispatcher(RequestDispatcher requestDispatcher) throws ConfigurationException {
83          try {
84              requestDispatcherPool.freeWorkerFor(requestDispatcher);
85              if (log.isDebugEnabled()) log.debug(DISPATCHER_RELEASED + requestDispatcher);
86          } catch (WorkerException e) {
87              throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
88          }
89      }
90  
91      public void addResourceManager(ResourceManager resourceManager) throws ConfigurationException {
92          try {
93              Worker worker = new Worker();
94              worker.setObject(resourceManager);
95              this.resourceManagerPool.addWorker(worker);
96          } catch (WorkerException e) {
97              throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
98          }
99      }
100 
101     public synchronized ResourceManager obtainResourceManager() throws ConfigurationException {
102         try {
103             ResourceManager resourceManager = (ResourceManager) resourceManagerPool.hireWorker().getObject();
104             if (log.isDebugEnabled()) log.debug(RESOURCEMANAGER_OBTAINED + resourceManager);
105             return resourceManager;
106         } catch (WorkerException e) {
107             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
108         }
109     }
110 
111     public synchronized void releaseResourceManager(ResourceManager resourceManager) throws ConfigurationException {
112         try {
113             requestDispatcherPool.freeWorkerFor(resourceManager);
114             if (log.isDebugEnabled()) log.debug(RESOURCEMANAGER_RELEASED + resourceManager);
115         } catch (WorkerException e) {
116             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
117         }
118     }
119 
120     public void addFilterChainManager(FilterChainManager filterChainManager) throws ConfigurationException {
121         try {
122             Worker worker = new Worker();
123             worker.setObject(filterChainManager);
124             this.filterChainManagerPool.addWorker(worker);
125         } catch (WorkerException e) {
126             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
127         }
128     }
129 
130     public synchronized FilterChainManager obtainFilterChainManager() throws ConfigurationException {
131         try {
132             FilterChainManager filterChainManager = (FilterChainManager) filterChainManagerPool.hireWorker().getObject();
133             if (log.isDebugEnabled()) log.debug(FILTERCHAINMANAGER_OBTAINED + filterChainManager);
134             return filterChainManager;
135         } catch (WorkerException e) {
136             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
137         }
138     }
139 
140     public synchronized void releaseFilterChainManager(FilterChainManager filterChainManager) throws ConfigurationException {
141         try {
142             filterChainManagerPool.freeWorkerFor(filterChainManager);
143             if (log.isDebugEnabled()) log.debug(FILTERCHAINMANAGER_RELEASED + filterChainManager);
144         } catch (WorkerException e) {
145             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
146         }
147     }
148 
149     public void addEncoderManager(EncoderManager encoderManager) throws ConfigurationException {
150         try {
151             Worker worker = new Worker();
152             worker.setObject(encoderManager);
153             this.encoderManagerPool.addWorker(worker);
154         } catch (WorkerException e) {
155             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
156         }
157     }
158 
159     public synchronized EncoderManager obtainEncoderManager() throws ConfigurationException {
160         try {
161             EncoderManager encoderManager = (EncoderManager) encoderManagerPool.hireWorker().getObject();
162             if (log.isDebugEnabled()) log.debug(ENCODERMANAGER_OBTAINED + encoderManager);
163             return encoderManager;
164         } catch (WorkerException e) {
165             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
166         }
167     }
168 
169     public synchronized void releaseEncoderManager(EncoderManager encoderManager) throws ConfigurationException {
170         try {
171             if (log.isDebugEnabled()) log.debug(ENCODERMANAGER_RELEASED + encoderManager);
172             encoderManagerPool.freeWorkerFor(encoderManager);
173         } catch (WorkerException e) {
174             throw new ConfigurationException(CONFIGURATION_ERROR + e.getMessage());
175         }
176     }
177 
178     public String getProperty(String key) {
179         return properties.getProperty(key);
180     }
181 
182     public String getProperty(String key, String defaultValue) {
183         return properties.getProperty(key, defaultValue);
184     }
185 
186     public void setProperty(String key, String value) {
187         this.properties.setProperty(key, value);
188     }
189 
190     public void put(String key, Object value) {
191         this.properties.put(key, value);
192     }
193 
194     public void putAll(Map properties) {
195         this.properties.putAll(properties);
196     }
197 
198     public Object get(String key) {
199         return this.properties.get(key);
200     }
201 }