ServantManagerImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.naming.pcosnaming;
27
28import java.io.File;
29import java.io.FileInputStream;
30import java.io.FileOutputStream;
31import java.io.ObjectInputStream;
32import java.io.ObjectOutputStream;
33import java.io.Serializable;
34import java.util.Hashtable;
35
36import org.omg.CORBA.Policy;
37import org.omg.CORBA.LocalObject;
38
39import org.omg.PortableServer.POA;
40import org.omg.PortableServer.Servant;
41import org.omg.PortableServer.ForwardRequest;
42import org.omg.PortableServer.ServantLocator;
43import org.omg.PortableServer.LifespanPolicyValue;
44import org.omg.PortableServer.RequestProcessingPolicyValue;
45import org.omg.PortableServer.IdAssignmentPolicyValue;
46import org.omg.PortableServer.ServantRetentionPolicyValue;
47import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
48
49import com.sun.corba.se.spi.orb.ORB;
50
51/**
52 * @author      Rohit Garg
53 * @since       JDK1.2
54 */
55
56public class ServantManagerImpl extends org.omg.CORBA.LocalObject implements ServantLocator
57{
58
59    // computed using serialver tool
60
61    private static final long serialVersionUID = 4028710359865748280L;
62    private ORB orb;
63
64    private NameService theNameService;
65
66    private File logDir;
67
68    private Hashtable contexts;
69
70    private CounterDB counterDb;
71
72    private int counter;
73
74    private final static String objKeyPrefix = "NC";
75
76    ServantManagerImpl(ORB orb, File logDir, NameService aNameService)
77    {
78        this.logDir = logDir;
79        this.orb    = orb;
80        // initialize the counter database
81        counterDb   = new CounterDB(logDir);
82        contexts    = new Hashtable();
83        theNameService = aNameService;
84    }
85
86
87    public Servant preinvoke(byte[] oid, POA adapter, String operation,
88                             CookieHolder cookie) throws ForwardRequest
89    {
90
91        String objKey = new String(oid);
92
93        Servant servant = (Servant) contexts.get(objKey);
94
95        if (servant == null)
96        {
97                 servant =  readInContext(objKey);
98        }
99
100        return servant;
101    }
102
103    public void postinvoke(byte[] oid, POA adapter, String operation,
104                           java.lang.Object cookie, Servant servant)
105    {
106        // nada
107    }
108
109    public NamingContextImpl readInContext(String objKey)
110    {
111        NamingContextImpl context = (NamingContextImpl) contexts.get(objKey);
112        if( context != null )
113        {
114                // Returning Context from Cache
115                return context;
116        }
117
118        File contextFile = new File(logDir, objKey);
119        if (contextFile.exists()) {
120            try {
121                FileInputStream fis = new FileInputStream(contextFile);
122                ObjectInputStream ois = new ObjectInputStream(fis);
123                context = (NamingContextImpl) ois.readObject();
124                context.setORB( orb );
125                context.setServantManagerImpl( this );
126                context.setRootNameService( theNameService );
127                ois.close();
128            } catch (Exception ex) {
129            }
130        }
131
132        if (context != null)
133        {
134                contexts.put(objKey, context);
135        }
136        return context;
137    }
138
139    public NamingContextImpl addContext(String objKey,
140                                        NamingContextImpl context)
141    {
142        File contextFile =  new File(logDir, objKey);
143
144        if (contextFile.exists())
145        {
146            context = readInContext(objKey);
147        }
148        else {
149            try {
150                FileOutputStream fos = new FileOutputStream(contextFile);
151                ObjectOutputStream oos = new ObjectOutputStream(fos);
152                oos.writeObject(context);
153                oos.close();
154            } catch (Exception ex) {
155            }
156        }
157        try
158        {
159                contexts.remove( objKey );
160        }
161        catch( Exception e)
162        {
163        }
164        contexts.put(objKey, context);
165
166        return context;
167    }
168
169    public void updateContext( String objKey,
170                                   NamingContextImpl context )
171    {
172        File contextFile =  new File(logDir, objKey);
173        if (contextFile.exists())
174        {
175                contextFile.delete( );
176                contextFile =  new File(logDir, objKey);
177        }
178
179        try {
180                FileOutputStream fos = new FileOutputStream(contextFile);
181                ObjectOutputStream oos = new ObjectOutputStream(fos);
182                oos.writeObject(context);
183                oos.close();
184            } catch (Exception ex) {
185                ex.printStackTrace( );
186            }
187    }
188
189    public static String getRootObjectKey()
190    {
191        return objKeyPrefix + CounterDB.rootCounter;
192    }
193
194    public String getNewObjectKey()
195    {
196        return objKeyPrefix + counterDb.getNextCounter();
197    }
198
199
200
201}
202
203class CounterDB implements Serializable
204{
205
206    CounterDB (File logDir)
207    {
208        counterFileName = "counter";
209        counterFile = new File(logDir, counterFileName);
210        if (!counterFile.exists()) {
211            counter = new Integer(rootCounter);
212            writeCounter();
213        } else {
214            readCounter();
215        }
216    }
217
218    private void readCounter()
219    {
220        try {
221            FileInputStream fis = new FileInputStream(counterFile);
222            ObjectInputStream ois = new ObjectInputStream(fis);
223            counter = (Integer) ois.readObject();
224            ois.close();
225        } catch (Exception ex) {
226                                }
227    }
228
229    private void writeCounter()
230    {
231        try {
232            counterFile.delete();
233            FileOutputStream fos = new FileOutputStream(counterFile);
234            ObjectOutputStream oos = new ObjectOutputStream(fos);
235            oos.writeObject(counter);
236            oos.flush();
237            oos.close();
238
239        } catch (Exception ex) {
240        }
241    }
242
243    public synchronized int getNextCounter()
244    {
245        int counterVal = counter.intValue();
246        counter = new Integer(++counterVal);
247        writeCounter();
248
249        return counterVal;
250    }
251
252
253
254    private Integer counter;
255
256    private static String counterFileName = "counter";
257
258    private transient File counterFile;
259
260    public  final static int rootCounter = 0;
261}
262