001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.naming.lib;
018:
019: import org.objectweb.jorm.api.PClassMapping;
020: import org.objectweb.jorm.api.PException;
021: import org.objectweb.jorm.api.PMapper;
022: import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdBinderInfo;
023: import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdMgrImpl;
024: import org.objectweb.jorm.facility.naming.polymorphid.PolymorphIdPName;
025: import org.objectweb.jorm.facility.naming.polymorphid.PolymorphRefNC;
026: import org.objectweb.jorm.metainfo.api.Class;
027: import org.objectweb.jorm.metainfo.api.ClassMapping;
028: import org.objectweb.jorm.metainfo.api.ClassRef;
029: import org.objectweb.jorm.metainfo.api.CommonClassMapping;
030: import org.objectweb.jorm.metainfo.api.CompositeName;
031: import org.objectweb.jorm.metainfo.api.GenClassMapping;
032: import org.objectweb.jorm.metainfo.api.GenClassRef;
033: import org.objectweb.jorm.metainfo.api.Manager;
034: import org.objectweb.jorm.metainfo.api.MetaObject;
035: import org.objectweb.jorm.metainfo.api.NameDef;
036: import org.objectweb.jorm.metainfo.api.NameRef;
037: import org.objectweb.jorm.metainfo.api.PrimitiveElement;
038: import org.objectweb.jorm.metainfo.api.Reference;
039: import org.objectweb.jorm.naming.api.PBinder;
040: import org.objectweb.jorm.naming.api.PName;
041: import org.objectweb.jorm.naming.api.PNameCoder;
042: import org.objectweb.jorm.naming.api.PNamingContext;
043: import org.objectweb.jorm.type.api.PType;
044: import org.objectweb.jorm.type.api.PTypeSpace;
045: import org.objectweb.perseus.cache.api.CacheManager;
046: import org.objectweb.speedo.api.SpeedoException;
047: import org.objectweb.speedo.api.SpeedoProperties;
048: import org.objectweb.speedo.generation.jorm.JormMIMappingBuilder;
049: import org.objectweb.speedo.mapper.api.JormFactory;
050: import org.objectweb.speedo.metadata.SpeedoClass;
051: import org.objectweb.speedo.metadata.SpeedoColumn;
052: import org.objectweb.speedo.metadata.SpeedoExtension;
053: import org.objectweb.speedo.metadata.SpeedoField;
054: import org.objectweb.speedo.metadata.SpeedoIdentity;
055: import org.objectweb.speedo.naming.api.MIBuilderHelper;
056: import org.objectweb.speedo.naming.api.NamingManager;
057: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
058: import org.objectweb.util.monolog.api.Logger;
059:
060: import java.util.Collection;
061: import java.util.Map;
062: import java.util.Properties;
063:
064: /**
065: *
066: * @author S.Chassande-Barrioz
067: */
068: public class PolymorphIdNamingManager implements NamingManager {
069:
070: /**
071: * is the name of the composite name used for the identifiers in case of
072: * container identifier management.
073: */
074: private final static String POLYMORH_ID_NAME = "org.objectweb.jorm.facility.naming.polymorphid.PolymorphId";
075:
076: private final static String BINDER_CLASS_NAME = POLYMORH_ID_NAME
077: + "BinderInfo";
078:
079: /**
080: * is a name of field of the composite name used for the identifiers in case of
081: * container identifier management.
082: */
083: private final static String POLYMORH_ID_OID = "objectId";
084: /**
085: * is a name of field of the composite name used for the identifiers in case of
086: * container identifier management.
087: */
088: private final static String POLYMORH_ID_CID = "classId";
089:
090: private final static String OID = "oid";
091: private final static String CID = "cid";
092:
093: /**
094: * The manager of the naming used by the container (long, long). It
095: * provides PBinder, PNamingContext since a class name.
096: */
097: protected PolymorphIdMgrImpl cIdManager = null;
098:
099: private PMapper mapper;
100: private Logger logger;
101:
102: public PolymorphIdMgrImpl getcIdManager() throws PException {
103: if (cIdManager == null) {
104: cIdManager = new PolymorphIdMgrImpl();
105: cIdManager.setLogger(logger);
106: cIdManager.init(mapper,
107: PClassMapping.CREATE_STRUCTURE_IF_NEEDED);
108: }
109: return cIdManager;
110: }
111:
112: // IMPLEMENTATION OF THE METHOD FROM THE NamingManager INTERFACE //
113: //---------------------------------------------------------------//
114: public boolean supportPNamingcontext() {
115: return true;
116: }
117:
118: public Object encode(PName pn) throws PException {
119: if (pn instanceof PolymorphIdPName) {
120: return pn.getPNameManager().getPType().getJormName() + SEP
121: + pn.encodeString();
122: }
123: return null;
124: }
125:
126: public PName decode(PNameCoder pnc, Object oid,
127: java.lang.Class clazz, JormFactory jf) throws PException {
128: if (oid instanceof String) {
129: String stroid = (String) oid;
130: int idx = stroid.indexOf(SEP);
131: if (pnc != null) {
132: if (pnc instanceof PolymorphIdBinderInfo
133: || pnc instanceof PolymorphRefNC) {
134: if (idx != -1) {
135: //The oid contains the class name
136: return pnc.decodeString(stroid.substring(idx
137: + SEP.length()));
138: } else {
139: //The oid must decoded directly
140: return pnc.decodeString(stroid);
141: }
142:
143: } else {
144: //The pnc is not a BasidBinder, then the oid cannot be managed
145: return null;
146: }
147: } else {
148: //No pnc specified
149: if (idx != -1) {
150: //The oid contains the class name
151: String fqcn = stroid.substring(0, idx);
152: ClassLoader cl = getClass().getClassLoader();
153: if (cl == null) {
154: cl = ClassLoader.getSystemClassLoader();
155: }
156: try {
157: pnc = jf.getPBinder(fqcn, cl);
158: } catch (Exception e) {
159: }
160: return (pnc instanceof PolymorphIdBinderInfo
161: || pnc instanceof PolymorphRefNC ? pnc
162: .decodeString(stroid.substring(idx
163: + SEP.length())) : null);
164: } else {
165: //The oid cannot be managed
166: return null;
167: }
168: }
169: }
170: return null;
171: }
172:
173: public void setPMapper(PMapper mapper) {
174: this .mapper = mapper;
175: }
176:
177: public void setPmf(POManagerFactoryItf pmf) {
178:
179: }
180:
181: public void setLogger(Logger logger) {
182: this .logger = logger;
183: }
184:
185: public boolean canManage(SpeedoClass sc) {
186: return sc.identity.strategy == SpeedoIdentity.DATASTORE_POLYMORPHID;
187: }
188:
189: public boolean canProvidePBinder(Object hints,
190: ClassLoader classLoader) {
191: return BINDER_CLASS_NAME.equals(hints);
192: }
193:
194: public boolean canProvidePNamingContext(Object hints,
195: ClassLoader classLoader) {
196: return canProvidePBinder(hints, classLoader);
197: }
198:
199: public PBinder getPBinder(String className, String hints,
200: ClassLoader classLoader, byte mappingStructureRule,
201: Map cn2binder, Map cn2pnc) throws PException {
202: return getcIdManager().getPBinder(className);
203: }
204:
205: public PNamingContext getPNamingContext(String className,
206: String hints, ClassLoader classLoader,
207: byte mappingStructureRule, Map cn2binder, Map cn2pnc,
208: Manager miManager, PClassMapping pcm) throws PException {
209: return getcIdManager().getRefNC(className);
210: }
211:
212: private void defineNameDef(NameDef nd, MetaObject owner,
213: MIBuilderHelper mibh, String prefix)
214: throws SpeedoException, PException {
215: PrimitiveElement oid = mibh.createNameDefField(owner, prefix
216: + OID, PTypeSpace.LONG);
217: PrimitiveElement cid = mibh.createNameDefField(owner, prefix
218: + CID, PTypeSpace.LONG);
219: Manager manager = mibh.getManager(owner);
220: CompositeName speedoidcn = getPolymorphIdCN(manager);
221: NameRef nr = nd.createNameRef(speedoidcn);
222: nr.addProjection(POLYMORH_ID_OID, oid.getName());
223: nr.addProjection(POLYMORH_ID_CID, cid.getName());
224: }
225:
226: public void defineClassIdentifierNameDef(NameDef nd, Class jc,
227: SpeedoClass sc, ClassMapping cm, MIBuilderHelper mibh,
228: JormMIMappingBuilder mb, Collection createdMOs)
229: throws SpeedoException, PException {
230: defineNameDef(nd, jc, mibh, "");
231: mb.createClassIdentifierNameDefMapping(cm, nd, sc, mibh);
232: }
233:
234: public void defineClassReferenceNameDef(NameDef nd, ClassRef cr,
235: SpeedoField sf, SpeedoClass currentClass, ClassMapping cm,
236: MIBuilderHelper mibh, JormMIMappingBuilder mb)
237: throws SpeedoException, PException {
238: String prefix = mibh
239: .getNameDefFieldPrefix(cr, false, false, sf);
240: defineNameDef(nd, cr.getParent(), mibh, prefix);
241: mb.createClassRefNameDefMapping(cm, nd, sf);
242: }
243:
244: public void defineClassReferenceNameDef(NameDef nd, ClassRef cr,
245: SpeedoField sf, SpeedoClass currentClass,
246: GenClassMapping gcm, MIBuilderHelper mibh,
247: JormMIMappingBuilder mb) throws SpeedoException, PException {
248: String prefix = mibh.getNameDefFieldPrefix(cr, false, true, sf);
249: defineNameDef(nd, cr.getParent(), mibh, prefix);
250: mb.createClassRefNameDefMapping(gcm, nd, sf);
251: }
252:
253: public void defineGenClassIdentifierNameDef(NameDef nd,
254: GenClassRef gcr, SpeedoField sf, SpeedoClass currentClass,
255: GenClassMapping gcm, MIBuilderHelper mibh,
256: JormMIMappingBuilder mb) throws SpeedoException, PException {
257: defineNameDef(nd, gcr, mibh, "");
258: mb.createGenClassIdentifierNameDefMapping(gcm, nd, sf, mibh);
259: }
260:
261: public void defineGenClassReferenceNameDef(NameDef nd,
262: GenClassRef gcr, SpeedoField sf, SpeedoClass currentClass,
263: ClassMapping cm, MIBuilderHelper mibh,
264: JormMIMappingBuilder mb) throws SpeedoException, PException {
265: //The polymorph id does not support the mapping
266: // where the GC id is the source class id
267: // then the field name contains the GCR name as prefix
268: String prefix = gcr.getName() + "_";
269: defineNameDef(nd, gcr, mibh, prefix);
270: mb.createGenClassRefNameDefMapping(cm, nd, sf);
271: }
272:
273: public boolean needInheritanceDiscriminator(SpeedoClass sc)
274: throws SpeedoException {
275: return false;
276: }
277:
278: private CompositeName getPolymorphIdCN(Manager manager) {
279: CompositeName cn = manager.getCompositeName(POLYMORH_ID_NAME);
280: if (cn == null) {
281: cn = manager.createCompositeName(POLYMORH_ID_NAME);
282: cn.createCompositeNameField(POLYMORH_ID_CID,
283: PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE);
284: cn.createCompositeNameField(POLYMORH_ID_OID,
285: PTypeSpace.LONG, PType.NOSIZE, PType.NOSIZE);
286: }
287: return cn;
288: }
289:
290: public void getJormNamingConfig(NameDef nd,
291: SpeedoClass targetClass, MetaObject sourceMO, String key,
292: Properties result) {
293: result.setProperty(key, BINDER_CLASS_NAME);
294: }
295:
296: public String getPNameHints(SpeedoClass sc, NameDef nd) {
297: return "null";
298: }
299:
300: public Object[] getPNameHints2(SpeedoClass sc, NameDef nd) {
301: return new Object[] { PNH_NULL_VALUE };
302: }
303:
304: public String getGCPNameHints(SpeedoClass sc, NameDef nd) {
305: return "null";
306: }
307:
308: public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) {
309: return null;
310: }
311:
312: public SpeedoColumn[] getDefaultColumn(SpeedoClass sc) {
313: SpeedoColumn[] cols = new SpeedoColumn[] { new SpeedoColumn(),
314: new SpeedoColumn() };
315: cols[0].name = CID;
316: cols[0].targetField = CID;
317: cols[1].name = OID;
318: cols[1].targetField = OID;
319: return cols;
320: }
321:
322: public void setCache(CacheManager cache) {
323: }
324: }
|