OSDN Git Service

* config/i386/i386.md (*sinxf2): Rename to *sinxf2_i387.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / tools / gnu / classpath / tools / giop / nameservice / PersistentContext.java
1 /* PersistentContext.java -- The persistent naming context.
2  Copyright (C) 2006 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
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)
9  any later version.
10
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.
15
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
19  02110-1301 USA.
20
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
24  combination.
25
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. */
37
38
39 package gnu.classpath.tools.giop.nameservice;
40
41 import gnu.CORBA.NamingService.NameTransformer;
42 import gnu.CORBA.NamingService.TransientContext;
43
44 import java.io.File;
45
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;
53
54 /**
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.
58  * 
59  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
60  */
61 public class PersistentContext
62     extends TransientContext
63 {
64   /**
65    * Use serial version UID for interoperability.
66    */
67   private static final long serialVersionUID = 2;
68   
69   /**
70    * The folder, where the persistent context information is stored.
71    */
72   File contextFolder;
73   
74   /**
75    * The uinque context identifier.
76    */
77   static long num = System.currentTimeMillis();
78   
79   /**
80    * The naming service orb.
81    */
82   ORB orb;
83   
84   /**
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
87    * passed ORB.
88    * 
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.
95    */
96   public PersistentContext(ORB an_orb, File folder, boolean reset)
97   {
98     super(
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;
102     folder.mkdirs();
103     orb = an_orb;
104     orb.connect(this);
105   }
106   
107   /**
108    * Get the unique context number;
109    * 
110    * @return the context number
111    */
112   static synchronized String getNum()
113   {
114     return Long.toHexString(num++);
115   }
116   
117   /**
118    * Create new persistent context.
119    */
120   public NamingContext new_context()
121   {
122     File ctxFolder = new File(contextFolder, "ctx_"+getNum());
123     return new PersistentContext(orb, ctxFolder, true);
124   }
125   
126   /**
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.
129    * 
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.
134    */
135   public NamingContext bind_new_context(NameComponent[] a_name)
136       throws NotFound, AlreadyBound, CannotProceed, InvalidName
137   {
138     if (named_contexts.containsKey(a_name[0])
139         || named_objects.containsKey(a_name[0]))
140       throw new AlreadyBound();
141
142     NameTransformer transformer = new NameTransformer();
143
144     File ctxFolder = new File(contextFolder,
145                               transformer.toString(a_name).replace('/', '.')
146                                   + ".v" + getNum());
147
148     NamingContext child = new PersistentContext(orb, ctxFolder, true);
149     bind_context(a_name, child);
150     return child;
151   }  
152 }