KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jorm.api.PClassMapping;
21 import org.objectweb.jorm.api.PException;
22 import org.objectweb.jorm.api.PMapper;
23 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinderInfo;
24 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl;
25 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdPName;
26 import org.objectweb.jorm.facility.naming.polymorphid.PolymorphRefNC;
27 import org.objectweb.jorm.metainfo.api.CompositeName;
28 import org.objectweb.jorm.metainfo.api.CommonClassMapping;
29 import org.objectweb.jorm.metainfo.api.Manager;
30 import org.objectweb.jorm.metainfo.api.MetaObject;
31 import org.objectweb.jorm.metainfo.api.NameDef;
32 import org.objectweb.jorm.metainfo.api.NameRef;
33 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
34 import org.objectweb.jorm.metainfo.api.Reference;
35 import org.objectweb.jorm.metainfo.api.GenClassRef;
36 import org.objectweb.jorm.naming.api.PBinder;
37 import org.objectweb.jorm.naming.api.PName;
38 import org.objectweb.jorm.naming.api.PNameCoder;
39 import org.objectweb.jorm.naming.api.PNamingContext;
40 import org.objectweb.jorm.type.api.PType;
41 import org.objectweb.jorm.type.api.PTypeSpace;
42 import org.objectweb.perseus.cache.api.CacheManager;
43 import org.objectweb.speedo.mapper.api.JormFactory;
44 import org.objectweb.speedo.api.SpeedoException;
45 import org.objectweb.speedo.api.SpeedoProperties;
46 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder;
47 import org.objectweb.speedo.metadata.SpeedoClass;
48 import org.objectweb.speedo.metadata.SpeedoExtension;
49 import org.objectweb.speedo.metadata.SpeedoIdentity;
50 import org.objectweb.speedo.naming.api.MIBuilderHelper;
51 import org.objectweb.speedo.naming.api.NamingManager;
52 import org.objectweb.speedo.pm.api.ProxyManagerFactory;
53 import org.objectweb.util.monolog.api.Logger;
54
55 import java.util.Collection JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.util.Properties JavaDoc;
58
59 /**
60  *
61  * @author S.Chassande-Barrioz
62  */

