1 23 24 package org.objectweb.jorm.lib; 25 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.EventObject ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 import org.objectweb.jorm.api.ClassMappedEvent; 34 import org.objectweb.jorm.api.JormConfigurator; 35 import org.objectweb.jorm.api.PClassMapping; 36 import org.objectweb.jorm.api.PException; 37 import org.objectweb.jorm.api.PExceptionIO; 38 import org.objectweb.jorm.api.PExceptionProtocol; 39 import org.objectweb.jorm.api.PMapCluster; 40 import org.objectweb.jorm.api.PMapper; 41 import org.objectweb.jorm.api.PMapperListener; 42 import org.objectweb.jorm.api.PMappingCallback; 43 import org.objectweb.jorm.api.PMappingRequiredEvent; 44 import org.objectweb.jorm.api.PMappingStructuresManager; 45 import org.objectweb.jorm.metainfo.api.Manager; 46 import org.objectweb.jorm.type.api.PExceptionTyping; 47 import org.objectweb.jorm.type.api.PTypeSpace; 48 import org.objectweb.jorm.type.lib.PTypeSpacePAAH; 49 import org.objectweb.jorm.util.api.Loggable; 50 import org.objectweb.jorm.xml2mi.api.Parser; 51 import org.objectweb.medor.eval.prefetch.api.PrefetchCache; 52 import org.objectweb.util.monolog.api.BasicLevel; 53 import org.objectweb.util.monolog.api.Logger; 54 import org.objectweb.util.monolog.api.LoggerFactory; 55 56 59 public abstract class Mapper implements PMapper, Loggable, PMappingCallback { 60 63 static final int INITIALMAPSIZE = 100; 64 67 String mapperName = null; 68 72 Manager metainfoManager = null; 73 77 Parser parser = null; 78 82 PTypeSpace typeSpace = null; 83 87 HashMap mappedClasses = new HashMap (INITIALMAPSIZE); 88 93 PrefetchCache prefetchCache = null; 94 95 98 protected Logger logger = null; 99 102 protected LoggerFactory loggerFactory = null; 103 104 107 private JormConfigurator jormConfigurator = null; 108 109 private Class gcmClass = null; 110 111 PMappingStructuresManager pMappingStructuresManager = null; 112 113 private ArrayList listenerList = new ArrayList (); 114 115 116 120 public Mapper() throws PException { 121 jormConfigurator = new JormConfiguratorImpl(); 122 jormConfigurator.configure(PMapper.JORMPROPFILE); 123 jormConfigurator.configureLog(PMapper.JORMPROPFILE); 124 } 125 126 public Mapper(JormConfigurator c) { 127 jormConfigurator = c; 128 } 129 130 public void setJormConfigurator(JormConfigurator jormConfigurator) { 131 this.jormConfigurator = jormConfigurator; 132 } 133 134 public JormConfigurator getJormConfigurator() { 135 return jormConfigurator; 136 } 137 139 144 public void scheduleEvent(EventObject e) throws PException { 145 Iterator it = listenerList.iterator(); 146 while (it.hasNext()) { 147 PMapperListener l = (PMapperListener) it.next(); 148 if (e instanceof PMappingRequiredEvent) { 149 l.mappingRequired((PMappingRequiredEvent) e); 150 } else if (e instanceof ClassMappedEvent) { 151 l.classMapped((ClassMappedEvent) e); 152 } else { 153 throw new PException("Mapper cannot schedule this kind of event: " + e); 154 } 155 } 156 } 157 158 160 165 public String getMapperName() { 166 return mapperName; 167 } 168 169 173 public PMappingStructuresManager getPMappingStructuresManager() { 174 return pMappingStructuresManager; 175 } 176 177 183 public PClassMapping lookup(String classname) { 184 return (PClassMapping) mappedClasses.get(classname); 185 } 186 187 201 public void map(PClassMapping pcm) 202 throws PException { 203 map(null, pcm); 204 } 205 206 221 public void map(Object conn, PClassMapping pcm) 222 throws PException { 223 if (pcm == null) 224 throw new PExceptionProtocol( 225 "Cannot map null class mapping."); 226 PClassMapping ecm 227 = (PClassMapping) mappedClasses.get(pcm.getClassName()); 228 if (ecm == pcm) 229 return; 230 if (ecm != null) 231 throw new PExceptionProtocol( 232 "Class already mapped within this mapper."); 233 if (!pcm.isConform(mapperName)) 234 throw new PExceptionProtocol("Class mapping (" + pcm.getClass().getName() 235 + ") cannot be managed by this mapper (" 236 + mapperName + ")."); 237 if (typeSpace == null) 238 typeSpace = new PTypeSpacePAAH(); 239 ((Loggable) pcm).setLogger(loggerFactory.getLogger( 240 "org.objectweb.jorm." + mapperName + "." + pcm.getClassName())); 241 pcm.init(this, null); 242 mappedClasses.put(pcm.getClassName(), pcm); 243 scheduleEvent(new ClassMappedEvent(this, pcm.getClassName())); 244 if (logger != null) 245 logger.log(BasicLevel.INFO, 246 "New mapped class: " + pcm.getClassName()); 247 } 248 249 266 public void map(Object conn, PClassMapping pcm, boolean loadmeta) 267 throws PException { 268 if (loadmeta && ((metainfoManager == null) || (parser == null))) { 269 throw new PExceptionProtocol( 270 "Not enough means to load meta-information."); 271 } 272 map(pcm); 273 if (!loadmeta) 274 return; 275 parser.parse( 276 pcm.getClassName().replace('.', File.pathSeparatorChar) + ".pd"); 277 } 278 279 286 public void setMapperName(String mappername) { 287 mapperName = mappername; 288 } 289 290 public PClassMapping createGenClassMapping() throws PException { 291 try { 292 return (PClassMapping) gcmClass.newInstance(); 293 } catch (InstantiationException e) { 294 throw new PException(e, "Cannot instanciate gen class mapping Class."); 295 } catch (IllegalAccessException e) { 296 throw new PException(e, "Cannot access to gen class mapping Class."); 297 } 298 } 299 300 public Manager getMetaInfoManager() { 301 return metainfoManager; 302 } 303 304 308 public PTypeSpace getPTypeSpace() { 309 return typeSpace; 310 } 311 312 316 public void start() throws PException { 317 String basemn = mapperName; 318 int ind = mapperName.indexOf("."); 319 if (ind != -1) { 320 basemn = mapperName.substring(0, ind); 321 } 322 metainfoManager = jormConfigurator.getMIManager(); 323 parser = jormConfigurator.getParser(); 324 pMappingStructuresManager = jormConfigurator.getSchMgr(basemn); 325 pMappingStructuresManager.setPMapper(this); 326 pMappingStructuresManager.setLogger(logger); 327 typeSpace = metainfoManager.getPTypeSpace(); 328 gcmClass = jormConfigurator.getGcmClass(basemn); 329 } 330 331 335 public void stop() throws PException { 336 } 337 338 342 public void unmap(String classname) throws PException { 343 mappedClasses.remove(classname); 344 } 345 346 351 public void setPrefetchCache(PrefetchCache pc) throws PException { 352 if (prefetchCache != null) { 353 throw new PExceptionProtocol("A prefetch cache has already been assigned to this mapper"); 354 } 355 prefetchCache = pc; 356 } 357 358 361 public PrefetchCache getPrefetchCache() { 362 return prefetchCache; 363 } 364 365 367 372 public PMapCluster getPMapCluster(String jcname) throws PException { 373 if (pMappingStructuresManager == null) { 374 throw new PException("No pMappingStructuresManager associated with this mapper"); 375 } 376 return pMappingStructuresManager.getPMapCluster(jcname); 377 } 378 379 383 public Collection getPMapClusters() { 384 if (pMappingStructuresManager == null) { 385 return new ArrayList (); 386 } 387 return pMappingStructuresManager.getPMapClusters(); 388 } 389 390 public void addDependency(String jcname1, String jcname2) throws PException { 391 if (pMappingStructuresManager == null) { 392 throw new PException("No pMappingStructuresManager associated with this mapper"); 393 } 394 pMappingStructuresManager.addDependency(jcname1, jcname2); 395 } 396 397 public void declareClass(String jcname) { 398 pMappingStructuresManager.declareClass(jcname); 399 } 400 401 public void classDefined(String jcname) throws PException { 402 if (pMappingStructuresManager == null) { 403 throw new PException("No pMappingStructuresManager associated with this mapper"); 404 } 405 pMappingStructuresManager.classDefined(jcname); 406 } 407 408 413 public synchronized void addMapperEventListener(PMapperListener listener) { 414 if (listenerList.indexOf(listener) == -1) { 415 listenerList.add(listener); 416 } 417 } 418 419 424 public synchronized void removeMapperEventListener(PMapperListener listener) { 425 if (listenerList.indexOf(listener) != -1) { 426 listenerList.remove(listener); 427 } 428 } 429 430 434 public void setPMapper(PMapper pm) { 435 } 436 437 439 443 public void setLogger(Logger logger) { 444 } 445 446 450 public void setLoggerFactory(LoggerFactory loggerfactory) { 451 loggerFactory = loggerfactory; 452 logger = loggerFactory.getLogger("org.objectweb.jorm." + mapperName); 453 } 454 455 public Logger getLogger() { 456 return logger; 457 } 458 459 public LoggerFactory getLoggerFactory() { 460 return loggerFactory; 461 } 462 463 public String cn2mn(String cn) { 464 String shortMapperName = getMapperName(); 465 int idx = shortMapperName.indexOf("."); 466 if (idx != -1) { 467 shortMapperName = shortMapperName.substring(0, idx); 468 } 469 int ldot = cn.lastIndexOf('.'); 470 if (ldot == -1) 471 return shortMapperName + "." + cn; 472 return cn.substring(0, ldot + 1) + shortMapperName + cn.substring(ldot); 473 } 474 475 477 public boolean equals(Object o) { 478 if (this == o) 479 return true; 480 if (!(o instanceof PMapper)) 481 return false; 482 PMapper m = (PMapper) o; 483 return m.getMapperName().equals(mapperName); 484 } 485 486 public int hashCode() { 487 return mapperName.hashCode(); 488 } 489 } 490 | Popular Tags |