1 23 24 package com.sun.ejb.base.container; 25 26 import java.util.Timer ; 27 import java.util.logging.*; 28 29 import com.sun.logging.*; 30 31 import com.sun.corba.ee.spi.orbutil.threadpool.Work; 32 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPoolManager; 33 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPool; 34 import com.sun.enterprise.util.S1ASThreadPoolManager; 35 import com.sun.enterprise.util.ORBManager; 36 37 import com.sun.ejb.spi.io.J2EEObjectStreamFactory; 38 39 public class ContainerServiceImpl 40 implements com.sun.ejb.spi.container.ContainerService 41 { 42 43 private static String J2EE_OBJECT_FACTORY_NAME = 44 "com.sun.ejb.base.io.J2EEObjectStreamFactoryImpl"; 45 46 private static Logger _ejbLogger = 47 LogDomains.getLogger(LogDomains.EJB_LOGGER); 48 49 private Timer timer; 50 private J2EEObjectStreamFactory j2eeObjectStreamFactory; 51 52 public ContainerServiceImpl() { 53 } 54 55 public void initializeService() { 56 timer = new Timer (true); 57 58 try { 59 Class clazz = Class.forName(J2EE_OBJECT_FACTORY_NAME); 60 j2eeObjectStreamFactory = (J2EEObjectStreamFactory) clazz.newInstance(); 61 _ejbLogger.log(Level.FINE, "Instantiated J2EEObjectStreamFactory"); 62 } catch (Exception ex) { 63 _ejbLogger.log(Level.WARNING, "Couldn't instantiate " 64 + "J2EEObjectstreamFactory", ex); 65 } 66 } 67 68 public Timer getTimer() { 69 return timer; 70 } 71 72 public J2EEObjectStreamFactory getJ2EEObjectStreamFactory() { 73 return j2eeObjectStreamFactory; 74 } 75 76 public void scheduleWork(ClassLoader classLoader, Runnable target) { 77 ThreadPoolWork work = new ThreadPoolWork(classLoader, target); 78 79 try { 80 ThreadPoolManager threadpoolMgr = 81 S1ASThreadPoolManager.getThreadPoolManager(); 82 ThreadPool threadpool = threadpoolMgr.getDefaultThreadPool(); 83 threadpool.getAnyWorkQueue().addWork(work); 84 } catch (Throwable th) { 85 String errMsg = "Error while adding work to orb threadpool. " 86 + "Hence doing it in current thread"; 87 _ejbLogger.log(Level.WARNING, errMsg, th); 88 work.doWork(); 89 } 90 } 91 92 private static class ThreadPoolWork 93 implements com.sun.corba.ee.spi.orbutil.threadpool.Work 94 { 95 private final ClassLoader classLoader; 96 private final Runnable target; 97 private long enqueTime; 98 99 public ThreadPoolWork(ClassLoader classLoader, Runnable target) { 100 this.classLoader = classLoader; 101 this.target = target; 102 } 103 104 public void setEnqueueTime(long timeInMillis) { 105 enqueTime = timeInMillis; 106 } 107 108 public long getEnqueueTime() { 109 return enqueTime; 110 } 111 112 public void doWork() { 113 final ClassLoader prevClassLoader = 114 Thread.currentThread().getContextClassLoader(); 115 116 try { 117 java.security.AccessController.doPrivileged( 118 new java.security.PrivilegedAction () { 119 public java.lang.Object run() { 120 Thread.currentThread().setContextClassLoader( 121 classLoader); 122 return null; 123 } 124 } 125 ); 126 127 target.run(); 128 129 } catch (Throwable throwable) { 130 _ejbLogger.log(Level.FINE, "Error during execution", throwable); 131 } finally { 132 java.security.AccessController.doPrivileged( 133 new java.security.PrivilegedAction () { 134 public java.lang.Object run() { 135 Thread.currentThread().setContextClassLoader( 136 prevClassLoader); 137 return null; 138 } 139 } 140 ); 141 } 142 } 143 144 public String getName() { 145 return "ThreadPoolWork"; 146 } 147 148 } 150 151 public void shutdown() { 152 timer = null; 153 } 154 155 } 156 | Popular Tags |