KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > server > RequestManager


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

23 package fr.dyade.aaa.jndi2.server;
24
25 import java.util.*;
26 import java.io.*;
27 import javax.naming.*;
28
29 import fr.dyade.aaa.agent.*;
30 import fr.dyade.aaa.jndi2.impl.*;
31 import fr.dyade.aaa.jndi2.msg.*;
32
33 import org.objectweb.util.monolog.api.BasicLevel;
34 import org.objectweb.util.monolog.api.Logger;
35
36 public class RequestManager
37     implements LifeCycleListener, java.io.Serializable JavaDoc {
38
39   private Container container;
40
41   private transient ServerImpl impl;
42
43   public void setContainer(Container container) {
44     this.container = container;
45   }
46
47   public final AgentId getId() {
48     return container.getId();
49   }
50
51   public void sendTo(AgentId to, Notification not) {
52     container.sendNotification(to, not);
53   }
54
55   public void agentInitialize(boolean firstTime) throws Exception JavaDoc {
56     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
57       Trace.logger.log(BasicLevel.DEBUG,
58                        "\n\nRequestManager.agentInitialize(" +
59                        firstTime + ')');
60     // Create the object that handles the
61
// naming data.
62
impl = new ServerImpl(
63       AgentServer.getTransaction(),
64       getId(),
65       getRootOwnerId());
66     impl.initialize();
67   }
68
69   public void agentFinalize(boolean lastTime) {}
70
71   /**
72    * Returns the root naming context owner
73    * identifier.
74    * May be overridden by a subclass.
75    */

76   protected AgentId getRootOwnerId() {
77     return getId();
78   }
79
80   protected final ServerImpl getServerImpl() {
81     return impl;
82   }
83
84   public JndiReply invoke(RequestContext reqCtx) {
85     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
86       Trace.logger.log(BasicLevel.DEBUG,
87                        "RequestManager.invoke(" + reqCtx + ')');
88     JndiRequest request = reqCtx.getRequest();
89     try {
90       if (request instanceof JndiReadRequest) {
91         // 1- Dispatch the read requests
92
return invokeReadRequest(reqCtx);
93       } if (request instanceof JndiAdminRequest) {
94         return invokeAdminRequest(reqCtx);
95       } else {
96         // 2- Dispatch the write requests
97
return invokeWriteRequest(reqCtx);
98       }
99     } catch (MissingContextException mce) {
100       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
101         Trace.logger.log(BasicLevel.DEBUG, "", mce);
102       return onMissingContext(mce, reqCtx);
103     } catch (MissingRecordException nnfe) {
104       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
105         Trace.logger.log(BasicLevel.DEBUG, "", nnfe);
106       return onMissingRecord(nnfe, reqCtx);
107     } catch (NamingException nexc) {
108       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
109         Trace.logger.log(BasicLevel.DEBUG, "", nexc);
110       return new JndiError(nexc);
111     }
112   }
113
114   protected JndiReply invokeReadRequest(RequestContext reqCtx)
115     throws NamingException {
116     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
117       Trace.logger.log(BasicLevel.DEBUG,
118                        "RequestManager.invokeReadRequest(" + reqCtx + ')');
119     JndiRequest request = reqCtx.getRequest();
120     if (request instanceof LookupRequest) {
121       Object JavaDoc obj = lookup((LookupRequest)request);
122       if (obj != null) {
123         ObjectRecord or = (ObjectRecord)obj;
124         return new LookupReply(or.getObject());
125       } else {
126         // This is a context record
127
return new JndiReply();
128       }
129     } else if (request instanceof ListBindingsRequest) {
130       Object JavaDoc obj = listBindings((ListBindingsRequest)request);
131       return new ListBindingsReply((Binding[])obj);
132     } else if (request instanceof ListRequest) {
133       Object JavaDoc obj = list((ListRequest)request);
134       return new ListReply((NameClassPair[])obj);
135     } else {
136       return new JndiError(
137         new NamingException("Unknown operation"));
138     }
139   }
140
141   protected JndiReply invokeWriteRequest(
142     RequestContext reqCtx)
143     throws NamingException {
144     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
145       Trace.logger.log(BasicLevel.DEBUG,
146                        "RequestManager.invokeWriteRequest(" +
147                        reqCtx + ',' + ')');
148     try {
149       JndiRequest request = reqCtx.getRequest();
150       JndiReply reply;
151       if (request instanceof BindRequest) {
152         bind((BindRequest)request);
153         return new JndiReply();
154       } else if (request instanceof UnbindRequest) {
155         unbind((UnbindRequest)request);
156         return new JndiReply();
157       } else if (request instanceof CreateSubcontextRequest) {
158         createSubcontext(
159           (CreateSubcontextRequest)request);
160         return new JndiReply();
161       } else if (request instanceof DestroySubcontextRequest) {
162         destroySubcontext(
163           (DestroySubcontextRequest)request);
164         return new JndiReply();
165       } else {
166         return new JndiError(
167           new NamingException("Unknown operation"));
168       }
169     } catch (NotOwnerException noexc) {
170       if (Trace.logger.isLoggable(BasicLevel.DEBUG))
171         Trace.logger.log(BasicLevel.DEBUG, "", noexc);
172       return invokeOwner(
173         (AgentId)noexc.getOwner(),
174         reqCtx);
175     }
176   }
177
178   protected JndiReply invokeAdminRequest(
179     RequestContext reqCtx)
180     throws NamingException {
181     JndiRequest request = reqCtx.getRequest();
182     if (request instanceof ChangeOwnerRequest) {
183       changeOwner((ChangeOwnerRequest)request);
184       return new JndiReply();
185     } else {
186       return new JndiError(
187         new NamingException("Unknown admin operation"));
188     }
189   }
190
191   private void bind(BindRequest request)
192     throws NamingException {
193     if (request.isRebind()) {
194       impl.rebind(
195         request.getName(),
196         request.getObject());
197     } else {
198       impl.bind(
199         request.getName(),
200         request.getObject());
201     }
202   }
203
204   private void unbind(UnbindRequest request)
205     throws NamingException {
206     impl.unbind(request.getName());
207   }
208
209   private Record lookup(LookupRequest request)
210     throws NamingException {
211     return impl.lookup(request.getName());
212   }
213
214   private NameClassPair[] list(ListRequest request)
215     throws NamingException {
216     return impl.list(request.getName());
217   }
218
219   private Binding[] listBindings(ListBindingsRequest request)
220     throws NamingException {
221     return impl.listBindings(request.getName());
222   }
223
224   protected void createSubcontext(CreateSubcontextRequest request)
225     throws NamingException {
226     impl.createSubcontext(
227       request.getName());
228   }
229
230   private void destroySubcontext(DestroySubcontextRequest request)
231     throws NamingException {
232     impl.destroySubcontext(
233       request.getName());
234   }
235
236   protected void changeOwner(ChangeOwnerRequest request)
237     throws NamingException {
238     AgentId serverId;
239     try {
240       serverId =
241         AgentId.fromString(request.getOwnerId());
242     } catch (Exception JavaDoc exc) {
243       NamingException ne =
244         new NamingException(exc.toString());
245       ne.setRootCause(exc);
246       throw ne;
247     }
248     if (getId().equals(serverId))
249       throw new NamingException("Server already owner");
250     impl.changeOwner(serverId);
251   }
252
253   /**
254    * A centralized JNDI server returns a JNDI error
255    * explaining that this server is not the owner
256    * of the context on which the JNDI operation is called.
257    * A subclass may override this behavior in order
258    * to invoke the owner of the naming context.
259    *
260    * @param owner the identifier of the naming server that
261    * owns the naming context on which the
262    * JNDI operation is called.
263    *
264    * @param reqCtx the JNDI request context that raised
265    * the exception.
266    *
267    * @return the JNDI reply.
268    * May be <code>null</code> if the owner invocation
269    * is asynchronous.
270    */

271   protected JndiReply invokeOwner(AgentId owner,
272                                   RequestContext reqCtx) {
273     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
274       Trace.logger.log(BasicLevel.DEBUG, "RequestManager.invokeOwner(" +
275                        owner + ',' + reqCtx + ')');
276     return new JndiError(new NamingException("Not owner"));
277   }
278   
279   /**
280    * In a centralized JNDI server a missing context shows
281    * that the naming data are unconsistent. So it throws an error.
282    * A subclass may override this behavior in order
283    * to try to resolve the missing context.
284    *
285    * @param mce the missing context exception
286    *
287    * @param reqCtx the JNDI request context that raised
288    * the exception.
289    *
290    * @return the JNDI reply.
291    * May be <code>null</code> if the resolution is asynchronous.
292    */

293   protected JndiReply onMissingContext(MissingContextException mce,
294                                        RequestContext reqCtx) {
295     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
296       Trace.logger.log(BasicLevel.DEBUG, "RequestManager.onMissingContext(" +
297                        mce + ',' + reqCtx + ')');
298     throw new Error JavaDoc(mce.toString());
299   }
300
301   /**
302    * In a centralized JNDI server a missing record shows that the
303    * name asked by the JNDI request doesn't exist. So the
304    * <code>NameNotFoundException</code> is directly forwarded to
305    * the client.
306    * A subclass may override this behavior in order
307    * to try to resolve the missing record.
308    *
309    * @param mre the missing context exception
310    *
311    * @param reqCtx the JNDI request context that raised
312    * the exception.
313    *
314    * @return the JNDI reply.
315    * May be <code>null</code> if the resolution is asynchronous.
316    */

317   protected JndiReply onMissingRecord(MissingRecordException mre,
318                                       RequestContext reqCtx) {
319     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
320       Trace.logger.log(BasicLevel.DEBUG, "RequestManager.onMissingRecord(" +
321                        mre + ',' + reqCtx + ')');
322     return new JndiError(mre.getNameNotFoundException());
323   }
324
325   public void writeBag(ObjectOutputStream out)
326     throws IOException {
327     impl.writeBag(out);
328   }
329
330   public void readBag(ObjectInputStream in)
331     throws IOException, ClassNotFoundException JavaDoc {
332     impl = new ServerImpl(
333       AgentServer.getTransaction(),
334       getId(),
335       getRootOwnerId());
336     impl.readBag(in);
337   }
338 }
339
Popular Tags