KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > Enhydra


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: Enhydra.java,v 1.5 2005/06/07 13:58:04 slobodan Exp $
22  *
23  * formatted with JxBeauty (c) johann.langhofer@nextra.at
24  */

25
26
27 package com.lutris.appserver.server;
28
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import com.lutris.appserver.server.session.SessionManager;
33 import com.lutris.appserver.server.sql.DatabaseManager;
34 import com.lutris.logging.LogChannel;
35 import com.lutris.logging.Logger;
36 //import org.enhydra.dods.DODS;
37
//import org.enhydra.dods.DODSException;
38

39
40 /**
41  * The Enhydra class provides static methods that allow each application
42  * to conviently get at their application object. It also provides
43  * access <P>
44  *
45  * This class implements dynamically scoped global variables, accessed
46  * via static methods. Which variable is accessed (and thus which
47  * Application object is returned) depends on the flow of control of
48  * the program before the access (the call stack). <P>
49  *
50  * Currently a hashtable keyed on the threads themselves is used to
51  * store the different Application, in Java 1.2 threads will have a field
52  * already available for storing client data. <P>
53  *
54  * Normal usage of this class is to call <CODE>register()</CODE> when
55  * a thread enters your application, then <CODE>unRegister()</CODE>
56  * when it leaves. It is very important that every <CODE>register()</CODE>
57  * is matched with an <CODE>unRegister()</CODE>, or else it will be
58  * a memory leak, threads that re-enter the application will have
59  * a stale Application (until the first <CODE>register()</CODE>), and
60  * an extra pointer to the Application will be left around, preventing
61  * garbage collection. <P>
62  *
63  * If no thread is passed in (the normal usage), the current thread is
64  * used. If your application creates new threads, these will not initially be
65  * associated with any Application. These new threads can be passed in to
66  * <CODE>register()</CODE>, to assocaiate them with an Application.
67  * You may call <CODE>register()</CODE> before calling <CODE>start()</CODE>
68  * on the thread. <P>
69  *
70  * To ensure that every <CODE>register()</CODE> is balanced with an
71  * <CODE>unRegister()</CODE>, the following code is recommended:
72  * <PRE>
73  * Enhydra.register(application);
74  * try {
75  * doSomeWork();
76  * } finally {
77  * Enhydra.unRegister();
78  * }
79  * </PRE>
80  * This will ensure that the <CODE>unRegister()</CODE> is called even if
81  * an exception is thrown, but it will not interfere with the exception
82  * processing.
83  *
84  * @version $Revision: 1.5 $
85  * @author Paul Morgan
86  * @author Andy John
87  * @since LBS1.8
88  */

