KickJava   Java API By Example, From Geeks To Geeks.

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


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.asm.Type;
21 import org.objectweb.jorm.api.PException;
22 import org.objectweb.jorm.facility.naming.basidir.BasidBinder;
23 import org.objectweb.jorm.facility.naming.basidir.BasidName;
24 import org.objectweb.jorm.metainfo.api.Class;
25 import org.objectweb.jorm.metainfo.api.GenClassRef;
26 import org.objectweb.jorm.metainfo.api.CommonClassMapping;
27 import org.objectweb.jorm.metainfo.api.Manager;
28 import org.objectweb.jorm.metainfo.api.MetaObject;
29 import org.objectweb.jorm.metainfo.api.NameDef;
30 import org.objectweb.jorm.metainfo.api.Reference;
31 import org.objectweb.jorm.metainfo.api.TypedElement;
32 import org.objectweb.jorm.naming.api.PBinder;
33 import org.objectweb.jorm.naming.api.PExceptionNaming;
34 import org.objectweb.jorm.naming.api.PName;
35 import org.objectweb.jorm.naming.api.PNameCoder;
36 import org.objectweb.jorm.naming.lib.CTHelper;
37 import org.objectweb.jorm.type.api.PType;
38 import org.objectweb.speedo.api.SpeedoException;
39 import org.objectweb.speedo.api.SpeedoProperties;
40 import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder;
41 import org.objectweb.speedo.generation.lib.NamingRules;
42 import org.objectweb.speedo.mapper.api.JormFactory;
43 import org.objectweb.speedo.metadata.SpeedoClass;
44 import org.objectweb.speedo.metadata.SpeedoExtension;
45 import org.objectweb.speedo.metadata.SpeedoField;
46 import org.objectweb.speedo.metadata.SpeedoIdentity;
47 import org.objectweb.speedo.naming.api.MIBuilderHelper;
48 import org.objectweb.speedo.naming.api.NamingManager;
49 import org.objectweb.util.monolog.api.BasicLevel;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Map JavaDoc;
56 import java.util.Properties JavaDoc;
57
58 /**
59  * Is a manager of identifier naming compised of a single visible field.
60  *
61  * @author S.Chassande-Barrioz
62  */

