KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > container > ContainerServiceImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.base.container;
25
26 import java.util.Timer JavaDoc;
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 JavaDoc 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 JavaDoc timer;
50     private J2EEObjectStreamFactory j2eeObjectStreamFactory;
51
52     public ContainerServiceImpl() {
53     }
54     
55     public void initializeService() {
56         timer = new Timer JavaDoc(true);
57
58         try {
59             Class JavaDoc clazz = Class.forName(J2EE_OBJECT_FACTORY_NAME);
60             j2eeObjectStreamFactory = (J2EEObjectStreamFactory) clazz.newInstance();
61             _ejbLogger.log(Level.FINE, "Instantiated J2EEObjectStreamFactory");
62         } catch (Exception JavaDoc ex) {
63             _ejbLogger.log(Level.WARNING, "Couldn't instantiate "
64                     + "J2EEObjectstreamFactory", ex);
65         }
66     }
67
68     public Timer JavaDoc getTimer() {
69         return timer;
70     }
71
72     public J2EEObjectStreamFactory getJ2EEObjectStreamFactory() {
73         return j2eeObjectStreamFactory;
74     }
75
76     public void scheduleWork(ClassLoader JavaDoc classLoader, Runnable JavaDoc 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 JavaDoc th) {
85             String JavaDoc 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 JavaDoc classLoader;
96         private final Runnable JavaDoc target;
97         private long enqueTime;
98
99         public ThreadPoolWork(ClassLoader JavaDoc classLoader, Runnable JavaDoc 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 JavaDoc prevClassLoader =
114                 Thread.currentThread().getContextClassLoader();
115
116             try {
117                 java.security.AccessController.doPrivileged(
118                     new java.security.PrivilegedAction JavaDoc() {
119                         public java.lang.Object JavaDoc run() {
120                             Thread.currentThread().setContextClassLoader(
121                                 classLoader);
122                             return null;
123                         }
124                     }
125                 );
126
127                 target.run();
128
129             } catch (Throwable JavaDoc throwable) {
130                 _ejbLogger.log(Level.FINE, "Error during execution", throwable);
131             } finally {
132                 java.security.AccessController.doPrivileged(
133                     new java.security.PrivilegedAction JavaDoc() {
134                         public java.lang.Object JavaDoc run() {
135                             Thread.currentThread().setContextClassLoader(
136                                 prevClassLoader);
137                             return null;
138                         }
139                     }
140                 );
141             }
142         }
143         
144         public String JavaDoc getName() {
145             return "ThreadPoolWork";
146         }
147
148     } // end ThreadPoolWork
149

150
151     public void shutdown() {
152         timer = null;
153     }
154
155 }
156
Popular Tags