KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > spi > distributed > DistributedReadOnlyBeanServiceImpl


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.spi.distributed;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.util.concurrent.ConcurrentHashMap JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 import com.sun.logging.LogDomains;
37
38
39 class DistributedReadOnlyBeanServiceImpl
40     implements DistributedReadOnlyBeanService {
41     
42     private static Logger JavaDoc _logger;
43     static {
44         _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER);
45     }
46     
47     private ConcurrentHashMap JavaDoc<Long JavaDoc, ReadOnlyBeanRefreshHandlerInfo> refreshHandlers
48         = new ConcurrentHashMap JavaDoc<Long JavaDoc, ReadOnlyBeanRefreshHandlerInfo>();
49     
50     
51     private DistributedReadOnlyBeanNotifier robNotifier;
52     
53     public void setDistributedReadOnlyBeanNotifier(
54             DistributedReadOnlyBeanNotifier notifier) {
55         this.robNotifier = notifier;
56         _logger.log(Level.INFO, "Registered ReadOnlyBeanNotifier: "
57                 + notifier);
58     }
59     
60     public void addReadOnlyBeanRefreshEventHandler(
61             long ejbID, ClassLoader JavaDoc loader,
62             ReadOnlyBeanRefreshEventHandler handler) {
63         refreshHandlers.put(ejbID, new ReadOnlyBeanRefreshHandlerInfo(
64                 ejbID, loader, handler));
65         _logger.log(Level.INFO, "Registered ReadOnlyBeanRefreshEventHandler: "
66                 + ejbID + "; " + handler);
67     }
68
69     public void removeReadOnlyBeanRefreshEventHandler(long ejbID) {
70         refreshHandlers.remove(ejbID);
71     }
72
73     public void notifyRefresh(long ejbID, Object JavaDoc pk) {
74         if (robNotifier != null) {
75             byte[] pkData = null;
76             
77             ByteArrayOutputStream JavaDoc bos = null;
78             ObjectOutputStream JavaDoc oos = null;
79             try {
80                 bos = new ByteArrayOutputStream JavaDoc();
81                 oos = new ObjectOutputStream JavaDoc(bos);
82                 
83                 oos.writeObject(pk);
84                 oos.flush();
85                 bos.flush();
86                 pkData = bos.toByteArray();
87                 robNotifier.notifyRefresh(ejbID, pkData);
88             } catch (Exception JavaDoc ex) {
89                 _logger.log(Level.WARNING, "Error during notifyRefresh", ex);
90             } finally {
91                 if (oos != null) {
92                     try { oos.close(); } catch(IOException JavaDoc ioEx) {};
93                 }
94                 if (bos != null) {
95                     try { bos.close(); } catch(IOException JavaDoc ioEx) {};
96                 }
97             }
98         } else {
99             if (_logger.isLoggable(Level.FINE)) {
100                 _logger.log(Level.FINE,
101                         "DistributedReadOnlyBeanService ignoring request "
102                         + "for notifyRefresh: " + ejbID);
103             }
104         }
105     }
106
107     public void notifyRefreshAll(long ejbID) {
108         if (robNotifier != null) {
109             robNotifier.notifyRefreshAll(ejbID);
110         } else {
111             if (_logger.isLoggable(Level.FINE)) {
112                 _logger.log(Level.FINE,
113                         "DistributedReadOnlyBeanService ignoring request "
114                         + "for notifyRefreshAll: " + ejbID);
115             }
116         }
117     }
118     
119     public void handleRefreshRequest(long ejbID, byte[] pkData) {
120         refreshRequestReceived(false, ejbID, pkData);
121     }
122     
123     public void handleRefreshAllRequest(long ejbID) {
124         refreshRequestReceived(true, ejbID, null);
125     }
126     
127     private void refreshRequestReceived(boolean refreshAll,
128             long ejbID, byte[] pkData) {
129
130         final ReadOnlyBeanRefreshHandlerInfo info = refreshHandlers.get(ejbID);
131         if (info == null) {
132             //TODO: Log something
133
return;
134         }
135         
136         final Thread JavaDoc currentThread = Thread.currentThread();
137         final ClassLoader JavaDoc prevClassLoader = currentThread.getContextClassLoader();
138         
139         try {
140             java.security.AccessController.doPrivileged(
141                 new java.security.PrivilegedAction JavaDoc() {
142                     public java.lang.Object JavaDoc run() {
143                         currentThread.setContextClassLoader(info.loader);
144                         return null;
145                     }
146                 });
147             
148             if (! refreshAll) {
149                 ByteArrayInputStream JavaDoc bis = null;
150                 ObjectInputStream JavaDoc ois = null;
151                 Serializable JavaDoc pk = null;
152                 try {
153                     bis = new ByteArrayInputStream JavaDoc(pkData);
154                     ois = new ObjectInputStream JavaDoc(bis);
155                     
156                     pk = (Serializable JavaDoc) ois.readObject();
157                 } catch (IOException JavaDoc ioEx) {
158                     _logger.log(Level.WARNING, "Error during refresh", ioEx);
159                 } catch (ClassNotFoundException JavaDoc cnfEx) {
160                     _logger.log(Level.WARNING, "Error during refresh", cnfEx);
161                 } finally {
162                     if (ois != null) {
163                         try {
164                             ois.close();
165                         } catch(IOException JavaDoc ioEx) {
166                             _logger.log(Level.WARNING,
167                                     "Error while closing object stream", ioEx);
168                         };
169                     }
170                     if (bis != null) {
171                         try {
172                             bis.close();
173                         } catch(IOException JavaDoc ioEx) {
174                             _logger.log(Level.WARNING,
175                                     "Error while closing byte stream", ioEx);
176                         };
177                     }
178                 }
179                 if (pk != null) {
180                     info.handler.handleRefreshRequest(pk);
181                 }
182             } else {
183                 info.handler.handleRefreshAllRequest();
184             }
185         } catch (Exception JavaDoc ex) {
186             _logger.log(Level.WARNING, "Error during refresh", ex);
187         } finally {
188             java.security.AccessController.doPrivileged(
189                     new java.security.PrivilegedAction JavaDoc() {
190                         public java.lang.Object JavaDoc run() {
191                             currentThread.setContextClassLoader(prevClassLoader);
192                             return null;
193                         }
194                     });
195
196         }
197     }
198     
199     private static class ReadOnlyBeanRefreshHandlerInfo {
200         public long ejbId;
201         public ClassLoader JavaDoc loader;
202         public ReadOnlyBeanRefreshEventHandler handler;
203         
204         public ReadOnlyBeanRefreshHandlerInfo(long ejbId,
205                 ClassLoader JavaDoc loader, ReadOnlyBeanRefreshEventHandler handler) {
206             this.ejbId = ejbId;
207             this.loader = loader;
208             this.handler = handler;
209         }
210     }
211 }
212
Popular Tags