KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > pcosnaming > ServantManagerImpl


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
23 package com.sun.corba.se.impl.naming.pcosnaming;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import org.omg.CORBA.Policy JavaDoc;
34 import org.omg.CORBA.LocalObject JavaDoc;
35
36 import org.omg.PortableServer.POA JavaDoc;
37 import org.omg.PortableServer.Servant JavaDoc;
38 import org.omg.PortableServer.ForwardRequest JavaDoc;
39 import org.omg.PortableServer.ServantLocator JavaDoc;
40 import org.omg.PortableServer.LifespanPolicyValue JavaDoc;
41 import org.omg.PortableServer.RequestProcessingPolicyValue JavaDoc;
42 import org.omg.PortableServer.IdAssignmentPolicyValue JavaDoc;
43 import org.omg.PortableServer.ServantRetentionPolicyValue JavaDoc;
44 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder JavaDoc;
45
46 import com.sun.corba.se.spi.orb.ORB;
47
48 /**
49  * @version 1.6, 99/10/07
50  * @author Rohit Garg
51  * @since JDK1.2
52  */

53
54 public class ServantManagerImpl extends org.omg.CORBA.LocalObject JavaDoc implements ServantLocator JavaDoc
55 {
56
57     // computed using serialver tool
58

59     private static final long serialVersionUID = 4028710359865748280L;
60     private ORB orb;
61
62     private NameService theNameService;
63
64     private File JavaDoc logDir;
65
66     private Hashtable JavaDoc contexts;
67
68     private CounterDB counterDb;
69
70     private int counter;
71
72     private final static String JavaDoc objKeyPrefix = "NC";
73
74     ServantManagerImpl(ORB orb, File JavaDoc logDir, NameService aNameService)
75     {
76     this.logDir = logDir;
77     this.orb = orb;
78     // initialize the counter database
79
counterDb = new CounterDB(logDir);
80     contexts = new Hashtable JavaDoc();
81     theNameService = aNameService;
82     }
83
84
85     public Servant JavaDoc preinvoke(byte[] oid, POA JavaDoc adapter, String JavaDoc operation,
86                  CookieHolder JavaDoc cookie) throws ForwardRequest JavaDoc
87     {
88
89     String JavaDoc objKey = new String JavaDoc(oid);
90
91     Servant JavaDoc servant = (Servant JavaDoc) 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 JavaDoc adapter, String JavaDoc operation,
102                java.lang.Object JavaDoc cookie, Servant JavaDoc servant)
103     {
104     // nada
105
}
106
107     public NamingContextImpl readInContext(String JavaDoc objKey)
108     {
109     NamingContextImpl context = (NamingContextImpl) contexts.get(objKey);
110     if( context != null )
111     {
112         // Returning Context from Cache
113
return context;
114     }
115
116     File JavaDoc contextFile = new File JavaDoc(logDir, objKey);
117     if (contextFile.exists()) {
118         try {
119         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(contextFile);
120         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
121         context = (NamingContextImpl) ois.readObject();
122         context.setORB( orb );
123         context.setServantManagerImpl( this );
124         context.setRootNameService( theNameService );
125         ois.close();
126         } catch (Exception JavaDoc 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 JavaDoc objKey,
138                     NamingContextImpl context)
139     {
140     File JavaDoc contextFile = new File JavaDoc(logDir, objKey);
141
142     if (contextFile.exists())
143     {
144         context = readInContext(objKey);
145     }
146     else {
147         try {
148         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(contextFile);
149         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
150         oos.writeObject(context);
151         oos.close();
152         } catch (Exception JavaDoc ex) {
153         }
154     }
155     try
156     {
157         contexts.remove( objKey );
158     }
159     catch( Exception JavaDoc e)
160     {
161     }
162     contexts.put(objKey, context);
163
164     return context;
165     }
166
167     public void updateContext( String JavaDoc objKey,
168                    NamingContextImpl context )
169     {
170     File JavaDoc contextFile = new File JavaDoc(logDir, objKey);
171     if (contextFile.exists())
172     {
173         contextFile.delete( );
174         contextFile = new File JavaDoc(logDir, objKey);
175     }
176         
177         try {
178         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(contextFile);
179         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
180         oos.writeObject(context);
181         oos.close();
182         } catch (Exception JavaDoc ex) {
183         ex.printStackTrace( );
184         }
185     }
186
187     public static String JavaDoc getRootObjectKey()
188     {
189     return objKeyPrefix + CounterDB.rootCounter;
190     }
191
192     public String JavaDoc getNewObjectKey()
193     {
194     return objKeyPrefix + counterDb.getNextCounter();
195     }
196
197
198
199 }
200
201 class CounterDB implements Serializable JavaDoc
202 {
203
204     CounterDB (File JavaDoc logDir)
205     {
206         counterFileName = "counter";
207     counterFile = new File JavaDoc(logDir, counterFileName);
208     if (!counterFile.exists()) {
209         counter = new Integer JavaDoc(rootCounter);
210             writeCounter();
211     } else {
212         readCounter();
213     }
214     }
215
216     private void readCounter()
217     {
218     try {
219         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(counterFile);
220         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(fis);
221         counter = (Integer JavaDoc) ois.readObject();
222         ois.close();
223     } catch (Exception JavaDoc ex) {
224                 }
225     }
226
227     private void writeCounter()
228     {
229     try {
230         counterFile.delete();
231         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(counterFile);
232         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(fos);
233         oos.writeObject(counter);
234         oos.flush();
235         oos.close();
236
237     } catch (Exception JavaDoc ex) {
238     }
239     }
240
241     public synchronized int getNextCounter()
242     {
243     int counterVal = counter.intValue();
244     counter = new Integer JavaDoc(++counterVal);
245     writeCounter();
246
247     return counterVal;
248     }
249
250     
251
252     private Integer JavaDoc counter;
253
254     private static String JavaDoc counterFileName = "counter";
255
256     private transient File JavaDoc counterFile;
257
258     public final static int rootCounter = 0;
259 }
260
Popular Tags