1 18 package org.objectweb.jorm.naming.lib; 19 20 import org.objectweb.jorm.api.PMapperListener; 21 import org.objectweb.jorm.api.PMappingRequiredEvent; 22 import org.objectweb.jorm.api.ClassMappedEvent; 23 import org.objectweb.jorm.api.PMapper; 24 import org.objectweb.jorm.api.PClassMapping; 25 import org.objectweb.jorm.api.PException; 26 import org.objectweb.jorm.type.api.PType; 27 import org.objectweb.jorm.type.lib.PTypePAAH; 28 import org.objectweb.jorm.naming.api.NamingFilterKeyProvider; 29 import org.objectweb.jorm.naming.api.KeyFilteredNamingContext; 30 import org.objectweb.jorm.naming.api.PBinder; 31 import org.objectweb.jorm.util.api.Loggable; 32 import org.objectweb.util.monolog.api.Logger; 33 import org.objectweb.util.monolog.api.BasicLevel; 34 import org.objectweb.util.monolog.api.LoggerFactory; 35 36 import java.util.ArrayList ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.HashMap ; 40 import java.util.Set ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 44 51 public class KFPNCManager 52 implements PMapperListener, Loggable { 53 54 private Logger logger; 55 private LoggerFactory loggerFactory; 56 private boolean debug; 57 58 private Map cn2pnc; 59 60 65 private Map scn2binderKey = new HashMap (); 66 67 public KFPNCManager() { 68 cn2pnc = new HashMap (); 69 } 70 71 public KFPNCManager(Map cn2pnc) { 72 this.cn2pnc = cn2pnc; 73 } 74 75 public KeyFilteredNamingContext getKFPNC(String className) { 76 Object pnc = cn2pnc.get(className); 77 return (KeyFilteredNamingContext ) 78 (pnc instanceof KeyFilteredNamingContext ? pnc : null); 79 } 80 81 82 85 public void mappingRequired(PMappingRequiredEvent e) { 86 } 88 89 95 public void classMapped(ClassMappedEvent e) { 96 String className = e.getJormClassName(); 97 PMapper mapper = e.getPMapper(); 98 PType type = mapper.getPTypeSpace().getPType(className); 99 if (!(type instanceof PTypePAAH)) { 100 return; 101 } 102 PClassMapping pcm = mapper.lookup(className); 103 PTypePAAH classType = (PTypePAAH) type; 104 PType[] superTypes = classType.getInheritedPType(); 105 if (superTypes == null || superTypes.length == 0) { Object pnc = cn2pnc.get(className); 108 if (!(pnc instanceof KeyFilteredNamingContext)) { 109 return; 110 } 111 KeyFilteredNamingContext kfpnc = (KeyFilteredNamingContext) pnc; 112 HashSet subPCMs = new HashSet (); findSubPCM(className, subPCMs, mapper); 115 116 for(Iterator it = subPCMs.iterator(); it.hasNext();) { 118 PClassMapping subpcm = (PClassMapping) it.next(); 119 if (subpcm instanceof NamingFilterKeyProvider) { 120 Object key = ((NamingFilterKeyProvider) subpcm).getNamingFilterKey(); 121 if (debug) { 122 logger.log(BasicLevel.DEBUG, 123 "Register the binder of the '" 124 + subpcm.getClassName() 125 + "' sub class with the following key: " + key); 126 } 127 try { 128 kfpnc.exportClass(subpcm.getPBinder(), key); 129 } catch (PException e1) { 130 logger.log(BasicLevel.ERROR, 131 "Impossible to export a Binder into the KFPNC of the class " 132 + className, e1); 133 } 134 } 135 } 136 List list = (List ) scn2binderKey.remove(className); 137 if (list != null) { 138 while (!list.isEmpty()) { 139 Object [] obj = (Object []) list.remove(0); 140 if (debug) { 141 logger.log(BasicLevel.DEBUG, 142 "Register the binder of the '" 143 + ((PBinder)obj[0]).getBinderClassMapping().getClassName() 144 + "' sub class with the following key: " + obj[1]); 145 } 146 try { 147 kfpnc.exportClass((PBinder)obj[0], obj[1]); 148 } catch (PException e1) { 149 logger.log(BasicLevel.ERROR, 150 "Impossible to export a Binder into the KFPNC of the class " 151 + className, e1); 152 } 153 } 154 } 155 } else if (pcm instanceof NamingFilterKeyProvider) { 156 Object key = ((NamingFilterKeyProvider) pcm).getNamingFilterKey(); 157 if (debug) { 158 logger.log(BasicLevel.DEBUG, 159 "The '" + pcm.getClassName() 160 + "' provides the following key: " + key); 161 } 162 HashSet superClassNames = new HashSet (); 164 165 findAncestorNames(className, superClassNames, mapper); 167 PBinder binder = pcm.getPBinder(); 168 169 for(Iterator it = superClassNames.iterator(); it.hasNext();) { 171 String superClassName = (String ) it.next(); 172 KeyFilteredNamingContext kfpnc = getKFPNC(superClassName); 173 if (kfpnc == null) { 174 List list = (List ) scn2binderKey.get(superClassName); 175 if (list == null) { 176 list = new ArrayList (); 177 scn2binderKey.put(superClassName, list); 178 } 179 list.add(new Object []{binder, key}); 180 if (debug) { 181 logger.log(BasicLevel.DEBUG, 182 "No KPNC found for the ancestor " + superClassName 183 + ". The register of the class " 184 + pcm.getClassName() + " will be done later"); 185 } 186 } else { 187 if (debug) { 188 logger.log(BasicLevel.DEBUG, 189 "Register the binder of the '" 190 + pcm.getClassName() 191 + "' class with the following key: " + key 192 + " into the KFPNc of the ancestor " + superClassName); 193 } 194 try { 195 kfpnc.exportClass(binder, key); 196 } catch (PException e1) { 197 logger.log(BasicLevel.ERROR, 198 "Impossible to export a Binder into the KFPNC of the class " 199 + superClassName, e1); 200 } 201 } 202 } 203 } 204 } 205 206 214 private void findSubPCM(String className, Set result, PMapper mapper) { 215 PClassMapping pcm = mapper.lookup(className); 216 if (pcm != null && result.add(pcm)) { 217 PType classType = mapper.getPTypeSpace().getPType(className); 218 PType[] subTypes = classType.getSubTypes(); 219 for(int i=0; i<subTypes.length; i++) { 220 findSubPCM(subTypes[i].getJavaName(), result, mapper); 221 } 222 } else { 223 if (debug) { 224 logger.log(BasicLevel.DEBUG, "The sub class " 225 + className + " is not yet mapped"); 226 } 227 } 228 } 229 230 236 private void findAncestorNames(String className, Set result, PMapper mapper) { 237 PType type = mapper.getPTypeSpace().getPType(className); 238 if (!(type instanceof PTypePAAH)) { 239 return; 240 } 241 PTypePAAH classType = (PTypePAAH) type; 242 PType[] superTypes = classType.getInheritedPType(); 243 if (superTypes == null || superTypes.length == 0) { result.add(className); 245 } else { for(int i=0; i<superTypes.length; i++) { 247 findAncestorNames(superTypes[i].getJormName(), result, mapper); 248 } 249 } 250 } 251 252 253 256 public LoggerFactory getLoggerFactory() { 257 return loggerFactory; 258 } 259 260 public void setLoggerFactory(LoggerFactory loggerFactory) { 261 this.loggerFactory = loggerFactory; 262 } 263 264 public Logger getLogger() { 265 return logger; 266 } 267 268 public void setLogger(Logger logger) { 269 this.logger = logger; 270 debug = logger.isLoggable(BasicLevel.DEBUG); 271 } 272 } 273 | Popular Tags |