KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > WeakStopListener


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.loader;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import java.lang.ref.WeakReference JavaDoc;
35 import java.lang.reflect.Method JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Waits for the stop event and calls a stop() method.
41  */

42 public class WeakStopListener implements EnvironmentListener {
43   private static final L10N L = new L10N(WeakStopListener.class);
44   private static final Logger JavaDoc log = Log.open(WeakStopListener.class);
45
46   private WeakReference JavaDoc<Object JavaDoc> _resourceRef;
47
48   /**
49    * Creates the new stop listener.
50    *
51    * @param resource the resource which needs closing
52    */

53   public WeakStopListener(Object JavaDoc resource)
54   {
55     _resourceRef = new WeakReference JavaDoc<Object JavaDoc>(resource);
56   }
57
58   /**
59    * Handles the case where a class loader is activated.
60    */

61   public void environmentStart(EnvironmentClassLoader loader)
62   {
63   }
64   
65   /**
66    * Handles the case where a class loader is dropped.
67    */

68   public void environmentStop(EnvironmentClassLoader loader)
69   {
70     Object JavaDoc resource = _resourceRef.get();
71     if (resource == null)
72       return;
73     
74     Method JavaDoc destroy = getStopMethod(resource.getClass());
75
76     if (destroy == null)
77       return;
78     
79     try {
80       destroy.invoke(resource, (Object JavaDoc []) null);
81     } catch (Throwable JavaDoc e) {
82       log.log(Level.WARNING, e.toString(), e);
83     }
84   }
85
86   public static Method JavaDoc getStopMethod(Class JavaDoc cl)
87   {
88     try {
89       return cl.getMethod("stop", new Class JavaDoc[0]);
90     } catch (Throwable JavaDoc e) {
91     }
92
93     return null;
94   }
95 }
96
97
Popular Tags