89 public class Enhydra {
90   private static String JavaDoc DODS_CLASS_NAME_STRING = "org.enhydra.dods.DODS";
91   private static Class JavaDoc DodsClass;
92   private static boolean dodsThreadingIsSet=false;
93   private static boolean dodsIsSet=false;
94   
95   private static Class JavaDoc getDodsClass(){
96     try {
97         if(!dodsIsSet){
98             DodsClass=Class.forName(DODS_CLASS_NAME_STRING);
99             dodsIsSet=true;
100         }
101         return DodsClass;
102     } catch (Exception JavaDoc e) {
103         e.printStackTrace();
104         throw new RuntimeException JavaDoc(e.getMessage());
105     }
106   }
107   
108   private static void InitDodsThreading(){
109     if (!dodsThreadingIsSet){
110         DODS_setThreading(true);
111         dodsThreadingIsSet=true;
112     }
113   }
114   
115   private static void DODS_setThreading(boolean value){
116     try {
117         Class JavaDoc[] ArgClassArray = new Class JavaDoc[] {boolean.class};
118         Object JavaDoc[] ArgObject = new Object JavaDoc[] {Boolean.valueOf("true")};
119         Method JavaDoc DODSMethod = getDodsClass().getDeclaredMethod("setThreading",ArgClassArray);
120         DODSMethod.invoke(getDodsClass(),ArgObject);
121     } catch (Exception JavaDoc e){
122         throw new RuntimeException JavaDoc(e.getMessage());
123     }
124   }
125   
126   private static void DODS_register(Thread JavaDoc currentThread,DatabaseManager dbm){
127     try {
128         InitDodsThreading();
129         Class JavaDoc[] ArgClassArray = new Class JavaDoc[] {Thread JavaDoc.class,DatabaseManager.class};
130         Object JavaDoc[] ArgObject = new Object JavaDoc[] {currentThread,dbm};
131         Method JavaDoc DODSMethod = getDodsClass().getDeclaredMethod("register",ArgClassArray);
132         DODSMethod.invoke(getDodsClass(),ArgObject);
133     } catch (Exception JavaDoc e) {
134         e.printStackTrace();
135         throw new RuntimeException JavaDoc(e.getMessage());
136     }
137   }
138   
139   private static void DODS_registerLogChannel(Thread JavaDoc currentThread,LogChannel lc){
140     try{
141         InitDodsThreading();
142         Class JavaDoc[] ArgClassArray = new Class JavaDoc[] {Thread JavaDoc.class,LogChannel.class};
143         Object JavaDoc[] ArgObject = new Object JavaDoc[] {currentThread,lc};
144         Method JavaDoc DODSMethod = getDodsClass().getDeclaredMethod("registerLogChannel",ArgClassArray);
145         DODSMethod.invoke(getDodsClass(),ArgObject);
146     } catch (Exception JavaDoc e) {
147         throw new RuntimeException JavaDoc(e.getMessage());
148     }
149   }
150   
151   private static void DODS_unregister(Thread JavaDoc currentThread){
152     try{
153         InitDodsThreading();
154         
155         Class JavaDoc[] ArgClassArray = new Class JavaDoc[] {Thread JavaDoc.class};
156         Object JavaDoc[] ArgObject = new Object JavaDoc[] {currentThread};
157         
158         Method JavaDoc DODSMethod = getDodsClass().getDeclaredMethod("unregister",ArgClassArray);
159         DODSMethod.invoke(getDodsClass(),ArgObject);
160         
161         Method JavaDoc DODSunregisterLogChannel = getDodsClass().getDeclaredMethod("unregisterLogChannel",ArgClassArray);
162         DODSunregisterLogChannel.invoke(getDodsClass(),ArgObject);
163         
164     } catch (Exception JavaDoc e) {
165       throw new RuntimeException JavaDoc(e.getMessage());
166     }
167   }
168   // dp 13.10.03 end
169

170   
171   // One application per thread.
172
private static Hashtable JavaDoc applications = new Hashtable JavaDoc();
173   // compliance with WEBDOCWF begin
174
private static Hashtable JavaDoc htAppCount = new Hashtable JavaDoc();
175   // original line
176
//
177
// compliance with WEBDOCWF end
178

179 // static {
180
// DODS.setThreading(true);
181
// }
182
/**
183    * Prevent instantiation
184    */

185   protected Enhydra () {
186   }
187
188   /**
189    * Associate the given application with the current thread.
190    * After this call, calls to <CODE>getApplication()</CODE>
191    * made by this thread will return <CODE>app</CODE>.
192    * Be sure to call <CODE>unRegister()</CODE> when you are done
193    * with the thread. This must be called before the any calls to
194    * <CODE>getApplication()</CODE>.
195    *
196    * @param app The Application object to associate with the
197    * current thread. This Application will be returned by calls made
198    * by this thread to <CODE>getApplication()</CODE>.
199    */

200   public static void register (Application app) {
201       applications.put(Thread.currentThread(), app);
202       DatabaseManager databaseManager = app.getDatabaseManager();
203       if (databaseManager != null){
204       DODS_register(Thread.currentThread(),databaseManager);
205       LogChannel channel = app.getLogChannel();
206       if (channel == null){
207           try{
208           //DODS.configureStandardLogerChannel();
209
InitDodsThreading();
210           Method JavaDoc DODSMethod = getDodsClass()
211               .getDeclaredMethod("configureStandardLogerChannel",
212                      new Class JavaDoc[]{});
213           DODSMethod.invoke(getDodsClass(),new Object JavaDoc[]{});
214           } catch (Exception JavaDoc e) {
215           throw new RuntimeException JavaDoc(e.getMessage());
216           }
217       }
218       DODS_registerLogChannel(Thread.currentThread(),channel);
219       }
220   }
221
222   /**
223    * Associate the given application with the given thread.
224    * After this call, calls to <CODE>getApplication()</CODE>
225    * made by the specified thread thread will return <CODE>app</CODE>.
226    * Be sure to call <CODE>unRegister()</CODE> when you are done
227    * with the thread. This must be called before the thread calls
228    * <CODE>getApplication()</CODE>.
229    *
230    * @param thread
231    * the thread to associate with <CODE>app</CODE>.
232    * @param app
233    * the Application object to associate with the
234    * specified thread. This Application will be returned by calls made
235    * by this thread to <CODE>getApplication()</CODE>.
236    */

237   // compliance with WEBDOCWF begin
238
public static synchronized void register (Thread JavaDoc thread, Application app) {
239     Integer JavaDoc intRefCount = (Integer JavaDoc)htAppCount.get(thread);
240     if (intRefCount != null && intRefCount.intValue() > 0) {
241       // increase the reference counter
242
htAppCount.put(thread, new Integer JavaDoc(intRefCount.intValue() + 1));
243     }
244     else {
245       // set the AppCounter to the initial value of 1
246
htAppCount.put(thread, new Integer JavaDoc(1));
247       applications.put(thread, app);
248     }
249     DatabaseManager databaseManager = app.getDatabaseManager();
250     if (databaseManager != null) {
251     DODS_register(thread,databaseManager);
252     LogChannel channel = app.getLogChannel();
253         if (channel != null){
254         DODS_registerLogChannel(Thread.currentThread(),channel);
255     }
256     }
257     // original line
258
/*
259      public static void register(Thread thread, Application app) {
260      applications.put(thread, app);
261      */

262     // compliance with WEBDOCWF end
263
}
264
265   /**
266    * Remove the association between the current thread and it's Application.
267    * This must be called when the thread is done using this Enhydra class.
268    * The current thread should not call <CODE>getApplication()</CODE>
269    * after this.
270    */

271   public static void unRegister () {
272     unRegister(Thread.currentThread());
273   }
274
275   /**
276    * Remove the association between the specified thread and it's Application.
277    * This must be called when the thread is done using this Enhydra class.
278    * The thread should not call <CODE>getApplication()</CODE>
279    * after this.
280    *
281    * @param thread
282    * the thread to operate on.
283    */

284   // compliance with WEBDOCWF begin
285
public static synchronized void unRegister (Thread JavaDoc thread) {
286     Integer JavaDoc intAppCount = (Integer JavaDoc)htAppCount.get(thread);
287     if (intAppCount != null && intAppCount.intValue() > 1) {
288       // decrease the reference counter
289
htAppCount.put(thread, new Integer JavaDoc(intAppCount.intValue() - 1));
290     }
291     else {
292       htAppCount.remove(thread);
293       try {
294          if (dodsIsSet){
295             DODS_unregister(thread);
296          }
297       } catch (Exception JavaDoc e) {}
298     }
299   }
300
301   /**
302    * Return the application object for current application.
303    * Returns null if there is no Application currently associated
304    * with the current thread.
305    *
306    * @return The application object.
307    */

308   public static Application getApplication () {
309     return (Application)applications.get(Thread.currentThread());
310   }
311
312   /**
313    * Return the database manager object for current application.
314    * Returns null if there is no database manager associated
315    * with the application or there is no application associated
316    * with the current thread.
317    *
318    * @return The database manager object if available else null.
319    */

320   public static DatabaseManager getDatabaseManager () {
321     Application app = getApplication();
322     if (app != null) {
323       return app.getDatabaseManager();
324     }
325     else {
326       return null;
327     }
328   }
329
330   /**
331    * Return the session manager object for current application.
332    * Returns null if there is no session manager associated
333    * with the application or there is no application associated
334    * with the current thread.
335    *
336    * @return The session manager object if available else null.
337    */

338   public static SessionManager getSessionManager () {
339     Application app = getApplication();
340     if (app != null) {
341       return app.getSessionManager();
342     }
343     else {
344       return null;
345     }
346   }
347
348   /**
349    * Return the LogChannel in the current Application object, or
350    * a log channel to the facility "Enhydra" if there is no current
351    * Application set.
352    *
353    * @return The LogChannel for the current application, or a
354    * generic LogChannel.
355    */

356     public static LogChannel getLogChannel() {
357     Application app = getApplication();
358     LogChannel chan = null;
359     if (app != null)
360         chan = app.getLogChannel();
361     if (chan != null)
362             return chan;
363     Logger logger = Logger.getCentralLogger();
364     if (logger == null)
365         return null; // This should never happen.
366
return logger.getChannel("Enhydra");
367     }
368 }
369
370
371
372
Popular Tags