1 30 package com.genimen.djeneric.repository; 31 32 import java.io.BufferedReader ; 33 import java.io.BufferedWriter ; 34 import java.io.ByteArrayInputStream ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileNotFoundException ; 38 import java.io.FileWriter ; 39 import java.io.InputStream ; 40 import java.io.InputStreamReader ; 41 import java.lang.reflect.Constructor ; 42 import java.lang.reflect.InvocationTargetException ; 43 import java.util.ArrayList ; 44 import java.util.Collections ; 45 46 import javax.xml.parsers.DocumentBuilder ; 47 import javax.xml.parsers.DocumentBuilderFactory ; 48 import javax.xml.transform.OutputKeys ; 49 import javax.xml.transform.Transformer ; 50 import javax.xml.transform.TransformerFactory ; 51 import javax.xml.transform.dom.DOMSource ; 52 import javax.xml.transform.stream.StreamResult ; 53 54 import org.w3c.dom.Document ; 55 import org.w3c.dom.Element ; 56 import org.w3c.dom.NodeList ; 57 58 import com.genimen.djeneric.language.Messages; 59 import com.genimen.djeneric.repository.exceptions.DjenericException; 60 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 61 import com.genimen.djeneric.repository.rdbms.RdbmsPersistenceManager; 62 import com.genimen.djeneric.repository.rdbmsdirect.RdbmsDirectPersistenceManager; 63 64 69 public class DjPersistenceManagerFactory 70 { 71 74 public final static String TYPE_POLYMORPH = "Polymorph RDBMS"; 75 78 public final static String TYPE_DIRECT = "DirectMap RDBMS"; 79 80 83 public final static String REPOSITORIES = "repositories"; 84 87 public final static String REPOSITORY_ELEMENT = "repository"; 88 91 public final static String REPOSITORY_NAME = "name"; 92 95 public final static String REPOSITORY_LOCATION = "location"; 96 99 public final static String REPOSITORY_URL = "url"; 100 103 public final static String REPOSITORY_DRIVER = "driver"; 104 107 public final static String REPOSITORY_SHARED_CONN = "sharedconnection"; 108 111 public final static String REPOSITORY_USER = "shareduser"; 112 115 public final static String REPOSITORY_PASSWORD = "sharedpassword"; 116 119 public final static String REPOSITORY_TRACELEVEL = "tracelevel"; 120 121 private final static String DEFAULT_CONFIGFILE_NAME = "repositories.xml"; 122 123 private String _configFileName = DEFAULT_CONFIGFILE_NAME; 124 125 private DjMessenger _messenger = null; 126 127 ArrayList _allRepositoryDescriptors = new ArrayList (); 128 129 139 public DjPersistenceManagerFactory(DjMessenger messenger, String configFileName) throws DjenericException, 140 FileNotFoundException 141 { 142 setMessenger(messenger); 143 _configFileName = configFileName; 144 loadRepositoryDescriptors(configFileName); 145 } 146 147 public DjPersistenceManagerFactory(DjMessenger messenger, InputStream configInputStream) throws DjenericException 148 { 149 setMessenger(messenger); 150 _configFileName = "provided input stream"; 151 loadRepositoryDescriptors(configInputStream); 152 } 153 154 162 public DjPersistenceManagerFactory(DjMessenger messenger) throws DjenericException, FileNotFoundException 163 { 164 this(messenger, DEFAULT_CONFIGFILE_NAME); 165 } 166 167 175 public DjPersistenceManagerFactory(String configFileName) throws DjenericException, FileNotFoundException 176 { 177 this(null, configFileName); 178 } 179 180 public DjPersistenceManagerFactory(InputStream configStream) throws DjenericException 181 { 182 this(null, configStream); 183 } 184 185 191 public DjPersistenceManagerFactory() throws DjenericException, FileNotFoundException 192 { 193 this(null, DEFAULT_CONFIGFILE_NAME); 194 } 195 196 203 public DjPersistenceManager createInstance() throws DjenericException 204 { 205 DjRepositoryDescriptor selected = null; 206 if (_allRepositoryDescriptors.size() == 0) 207 { 208 throw new DjenericException(Messages.getString("DjPersistenceManagerFactory.NoRepositoriesDefinedIn", 209 _configFileName)); 210 } 211 212 if (_allRepositoryDescriptors.size() == 1) 213 { 214 selected = (DjRepositoryDescriptor) _allRepositoryDescriptors.get(0); 215 } 216 else 217 { 218 selected = _messenger.selectRepository(getRepositoryDescriptors()); 219 } 220 return createInstance(selected); 221 } 222 223 232 public DjPersistenceManager createInstance(String repositoryName) throws DjenericException 233 { 234 return createInstance(getRepositoryDescriptor(repositoryName)); 235 } 236 237 public DjPersistenceManager createInstance(String repositoryName, String userid, String password) 238 throws DjenericException 239 { 240 return createInstance(getRepositoryDescriptor(repositoryName), userid, password); 241 } 242 243 public DjPersistenceManager createInstance(DjRepositoryDescriptor rdesc) throws DjenericException 244 { 245 return createInstance(rdesc, null, null); 246 } 247 248 257 public DjPersistenceManager createInstance(DjRepositoryDescriptor rdesc, String userid, String password) 258 throws DjenericException 259 { 260 DjPersistenceManager result; 261 if (rdesc.getRepositoryLocation().equals(TYPE_POLYMORPH)) 262 { 263 result = new RdbmsPersistenceManager(_messenger, rdesc, userid, password); 264 } 265 else if (rdesc.getRepositoryLocation().equals(TYPE_DIRECT)) 266 { 267 result = new RdbmsDirectPersistenceManager(_messenger, rdesc, userid, password); 268 } 269 else 270 { 271 Object mgr = null; 272 try 273 { 274 Class clz = Thread.currentThread().getContextClassLoader().loadClass(rdesc.getRepositoryLocation()); 275 Class [] params = new Class []{DjMessenger.class, DjRepositoryDescriptor.class, String .class, String .class}; 276 277 Constructor constructor = clz.getConstructor(params); 278 mgr = constructor.newInstance(new Object []{_messenger, rdesc, userid, password}); 279 } 280 catch (InvocationTargetException ite) 281 { 282 throw new DjenericException(ite.getCause()); 283 } 284 catch (Throwable x) 285 { 286 throw new DjenericException(x); 287 } 288 289 if (!(mgr instanceof DjPersistenceManager)) 290 { 291 throw new DjenericException("Specified Persistence manager (" + mgr.getClass().getName() 292 + ") is not an instance of DjPersistenceManager."); 293 } 294 295 result = (DjPersistenceManager) mgr; 296 } 297 298 return result; 299 } 300 301 307 public DjRepositoryDescriptor[] getRepositoryDescriptors() 308 { 309 return (DjRepositoryDescriptor[]) _allRepositoryDescriptors.toArray(new DjRepositoryDescriptor[0]); 310 } 311 312 321 public DjRepositoryDescriptor getRepositoryDescriptor(String repositoryName) throws ObjectNotDefinedException 322 { 323 for (int i = 0; i < _allRepositoryDescriptors.size(); i++) 324 { 325 DjRepositoryDescriptor rd = (DjRepositoryDescriptor) _allRepositoryDescriptors.get(i); 326 if (rd.getName().equalsIgnoreCase(repositoryName)) return rd; 327 } 328 throw new ObjectNotDefinedException(Messages.getString("DjPersistenceManagerFactory.RepositoryUndefined", 329 repositoryName)); 330 } 331 332 339 public void addRepositoryDescriptor(DjRepositoryDescriptor repos) 340 { 341 _allRepositoryDescriptors.add(repos); 342 } 343 344 350 public void removeRepositoryDescriptor(DjRepositoryDescriptor repos) 351 { 352 _allRepositoryDescriptors.remove(repos); 353 } 354 355 public void loadRepositoryDescriptors(String configFileName) throws FileNotFoundException , DjenericException 356 { 357 try 358 { 359 File inFile = new File (configFileName); 360 FileInputStream fis = new FileInputStream (inFile); 361 362 loadRepositoryDescriptors(fis); 363 } 364 catch (FileNotFoundException fnf) 365 { 366 throw new FileNotFoundException (Messages.getString("DjPersistenceManagerFactory.CouldNotLoad", configFileName)); 367 } 368 369 } 370 371 public void loadRepositoryDescriptors(InputStream configStream) throws DjenericException 372 { 373 StringBuffer src = new StringBuffer (10000); 374 try 375 { 376 _allRepositoryDescriptors.clear(); 377 BufferedReader br = new BufferedReader (new InputStreamReader (configStream)); 378 String ln; 379 while ((ln = br.readLine()) != null) 380 { 381 src.append(ln); 382 src.append("\n"); 383 } 384 br.close(); 385 386 if (src.toString().trim().length() == 0) return; 387 388 ByteArrayInputStream sr = new ByteArrayInputStream (new String (src).getBytes(DjPersistenceManager.ENCODING_METHOD)); 389 390 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 391 Document xmlDoc = dfactory.newDocumentBuilder().parse(sr); 392 393 NodeList repositories = xmlDoc.getElementsByTagName(REPOSITORY_ELEMENT); 394 for (int d = 0; d < repositories.getLength(); d++) 395 { 396 if (repositories.item(d) instanceof Element ) 397 { 398 Element repository = (Element ) (repositories.item(d)); 399 DjRepositoryDescriptor theRepository = new DjRepositoryDescriptor(repository.getAttribute(REPOSITORY_NAME), 400 repository.getAttribute(REPOSITORY_LOCATION), repository.getAttribute(REPOSITORY_DRIVER), repository 401 .getAttribute(REPOSITORY_URL)); 402 if (repository.getAttribute(REPOSITORY_SHARED_CONN).equalsIgnoreCase("true")) 403 { 404 theRepository.setSharedUser(repository.getAttribute(REPOSITORY_USER)); 405 theRepository.setSharedPassword(repository.getAttribute(REPOSITORY_PASSWORD)); 406 theRepository.setSharedConnection(true); 407 } 408 theRepository.setTraceLevel(convert2Int(repository.getAttribute(REPOSITORY_TRACELEVEL))); 409 addRepositoryDescriptor(theRepository); 410 } 411 } 412 } 413 catch (Exception e) 414 { 415 throw new DjenericException(e); 416 } 417 418 } 419 420 428 public void saveRepositoryDescriptors(String configFileName) throws DjenericException 429 { 430 try 431 { 432 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 433 DocumentBuilder builder = factory.newDocumentBuilder(); 434 Document doc = builder.newDocument(); 435 436 Element root = doc.createElement(REPOSITORIES); 437 438 doc.appendChild(root); 439 440 Collections.sort(_allRepositoryDescriptors); 441 442 DjRepositoryDescriptor[] reps = getRepositoryDescriptors(); 443 for (int i = 0; i < reps.length; i++) 444 { 445 Element repElem = doc.createElement(REPOSITORY_ELEMENT); 446 repElem.setAttribute(REPOSITORY_NAME, reps[i].getName()); 447 repElem.setAttribute(REPOSITORY_LOCATION, reps[i].getRepositoryLocation()); 448 repElem.setAttribute(REPOSITORY_DRIVER, reps[i].getDriver()); 449 repElem.setAttribute(REPOSITORY_URL, reps[i].getUrl()); 450 repElem.setAttribute(REPOSITORY_USER, reps[i].getSharedUser()); 451 repElem.setAttribute(REPOSITORY_PASSWORD, reps[i].getSharedPassword()); 452 repElem.setAttribute(REPOSITORY_SHARED_CONN, "" + reps[i].isSharedConnection()); 453 repElem.setAttribute(REPOSITORY_TRACELEVEL, "" + reps[i].getTraceLevel()); 454 455 root.appendChild(repElem); 456 } 457 458 BufferedWriter w = new BufferedWriter (new FileWriter (configFileName)); 459 460 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 461 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 462 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 463 serializer.transform(new DOMSource (doc), new StreamResult (w)); 464 w.close(); 465 } 466 catch (Exception x) 467 { 468 throw new DjenericException(x); 469 } 470 } 471 472 479 protected int convert2Int(String str) 480 { 481 int result = 0; 482 if (str.trim().length() > 0) 483 { 484 result = Integer.parseInt(str); 485 } 486 return result; 487 } 488 489 495 public void setMessenger(DjMessenger messenger) 496 { 497 _messenger = messenger; 498 } 499 500 } | Popular Tags |