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.longid.LongIdBinder;
022: import org.objectweb.jorm.facility.naming.longid.LongIdManager;
023: import org.objectweb.jorm.facility.naming.longid.LongIdPNC;
024: import org.objectweb.jorm.facility.naming.longid.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 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 -1 value. The long field can be a
043: * 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.longid.LongIdManager
048: * @author S.Chassande-Barrioz
049: */
050: public class LongIdNamingManager extends CommonLongIdNamingManager {
051:
052: private final static String LONG_ID_NAME = "org.objectweb.jorm.facility.naming.longid.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: 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.LONG;
070: }
071:
072: protected java.lang.Class getJavaFieldType() {
073: return Long.TYPE;
074: }
075:
076: protected BasicOperand getBasicOperand() {
077: return new BasicOperand(1l << 44);
078: }
079:
080: protected boolean checkFieldType(String type) {
081: return "long".equals(type);
082: }
083:
084: public PMapper getMapper() {
085: return lidm.getMapper();
086: }
087:
088: protected String getName() {
089: return "lid";
090: }
091:
092: public PBinder newClassPBinder(String className, Object conn)
093: throws PException {
094: return lidm.newClassPBinder(className, conn);
095: }
096:
097: public PNamingContext newClassPNamingContext() throws PException {
098: return lidm.newClassPNamingContext();
099: }
100:
101: public PBinder newGenClassPBinder() throws PException {
102: return lidm.newGenClassPBinder();
103: }
104:
105: public void setPMapper(PMapper mapper) {
106: super .setPMapper(mapper);
107: try {
108: lidm.setPMapper(mapper);
109: } catch (PException e) {
110: e.printStackTrace();
111: }
112: }
113:
114: public java.lang.Class getLongIdManagerClass() {
115: return LongIdManager.class;
116: }
117:
118: public java.lang.Class getPBinderClass() {
119: return LongIdBinder.class;
120: }
121:
122: public java.lang.Class getPNameClass() {
123: return LongIdPName.class;
124: }
125:
126: public java.lang.Class getPNamingContextClass() {
127: return LongIdPNC.class;
128: }
129:
130: protected String getBinderForGenClass() {
131: return BINDER_FOR_GENCLASS;
132: }
133:
134: protected String getBinderForClass() {
135: return BINDER_FOR_CLASS;
136: }
137:
138: protected String getHiddenLidFieldName() {
139: return HIDDEN_LID_FIELD_NAME;
140: }
141:
142: public boolean canManage(SpeedoClass sc) {
143: return sc.identity.strategy == SpeedoIdentity.DATASTORE_LONG;
144: }
145:
146: public Object encode(PName pn) throws PException {
147: if (pn instanceof LongIdPName) {
148: return pn.getPNameManager().getPType().getJormName() + SEP
149: + pn.encodeLong();
150: }
151: return null;
152: }
153:
154: protected PName decodeLong(PNameCoder pnc, String idStr)
155: throws PException {
156: return pnc.decodeLong(Long.parseLong(idStr));
157: }
158:
159: public String getGCPNameHints(SpeedoClass sc, NameDef nd) {
160: return "new Long(speedoPO.getPName().encodeLong())";
161: }
162: }
|