1 25 26 package org.objectweb.speedo.sequence.lib; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.jdo.datastore.Sequence; 32 33 import org.objectweb.fractal.api.control.BindingController; 34 import org.objectweb.jorm.api.PClassMapping; 35 import org.objectweb.jorm.api.PException; 36 import org.objectweb.jorm.api.PMapper; 37 import org.objectweb.jorm.facility.naming.generator.LongGenMgr; 38 import org.objectweb.jorm.facility.naming.generator.LongGenMgrRegistry; 39 import org.objectweb.jorm.facility.naming.rdbsequence.RdbSequenceLongGen; 40 import org.objectweb.jorm.mapper.rdb.lib.PMapperRdb; 41 import org.objectweb.speedo.sequence.lib.SpeedoSequence; 42 import org.objectweb.speedo.api.ExceptionHelper; 43 import org.objectweb.speedo.pm.api.ProxyManagerFactory; 44 import org.objectweb.speedo.sequence.api.SequenceManager; 45 import org.objectweb.util.monolog.api.BasicLevel; 46 import org.objectweb.util.monolog.api.Logger; 47 48 52 public class SpeedoSequenceManager implements SequenceManager, BindingController { 53 54 public final static String PMF_BINDING = "proxy-manager-factory"; 55 public final static String MAPPER_BINDING = "mapper"; 56 public final static String MAPPER_RDB_BINDING = "mapper-rdb"; 57 58 59 62 private PMapper mapper = null; 63 64 67 private PMapperRdb mapperRdb = null; 68 69 72 private ProxyManagerFactory pmf = null; 73 74 77 private Logger logger = null; 78 79 80 public Logger getLogger() { 81 return logger; 82 } 83 public void setLogger(Logger logger) { 84 this.logger = logger; 85 } 86 87 public PMapper getMapper() { 88 return mapper; 89 } 90 public void setMapper(PMapper mapper) { 91 this.mapper = mapper; 92 } 93 94 public PMapperRdb getMapperRdb() { 95 return mapperRdb; 96 } 97 public void setMapperRdb(PMapperRdb mapperRdb) { 98 this.mapperRdb = mapperRdb; 99 } 100 103 public String [] listFc() { 104 return new String [] { 105 PMF_BINDING, 106 MAPPER_BINDING, 107 MAPPER_RDB_BINDING 108 }; 109 } 110 111 public Object lookupFc(String s) { 112 if (PMF_BINDING.equals(s)) { 113 return pmf; 114 } else if(MAPPER_BINDING.equals(s)) { 115 return mapper; 116 } else if (MAPPER_RDB_BINDING.equals(s)) { 117 return mapperRdb; 118 } else 119 return null; 120 } 121 122 public void bindFc(String s, Object o) { 123 if ("logger".equals(s)) { 124 logger = (Logger) o; 125 } else if (PMF_BINDING.equals(s)) { 126 pmf = (ProxyManagerFactory) o; 127 } else if (MAPPER_BINDING.equals(s)) { 128 mapper = (PMapper) o; 129 } else if (MAPPER_RDB_BINDING.equals(s)) { 130 mapperRdb = (PMapperRdb) o; 131 } 132 } 133 134 public void unbindFc(String s) { 135 if (PMF_BINDING.equals(s)) { 136 pmf = null; 137 } else if (MAPPER_BINDING.equals(s)) { 138 mapper = null; 139 } else if (MAPPER_RDB_BINDING.equals(s)) { 140 mapperRdb = null; 141 } 142 } 143 144 147 152 private Map sequenceName2sequences = new HashMap (); 153 154 157 private Object longGenMgr = null; 158 159 162 public Object getLongGenMgr() { 163 return longGenMgr; 164 } 165 166 169 public void setLongGenMgr(Object longGenMgr) { 170 if (longGenMgr instanceof LongGenMgr) { 171 this.longGenMgr = longGenMgr; 172 } 173 } 174 175 181 public void addSequence(Object sequence) { 182 if (sequence instanceof SpeedoSequence) { 183 SpeedoSequence s = (SpeedoSequence) sequence; 184 String fqName = (s.packageName == null )?(""):(s.packageName + ".") + s.name; 185 if (!sequenceName2sequences.containsKey(fqName)) { 187 sequenceName2sequences.put(fqName, s); 188 s.setSequenceManager(this); 189 } 190 } 191 } 192 193 199 public void removeSequence(String sequenceName) { 200 sequenceName2sequences.remove(sequenceName); 201 } 202 203 209 public Sequence getSequence(String name) { 210 SpeedoSequence s = (SpeedoSequence) sequenceName2sequences.get(name); 211 if (s != null) { 212 try { 213 initSequence(s); 214 } catch (Exception e) { 215 logger.log(BasicLevel.ERROR, 216 "Impossible to get the sequence " 217 + name, ExceptionHelper.getNested(e)); 218 return null; 219 } 220 } 221 return s; 222 } 223 224 public ProxyManagerFactory getPMF() { 225 return pmf; 226 } 227 228 public void setPMF(ProxyManagerFactory pmf) { 229 this.pmf = pmf; 230 } 231 232 240 private void initSequence(Object sequence) throws PException { 241 if (sequence instanceof SpeedoSequence) { 242 try { 243 SpeedoSequence s = (SpeedoSequence) sequence; 244 if (s.datastoreName == null || s.datastoreName.equals("")) { 246 if (longGenMgr == null) { 247 synchronized (LongGenMgrRegistry.class) { 249 longGenMgr = LongGenMgrRegistry.getLongGenMgr(mapper); 250 if (longGenMgr == null) { 251 try { 253 longGenMgr = (LongGenMgr) Class.forName(getLongGenMgrName()).newInstance(); 254 } catch (Exception e) { 255 throw new PException(e, "Cannot create LongGenMgr (probably a ClassLoader problem): " + getLongGenMgrName()); 256 } 257 ((LongGenMgr) longGenMgr).init(mapper, PClassMapping.CREATE_STRUCTURE_IF_NEEDED); 258 LongGenMgrRegistry.registerLonGenMgr((LongGenMgr)longGenMgr); 260 } 261 } 262 } 263 s.setLongGen(((LongGenMgr) longGenMgr).getLongGen(s.getName())); 265 } else { 266 if (mapperRdb != null) { 268 if (s.getLongGen() == null) { 269 RdbSequenceLongGen rdbLongGen = new RdbSequenceLongGen(mapperRdb, s.datastoreName, false); 270 if(s.start != null) { 271 rdbLongGen.setSequenceStart(s.start); 272 } 273 if(s.increment != null) { 274 rdbLongGen.setSequenceIncrement(s.increment); 275 } 276 if(s.cache != null) { 277 rdbLongGen.setSequenceStart(s.cache); 278 } 279 Object conn = mapperRdb.getConnection(); 281 rdbLongGen.createSequence(conn); 282 s.setLongGen(rdbLongGen); 284 mapperRdb.closeConnection(conn); 286 } 287 } else { 288 throw new PException("Problem while init the sequence " 289 + ((SpeedoSequence)sequence).getName() + ": " 290 + "a rdb sequence has been defined in a jdo file and no rdb is linked to the application."); 291 } 292 } 293 } catch (Exception e) { 294 throw new PException(e, "Problem while init the sequence " + ((SpeedoSequence)sequence).getName()); 295 } 296 } 297 } 298 299 304 private String getLongGenMgrName() { 305 String mn = mapper.getMapperName(); 306 int idx = mn.indexOf('.'); 307 if (idx != -1) { 308 mn = mn.substring(0, idx); 309 } 310 return "org.objectweb.jorm.facility.naming.generator." 311 + mn + ".LongGenIncrMapping"; 312 } 313 } 314 | Popular Tags |