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 java.util.ArrayList;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: import org.objectweb.jorm.api.PClassMapping;
024: import org.objectweb.jorm.api.PException;
025: import org.objectweb.jorm.api.PMapper;
026: import org.objectweb.jorm.metainfo.api.Manager;
027: import org.objectweb.jorm.naming.api.PBinder;
028: import org.objectweb.jorm.naming.api.PNamingContext;
029: import org.objectweb.jorm.naming.api.PolymorphicPNamingContext;
030: import org.objectweb.perseus.cache.api.CacheManager;
031: import org.objectweb.speedo.api.SpeedoException;
032: import org.objectweb.speedo.metadata.SpeedoClass;
033: import org.objectweb.speedo.metadata.SpeedoColumn;
034: import org.objectweb.speedo.naming.api.NamingManager;
035: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
036: import org.objectweb.util.monolog.api.Logger;
037:
038: /**
039: *
040: * @author S.Chassande-Barrioz
041: */
042: public abstract class NamingManagerHelper implements NamingManager {
043:
044: public final static String HINTS_SEP = ",";
045:
046: public final static int ID_CAT_IDX = 0;
047: public final static int BINDER_IDX = 1;
048: public final static int PNC_IDX = 2;
049: public final static int PCLASS_IDX = 3;
050:
051: public final static String POLYMORPHIC_PNC = "org.objectweb.jorm.naming.lib.BasicPolymorphicPNamingContext";
052:
053: protected Logger logger;
054: protected CacheManager cache;
055: protected POManagerFactoryItf pmf;
056: protected PMapper mapper;
057:
058: public boolean supportPNamingcontext() {
059: return true;
060: }
061:
062: public void setPMapper(PMapper mapper) {
063: this .mapper = mapper;
064: }
065:
066: public void setCache(CacheManager cache) {
067: this .cache = cache;
068: }
069:
070: public void setLogger(Logger logger) {
071: this .logger = logger;
072: }
073:
074: public void setPmf(POManagerFactoryItf pmf) {
075: this .pmf = pmf;
076: }
077:
078: public NamingManager.NamingField[] getNamingfields(SpeedoClass sc)
079: throws PException {
080: return null;
081: }
082:
083: protected abstract String getName();
084:
085: public boolean canProvidePBinder(Object hints,
086: ClassLoader classLoader) {
087: return hints instanceof String
088: && ((String) hints).startsWith(getName() + HINTS_SEP);
089: }
090:
091: public boolean canProvidePNamingContext(Object hints,
092: ClassLoader classLoader) {
093: return hints instanceof String
094: && ((String) hints).startsWith(getName() + HINTS_SEP);
095: }
096:
097: public PBinder getPBinder(String className, String hints,
098: ClassLoader classLoader, byte mappingStructureRule,
099: Map cn2binder, Map cn2pnc) throws PException {
100:
101: try {
102: return (PBinder) classLoader.loadClass(
103: getBinderClassNameFromHints(hints, getName()))
104: .newInstance();
105: } catch (Exception e) {
106: throw new PException(e,
107: "Impossible to instanciate the binder of the class "
108: + hints);
109: }
110: }
111:
112: public PNamingContext getPNamingContext(String className,
113: String hints, ClassLoader classLoader,
114: byte mappingStructureRule, Map cn2binder, Map cn2pnc,
115: Manager miManager, PClassMapping pcm) throws PException {
116: String[] tokens = getTokens(hints);
117: PNamingContext pnc = null;
118: if (tokens[BINDER_IDX].equals(tokens[PNC_IDX])) {
119: //The binder must be used as PNamingContext
120: pnc = (PNamingContext) cn2binder.get(className);
121: if (pnc == null) {
122: //instanciate the binder/PNC
123: pnc = (PNamingContext) getPBinder(className, hints,
124: classLoader, mappingStructureRule, cn2binder,
125: cn2pnc);
126: //register the binder/PNC as binder
127: cn2binder.put(className, pnc);
128: }
129: return pnc;
130: } else {
131: boolean register = false;
132: if (!tokens[PCLASS_IDX].equals(className)
133: && !tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
134: //The PNC of THE ancestor must be used
135: pnc = (PNamingContext) cn2pnc.get(tokens[PCLASS_IDX]);
136: if (pnc == null) {
137: register = true;
138: }
139: }
140: if (pnc == null) {
141: //instanciate the PNC
142: try {
143: pnc = (PNamingContext) classLoader.loadClass(
144: tokens[PNC_IDX]).newInstance();
145: //if the PNC is polymorphic
146: if (tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
147: PolymorphicPNamingContext polymorphicPNC = (PolymorphicPNamingContext) pnc;
148: //link the cache
149: //cache null
150: polymorphicPNC.setCache(this .cache);
151: //instanciate a new binder
152: PBinder delegatedBinder = getPBinder(className,
153: hints, classLoader,
154: mappingStructureRule, cn2binder, cn2pnc);
155: //link a new instanciated and dedicated binder
156: polymorphicPNC
157: .setDelegatedBinder(delegatedBinder);
158: delegatedBinder.setPClassMapping(pcm);
159: delegatedBinder.setPType(pcm.getPType());
160: //link the PClassMapping
161: polymorphicPNC.setPcm(delegatedBinder
162: .getBinderClassMapping());
163: }
164: } catch (Exception e) {
165: throw new PException(e,
166: "Impossible to instanciate the PNC of the class "
167: + hints);
168: }
169: if (register) {
170: cn2pnc.put(tokens[PCLASS_IDX], pnc);
171: }
172: }
173: }
174: return pnc;
175: }
176:
177: public SpeedoColumn[] getDefaultColumn(SpeedoClass sc) {
178: return null;
179: }
180:
181: public static String[] getTokens(Object o) {
182: if (!(o instanceof String)) {
183: return null;
184: }
185: StringTokenizer st = new StringTokenizer((String) o, HINTS_SEP,
186: true);
187: ArrayList tokens = new ArrayList();
188: boolean previousIsSep = false;
189: while (st.hasMoreTokens()) {
190: String token = st.nextToken();
191: if (HINTS_SEP.equals(token)) {
192: if (previousIsSep) {
193: tokens.add("");
194: }
195: previousIsSep = true;
196: } else {
197: tokens.add(token);
198: previousIsSep = false;
199: }
200: }
201: tokens.add("");
202: return (String[]) tokens.toArray(new String[tokens.size()]);
203: }
204:
205: public static String getBinderClassNameFromHints(Object hints,
206: String idCatName) {
207: String[] tokens = getTokens(hints);
208: if (tokens == null || tokens.length < 4
209: || !tokens[0].equals(idCatName)) {
210: return null;
211: } else {
212: return tokens[BINDER_IDX];
213: }
214: }
215:
216: public static String getPNCClassNameFromHints(Object hints,
217: String idCatName) {
218: String[] tokens = getTokens(hints);
219: if (tokens == null || tokens.length < 4
220: || !tokens[0].equals(idCatName)) {
221: return null;
222: } else {
223: return tokens[PNC_IDX];
224: }
225: }
226:
227: public boolean needInheritanceDiscriminator(SpeedoClass sc)
228: throws SpeedoException {
229: return false;
230: }
231: }
|