1. /*
  2. * @(#)ServantManagerImpl.java 1.12 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)ServantManagerImpl.java 1.12 03/12/19
  9. *
  10. * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
  11. * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
  12. *
  13. * This software is the confidential and proprietary information of Sun
  14. * Microsystems, Inc. ("Confidential Information"). You shall not
  15. * disclose such Confidential Information and shall use it only in
  16. * accordance with the terms of the license agreement you entered into
  17. * with Sun.
  18. *
  19. * CopyrightVersion 1.2
  20. *
  21. */
  22. package com.sun.corba.se.impl.naming.pcosnaming;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.ObjectInputStream;
  27. import java.io.ObjectOutputStream;
  28. import java.io.Serializable;
  29. import java.util.Hashtable;
  30. import org.omg.CORBA.Policy;
  31. import org.omg.CORBA.LocalObject;
  32. import org.omg.PortableServer.POA;
  33. import org.omg.PortableServer.Servant;
  34. import org.omg.PortableServer.ForwardRequest;
  35. import org.omg.PortableServer.ServantLocator;
  36. import org.omg.PortableServer.LifespanPolicyValue;
  37. import org.omg.PortableServer.RequestProcessingPolicyValue;
  38. import org.omg.PortableServer.IdAssignmentPolicyValue;
  39. import org.omg.PortableServer.ServantRetentionPolicyValue;
  40. import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
  41. import com.sun.corba.se.spi.orb.ORB;
  42. /**
  43. * @version 1.6, 99/10/07
  44. * @author Rohit Garg
  45. * @since JDK1.2
  46. */
  47. public class ServantManagerImpl extends org.omg.CORBA.LocalObject implements ServantLocator
  48. {
  49. // computed using serialver tool
  50. private static final long serialVersionUID = 4028710359865748280L;
  51. private ORB orb;
  52. private NameService theNameService;
  53. private File logDir;
  54. private Hashtable contexts;
  55. private CounterDB counterDb;
  56. private int counter;
  57. private final static String objKeyPrefix = "NC";
  58. ServantManagerImpl(ORB orb, File logDir, NameService aNameService)
  59. {
  60. this.logDir = logDir;
  61. this.orb = orb;
  62. // initialize the counter database
  63. counterDb = new CounterDB(logDir);
  64. contexts = new Hashtable();
  65. theNameService = aNameService;
  66. }
  67. public Servant preinvoke(byte[] oid, POA adapter, String operation,
  68. CookieHolder cookie) throws ForwardRequest
  69. {
  70. String objKey = new String(oid);
  71. Servant servant = (Servant) contexts.get(objKey);
  72. if (servant == null)
  73. {
  74. servant = readInContext(objKey);
  75. }
  76. return servant;
  77. }
  78. public void postinvoke(byte[] oid, POA adapter, String operation,
  79. java.lang.Object cookie, Servant servant)
  80. {
  81. // nada
  82. }
  83. public NamingContextImpl readInContext(String objKey)
  84. {
  85. NamingContextImpl context = (NamingContextImpl) contexts.get(objKey);
  86. if( context != null )
  87. {
  88. // Returning Context from Cache
  89. return context;
  90. }
  91. File contextFile = new File(logDir, objKey);
  92. if (contextFile.exists()) {
  93. try {
  94. FileInputStream fis = new FileInputStream(contextFile);
  95. ObjectInputStream ois = new ObjectInputStream(fis);
  96. context = (NamingContextImpl) ois.readObject();
  97. context.setORB( orb );
  98. context.setServantManagerImpl( this );
  99. context.setRootNameService( theNameService );
  100. ois.close();
  101. } catch (Exception ex) {
  102. }
  103. }
  104. if (context != null)
  105. {
  106. contexts.put(objKey, context);
  107. }
  108. return context;
  109. }
  110. public NamingContextImpl addContext(String objKey,
  111. NamingContextImpl context)
  112. {
  113. File contextFile = new File(logDir, objKey);
  114. if (contextFile.exists())
  115. {
  116. context = readInContext(objKey);
  117. }
  118. else {
  119. try {
  120. FileOutputStream fos = new FileOutputStream(contextFile);
  121. ObjectOutputStream oos = new ObjectOutputStream(fos);
  122. oos.writeObject(context);
  123. oos.close();
  124. } catch (Exception ex) {
  125. }
  126. }
  127. try
  128. {
  129. contexts.remove( objKey );
  130. }
  131. catch( Exception e)
  132. {
  133. }
  134. contexts.put(objKey, context);
  135. return context;
  136. }
  137. public void updateContext( String objKey,
  138. NamingContextImpl context )
  139. {
  140. File contextFile = new File(logDir, objKey);
  141. if (contextFile.exists())
  142. {
  143. contextFile.delete( );
  144. contextFile = new File(logDir, objKey);
  145. }
  146. try {
  147. FileOutputStream fos = new FileOutputStream(contextFile);
  148. ObjectOutputStream oos = new ObjectOutputStream(fos);
  149. oos.writeObject(context);
  150. oos.close();
  151. } catch (Exception ex) {
  152. ex.printStackTrace( );
  153. }
  154. }
  155. public static String getRootObjectKey()
  156. {
  157. return objKeyPrefix + CounterDB.rootCounter;
  158. }
  159. public String getNewObjectKey()
  160. {
  161. return objKeyPrefix + counterDb.getNextCounter();
  162. }
  163. }
  164. class CounterDB implements Serializable
  165. {
  166. CounterDB (File logDir)
  167. {
  168. counterFileName = "counter";
  169. counterFile = new File(logDir, counterFileName);
  170. if (!counterFile.exists()) {
  171. counter = new Integer(rootCounter);
  172. writeCounter();
  173. } else {
  174. readCounter();
  175. }
  176. }
  177. private void readCounter()
  178. {
  179. try {
  180. FileInputStream fis = new FileInputStream(counterFile);
  181. ObjectInputStream ois = new ObjectInputStream(fis);
  182. counter = (Integer) ois.readObject();
  183. ois.close();
  184. } catch (Exception ex) {
  185. }
  186. }
  187. private void writeCounter()
  188. {
  189. try {
  190. counterFile.delete();
  191. FileOutputStream fos = new FileOutputStream(counterFile);
  192. ObjectOutputStream oos = new ObjectOutputStream(fos);
  193. oos.writeObject(counter);
  194. oos.flush();
  195. oos.close();
  196. } catch (Exception ex) {
  197. }
  198. }
  199. public synchronized int getNextCounter()
  200. {
  201. int counterVal = counter.intValue();
  202. counter = new Integer(++counterVal);
  203. writeCounter();
  204. return counterVal;
  205. }
  206. private Integer counter;
  207. private static String counterFileName = "counter";
  208. private transient File counterFile;
  209. public final static int rootCounter = 0;
  210. }