KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > impl > ContextManager


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Sofiane Chibani
21  * Contributor(s): David Feliot, Nicolas Tachker
22  */

23 package fr.dyade.aaa.jndi2.impl;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.naming.*;
28
29 import fr.dyade.aaa.util.*;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 public class ContextManager
35     implements java.io.Serializable JavaDoc {
36
37   private ContextTable contextIdTable;
38
39   private ContextTable contextNameTable;
40
41   private NamingContextId rootContextId;
42
43   private StorageManager storageManager;
44
45   public ContextManager(Transaction transaction,
46                         Object JavaDoc serverId,
47                         Object JavaDoc rootOwnerId) {
48     if (transaction instanceof NullTransaction) {
49       contextNameTable = new SimpleContextTable();
50       contextIdTable = new SimpleContextTable();
51     } else {
52       contextNameTable = new ContextCache();
53       contextIdTable = new ContextCache();
54     }
55     rootContextId = new NamingContextId(
56       rootOwnerId, 0);
57     storageManager = new StorageManager(
58       transaction, serverId);
59   }
60
61   public void initialize() throws Exception JavaDoc {
62     storageManager.initialize();
63   }
64
65   private void put(NamingContext nc) {
66     contextIdTable.put(nc.getId(), nc);
67   }
68
69   private void put(CompositeName name, NamingContext nc) {
70     contextNameTable.put(name, nc);
71   }
72
73   public NamingContext getNamingContext(NamingContextId ncid)
74     throws NamingException {
75     return getNamingContext(ncid, true);
76   }
77
78   public NamingContext getNamingContext(
79     NamingContextId ncid,
80     boolean cache)
81     throws NamingException {
82     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
83       Trace.logger.log(BasicLevel.DEBUG,
84                        "ContextManager.getNamingContext(" +
85                        ncid + ',' + cache + ')');
86     NamingContext nc = contextIdTable.get(ncid);
87     if (nc != null) return nc ;
88
89     nc = storageManager.loadNamingContext(ncid);
90     if (cache && nc != null) put(nc);
91     return nc;
92   }
93
94   private NamingContext getNamingContextFromName(CompositeName name)
95     throws NamingException {
96     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
97       Trace.logger.log(
98         BasicLevel.DEBUG,
99         "ContextManager.getNamingContextFromName(" +
100         name + ')');
101
102     // 1- Try to get the context directly from the cache
103
NamingContext nc = contextNameTable.get(name);
104     if (nc != null) return nc;
105
106     // 2- Try to get the context id from the index of
107
// the storage manager.
108
NamingContextId ncid =
109       storageManager.getIdFromName(name);
110     if (ncid != null) {
111       // 3- Get the naming context
112
nc = getNamingContext(ncid);
113       if (nc == null) throw new Error JavaDoc(
114         "Missing context: name=" + name +
115         ", id=" + ncid);
116       put(name, nc);
117       return nc;
118     } else {
119       return null;
120     }
121   }
122   
123   public NamingContext getNamingContext(CompositeName name)
124     throws NamingException {
125     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
126       Trace.logger.log(
127         BasicLevel.DEBUG,
128         "ContextManager.getNamingContext(" +
129         name + ')');
130
131     NamingContext nc = getNamingContextFromName(name);
132     if (nc != null) return nc;
133
134     // Go upward the naming path in order to
135
// to find out which name is missing.
136
CompositeName parentName = name;
137     NamingContext parentNc = null;
138     int unresolvedIndex = 0;
139     for (int i = 0; i < name.size(); i++) {
140       parentName = (CompositeName)parentName.clone();
141       parentName.remove(parentName.size() - 1);
142       parentNc = getNamingContextFromName(parentName);
143       if (parentNc != null) {
144         unresolvedIndex = name.size() - 1 - i;
145         break;
146       }
147     }
148
149     if (parentNc == null) {
150       // Shows that the root context is missing
151
throw new MissingContextException(
152         rootContextId, name);
153     }
154
155     // Find out why the naming context has not been
156
// found.
157
String JavaDoc unresolvedName = name.get(unresolvedIndex);
158     Record record = parentNc.getRecord(unresolvedName);
159     if (record == null) {
160       NameNotFoundException nnfe =
161         new NameNotFoundException();
162       CompositeName resolvedName = new CompositeName();
163       for (int j = 0; j < unresolvedIndex; j++) {
164         resolvedName.add(name.get(j));
165       }
166       nnfe.setResolvedName(resolvedName);
167       throw new MissingRecordException(
168         parentNc.getId(),
169         parentNc.getOwnerId(),
170         nnfe);
171     } else if (record instanceof ContextRecord) {
172       ContextRecord ctxRecord = (ContextRecord)record;
173       // The naming context is missing.
174
// (we would have found it during the upward search)
175
throw new MissingContextException(
176         ctxRecord.getId(), name);
177     } else {
178       throw new NotContextException();
179     }
180   }
181
182   public void delete(NamingContextId ncid,
183                      CompositeName name)
184     throws NamingException {
185     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
186       Trace.logger.log(BasicLevel.DEBUG,
187                        "ContextManager.delete(" +
188                        ncid + ',' + name + ')');
189     contextIdTable.remove(ncid);
190     contextNameTable.remove(name);
191     storageManager.delete(ncid, name);
192   }
193
194   public NamingContextInfo[] copyNamingContexts(Object JavaDoc serverId)
195     throws NamingException {
196     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
197       Trace.logger.log(BasicLevel.DEBUG,
198                        "ContextManager.getNamingContexts(" +
199                        serverId + ')');
200     Vector contexts = new Vector();
201     Enumeration nameEnum = storageManager.getContextNames();
202     Enumeration idEnum = storageManager.getContextIds();
203     while (idEnum.hasMoreElements()) {
204       NamingContextId ncid =
205         (NamingContextId)idEnum.nextElement();
206       CompositeName name =
207         (CompositeName)nameEnum.nextElement();
208       NamingContext nc = getNamingContext(ncid, false);
209       if (nc.getOwnerId().equals(serverId)) {
210         NamingContext ncCopy = (NamingContext)nc.clone();
211         contexts.addElement(
212           new NamingContextInfo(ncCopy, name));
213       }
214     }
215     
216     NamingContextInfo[] res =
217       new NamingContextInfo[contexts.size()];
218     contexts.copyInto(res);
219     return res;
220   }
221
222   public NamingContext newNamingContext(Object JavaDoc ownerId,
223                                         NamingContextId ncid,
224                                         CompositeName name)
225     throws NamingException {
226     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
227       Trace.logger.log(BasicLevel.DEBUG,
228                        "ContextManager.newNamingContext(" +
229                        ownerId + ',' + ncid + ',' + name + ')');
230     NamingContext nc =
231       storageManager.newNamingContext(
232         ownerId, ncid, name);
233     put(nc);
234     put(name, nc);
235     return nc;
236   }
237
238   public void addNamingContext(NamingContextInfo ncInfo)
239     throws NamingException {
240     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
241       Trace.logger.log(BasicLevel.DEBUG,
242                        "ContextManager.addNamingContext(" +
243                        ncInfo + ')');
244     NamingContext nc = ncInfo.getNamingContext();
245     CompositeName name = ncInfo.getCompositeName();
246     storageManager.addNamingContext(
247       nc, name);
248     put(nc);
249     put(name, nc);
250   }
251   
252   public NamingContext getRootNamingContext()
253     throws NamingException {
254     return getNamingContext(rootContextId);
255   }
256
257   public void storeNamingContext(NamingContext nc)
258     throws NamingException {
259     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
260       Trace.logger.log(BasicLevel.DEBUG,
261                        "ContextManager.storeNamingContext(" +
262                        nc + ')');
263     storageManager.storeNamingContext(nc);
264   }
265
266   public NamingContextInfo[] changeOwner(
267     Object JavaDoc formerOwnerId,
268     Object JavaDoc newOwnerId)
269     throws NamingException {
270     Vector updatedContexts = new Vector();
271     Enumeration idEnum = storageManager.getContextIds();
272     Enumeration nameEnum = storageManager.getContextNames();
273     while (idEnum.hasMoreElements()) {
274       NamingContextId ncid =
275         (NamingContextId)idEnum.nextElement();
276       CompositeName name =
277         (CompositeName)nameEnum.nextElement();
278       NamingContext nc = getNamingContext(ncid, false);
279       if (nc.getOwnerId().equals(formerOwnerId)) {
280         nc.setOwnerId(newOwnerId);
281         storageManager.storeNamingContext(nc);
282         updatedContexts.addElement(
283           new NamingContextInfo(nc, name));
284       }
285     }
286     NamingContextInfo[] res =
287       new NamingContextInfo[updatedContexts.size()];
288     updatedContexts.copyInto(res);
289     return res;
290   }
291
292   public void resetNamingContext(NamingContext context)
293     throws NamingException {
294     storageManager.storeNamingContext(context);
295   }
296
297   public void writeBag(ObjectOutputStream out)
298     throws IOException {
299     out.writeObject(contextIdTable);
300     out.writeObject(contextNameTable);
301     storageManager.writeBag(out);
302   }
303
304   public void readBag(ObjectInputStream in)
305     throws IOException, ClassNotFoundException JavaDoc {
306     contextIdTable = (ContextTable)in.readObject();
307     contextNameTable = (ContextTable)in.readObject();
308     storageManager.readBag(in);
309   }
310 }
311
Popular Tags