KickJava   Java API By Example, From Geeks To Geeks.

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


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 StorageManager {
35
36   public static final String JavaDoc ROOT = "jndiStorage";
37
38   public static final String JavaDoc CTX_COUNTER = "jndiCtxCounter";
39
40   public static final String JavaDoc CTX_INDEX = "jndiCtxIndex";
41
42   private long contextCounter;
43
44   private Transaction transaction;
45   
46   private Hashtable nameToIdIndex;
47
48   private Object JavaDoc serverId;
49
50   public StorageManager(Transaction transaction,
51                         Object JavaDoc serverId) {
52     this.transaction = transaction;
53     this.serverId = serverId;
54   }
55
56   public void initialize() throws Exception JavaDoc {
57     // Load the local context counter
58
Long JavaDoc contextCounterL = (Long JavaDoc)transaction.load(
59       CTX_COUNTER);
60     if (contextCounterL == null) {
61       contextCounter = 0;
62     } else {
63       contextCounter =
64         ((Long JavaDoc)contextCounterL).longValue();
65     }
66
67     // Load the context index
68
nameToIdIndex = (Hashtable)transaction.load(
69       CTX_INDEX);
70     if (nameToIdIndex == null) {
71       nameToIdIndex = new Hashtable();
72     }
73   }
74
75   public NamingContext newNamingContext(Object JavaDoc ownerId,
76                                         NamingContextId ncid,
77                                         CompositeName name)
78     throws NamingException {
79     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
80       Trace.logger.log(BasicLevel.DEBUG,
81                        "StorageManager.newNamingContext(" +
82                        ownerId + ',' + name + ')');
83     if (ncid == null) {
84       ncid = newNamingContextId();
85     }
86     NamingContext nc = new NamingContext(
87       ncid,
88       ownerId);
89     addNamingContext(
90       nc,
91       name);
92     return nc;
93   }
94
95   public void addNamingContext(NamingContext nc,
96                                CompositeName name)
97     throws NamingException {
98     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
99       Trace.logger.log(BasicLevel.DEBUG,
100                        "StorageManager.addNamingContext(" +
101                        nc + ',' +
102                        name + ')');
103     nameToIdIndex.put(name, nc.getId());
104     storeIndex();
105     storeNamingContext(nc);
106   }
107
108   private NamingContextId newNamingContextId() throws NamingException {
109     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
110       Trace.logger.log(BasicLevel.DEBUG,
111                        "StorageManager.newNamingContextId()");
112     NamingContextId ncid = new NamingContextId(
113       serverId, contextCounter);
114     contextCounter++;
115     try {
116       transaction.save(new Long JavaDoc(contextCounter), CTX_COUNTER);
117       return ncid;
118     } catch (IOException ioexc) {
119       NamingException nexc = new NamingException();
120       nexc.setRootCause(ioexc);
121       throw nexc;
122     }
123   }
124
125   public void storeNamingContext(NamingContext nc)
126     throws NamingException {
127     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
128       Trace.logger.log(BasicLevel.DEBUG,
129                        "StorageManager.storeNamingContext(" +
130                        nc + ')');
131     try {
132       transaction.save(nc, ROOT, nc.getId().toString());
133     } catch (IOException exc) {
134       NamingException ne = new NamingException(exc.getMessage());
135       ne.setRootCause(exc);
136       throw ne;
137     }
138   }
139
140   public NamingContext loadNamingContext(NamingContextId ncid)
141     throws NamingException {
142     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
143       Trace.logger.log(
144         BasicLevel.DEBUG,
145         "StorageManager.loadNamingContext(" +
146         ncid + ')');
147     return loadNamingContext(ncid.toString());
148   }
149   
150   public NamingContext loadNamingContext(String JavaDoc fileName)
151     throws NamingException {
152     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
153       Trace.logger.log(
154         BasicLevel.DEBUG,
155         "StorageManager.loadNamingContext(" + fileName + ')');
156     try {
157       Object JavaDoc obj = transaction.load(
158         ROOT, fileName);
159       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
160         Trace.logger.log(
161           BasicLevel.DEBUG,
162           " -> obj = " + obj);
163       return (NamingContext)obj;
164     } catch (IOException exc) {
165       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
166         Trace.logger.log(BasicLevel.DEBUG, "", exc);
167       NamingException ne = new NamingException(exc.getMessage());
168       ne.setRootCause(exc);
169       throw ne;
170     } catch (ClassNotFoundException JavaDoc exc2) {
171       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
172         Trace.logger.log(BasicLevel.DEBUG, "", exc2);
173       NamingException ne = new NamingException(exc2.getMessage());
174       ne.setRootCause(exc2);
175       throw ne;
176     }
177   }
178
179   public void delete(NamingContextId ncid,
180                      CompositeName name)
181     throws NamingException {
182     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
183       Trace.logger.log(BasicLevel.DEBUG,
184                        "StorageManager.delete(" +
185                        ncid + ',' + name + ')');
186     transaction.delete(ROOT, ncid.toString());
187     nameToIdIndex.remove(name);
188     storeIndex();
189   }
190
191   private void storeIndex() throws NamingException {
192     try {
193       transaction.save(nameToIdIndex, CTX_INDEX);
194     } catch (IOException exc) {
195       NamingException ne = new NamingException(
196         exc.getMessage());
197       ne.setRootCause(exc);
198       throw ne;
199     }
200   }
201     
202   public Enumeration getContextIds() {
203     return nameToIdIndex.elements();
204   }
205
206   public Enumeration getContextNames() {
207     return nameToIdIndex.keys();
208   }
209
210   public NamingContextId getIdFromName(CompositeName name) {
211     return (NamingContextId)nameToIdIndex.get(name);
212   }
213
214   public void writeBag(ObjectOutputStream out)
215     throws IOException {
216     out.writeLong(contextCounter);
217     out.writeObject(nameToIdIndex);
218   }
219
220   public void readBag(ObjectInputStream in)
221     throws IOException, ClassNotFoundException JavaDoc {
222     contextCounter = in.readLong();
223     nameToIdIndex = (Hashtable)in.readObject();
224   }
225 }
226
Popular Tags