KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > naming > lib > NamingManagerHelper


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; 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.speedo.naming.lib;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.objectweb.jorm.api.PClassMapping;
25 import org.objectweb.jorm.api.PException;
26 import org.objectweb.jorm.api.PMapper;
27 import org.objectweb.jorm.metainfo.api.Manager;
28 import org.objectweb.jorm.naming.api.PBinder;
29 import org.objectweb.jorm.naming.api.PNamingContext;
30 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext;
31 import org.objectweb.perseus.cache.api.CacheManager;
32 import org.objectweb.speedo.metadata.SpeedoClass;
33 import org.objectweb.speedo.naming.api.NamingManager;
34 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public abstract class NamingManagerHelper implements NamingManager {
42
43     public final static String JavaDoc HINTS_SEP = ",";
44
45     public final static int ID_CAT_IDX = 0;
46     public final static int BINDER_IDX = 1;
47     public final static int PNC_IDX = 2;
48     public final static int PCLASS_IDX = 3;
49
50     public final static String JavaDoc POLYMORPHIC_PNC = "org.objectweb.jorm.naming.lib.BasicPolymorphicPNamingContext";
51     
52     protected Logger logger;
53     protected CacheManager cache;
54     protected ProxyManagerFactory pmf;
55     
56     public boolean supportPNamingcontext() {
57         return true;
58     }
59
60     public void setPMapper(PMapper mapper) {
61     }
62
63     public void setCache(CacheManager cache) {
64         this.cache = cache;
65     }
66     
67     public void setLogger(Logger logger) {
68         this.logger = logger;
69     }
70
71     public void setPmf(ProxyManagerFactory pmf) {
72         this.pmf = pmf;
73     }
74     
75     public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) throws PException {
76         return null;
77     }
78
79     protected abstract String JavaDoc getName();
80
81     public boolean canProvidePBinder(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
82         return hints instanceof String JavaDoc
83             && ((String JavaDoc) hints).startsWith(getName() + HINTS_SEP);
84     }
85
86     public boolean canProvidePNamingContext(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
87         return hints instanceof String JavaDoc
88             && ((String JavaDoc) hints).startsWith(getName() + HINTS_SEP);
89     }
90
91     public PBinder getPBinder(String JavaDoc className,
92                               String JavaDoc hints,
93                               ClassLoader JavaDoc classLoader,
94                               byte mappingStructureRule,
95                               Map JavaDoc cn2binder,
96                               Map JavaDoc cn2pnc) throws PException {
97
98         try {
99             return (PBinder) classLoader.loadClass(
100                 getBinderClassNameFromHints(hints, getName())).newInstance();
101         } catch (Exception JavaDoc e) {
102             throw new PException(e,
103                     "Impossible to instanciate the binder of the class "
104                     + hints);
105         }
106     }
107
108     public PNamingContext getPNamingContext(String JavaDoc className,
109                                             String JavaDoc hints,
110                                             ClassLoader JavaDoc classLoader,
111                                             byte mappingStructureRule,
112                                             Map JavaDoc cn2binder,
113                                             Map JavaDoc cn2pnc,
114                                             Manager miManager,
115                                             PClassMapping pcm) throws PException {
116         String JavaDoc[] tokens = getTokens(hints);
117         PNamingContext pnc = null;
118         if (tokens[BINDER_IDX].equals(tokens[PNC_IDX])) {
119             //The binder must be used as PNamingContext
120
pnc = (PNamingContext) cn2binder.get(className);
121             if (pnc == null) {
122                 //instanciate the binder/PNC
123
pnc = (PNamingContext) getPBinder(className, hints, classLoader,
124                     mappingStructureRule, cn2binder, cn2pnc);
125                 //register the binder/PNC as binder
126
cn2binder.put(className, pnc);
127             }
128             return pnc;
129         } else {
130             boolean register = false;
131             if (!tokens[PCLASS_IDX].equals(className) && !tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
132                 //The PNC of THE ancestor must be used
133
pnc = (PNamingContext) cn2pnc.get(tokens[PCLASS_IDX]);
134                 if (pnc == null) {
135                     register = true;
136                 }
137             }
138             if (pnc == null) {
139                 //instanciate the PNC
140
try {
141                     pnc = (PNamingContext) classLoader
142                         .loadClass(tokens[PNC_IDX]).newInstance();
143                     //if the PNC is polymorphic
144
if (tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
145                         PolymorphicPNamingContext polymorphicPNC = (PolymorphicPNamingContext) pnc;
146                         //link the cache
147
//cache null
148
polymorphicPNC.setCache(this.cache);
149                         //instanciate a new binder
150
PBinder delegatedBinder = getPBinder(className, hints, classLoader,
151                                 mappingStructureRule, cn2binder, cn2pnc);
152                         //link a new instanciated and dedicated binder
153
polymorphicPNC.setDelegatedBinder(delegatedBinder);
154                         delegatedBinder.setPClassMapping(pcm);
155                         delegatedBinder.setPType(pcm.getPType());
156                         //link the PClassMapping
157
polymorphicPNC.setPcm(delegatedBinder.getBinderClassMapping());
158                     }
159                 } catch (Exception JavaDoc e) {
160                     throw new PException(e,
161                         "Impossible to instanciate the PNC of the class "
162                         + hints);
163                 }
164                 if (register) {
165                     cn2pnc.put(tokens[PCLASS_IDX], pnc);
166                 }
167             }
168         }
169         return pnc;
170     }
171
172     public static String JavaDoc[] getTokens(Object JavaDoc o) {
173         if (!(o instanceof String JavaDoc)) {
174             return null;
175         }
176         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc((String JavaDoc) o, HINTS_SEP, true);
177         ArrayList JavaDoc tokens = new ArrayList JavaDoc();
178         boolean previousIsSep = false;
179         while (st.hasMoreTokens()) {
180             String JavaDoc token = st.nextToken();
181             if (HINTS_SEP.equals(token)) {
182                 if (previousIsSep) {
183                     tokens.add("");
184                 }
185                 previousIsSep = true;
186             } else {
187                 tokens.add(token);
188                 previousIsSep = false;
189             }
190         }
191         tokens.add("");
192         return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
193     }
194
195     public static String JavaDoc getBinderClassNameFromHints(Object JavaDoc hints, String JavaDoc idCatName) {
196         String JavaDoc[] tokens = getTokens(hints);
197         if (tokens == null || tokens.length < 4
198             || !tokens[0].equals(idCatName)) {
199             return null;
200         } else {
201             return tokens[BINDER_IDX];
202         }
203     }
204
205     public static String JavaDoc getPNCClassNameFromHints(Object JavaDoc hints, String JavaDoc idCatName) {
206         String JavaDoc[] tokens = getTokens(hints);
207         if (tokens == null || tokens.length < 4
208             || !tokens[0].equals(idCatName)) {
209             return null;
210         } else {
211             return tokens[PNC_IDX];
212         }
213     }
214 }
215
Popular Tags