1 23 24 package com.sun.ejb.spi.distributed; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.io.Serializable ; 32 import java.util.concurrent.ConcurrentHashMap ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 36 import com.sun.logging.LogDomains; 37 38 39 class DistributedReadOnlyBeanServiceImpl 40 implements DistributedReadOnlyBeanService { 41 42 private static Logger _logger; 43 static { 44 _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER); 45 } 46 47 private ConcurrentHashMap <Long , ReadOnlyBeanRefreshHandlerInfo> refreshHandlers 48 = new ConcurrentHashMap <Long , 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 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 pk) { 74 if (robNotifier != null) { 75 byte[] pkData = null; 76 77 ByteArrayOutputStream bos = null; 78 ObjectOutputStream oos = null; 79 try { 80 bos = new ByteArrayOutputStream (); 81 oos = new ObjectOutputStream (bos); 82 83 oos.writeObject(pk); 84 oos.flush(); 85 bos.flush(); 86 pkData = bos.toByteArray(); 87 robNotifier.notifyRefresh(ejbID, pkData); 88 } catch (Exception ex) { 89 _logger.log(Level.WARNING, "Error during notifyRefresh", ex); 90 } finally { 91 if (oos != null) { 92 try { oos.close(); } catch(IOException ioEx) {}; 93 } 94 if (bos != null) { 95 try { bos.close(); } catch(IOException 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 return; 134 } 135 136 final Thread currentThread = Thread.currentThread(); 137 final ClassLoader prevClassLoader = currentThread.getContextClassLoader(); 138 139 try { 140 java.security.AccessController.doPrivileged( 141 new java.security.PrivilegedAction () { 142 public java.lang.Object run() { 143 currentThread.setContextClassLoader(info.loader); 144 return null; 145 } 146 }); 147 148 if (! refreshAll) { 149 ByteArrayInputStream bis = null; 150 ObjectInputStream ois = null; 151 Serializable pk = null; 152 try { 153 bis = new ByteArrayInputStream (pkData); 154 ois = new ObjectInputStream (bis); 155 156 pk = (Serializable ) ois.readObject(); 157 } catch (IOException ioEx) { 158 _logger.log(Level.WARNING, "Error during refresh", ioEx); 159 } catch (ClassNotFoundException cnfEx) { 160 _logger.log(Level.WARNING, "Error during refresh", cnfEx); 161 } finally { 162 if (ois != null) { 163 try { 164 ois.close(); 165 } catch(IOException 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 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 ex) { 186 _logger.log(Level.WARNING, "Error during refresh", ex); 187 } finally { 188 java.security.AccessController.doPrivileged( 189 new java.security.PrivilegedAction () { 190 public java.lang.Object 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 loader; 202 public ReadOnlyBeanRefreshEventHandler handler; 203 204 public ReadOnlyBeanRefreshHandlerInfo(long ejbId, 205 ClassLoader loader, ReadOnlyBeanRefreshEventHandler handler) { 206 this.ejbId = ejbId; 207 this.loader = loader; 208 this.handler = handler; 209 } 210 } 211 } 212 | Popular Tags |