1 package org.apache.torque.map; 2 3 21 22 import java.lang.reflect.Method ; 23 import java.text.MessageFormat ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.StringTokenizer ; 29 30 import org.apache.commons.collections.map.ListOrderedMap; 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.torque.TorqueException; 33 import org.apache.torque.adapter.IDMethod; 34 import org.apache.torque.oid.IDBroker; 35 import org.apache.torque.oid.IdGenerator; 36 37 45 public class DatabaseMap implements java.io.Serializable 46 { 47 51 public static final char STD_SEPARATOR_CHAR = '_'; 52 53 56 public static final char SCHEMA_SEPARATOR_CHAR = '.'; 57 58 62 public static final String INIT_CLASS_NAME_FORMAT = 63 "org.apache.torque.linkage.{0}MapInit"; 64 65 68 protected static final String [] ERROR_MESSAGES_INIT = { 69 "Invalid Torque OM setup for Database \"{0}\".\n" 70 + "Database Map initialization class, \"{1}\"," + " " 71 + "could not be found in your classpath.", 72 "Invalid Torque OM setup for Database \"{0}\".\n" 73 + "A class that the Database Map initialization class, \"{1}\", " 74 + "depends on could not be found.", 75 "Invalid Torque OM setup for Database \"{0}\".\n" 76 + "Something unexpected happened doing Class.forName(\"{1}\"). " 77 + "See the nested exception for details.", 78 "Invalid Torque OM setup for Database \"{0}\".\n" 79 + "An error occured invoking the init() method in class, \"{1}\"" 80 }; 81 82 83 private static final long serialVersionUID = 955251837095032274L; 84 85 86 private static final int ID_GENERATORS_INITIAL_SIZE = 6; 87 88 89 private String name; 90 91 92 private Map tables; 93 94 98 private TableMap idTable = null; 99 100 101 private IDBroker idBroker = null; 102 103 104 private HashMap idGenerators; 105 106 107 private boolean isInitialized = false; 108 109 112 public DatabaseMap() 113 { 114 tables = Collections.synchronizedMap(new ListOrderedMap()); 115 idGenerators = new HashMap (ID_GENERATORS_INITIAL_SIZE); 116 } 117 118 126 public DatabaseMap(String name, int numberOfTables) 127 { 128 this.name = name; 129 tables = Collections.synchronizedMap(new ListOrderedMap()); 130 idGenerators = new HashMap (ID_GENERATORS_INITIAL_SIZE); 131 } 132 133 140 public DatabaseMap(String name) 141 { 142 this.name = name; 143 tables = Collections.synchronizedMap(new ListOrderedMap()); 144 idGenerators = new HashMap (ID_GENERATORS_INITIAL_SIZE); 145 } 146 147 153 public boolean containsTable(TableMap table) 154 { 155 return containsTable(table.getName()); 156 } 157 158 164 public boolean containsTable(String name) 165 { 166 if (name.indexOf('.') > 0) 167 { 168 name = name.substring(0, name.indexOf('.')); 169 } 170 return tables.containsKey(name); 171 } 172 173 178 public TableMap getIdTable() 179 { 180 return idTable; 181 } 182 183 191 public IDBroker getIDBroker() 192 { 193 return idBroker; 194 } 195 196 203 public String getName() 204 { 205 return name; 206 } 207 208 219 public TableMap getTable(String name) 220 { 221 return (TableMap) tables.get(name); 222 } 223 224 234 public TableMap[] getTables() 235 { 236 TableMap[] dbTables = new TableMap[tables.size()]; 237 synchronized (tables) 238 { 239 Iterator it = tables.values().iterator(); 240 int i = 0; 241 while (it.hasNext()) 242 { 243 dbTables[i++] = (TableMap) it.next(); 244 } 245 } 246 return dbTables; 247 } 248 249 255 public void addTable(String tableName) 256 { 257 TableMap tmap = new TableMap(tableName, this); 258 tables.put(tableName, tmap); 259 } 260 261 268 public void addTable(String tableName, int numberOfColumns) 269 { 270 TableMap tmap = new TableMap(tableName, numberOfColumns, this); 271 tables.put(tableName, tmap); 272 } 273 274 279 public void addTable(TableMap map) 280 { 281 tables.put(map.getName(), map); 282 } 283 284 289 public void setIdTable(TableMap idTable) 290 { 291 this.idTable = idTable; 292 addTable(idTable); 293 } 294 295 300 public void setIdTable(String tableName) 301 { 302 TableMap tmap = new TableMap(tableName, this); 303 setIdTable(tmap); 304 } 305 306 314 public void addIdGenerator(String type, IdGenerator idGen) 315 { 316 idGenerators.put(type, idGen); 317 } 318 319 328 public IdGenerator getIdGenerator(String type) 329 { 330 return (IdGenerator) idGenerators.get(type); 331 } 332 333 341 public synchronized boolean startIdBroker() 342 { 343 if (idBroker == null) 344 { 345 setIdTable("ID_TABLE"); 346 TableMap tMap = getIdTable(); 347 tMap.addPrimaryKey("ID_TABLE_ID", new Integer (0)); 348 tMap.addColumn("TABLE_NAME", ""); 349 tMap.addColumn("NEXT_ID", new Integer (0)); 350 tMap.addColumn("QUANTITY", new Integer (0)); 351 idBroker = new IDBroker(idTable); 352 addIdGenerator(IDMethod.ID_BROKER, idBroker); 353 return true; 354 } 355 return false; 356 } 357 358 394 public synchronized void initialize() throws TorqueException 395 { 396 if (isInitialized) 397 { 398 return; 399 } 400 String initClassName = MessageFormat.format(INIT_CLASS_NAME_FORMAT, 401 new Object [] { 402 javanameMethod(getName()) 403 }); 404 405 Class initClass = null; 406 try 407 { 408 initClass = Class.forName(initClassName); 409 } 410 catch (ClassNotFoundException e) 411 { 412 throw new TorqueException(MessageFormat.format( 413 ERROR_MESSAGES_INIT[0], 414 new Object [] { 415 getName(), 416 initClassName 417 }), 418 e); 419 } 420 catch (LinkageError e) 421 { 422 throw new TorqueException(MessageFormat.format( 423 ERROR_MESSAGES_INIT[1], 424 new Object [] { 425 getName(), initClassName 426 }), 427 e); 428 } 429 catch (Throwable e) 430 { 431 throw new TorqueException(MessageFormat.format( 432 ERROR_MESSAGES_INIT[2], 433 new Object [] { 434 getName(), initClassName 435 }), 436 e); 437 } 438 try 439 { 440 Method initMethod = initClass.getMethod("init", (Class []) null); 441 initMethod.invoke(null, (Object []) null); 442 } 443 catch (Exception e) 444 { 445 throw new TorqueException(MessageFormat.format( 446 ERROR_MESSAGES_INIT[3], 447 new Object [] { 448 getName(), initClassName 449 }), 450 e); 451 } 452 isInitialized = true; 453 } 454 455 466 protected String javanameMethod(String schemaName) 467 { 468 StringBuffer result = new StringBuffer (); 469 StringTokenizer tok = new StringTokenizer 470 (schemaName, String.valueOf(STD_SEPARATOR_CHAR)); 471 while (tok.hasMoreTokens()) 472 { 473 String namePart = (String ) tok.nextElement(); 474 result.append(StringUtils.capitalize(namePart)); 475 } 476 477 schemaName = result.toString(); 480 result = new StringBuffer (); 481 482 tok = new StringTokenizer 483 (schemaName, String.valueOf(SCHEMA_SEPARATOR_CHAR)); 484 while (tok.hasMoreTokens()) 485 { 486 String namePart = (String ) tok.nextElement(); 487 result.append(StringUtils.capitalize(namePart)); 488 } 489 return result.toString(); 490 } 491 } 492 | Popular Tags |