1 /* PersistentContext.java -- The persistent naming context.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.tools.giop.nameservice;
41 import gnu.CORBA.NamingService.NameTransformer;
42 import gnu.CORBA.NamingService.TransientContext;
46 import org.omg.CORBA.ORB;
47 import org.omg.CosNaming.NameComponent;
48 import org.omg.CosNaming.NamingContext;
49 import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
50 import org.omg.CosNaming.NamingContextPackage.CannotProceed;
51 import org.omg.CosNaming.NamingContextPackage.InvalidName;
52 import org.omg.CosNaming.NamingContextPackage.NotFound;
55 * This class implements the persistent naming service, defined by
56 * {@link NamingContext}. The 'persistent' means that the service remembers the
57 * mappings, stored between restarts.
59 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
61 public class PersistentContext
62 extends TransientContext
65 * Use serial version UID for interoperability.
67 private static final long serialVersionUID = 2;
70 * The folder, where the persistent context information is stored.
75 * The uinque context identifier.
77 static long num = System.currentTimeMillis();
80 * The naming service orb.
85 * Create the persistent naming context that will store the files in the given
86 * folder of the local file system. This method also connects object to the
89 * @param an_orb the naming service ORB, used to obtain and produce the object
90 * stringified references.
91 * @param folder the folder, where the persistent information is stored.
92 * @param reset if true, the previous naming data are discarded. If false
93 * (normally expected), they are loaded from the persistent memory to
94 * provide the persistence.
96 public PersistentContext(ORB an_orb, File folder, boolean reset)
99 new PersistentContextMap(an_orb, new File(folder, "contexts.txt"), reset),
100 new PersistentMap(an_orb, new File(folder, "objects.txt"), reset));
101 contextFolder = folder;
108 * Get the unique context number;
110 * @return the context number
112 static synchronized String getNum()
114 return Long.toHexString(num++);
118 * Create new persistent context.
120 public NamingContext new_context()
122 File ctxFolder = new File(contextFolder, "ctx_"+getNum());
123 return new PersistentContext(orb, ctxFolder, true);
127 * Create a new context and give it a given name (bound it) in the current
128 * context. The method benefits from passing the better readable context name.
130 * @param a_name the name being given to the new context.
131 * @return the newly created context.
132 * @throws AlreadyBound if the name is already in use.
133 * @throws InvalidName if the name has zero length or otherwise invalid.
135 public NamingContext bind_new_context(NameComponent[] a_name)
136 throws NotFound, AlreadyBound, CannotProceed, InvalidName
138 if (named_contexts.containsKey(a_name[0])
139 || named_objects.containsKey(a_name[0]))
140 throw new AlreadyBound();
142 NameTransformer transformer = new NameTransformer();
144 File ctxFolder = new File(contextFolder,
145 transformer.toString(a_name).replace('/', '.')
148 NamingContext child = new PersistentContext(orb, ctxFolder, true);
149 bind_context(a_name, child);