63 public class UserIdSingleNamingManager
64     extends NamingManagerHelper
65     implements NamingManager {
66
67     private final static String JavaDoc SINGLE_USER_ID = "suid";
68
69     private int getCodingType(String JavaDoc fn, MetaObject mo) {
70         TypedElement te = null;
71         if (mo instanceof Class JavaDoc) {
72             te = ((Class JavaDoc) mo).getTypedElement(fn);
73         } else if (mo instanceof GenClassRef) {
74             te = ((GenClassRef) mo).getHiddenField(fn);
75         } else {
76             return -1;
77         }
78         return CTHelper.ptc2ct(te.getType().getTypeCode());
79     }
80
81     protected String JavaDoc getName() {
82         return SINGLE_USER_ID;
83     }
84     // IMPLEMENTATION OF THE METHOD FROM THE NamingManager INTERFACE //
85
//---------------------------------------------------------------//
86

87     public Object JavaDoc encode(PName pn) throws PException {
88         if (pn instanceof BasidName) {
89             return pn.getPNameManager().getPType().getJormName()
90                     + SEP + pn.encodeString();
91         }
92         return null;
93     }
94
95     public PName decode(PNameCoder pnc,
96                         Object JavaDoc oid,
97                         java.lang.Class JavaDoc clazz,
98                         JormFactory jf) throws PException {
99         if (oid instanceof String JavaDoc) {
100             String JavaDoc stroid = (String JavaDoc) oid;
101             int idx = stroid.indexOf(SEP);
102             if (pnc != null) {
103                 if (pnc.codingSupported(PNameCoder.CTCOMPOSITE)) {
104                     //The pnc is not a BasidBinder, then the oid cannot be managed
105
return null;
106                 }
107                 if (idx != -1) {
108                     //The oid contains the class name
109
return pnc.decodeString(stroid.substring(idx + SEP.length()));
110                 } else {
111                     //The oid must decoded directly
112
return pnc.decodeString(stroid);
113                 }
114             }
115             //No pnc specified
116
if (idx == -1) {
117                 //The oid cannot be managed
118
return null;
119             }
120             //The oid contains the class name
121
String JavaDoc fqcn = stroid.substring(0, idx);
122             ClassLoader JavaDoc cl = jf.getClass().getClassLoader();
123             if (cl == null) {
124                 cl = ClassLoader.getSystemClassLoader();
125             }
126             try {
127                 pnc = jf.getPNamingContext(fqcn, cl);
128             } catch (Exception JavaDoc e) {
129                 return null;
130             }
131             if (pnc == null
132                 || pnc.codingSupported(PNameCoder.CTCOMPOSITE)) {
133                 return null;
134             }
135             return pnc.decodeString(stroid.substring(idx + SEP.length()));
136         } else if (pnc instanceof BasidBinder) {
137             switch (((BasidBinder) pnc).getCodingType()) {
138             case PNameCoder.CTCHAR:
139                 return pnc.decodeChar(((Character JavaDoc) oid).charValue());
140             case PNameCoder.CTBYTE:
141                 return pnc.decodeByte(((Byte JavaDoc) oid).byteValue());
142             case PNameCoder.CTSHORT:
143                 return pnc.decodeShort(((Short JavaDoc) oid).shortValue());
144             case PNameCoder.CTINT:
145                 return pnc.decodeInt(((Integer JavaDoc) oid).intValue());
146             case PNameCoder.CTLONG:
147                 return pnc.decodeLong(((Long JavaDoc) oid).longValue());
148             case PNameCoder.CTOCHAR:
149                 return pnc.decodeOchar((Character JavaDoc) oid);
150             case PNameCoder.CTOBYTE:
151                 return pnc.decodeObyte((Byte JavaDoc) oid);
152             case PNameCoder.CTOSHORT:
153                 return pnc.decodeOshort((Short JavaDoc) oid);
154             case PNameCoder.CTOINT:
155                 return pnc.decodeOint((Integer JavaDoc) oid);
156             case PNameCoder.CTOLONG:
157                 return pnc.decodeOlong((Long JavaDoc) oid);
158             case PNameCoder.CTSTRING:
159                 return pnc.decodeString((String JavaDoc) oid);
160             case PNameCoder.CTDATE:
161                 return pnc.decodeDate((Date JavaDoc) oid);
162             case PNameCoder.CTCHARARRAY:
163                 return pnc.decodeCharArray((char[]) oid);
164             case PNameCoder.CTBYTEARRAY:
165                 return pnc.decode((byte[]) oid);
166             default:
167                 throw new PExceptionNaming("Unmanaged coding type: "
168                         + ((BasidBinder) pnc).getCodingType());
169             }
170         } else {
171             //oid
172
return null;
173         }
174     }
175
176     public boolean canManage(SpeedoClass sc) {
177         if (sc.identityType == SpeedoIdentity.USER_ID && sc.datastoreSequence == null) {
178             ArrayList JavaDoc idFields = new ArrayList JavaDoc();
179             Iterator JavaDoc fieldsIt = sc.jdoField.values().iterator();
180             while (fieldsIt.hasNext()) {
181                 SpeedoField sp = (SpeedoField) fieldsIt.next();
182                 if (sp.primaryKey) {
183                     idFields.add(sp);
184                 }
185             }
186             String JavaDoc objectidClass = sc.objectidClass;
187             if (sc.generateObjectId()) {
188                 objectidClass = NamingRules.generatedObjectIdName(sc.getFQName());
189             }
190             return objectidClass == null || objectidClass.length() == 0;
191         }
192         return false;
193     }
194
195     public boolean canProvidePBinder(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
196         if(!super.canProvidePBinder(hints, classLoader)) {
197             return false;
198         }
199         String JavaDoc ctStr = getBinderClassNameFromHints(hints, SINGLE_USER_ID);
200         if (ctStr == null) {
201             return false;
202         }
203         try {
204             Short.parseShort(ctStr);
205             return true;
206         } catch (NumberFormatException JavaDoc e) {
207             return false;
208         }
209     }
210
211     public boolean canProvidePNamingContext(Object JavaDoc hints, ClassLoader JavaDoc classLoader) {
212         return getPNCClassNameFromHints(hints, SINGLE_USER_ID) != null;
213     }
214
215     public PBinder getPBinder(String JavaDoc className, String JavaDoc hints, ClassLoader JavaDoc cl, byte mappingStructureRule, Map JavaDoc cn2binder, Map JavaDoc cn2pnc) {
216         String JavaDoc ctStr = getBinderClassNameFromHints(hints, SINGLE_USER_ID);
217         return new BasidBinder(Short.parseShort(ctStr));
218     }
219
220     /**
221      * sud,coding type, pnc class name, class name
222      */

223     public void getJormNamingConfig(NameDef nd,
224                                     SpeedoClass targetClass,
225                                     MetaObject sourceMO,
226                                     String JavaDoc key,
227                                     Properties result) throws SpeedoException {
228         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
229         sb.append(SINGLE_USER_ID);
230         sb.append(HINTS_SEP);
231         int ct = getCodingType(nd.getFieldName(), sourceMO);
232         sb.append(ct);
233         sb.append(HINTS_SEP);
234         String JavaDoc className = targetClass.getFQName();
235         SpeedoClass ancestor = targetClass.getAncestor();
236         if (sourceMO instanceof GenClassRef //For a Genclass ref or identifier
237
|| (ancestor == null //No inheritance
238
&& targetClass.jormclass.getSubClasses().isEmpty())) {
239             sb.append(ct);
240         } else {
241             if (ancestor == null) {
242                 ancestor = targetClass;
243             }
244             try {
245                 if (targetClass.jormclass.getSubClasses().isEmpty()) {
246                     sb.append(ct);
247                 } else if (targetClass.jormclass.detectFilterElementNotInPK(targetClass.jormclass.getInheritanceFilter(nd), nd)) {
248                     //if has child(ren)
249
//and one of the fields of the filter is not in the primary key,
250
//use a PolymorphicPNC
251
sb.append(POLYMORPHIC_PNC);
252                 } else {
253                     //use a kfpnc
254
sb.append(NamingRules.kfpncName(ancestor.getFQName()));
255                 }
256             } catch (Exception JavaDoc e) {
257                 logger.log(BasicLevel.ERROR, "Error while retrieving the inheritance filter of the namedef:" + nd.toString());
258                 throw new SpeedoException("Error while retrieving the inheritance filter of the namedef:" + nd.toString(), e);
259             }
260             className = ancestor.getFQName();
261         }
262         sb.append(HINTS_SEP);
263         sb.append(className);
264         result.setProperty(key, sb.toString());
265     }
266
267     public String JavaDoc getPNameHints(SpeedoClass sc, NameDef nd) {
268         //provide the field value as Object format
269
String JavaDoc fn = nd.getFieldName();
270         int tc = sc.jormclass.getTypedElement(fn).getType().getTypeCode();
271         fn = "jdoReferenceState." + fn;
272         switch (tc) {
273         case PType.TYPECODE_CHAR:
274             return "new Character(" + fn + ")";
275         case PType.TYPECODE_BYTE:
276             return "new Byte(" + fn + ")";
277         case PType.TYPECODE_SHORT:
278             return "new Short(" + fn + ")";
279         case PType.TYPECODE_INT:
280             return "new Integer(" + fn + ")";
281         case PType.TYPECODE_LONG:
282             return "new Long(" + fn + ")";
283         default:
284             return fn;
285         }
286     }
287
288     public String JavaDoc getGCPNameHints(SpeedoClass sc, NameDef nd) {
289         return "proxy.getPName()";
290     }
291
292     public void fillNameDef(MIBuilderHelper mibh,
293                             Manager manager,
294                             NameDef nd,
295                             SpeedoClass tsc,
296                             SpeedoClass ssc,
297                             MetaObject mo,
298                             Reference ref,
299                             CommonClassMapping hcm,
300                             JormMIMappingBuilder mb,
301                             boolean isIdentifier,
302                             boolean isInGenClass,
303                             boolean createField,
304                             Collection JavaDoc createdMOs) throws SpeedoException, PException {
305         ArrayList JavaDoc idFields = new ArrayList JavaDoc();
306         Iterator JavaDoc fieldsIt = tsc.jdoField.values().iterator();
307         while (fieldsIt.hasNext()) {
308             SpeedoField sp = (SpeedoField) fieldsIt.next();
309             if (sp.primaryKey) {
310                 idFields.add(sp);
311             }
312         }
313         if (idFields.size() == 0) {
314             throw new SpeedoException(
315                     mibh.getErrorMessage(tsc, mo, ref)
316                     + "no primary key field and no object id defined");
317         }
318         if (idFields.size() > 1) {
319             String JavaDoc fn = "[";
320             String JavaDoc sep = "";
321             for (int i = 0; i < idFields.size(); i++) {
322                 fn += sep + ((SpeedoField) idFields.get(i)).name;
323                 sep = ",";
324             }
325             fn += "]";
326             throw new SpeedoException(
327                     mibh.getErrorMessage(tsc, mo, ref)
328                     + "there are several primary key fields "
329                     + fn
330                     + " and no an object id class.");
331         }
332         SpeedoField sf = (SpeedoField) idFields.get(0);
333         String JavaDoc clafn = mibh.getNameDefFieldPrefix(
334                 ref, isIdentifier, isInGenClass) + sf.name;
335         if (createField) {
336             int size = PType.NOSIZE;
337             SpeedoExtension se = sf.getExtension(SpeedoProperties.VENDOR_NAME,SpeedoProperties.SIZE);
338             if (se != null) {
339                 try {
340                     size = Integer.parseInt(se.value);
341                 } catch (NumberFormatException JavaDoc e) {
342                     //The error will occur during the parsing of the primitive
343
// field in the referenced class
344
}
345             }
346             int scale = PType.NOSIZE;
347             se = sf.getExtension(SpeedoProperties.VENDOR_NAME,SpeedoProperties.SCALE);
348             if (se != null) {
349                 try {
350                     scale = Integer.parseInt(se.value);
351                 } catch (NumberFormatException JavaDoc e) {
352                     //The error will occur during the parsing of the primitive
353
// field in the referenced class
354
}
355             }
356             mibh.createNameDefField(mo, clafn,
357                     mibh.getPrimitivePType(Type.getType(sf.desc)), size, scale);
358         }
359         nd.setFieldName(clafn);
360         if (createField && (mb != null)) {
361             mb.createNameDefMapping(hcm, nd, ssc, isIdentifier, isInGenClass);
362         }
363     }
364 }
365
Popular Tags