KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > ResourceManagerImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jca;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.loader.CloseListener;
33 import com.caucho.loader.DynamicClassLoader;
34 import com.caucho.loader.Environment;
35 import com.caucho.loader.EnvironmentLocal;
36 import com.caucho.log.Log;
37 import com.caucho.util.L10N;
38
39 import javax.naming.Context JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.resource.spi.BootstrapContext JavaDoc;
42 import javax.resource.spi.ResourceAdapter JavaDoc;
43 import javax.resource.spi.XATerminator JavaDoc;
44 import javax.resource.spi.work.WorkManager JavaDoc;
45 import java.lang.ref.SoftReference JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Timer JavaDoc;
48 import java.util.logging.Level JavaDoc;
49 import java.util.logging.Logger JavaDoc;
50
51 /**
52  * Implementation of the resource manager.
53  */

54 public class ResourceManagerImpl implements BootstrapContext JavaDoc {
55   private static final L10N L = new L10N(ResourceManagerImpl.class);
56   private static final Logger JavaDoc log = Log.open(ResourceManagerImpl.class);
57
58   private static EnvironmentLocal<ResourceManagerImpl> _localManager =
59     new EnvironmentLocal<ResourceManagerImpl>();
60
61   private UserTransactionProxy _tm;
62
63   private ArrayList JavaDoc<ResourceAdapter JavaDoc> _resources =
64     new ArrayList JavaDoc<ResourceAdapter JavaDoc>();
65
66   private ArrayList JavaDoc<ConnectionPool> _connectionManagers =
67     new ArrayList JavaDoc<ConnectionPool>();
68
69   private ArrayList JavaDoc<SoftReference JavaDoc<Timer JavaDoc>> _timers =
70     new ArrayList JavaDoc<SoftReference JavaDoc<Timer JavaDoc>>();
71
72   private WorkManagerImpl _workManager;
73
74   private boolean _isInit;
75   private boolean _isClosed;
76
77   /**
78    * Constructor.
79    */

80   private ResourceManagerImpl()
81   {
82     Environment.addClassLoaderListener(new CloseListener(this));
83
84     try {
85       Context JavaDoc ic = new InitialContext JavaDoc();
86
87       _tm = (UserTransactionProxy) ic.lookup("java:comp/UserTransaction");
88     } catch (Exception JavaDoc e) {
89       log.log(Level.WARNING, e.toString(), e);
90       
91       throw new RuntimeException JavaDoc(e);
92     }
93   }
94
95   /**
96    * Returns the impl.
97    */

98   public static ResourceManagerImpl createLocalManager()
99   {
100     ResourceManagerImpl rm;
101     
102     synchronized (_localManager) {
103       rm = _localManager.getLevel();
104
105       if (rm == null) {
106     rm = new ResourceManagerImpl();
107     _localManager.set(rm);
108       }
109     }
110
111     return rm;
112   }
113
114   /**
115    * Returns the impl.
116    */

117   public static ResourceManagerImpl getLocalManager()
118   {
119     return _localManager.getLevel();
120   }
121
122   /**
123    * Adds a resource to the manager.
124    */

125   public static void addResource(ResourceAdapter JavaDoc resource)
126     throws ConfigException
127   {
128     ResourceManagerImpl rm = createLocalManager();
129
130     rm.addResourceImpl(resource);
131   }
132
133   /**
134    * Adds a resource to the resource manager.
135    */

136   private void addResourceImpl(ResourceAdapter JavaDoc resource)
137   {
138     try {
139       resource.start(this);
140     } catch (Throwable JavaDoc e) {
141       log.log(Level.WARNING, e.toString(), e);
142     }
143
144     _resources.add(resource);
145   }
146
147   /**
148    * Returns a connection manager
149    */

150   public ConnectionPool createConnectionPool()
151   {
152     ConnectionPool cm = new ConnectionPool();
153
154     cm.setTransactionManager(_tm);
155
156     _connectionManagers.add(cm);
157
158     return cm;
159   }
160
161   /**
162    * Returns a WorkManager instance.
163    */

164   public WorkManager JavaDoc getWorkManager()
165   {
166     synchronized (this) {
167       if (_workManager == null)
168     _workManager = new WorkManagerImpl();
169     }
170
171     return _workManager;
172   }
173
174   /**
175    * Returns an XATerminator. The XATerminator could be used for
176    * transaction completion and crash recovery.
177    */

178   public XATerminator JavaDoc getXATerminator()
179   {
180     return null;
181   }
182
183   /**
184    * Creates a new Timer instance.
185    */

186   public Timer JavaDoc createTimer()
187   {
188     TimerImpl timer = new TimerImpl(this);
189
190     synchronized (_timers) {
191       SoftReference JavaDoc<Timer JavaDoc> timerRef = new SoftReference JavaDoc<Timer JavaDoc>(timer);
192       
193       _timers.add(timerRef);
194     }
195     
196     return timer;
197   }
198
199   /**
200    * Removes a new Timer instance.
201    */

202   void removeTimer(Timer JavaDoc timer)
203   {
204     if (_timers == null)
205       return;
206     
207     synchronized (_timers) {
208       for (int i = _timers.size(); i >= 0; i--) {
209     SoftReference JavaDoc<Timer JavaDoc> timerRef = _timers.get(i);
210     Timer JavaDoc oldTimer = timerRef.get();
211
212     if (oldTimer == null)
213       _timers.remove(i);
214     else if (oldTimer == timer)
215       _timers.remove(i);
216       }
217     }
218   }
219   
220   /**
221    * Handles the case where a class loader is activated.
222    */

223   public void classLoaderInit(DynamicClassLoader loader)
224   {
225   }
226   
227   /**
228    * Handles the case where a class loader is dropped.
229    */

230   public void classLoaderDestroy(DynamicClassLoader loader)
231   {
232     destroy();
233   }
234
235   /**
236    * Closes the resource manager.
237    */

238   public void destroy()
239   {
240     ArrayList JavaDoc<ConnectionPool> connectionManagers;
241     ArrayList JavaDoc<ResourceAdapter JavaDoc> resources;
242     ArrayList JavaDoc<SoftReference JavaDoc<Timer JavaDoc>> timers;
243     
244     synchronized (this) {
245       if (_isClosed)
246     return;
247       _isClosed = true;
248       
249       connectionManagers = _connectionManagers;
250       _connectionManagers = null;
251       
252       resources = _resources;
253       _resources = null;
254       
255       timers = _timers;
256       _timers = null;
257     }
258
259     // Kill timers first, so they won't try to spawn work tasks
260
for (int i = 0; i < timers.size(); i++) {
261       SoftReference JavaDoc<Timer JavaDoc> timerRef = timers.get(i);
262       Timer JavaDoc timer = timerRef.get();
263
264       try {
265     if (timer != null)
266       timer.cancel();
267       } catch (Throwable JavaDoc e) {
268     log.log(Level.WARNING, e.toString(), e);
269       }
270     }
271
272     // cancel the work managers
273
if (_workManager != null)
274       _workManager.destroy();
275
276     for (int i = 0; i < connectionManagers.size(); i++) {
277       ConnectionPool connectionManager = connectionManagers.get(i);
278
279       try {
280     connectionManager.destroy();
281       } catch (Throwable JavaDoc e) {
282     log.log(Level.WARNING, e.toString(), e);
283       }
284     }
285
286     // finally, close the resources
287
for (int i = 0; i < resources.size(); i++) {
288       ResourceAdapter JavaDoc resource = resources.get(i);
289
290       try {
291     resource.stop();
292       } catch (Throwable JavaDoc e) {
293     log.log(Level.WARNING, e.toString(), e);
294       }
295     }
296   }
297
298   public class TimerImpl extends Timer JavaDoc {
299     private ResourceManagerImpl _rm;
300
301     TimerImpl(ResourceManagerImpl rm)
302     {
303       super(true);
304
305       _rm = rm;
306     }
307     
308     public void cancel()
309     {
310       _rm.removeTimer(this);
311
312       super.cancel();
313     }
314   }
315 }
316
Popular Tags