1 18 package org.objectweb.speedo.naming.lib; 19 20 import java.util.ArrayList ; 21 import java.util.Map ; 22 import java.util.StringTokenizer ; 23 24 import org.objectweb.jorm.api.PClassMapping; 25 import org.objectweb.jorm.api.PException; 26 import org.objectweb.jorm.api.PMapper; 27 import org.objectweb.jorm.metainfo.api.Manager; 28 import org.objectweb.jorm.naming.api.PBinder; 29 import org.objectweb.jorm.naming.api.PNamingContext; 30 import org.objectweb.jorm.naming.api.PolymorphicPNamingContext; 31 import org.objectweb.perseus.cache.api.CacheManager; 32 import org.objectweb.speedo.metadata.SpeedoClass; 33 import org.objectweb.speedo.naming.api.NamingManager; 34 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 35 import org.objectweb.util.monolog.api.Logger; 36 37 41 public abstract class NamingManagerHelper implements NamingManager { 42 43 public final static String HINTS_SEP = ","; 44 45 public final static int ID_CAT_IDX = 0; 46 public final static int BINDER_IDX = 1; 47 public final static int PNC_IDX = 2; 48 public final static int PCLASS_IDX = 3; 49 50 public final static String POLYMORPHIC_PNC = "org.objectweb.jorm.naming.lib.BasicPolymorphicPNamingContext"; 51 52 protected Logger logger; 53 protected CacheManager cache; 54 protected ProxyManagerFactory pmf; 55 56 public boolean supportPNamingcontext() { 57 return true; 58 } 59 60 public void setPMapper(PMapper mapper) { 61 } 62 63 public void setCache(CacheManager cache) { 64 this.cache = cache; 65 } 66 67 public void setLogger(Logger logger) { 68 this.logger = logger; 69 } 70 71 public void setPmf(ProxyManagerFactory pmf) { 72 this.pmf = pmf; 73 } 74 75 public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) throws PException { 76 return null; 77 } 78 79 protected abstract String getName(); 80 81 public boolean canProvidePBinder(Object hints, ClassLoader classLoader) { 82 return hints instanceof String 83 && ((String ) hints).startsWith(getName() + HINTS_SEP); 84 } 85 86 public boolean canProvidePNamingContext(Object hints, ClassLoader classLoader) { 87 return hints instanceof String 88 && ((String ) hints).startsWith(getName() + HINTS_SEP); 89 } 90 91 public PBinder getPBinder(String className, 92 String hints, 93 ClassLoader classLoader, 94 byte mappingStructureRule, 95 Map cn2binder, 96 Map cn2pnc) throws PException { 97 98 try { 99 return (PBinder) classLoader.loadClass( 100 getBinderClassNameFromHints(hints, getName())).newInstance(); 101 } catch (Exception e) { 102 throw new PException(e, 103 "Impossible to instanciate the binder of the class " 104 + hints); 105 } 106 } 107 108 public PNamingContext getPNamingContext(String className, 109 String hints, 110 ClassLoader classLoader, 111 byte mappingStructureRule, 112 Map cn2binder, 113 Map cn2pnc, 114 Manager miManager, 115 PClassMapping pcm) throws PException { 116 String [] tokens = getTokens(hints); 117 PNamingContext pnc = null; 118 if (tokens[BINDER_IDX].equals(tokens[PNC_IDX])) { 119 pnc = (PNamingContext) cn2binder.get(className); 121 if (pnc == null) { 122 pnc = (PNamingContext) getPBinder(className, hints, classLoader, 124 mappingStructureRule, cn2binder, cn2pnc); 125 cn2binder.put(className, pnc); 127 } 128 return pnc; 129 } else { 130 boolean register = false; 131 if (!tokens[PCLASS_IDX].equals(className) && !tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) { 132 pnc = (PNamingContext) cn2pnc.get(tokens[PCLASS_IDX]); 134 if (pnc == null) { 135 register = true; 136 } 137 } 138 if (pnc == null) { 139 try { 141 pnc = (PNamingContext) classLoader 142 .loadClass(tokens[PNC_IDX]).newInstance(); 143 if (tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) { 145 PolymorphicPNamingContext polymorphicPNC = (PolymorphicPNamingContext) pnc; 146 polymorphicPNC.setCache(this.cache); 149 PBinder delegatedBinder = getPBinder(className, hints, classLoader, 151 mappingStructureRule, cn2binder, cn2pnc); 152 polymorphicPNC.setDelegatedBinder(delegatedBinder); 154 delegatedBinder.setPClassMapping(pcm); 155 delegatedBinder.setPType(pcm.getPType()); 156 polymorphicPNC.setPcm(delegatedBinder.getBinderClassMapping()); 158 } 159 } catch (Exception e) { 160 throw new PException(e, 161 "Impossible to instanciate the PNC of the class " 162 + hints); 163 } 164 if (register) { 165 cn2pnc.put(tokens[PCLASS_IDX], pnc); 166 } 167 } 168 } 169 return pnc; 170 } 171 172 public static String [] getTokens(Object o) { 173 if (!(o instanceof String )) { 174 return null; 175 } 176 StringTokenizer st = new StringTokenizer ((String ) o, HINTS_SEP, true); 177 ArrayList tokens = new ArrayList (); 178 boolean previousIsSep = false; 179 while (st.hasMoreTokens()) { 180 String token = st.nextToken(); 181 if (HINTS_SEP.equals(token)) { 182 if (previousIsSep) { 183 tokens.add(""); 184 } 185 previousIsSep = true; 186 } else { 187 tokens.add(token); 188 previousIsSep = false; 189 } 190 } 191 tokens.add(""); 192 return (String []) tokens.toArray(new String [tokens.size()]); 193 } 194 195 public static String getBinderClassNameFromHints(Object hints, String idCatName) { 196 String [] tokens = getTokens(hints); 197 if (tokens == null || tokens.length < 4 198 || !tokens[0].equals(idCatName)) { 199 return null; 200 } else { 201 return tokens[BINDER_IDX]; 202 } 203 } 204 205 public static String getPNCClassNameFromHints(Object hints, String idCatName) { 206 String [] tokens = getTokens(hints); 207 if (tokens == null || tokens.length < 4 208 || !tokens[0].equals(idCatName)) { 209 return null; 210 } else { 211 return tokens[PNC_IDX]; 212 } 213 } 214 } 215 | Popular Tags |