1 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 { 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 { 56 if (Trace.logger.isLoggable(BasicLevel.DEBUG)) 57 Trace.logger.log(BasicLevel.DEBUG, 58 "\n\nRequestManager.agentInitialize(" + 59 firstTime + ')'); 60 impl = new ServerImpl( 63 AgentServer.getTransaction(), 64 getId(), 65 getRootOwnerId()); 66 impl.initialize(); 67 } 68 69 public void agentFinalize(boolean lastTime) {} 70 71 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 return invokeReadRequest(reqCtx); 93 } if (request instanceof JndiAdminRequest) { 94 return invokeAdminRequest(reqCtx); 95 } else { 96 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 obj = lookup((LookupRequest)request); 122 if (obj != null) { 123 ObjectRecord or = (ObjectRecord)obj; 124 return new LookupReply(or.getObject()); 125 } else { 126 return new JndiReply(); 128 } 129 } else if (request instanceof ListBindingsRequest) { 130 Object obj = listBindings((ListBindingsRequest)request); 131 return new ListBindingsReply((Binding[])obj); 132 } else if (request instanceof ListRequest) { 133 Object 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 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 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 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 (mce.toString()); 299 } 300 301 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 { 332 impl = new ServerImpl( 333 AgentServer.getTransaction(), 334 getId(), 335 getRootOwnerId()); 336 impl.readBag(in); 337 } 338 } 339 | Popular Tags |