1 25 26 27 package com.lutris.appserver.server; 28 29 import java.lang.reflect.Method ; 30 import java.util.Hashtable ; 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 39 40 89 public class Enhydra { 90 private static String DODS_CLASS_NAME_STRING = "org.enhydra.dods.DODS"; 91 private static Class DodsClass; 92 private static boolean dodsThreadingIsSet=false; 93 private static boolean dodsIsSet=false; 94 95 private static Class getDodsClass(){ 96 try { 97 if(!dodsIsSet){ 98 DodsClass=Class.forName(DODS_CLASS_NAME_STRING); 99 dodsIsSet=true; 100 } 101 return DodsClass; 102 } catch (Exception e) { 103 e.printStackTrace(); 104 throw new RuntimeException (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 [] ArgClassArray = new Class [] {boolean.class}; 118 Object [] ArgObject = new Object [] {Boolean.valueOf("true")}; 119 Method DODSMethod = getDodsClass().getDeclaredMethod("setThreading",ArgClassArray); 120 DODSMethod.invoke(getDodsClass(),ArgObject); 121 } catch (Exception e){ 122 throw new RuntimeException (e.getMessage()); 123 } 124 } 125 126 private static void DODS_register(Thread currentThread,DatabaseManager dbm){ 127 try { 128 InitDodsThreading(); 129 Class [] ArgClassArray = new Class [] {Thread .class,DatabaseManager.class}; 130 Object [] ArgObject = new Object [] {currentThread,dbm}; 131 Method DODSMethod = getDodsClass().getDeclaredMethod("register",ArgClassArray); 132 DODSMethod.invoke(getDodsClass(),ArgObject); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 throw new RuntimeException (e.getMessage()); 136 } 137 } 138 139 private static void DODS_registerLogChannel(Thread currentThread,LogChannel lc){ 140 try{ 141 InitDodsThreading(); 142 Class [] ArgClassArray = new Class [] {Thread .class,LogChannel.class}; 143 Object [] ArgObject = new Object [] {currentThread,lc}; 144 Method DODSMethod = getDodsClass().getDeclaredMethod("registerLogChannel",ArgClassArray); 145 DODSMethod.invoke(getDodsClass(),ArgObject); 146 } catch (Exception e) { 147 throw new RuntimeException (e.getMessage()); 148 } 149 } 150 151 private static void DODS_unregister(Thread currentThread){ 152 try{ 153 InitDodsThreading(); 154 155 Class [] ArgClassArray = new Class [] {Thread .class}; 156 Object [] ArgObject = new Object [] {currentThread}; 157 158 Method DODSMethod = getDodsClass().getDeclaredMethod("unregister",ArgClassArray); 159 DODSMethod.invoke(getDodsClass(),ArgObject); 160 161 Method DODSunregisterLogChannel = getDodsClass().getDeclaredMethod("unregisterLogChannel",ArgClassArray); 162 DODSunregisterLogChannel.invoke(getDodsClass(),ArgObject); 163 164 } catch (Exception e) { 165 throw new RuntimeException (e.getMessage()); 166 } 167 } 168 170 171 private static Hashtable applications = new Hashtable (); 173 private static Hashtable htAppCount = new Hashtable (); 175 179 185 protected Enhydra () { 186 } 187 188 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 InitDodsThreading(); 210 Method DODSMethod = getDodsClass() 211 .getDeclaredMethod("configureStandardLogerChannel", 212 new Class []{}); 213 DODSMethod.invoke(getDodsClass(),new Object []{}); 214 } catch (Exception e) { 215 throw new RuntimeException (e.getMessage()); 216 } 217 } 218 DODS_registerLogChannel(Thread.currentThread(),channel); 219 } 220 } 221 222 237 public static synchronized void register (Thread thread, Application app) { 239 Integer intRefCount = (Integer )htAppCount.get(thread); 240 if (intRefCount != null && intRefCount.intValue() > 0) { 241 htAppCount.put(thread, new Integer (intRefCount.intValue() + 1)); 243 } 244 else { 245 htAppCount.put(thread, new Integer (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 262 } 264 265 271 public static void unRegister () { 272 unRegister(Thread.currentThread()); 273 } 274 275 284 public static synchronized void unRegister (Thread thread) { 286 Integer intAppCount = (Integer )htAppCount.get(thread); 287 if (intAppCount != null && intAppCount.intValue() > 1) { 288 htAppCount.put(thread, new Integer (intAppCount.intValue() - 1)); 290 } 291 else { 292 htAppCount.remove(thread); 293 try { 294 if (dodsIsSet){ 295 DODS_unregister(thread); 296 } 297 } catch (Exception e) {} 298 } 299 } 300 301 308 public static Application getApplication () { 309 return (Application)applications.get(Thread.currentThread()); 310 } 311 312 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 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 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; return logger.getChannel("Enhydra"); 367 } 368 } 369 370 371 372 | Popular Tags |