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.PException;
020: import org.objectweb.jorm.api.PMapper;
021: import org.objectweb.jorm.facility.naming.olongid.LongIdPBinder;
022: import org.objectweb.jorm.facility.naming.olongid.LongIdPNC;
023: import org.objectweb.jorm.facility.naming.olongid.LongIdManager;
024: import org.objectweb.jorm.facility.naming.olongid.LongIdPName;
025: import org.objectweb.jorm.metainfo.api.NameDef;
026: import org.objectweb.jorm.naming.api.PBinder;
027: import org.objectweb.jorm.naming.api.PName;
028: import org.objectweb.jorm.naming.api.PNameCoder;
029: import org.objectweb.jorm.naming.api.PNamingContext;
030: import org.objectweb.jorm.type.api.PType;
031: import org.objectweb.jorm.type.api.PTypeSpace;
032: import org.objectweb.medor.expression.lib.BasicOperand;
033: import org.objectweb.speedo.metadata.SpeedoClass;
034: import org.objectweb.speedo.metadata.SpeedoIdentity;
035:
036: /**
037: * This is naming manager manages identifier based on a java.lang.Long value
038: * generated by Speedo, using a persistent generator (Speedo structure in
039: * database). The long value is composed of two parts. Some bits are used for
040: * the class identifier, and the rest is used to identify the object instance
041: * in the class. This identifier format supports very well the polymorphism.
042: * The null reference is represented by the NULL value.The java.lang.Long field
043: * can be a visible persistent field, otherwise it is hidden by Speedo/JORM
044: * implementation.
045: *
046: * @see org.objectweb.speedo.naming.api.NamingManager
047: * @see org.objectweb.jorm.facility.naming.olongid.LongIdManager
048: * @author S.Chassande-Barrioz
049: */
050: public class OLongIdNamingManager extends CommonLongIdNamingManager {
051:
052: private final static String LONG_ID_NAME = "org.objectweb.jorm.facility.naming.olongid.LongId";
053: private final static String LONG_ID_LID = "lid";
054: private final static String BINDER_FOR_CLASS = "c";
055: private final static String BINDER_FOR_GENCLASS = "gc";
056: private final static String HIDDEN_LID_FIELD_NAME = "jdolid";
057:
058: private LongIdManager lidm = new LongIdManager();
059:
060: protected String getLongIdName() {
061: return LONG_ID_NAME;
062: }
063:
064: protected String getLongIdLid() {
065: return LONG_ID_LID;
066: }
067:
068: protected PType getFieldType() {
069: return PTypeSpace.OBJLONG;
070: }
071:
072: protected BasicOperand getBasicOperand() {
073: return new BasicOperand(new Long(1l << 44), PTypeSpace.OBJLONG);
074: }
075:
076: protected boolean checkFieldType(String type) {
077: return "Long".equals(type) || "java.lang.Long".equals(type);
078: }
079:
080: protected String getBinderForClass() {
081: return BINDER_FOR_CLASS;
082: }
083:
084: protected String getBinderForGenClass() {
085: return BINDER_FOR_GENCLASS;
086: }
087:
088: protected PName decodeLong(PNameCoder pnc, String idStr)
089: throws PException {
090: return pnc.decodeOlong(Long.valueOf(idStr));
091: }
092:
093: protected String getHiddenLidFieldName() {
094: return HIDDEN_LID_FIELD_NAME;
095: }
096:
097: protected Class getJavaFieldType() {
098: return Long.class;
099: }
100:
101: public PMapper getMapper() {
102: return lidm.getMapper();
103: }
104:
105: protected String getName() {
106: return "olid";
107: }
108:
109: public PBinder newClassPBinder(String className, Object conn)
110: throws PException {
111: return lidm.newClassPBinder(className, conn);
112: }
113:
114: public PNamingContext newClassPNamingContext() throws PException {
115: return lidm.newClassPNamingContext();
116: }
117:
118: public PBinder newGenClassPBinder() throws PException {
119: return lidm.newGenClassPBinder();
120: }
121:
122: public void setPMapper(PMapper mapper) {
123: super .setPMapper(mapper);
124: try {
125: lidm.setPMapper(mapper);
126: } catch (PException e) {
127: e.printStackTrace();
128: }
129: }
130:
131: public java.lang.Class getLongIdManagerClass() {
132: return LongIdManager.class;
133: }
134:
135: public java.lang.Class getPBinderClass() {
136: return LongIdPBinder.class;
137: }
138:
139: public java.lang.Class getPNameClass() {
140: return LongIdPName.class;
141: }
142:
143: public java.lang.Class getPNamingContextClass() {
144: return LongIdPNC.class;
145: }
146:
147: // IMPLEMENTATION OF THE METHOD FROM THE NamingManager INTERFACE //
148: //---------------------------------------------------------------//
149: public boolean canManage(SpeedoClass sc) {
150: return sc.identity.strategy == SpeedoIdentity.DATASTORE_OLONG;
151: }
152:
153: public Object encode(PName pn) throws PException {
154: if (pn instanceof LongIdPName) {
155: return pn.getPNameManager().getPType().getJormName() + SEP
156: + pn.encodeOlong();
157: }
158: return null;
159: }
160:
161: public String getGCPNameHints(SpeedoClass sc, NameDef nd) {
162: return "speedoPO.getPName().encodeOlong()";
163: }
164: }
|