1 7 22 23 package com.sun.corba.se.impl.naming.pcosnaming; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.Serializable ; 31 import java.util.Hashtable ; 32 33 import org.omg.CORBA.Policy ; 34 import org.omg.CORBA.LocalObject ; 35 36 import org.omg.PortableServer.POA ; 37 import org.omg.PortableServer.Servant ; 38 import org.omg.PortableServer.ForwardRequest ; 39 import org.omg.PortableServer.ServantLocator ; 40 import org.omg.PortableServer.LifespanPolicyValue ; 41 import org.omg.PortableServer.RequestProcessingPolicyValue ; 42 import org.omg.PortableServer.IdAssignmentPolicyValue ; 43 import org.omg.PortableServer.ServantRetentionPolicyValue ; 44 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder ; 45 46 import com.sun.corba.se.spi.orb.ORB; 47 48 53 54 public class ServantManagerImpl extends org.omg.CORBA.LocalObject implements ServantLocator 55 { 56 57 59 private static final long serialVersionUID = 4028710359865748280L; 60 private ORB orb; 61 62 private NameService theNameService; 63 64 private File logDir; 65 66 private Hashtable contexts; 67 68 private CounterDB counterDb; 69 70 private int counter; 71 72 private final static String objKeyPrefix = "NC"; 73 74 ServantManagerImpl(ORB orb, File logDir, NameService aNameService) 75 { 76 this.logDir = logDir; 77 this.orb = orb; 78 counterDb = new CounterDB(logDir); 80 contexts = new Hashtable (); 81 theNameService = aNameService; 82 } 83 84 85 public Servant preinvoke(byte[] oid, POA adapter, String operation, 86 CookieHolder cookie) throws ForwardRequest 87 { 88 89 String objKey = new String (oid); 90 91 Servant servant = (Servant ) contexts.get(objKey); 92 93 if (servant == null) 94 { 95 servant = readInContext(objKey); 96 } 97 98 return servant; 99 } 100 101 public void postinvoke(byte[] oid, POA adapter, String operation, 102 java.lang.Object cookie, Servant servant) 103 { 104 } 106 107 public NamingContextImpl readInContext(String objKey) 108 { 109 NamingContextImpl context = (NamingContextImpl) contexts.get(objKey); 110 if( context != null ) 111 { 112 return context; 114 } 115 116 File contextFile = new File (logDir, objKey); 117 if (contextFile.exists()) { 118 try { 119 FileInputStream fis = new FileInputStream (contextFile); 120 ObjectInputStream ois = new ObjectInputStream (fis); 121 context = (NamingContextImpl) ois.readObject(); 122 context.setORB( orb ); 123 context.setServantManagerImpl( this ); 124 context.setRootNameService( theNameService ); 125 ois.close(); 126 } catch (Exception ex) { 127 } 128 } 129 130 if (context != null) 131 { 132 contexts.put(objKey, context); 133 } 134 return context; 135 } 136 137 public NamingContextImpl addContext(String objKey, 138 NamingContextImpl context) 139 { 140 File contextFile = new File (logDir, objKey); 141 142 if (contextFile.exists()) 143 { 144 context = readInContext(objKey); 145 } 146 else { 147 try { 148 FileOutputStream fos = new FileOutputStream (contextFile); 149 ObjectOutputStream oos = new ObjectOutputStream (fos); 150 oos.writeObject(context); 151 oos.close(); 152 } catch (Exception ex) { 153 } 154 } 155 try 156 { 157 contexts.remove( objKey ); 158 } 159 catch( Exception e) 160 { 161 } 162 contexts.put(objKey, context); 163 164 return context; 165 } 166 167 public void updateContext( String objKey, 168 NamingContextImpl context ) 169 { 170 File contextFile = new File (logDir, objKey); 171 if (contextFile.exists()) 172 { 173 contextFile.delete( ); 174 contextFile = new File (logDir, objKey); 175 } 176 177 try { 178 FileOutputStream fos = new FileOutputStream (contextFile); 179 ObjectOutputStream oos = new ObjectOutputStream (fos); 180 oos.writeObject(context); 181 oos.close(); 182 } catch (Exception ex) { 183 ex.printStackTrace( ); 184 } 185 } 186 187 public static String getRootObjectKey() 188 { 189 return objKeyPrefix + CounterDB.rootCounter; 190 } 191 192 public String getNewObjectKey() 193 { 194 return objKeyPrefix + counterDb.getNextCounter(); 195 } 196 197 198 199 } 200 201 class CounterDB implements Serializable 202 { 203 204 CounterDB (File logDir) 205 { 206 counterFileName = "counter"; 207 counterFile = new File (logDir, counterFileName); 208 if (!counterFile.exists()) { 209 counter = new Integer (rootCounter); 210 writeCounter(); 211 } else { 212 readCounter(); 213 } 214 } 215 216 private void readCounter() 217 { 218 try { 219 FileInputStream fis = new FileInputStream (counterFile); 220 ObjectInputStream ois = new ObjectInputStream (fis); 221 counter = (Integer ) ois.readObject(); 222 ois.close(); 223 } catch (Exception ex) { 224 } 225 } 226 227 private void writeCounter() 228 { 229 try { 230 counterFile.delete(); 231 FileOutputStream fos = new FileOutputStream (counterFile); 232 ObjectOutputStream oos = new ObjectOutputStream (fos); 233 oos.writeObject(counter); 234 oos.flush(); 235 oos.close(); 236 237 } catch (Exception ex) { 238 } 239 } 240 241 public synchronized int getNextCounter() 242 { 243 int counterVal = counter.intValue(); 244 counter = new Integer (++counterVal); 245 writeCounter(); 246 247 return counterVal; 248 } 249 250 251 252 private Integer counter; 253 254 private static String counterFileName = "counter"; 255 256 private transient File counterFile; 257 258 public final static int rootCounter = 0; 259 } 260 | Popular Tags |