63 public class PolymorphIdNamingManager implements NamingManager {
64
65     /**
66      * is the name of the composite name used for the identifiers in case of
67      * container identifier management.
68      */

69     private final static String JavaDoc POLYMORH_ID_NAME
70             = "org.objectweb.jorm.facility.naming.polymorphid.PolymorphId";
71
72     private final static String JavaDoc BINDER_CLASS_NAME
73             = POLYMORH_ID_NAME + "BinderInfo";
74
75     /**
76      * is a name of field of the composite name used for the identifiers in case of
77      * container identifier management.
78      */

79     private final static String JavaDoc POLYMORH_ID_OID = "objectId";
80     /**
81      * is a name of field of the composite name used for the identifiers in case of
82      * container identifier management.
83      */

84     private final static String JavaDoc POLYMORH_ID_CID = "classId";
85
86     /**
87      * The manager of the naming used by the container (long, long). It
88      * provides PBinder, PNamingContext since a class name.
89      */

90     protected PolymorphIdMgrImpl cIdManager = null;
91
92     private PMapper mapper;
93     private ProxyManagerFactory pmf;
94     private Logger logger;
95
96     public PolymorphIdMgrImpl getcIdManager() throws PException {
97         if (cIdManager == null) {
98             cIdManager = new PolymorphIdMgrImpl();
99             cIdManager.setLogger(logger);
100             cIdManager.init(mapper, PClassMapping.CREATE_STRUCTURE_IF_NEEDED);
101         }
102         return cIdManager;
103     }
104
105     // IMPLEMENTATION OF THE METHOD FROM THE NamingManager INTERFACE //
106
//---------------------------------------------------------------//
107
public boolean supportPNamingcontext() {
108         return true;
109     }
110
111     public Object JavaDoc encode(PName pn) throws PException {
112         if (pn instanceof PolymorphIdPName) {
113             return pn.getPNameManager().getPType().getJormName()
114                     + SEP + pn.encodeString();
115         }
116         return null;
117     }
118
119     public PName decode(PNameCoder pnc, Object JavaDoc oid, Class JavaDoc clazz, JormFactory jf) throws PException {
120         if (oid instanceof String JavaDoc) {
121             String JavaDoc stroid = (String JavaDoc) oid;
122             int idx = stroid.indexOf(SEP);
123             if (pnc != null) {
124                 if (pnc instanceof PolymorphIdBinderInfo
125                         || pnc instanceof PolymorphRefNC) {
126                     if (idx != -1) {
127                         //The oid contains the class name
128
return pnc.decodeString(stroid.substring(idx + SEP.length()));
129                     } else {
130                         //The oid must decoded directly
131
return pnc.decodeString(stroid);
132                     }
133
134                 } else {
135                     //The pnc is not a BasidBinder, then the oid cannot be managed
136
return null;
137                 }
138             } else {
139                 //No pnc specified
140
if (idx != -1) {
141                     //The oid contains the class name
142
String JavaDoc fqcn = stroid.substring(0, idx);
143                     ClassLoader JavaDoc cl = getClass().getClassLoader();
144                     if (cl == null) {
145                         cl = ClassLoader.getSystemClassLoader();
146                     }
147                     try {
148                         pnc = jf.getPBinder(fqcn, cl);
149                     } catch (Exception JavaDoc e) {
150                     }
151                     return (pnc instanceof PolymorphIdBinderInfo
152                             || pnc instanceof PolymorphRefNC
153                             ? pnc.decodeString(stroid.substring(idx + SEP.length()))
154                             : null);
155                 } else {
156                     //The oid cannot be managed
157
return null;
158                 }
159             }
160         }
161         return null;
162     }
163
164     public void setPMapper(PMapper mapper) {
165         this.mapper = mapper;
166     }
167
168     public void setPmf(ProxyManagerFactory pmf) {
169         
170     }
171     
172     public void setLogger(Logger logger) {
173         this.logger = logger;
174     }
175
176     public boolean canManage(SpeedoClass sc) {
177         if (sc.identityType == SpeedoIdentity.CONTAINER_ID) {
178             SpeedoExtension se = sc.getExtension(
179                     SpeedoProperties.VENDOR_NAME, SpeedoProperties.ID);
180             return se != null && se.value == SpeedoProperties.ID_POLYMORPH_2L;
181         } else {
182             return false;
183         }
184     }
185
186     public boolean canProvidePBinder(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
187         return BINDER_CLASS_NAME.equals(hints);
188     }
189
190     public boolean canProvidePNamingContext(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
191         return canProvidePBinder(hints, classLoader);
192     }
193
194     public PBinder getPBinder(String JavaDoc className,
195                               String JavaDoc hints,
196                               ClassLoader JavaDoc classLoader,
197                               byte mappingStructureRule,
198                               Map JavaDoc cn2binder,
199                               Map JavaDoc cn2pnc) throws PException {
200         return getcIdManager().getPBinder(className);
201     }
202
203     public PNamingContext getPNamingContext(String JavaDoc className,
204                                             String JavaDoc hints,
205                                             ClassLoader JavaDoc classLoader,
206                                             byte mappingStructureRule,
207                                             Map JavaDoc cn2binder,
208                                             Map JavaDoc cn2pnc,
209                                             Manager miManager,
210                                             PClassMapping pcm) throws PException {
211         return getcIdManager().getRefNC(className);
212     }
213
214     public void fillNameDef(MIBuilderHelper mibh,
215                             Manager manager,
216                             NameDef nd,
217                             SpeedoClass tsc,
218                             SpeedoClass ssc,
219                             MetaObject mo,
220                             Reference ref,
221                             CommonClassMapping hcm,
222                             JormMIMappingBuilder mb,
223                             boolean isIdentifier,
224                             boolean isInGenClass,
225                             boolean createField,
226                             Collection JavaDoc createdMOs) throws SpeedoException, PException {
227         String JavaDoc prefix = mibh.getNameDefFieldPrefix(ref, isIdentifier, isInGenClass);
228         if (!isIdentifier && !isInGenClass && ref instanceof GenClassRef) {
229             //The polymorph id does not support the mapping
230
// where the GC id is the source class id
231
prefix = ref.getName() + "_";
232         }
233         PrimitiveElement oid, cid;
234         String JavaDoc oidname = prefix + "o_id";
235         String JavaDoc cidname = prefix + "c_id";
236         if (true || createField) {
237             oid = mibh.createNameDefField(mo, oidname, PTypeSpace.LONG);
238             cid = mibh.createNameDefField(mo, cidname, PTypeSpace.LONG);
239         } else {
240             cid = mibh.getPrimitiveField(mo, cidname);
241             if (cid == null) {
242                 throw new SpeedoException(
243                         mibh.getErrorMessage(tsc, mo, ref)
244                         + " Impossible to get the field '" + cidname + "'");
245             }
246             oid = mibh.getPrimitiveField(mo, oidname);
247             if (cid == null) {
248                 throw new SpeedoException(
249                         mibh.getErrorMessage(tsc, mo, ref)
250                         + " Impossible to get the field '" + oidname + "'");
251             }
252         }
253         CompositeName speedoidcn = getPolymorphIdCN(manager);
254         NameRef nr = nd.createNameRef(speedoidcn);
255         nr.addProjection(POLYMORH_ID_OID, oid.getName());
256         nr.addProjection(POLYMORH_ID_CID, cid.getName());
257         if (( mb != null) && (true || createField)) {
258             mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass);
259         }
260     }
261
262     private CompositeName getPolymorphIdCN(Manager manager) {
263         CompositeName cn = manager.getCompositeName(POLYMORH_ID_NAME);
264         if (cn == null) {
265             cn = manager.createCompositeName(POLYMORH_ID_NAME);
266             cn.createCompositeNameField(POLYMORH_ID_CID, PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE);
267             cn.createCompositeNameField(POLYMORH_ID_OID, PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE);
268         }
269         return cn;
270     }
271
272     public void getJormNamingConfig(NameDef nd,
273                                     SpeedoClass targetClass,
274                                     MetaObject sourceMO,
275                                     String JavaDoc key,
276                                     Properties result) {
277         result.setProperty(key, BINDER_CLASS_NAME);
278     }
279
280     public String JavaDoc getPNameHints(SpeedoClass sc, NameDef nd) {
281         return "null";
282     }
283
284     public String JavaDoc getGCPNameHints(SpeedoClass sc, NameDef nd) {
285         return "null";
286     }
287
288     public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) {
289         return null;
290     }
291
292     public void setCache(CacheManager cache) {
293     }
294 }
295
Popular Tags