KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > naming > BindingAC


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.naming;
19
20 import org.apache.log4j.Logger;
21 import org.objectweb.jac.core.*;
22 import org.objectweb.jac.core.Naming;
23 import org.objectweb.jac.core.dist.*;
24 import org.objectweb.jac.util.*;
25 import org.objectweb.jac.util.ExtArrays;
26
27 /**
28  * This aspect component implements the default binding policy for
29  * the JAC system.
30  *
31  * <p>The binding aspect uses the naming aspect. Do not try to use it
32  * alone.
33  *
34  * @see NamingAC */

35
36 public class BindingAC extends AspectComponent {
37
38     static Logger logger = Logger.getLogger("naming");
39     static Logger loggerSerial = Logger.getLogger("serialization");
40
41     /**
42      * Bind a deserialized JAC object.
43      *
44      * <p>This method takes the name filled by the naming aspect within
45      * the serialized infos and wraps the final object by a
46      * <code>BindingWrapper</code> so that it can be resolved later
47      * on. If the forwarding is disabled for this object, the new
48      * object is registered within the repository and is not wrapped by
49      * a binding wrapper.
50      *
51      * @param orgObject the JAC object that is being deserialized.
52      *
53      * @see org.objectweb.jac.util.Repository#register(String,Object)
54      * @see org.objectweb.jac.aspects.naming.NamingAC#whenSerialized(Wrappee,SerializedJacObject)
55      * @see #whenDeserialized(SerializedJacObject,Wrappee)
56      * @see org.objectweb.jac.core.SerializedJacObject#getACInfos(String)
57      * @see BindingWrapper
58      */

59    
60     public Wrappee whenDeserialized(SerializedJacObject orgObject,
61                                     Wrappee finalObject) {
62       
63         String JavaDoc name = (String JavaDoc)orgObject.getACInfos("naming");
64         loggerSerial.debug("binding "+orgObject+"(name = "+name+")");
65         if (name != null) {
66             Object JavaDoc newFinal = NameRepository.get().getObject(name);
67             if (newFinal != null) {
68                 loggerSerial.debug(name+" exists on the site ("+newFinal+")");
69                 if(!name.equals(NameRepository.get().getName(finalObject))) {
70                     loggerSerial.debug("deleting "+finalObject);
71                     ObjectRepository.delete(finalObject);
72                 }
73                 loggerSerial.debug("replacing with "+newFinal);
74                 finalObject = (Wrappee)newFinal;
75             } else {
76                 if (orgObject.isForwarder()) {
77                     loggerSerial.debug("is forwarder");
78                     Wrapping.wrapAll(
79                         ((Wrappee)finalObject),
80                         null,
81                         new BindingWrapper(this, name));
82                 } else {
83                     loggerSerial.debug("is new object");
84                     // Upcall the ACManager to notify it from a kind of
85
// remote instantiation
86
((ACManager)ACManager.get()).whenRemoteInstantiation(
87                         (Wrappee)finalObject,
88                         name);
89                 }
90             }
91         }
92         return finalObject;
93     }
94
95     /**
96      * Add the name to the SerializedJacObject when an object that is
97      * wrapped by a <code>BindingWrapper</code> serialized.
98      *
99      * @param finalObject the corresponding serialized structure.
100      *
101      * @see #whenDeserialized(SerializedJacObject,Wrappee)
102      * @see org.objectweb.jac.core.SerializedJacObject#setACInfos(String,Object)
103      * @see BindingWrapper
104      */

105
106     public Wrappee whenSerialized(Wrappee orgObject,
107                                   SerializedJacObject finalObject) {
108         if (orgObject.getClass().getName().startsWith("org.objectweb.jac.lib.java.util")) {
109             return orgObject;
110         }
111        
112         NameRepository nr = (NameRepository)NameRepository.get();
113         if (Wrapping.isExtendedBy(orgObject, null, BindingWrapper.class)) {
114             //System.out.println( "Serializing binder for " + orgObject );
115
String JavaDoc name =
116                 (String JavaDoc)Wrapping.invokeRoleMethod(orgObject,
117                                                   "getLogicalName",
118                                                   ExtArrays.emptyObjectArray);
119             if (name != null) {
120                 //System.out.println( "BINDING: serializing object " + name );
121
finalObject.setACInfos("naming", name);
122             }
123         }
124         return orgObject;
125     }
126
127     public void whenObjectMiss(String JavaDoc name) {
128         logger.debug("object miss in name repository: "+name);
129         if (name.equals(Naming.PARSER_NAME)) {
130             Parser acParser = new ParserImpl();
131             String JavaDoc crName = DistdClassLoader.classRepositoryName;
132             // crName == null means that we are on the master site
133
if (crName != null) {
134                 logger.debug("Create stub for "+Naming.PARSER_NAME+" on "+crName);
135                 RemoteRef rr = RemoteContainer.resolve(crName).bindTo(Naming.PARSER_NAME);
136                 Wrapping.wrapAll(((Wrappee)acParser),null,
137                                  new StubWrapper(this,rr));
138             } else {
139                 logger.debug("No stub for "+Naming.PARSER_NAME);
140             }
141             attrdef(BaseProgramListener.FOUND_OBJECT, acParser);
142         } else {
143             attrdef(BaseProgramListener.FOUND_OBJECT, null);
144         }
145     }
146 }
147
Popular Tags