1 19 20 package org.netbeans.modules.j2ee.persistence.provider; 21 22 import java.io.IOException ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import org.netbeans.api.db.explorer.ConnectionManager; 26 import org.netbeans.api.db.explorer.DatabaseConnection; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.modules.j2ee.persistence.api.PersistenceLocation; 29 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 30 import org.netbeans.modules.j2ee.persistence.dd.PersistenceUtils; 31 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 32 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Properties; 33 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Property; 34 import org.netbeans.modules.j2ee.persistence.spi.provider.PersistenceProviderSupplier; 35 import org.netbeans.modules.j2ee.persistence.spi.server.ServerStatusProvider; 36 import org.netbeans.modules.j2ee.persistence.unit.*; 37 import org.netbeans.modules.j2ee.persistence.wizard.Util; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.filesystems.Repository; 44 import org.openide.loaders.DataObject; 45 import org.openide.loaders.DataObjectNotFoundException; 46 import org.openide.util.Parameters; 47 48 55 public class ProviderUtil { 56 57 public static final Provider HIBERNATE_PROVIDER = new HibernateProvider(); 59 public static final Provider TOPLINK_PROVIDER = new ToplinkProvider(); 60 public static final Provider KODO_PROVIDER = new KodoProvider(); 61 public static final Provider DEFAULT_PROVIDER = new DefaultProvider(); 62 63 private ProviderUtil() { 64 } 65 66 78 public static Provider getProvider(String providerClass, Project project){ 79 80 if (null == providerClass || "".equals(providerClass.trim())){ 81 return getContainerManagedProvider(project); 82 } 83 84 for (Provider each : getAllProviders()){ 85 if (each.getProviderClass().equals(providerClass.trim())){ 86 return each; 87 } 88 } 89 return DEFAULT_PROVIDER; 91 92 } 93 94 95 104 private static Provider getContainerManagedProvider(Project project){ 105 106 PersistenceProviderSupplier providerSupplier = project.getLookup().lookup(PersistenceProviderSupplier.class); 107 108 if (providerSupplier == null 109 || !providerSupplier.supportsDefaultProvider() 110 || providerSupplier.getSupportedProviders().isEmpty()){ 111 112 return null; 113 } 114 115 return providerSupplier.getSupportedProviders().get(0); 116 } 117 118 129 public static DatabaseConnection getConnection(PersistenceUnit pu) { 130 131 Parameters.notNull("pu", pu); 133 if (pu.getProperties() == null){ 134 return null; 135 } 136 137 String url = null; 138 String driver = null; 139 String username = null; 140 Property[] properties = pu.getProperties().getProperty2(); 141 Provider provider = getProvider(pu); 142 143 for (int i = 0; i < properties.length; i++) { 144 String key = properties[i].getName(); 145 if (key == null){ 146 continue; 147 } 148 if (key.equals(provider.getJdbcUrl())) { 149 url = properties[i].getValue(); 150 } else if (key.equals(provider.getJdbcDriver())) { 151 driver = properties[i].getValue(); 152 } else if (key.equals(provider.getJdbcUsername())) { 153 username = properties[i].getValue(); 154 } 155 } 156 DatabaseConnection[] connections = ConnectionManager.getDefault().getConnections(); 157 158 for (int i = 0; i < connections.length; i++) { 159 DatabaseConnection c = connections[i]; 160 if (c.getDatabaseURL().equals(url) && 162 c.getDriverClass().equals(driver) && 163 c.getUser().equals(username)) { 164 return c; 165 } 166 } 167 return null; 168 } 169 170 176 public static void setTableGeneration(PersistenceUnit persistenceUnit, String tableGenerationStrategy, Project project) { 177 String providerClass = persistenceUnit.getProvider(); 178 Provider provider = ProviderUtil.getProvider(providerClass, project); 179 setTableGeneration(persistenceUnit, tableGenerationStrategy, provider); 180 } 181 182 190 public static void setTableGeneration(PersistenceUnit persistenceUnit, String tableGenerationStrategy, Provider provider){ 191 Property tableGenerationProperty = provider.getTableGenerationProperty(tableGenerationStrategy); 192 Properties properties = persistenceUnit.getProperties(); 193 if (properties == null) { 194 properties = persistenceUnit.newProperties(); 195 persistenceUnit.setProperties(properties); 196 } 197 198 Property existing = getProperty(properties.getProperty2(), provider.getTableGenerationPropertyName()); 199 200 if (existing != null && tableGenerationProperty == null){ 201 properties.removeProperty2(existing); 202 } else if (existing != null && tableGenerationProperty != null){ 203 existing.setValue(tableGenerationProperty.getValue()); 204 } else if (tableGenerationProperty != null){ 205 properties.addProperty2(tableGenerationProperty); 206 } 207 208 } 209 210 221 public static void setProvider(PersistenceUnit persistenceUnit, Provider provider, 222 DatabaseConnection connection, String tableGenerationStrategy){ 223 224 Parameters.notNull("persistenceUnit", persistenceUnit); Parameters.notNull("connection", connection); Parameters.notNull("provider", provider); 228 removeProviderProperties(persistenceUnit); 229 persistenceUnit.setProvider(provider.getProviderClass()); 230 setDatabaseConnection(persistenceUnit, connection); 231 setTableGeneration(persistenceUnit, tableGenerationStrategy, provider); 232 } 233 234 235 242 public static void removeProviderProperties(PersistenceUnit persistenceUnit){ 243 Parameters.notNull("persistenceUnit", persistenceUnit); 245 Provider old = getProvider(persistenceUnit); 246 Property[] properties = getProperties(persistenceUnit); 247 248 if (old != null){ 249 for (int i = 0; i < properties.length; i++) { 250 Property each = properties[i]; 251 if (old.getPropertyNames().contains(each.getName())){ 252 persistenceUnit.getProperties().removeProperty2(each); 253 } 254 } 255 } 256 persistenceUnit.setProvider(null); 257 258 } 259 260 261 272 public static PersistenceUnit buildPersistenceUnit(String name, Provider provider, DatabaseConnection connection) { 273 274 Parameters.notNull("name", name); 275 Parameters.notNull("provider", provider); 276 Parameters.notNull("connection", connection); 277 278 PersistenceUnit persistenceUnit = new PersistenceUnit(); 279 persistenceUnit.setName(name); 280 persistenceUnit.setProvider(provider.getProviderClass()); 281 Properties properties = persistenceUnit.newProperties(); 282 Map connectionProperties = provider.getConnectionPropertiesMap(connection); 283 for (Iterator it = connectionProperties.keySet().iterator(); it.hasNext();) { 284 String propertyName = (String ) it.next(); 285 Property property = properties.newProperty(); 286 property.setName(propertyName); 287 property.setValue((String ) connectionProperties.get(propertyName)); 288 properties.addProperty2(property); 289 } 290 291 Map defaultProperties = provider.getDefaultVendorSpecificProperties(); 292 for (Iterator it = defaultProperties.keySet().iterator(); it.hasNext();) { 293 String propertyName = (String ) it.next(); 294 Property property = properties.newProperty(); 295 property.setName(propertyName); 296 property.setValue((String ) defaultProperties.get(propertyName)); 297 properties.addProperty2(property); 298 } 299 300 persistenceUnit.setProperties(properties); 301 return persistenceUnit; 302 } 303 304 305 313 public static void setDatabaseConnection(PersistenceUnit persistenceUnit, DatabaseConnection connection){ 314 315 Parameters.notNull("persistenceUnit", persistenceUnit); Parameters.notNull("connection", connection); 318 Provider provider = getProvider(persistenceUnit); 319 Property[] properties = getProperties(persistenceUnit); 320 321 Map <String , String > propertiesMap = provider.getConnectionPropertiesMap(connection); 322 323 for (String name : propertiesMap.keySet()) { 324 Property property = getProperty(properties, name); 325 if (property == null){ 326 327 if (persistenceUnit.getProperties() == null){ 328 persistenceUnit.setProperties(persistenceUnit.newProperties()); 329 } 330 331 property = persistenceUnit.getProperties().newProperty(); 332 property.setName(name); 333 persistenceUnit.getProperties().addProperty2(property); 334 } 335 336 String value = propertiesMap.get(name); 337 if (value == null){ 340 value = ""; 341 } 342 property.setValue(value); 343 } 344 } 345 346 352 static Property[] getProperties(PersistenceUnit persistenceUnit){ 353 if (persistenceUnit.getProperties() != null){ 354 return persistenceUnit.getProperties().getProperty2(); 355 } 356 return new Property[0]; 357 } 358 359 364 private static Property getProperty(Property[] properties, String propertyName){ 365 366 if (null == properties){ 367 return null; 368 } 369 370 for (int i = 0; i < properties.length; i++) { 371 Property each = properties[i]; 372 if (each.getName() != null && each.getName().equals(propertyName)){ 373 return each; 374 } 375 } 376 377 return null; 378 } 379 380 387 public static Property getProperty(PersistenceUnit persistenceUnit, String propertyName){ 388 if (persistenceUnit.getProperties() == null){ 389 return null; 390 } 391 return getProperty(persistenceUnit.getProperties().getProperty2(), propertyName); 392 } 393 394 403 public static Provider getProvider(PersistenceUnit persistenceUnit){ 404 Parameters.notNull("persistenceUnit", persistenceUnit); 406 for (Provider each : getAllProviders()){ 407 if(each.getProviderClass().equals(persistenceUnit.getProvider())){ 408 return each; 409 } 410 } 411 return DEFAULT_PROVIDER; 412 } 413 414 418 public static boolean isValid(PUDataObject puDataObject){ 419 return null == puDataObject ? false : puDataObject.parseDocument(); 420 } 421 422 431 public static PersistenceUnit[] getPersistenceUnits(PUDataObject puDataObject){ 432 if (puDataObject.getPersistence() == null){ 433 return new PersistenceUnit[0]; 434 } 435 return puDataObject.getPersistence().getPersistenceUnit(); 436 } 437 438 446 public static void renameManagedClass(PersistenceUnit persistenceUnit, String newName, 447 String oldName, PUDataObject dataObject){ 448 449 dataObject.removeClass(persistenceUnit, oldName); 450 dataObject.addClass(persistenceUnit, newName); 451 452 } 453 454 461 public static void removeManagedClass(PersistenceUnit persistenceUnit, String clazz, 462 PUDataObject dataObject){ 463 464 dataObject.removeClass(persistenceUnit, clazz); 465 } 466 467 474 public static void addManagedClass(PersistenceUnit persistenceUnit, String clazz, 475 PUDataObject dataObject){ 476 477 dataObject.addClass(persistenceUnit, clazz); 478 } 479 480 481 489 public static void addPersistenceUnit(PersistenceUnit persistenceUnit, Project project) throws InvalidPersistenceXmlException{ 490 PUDataObject pud = getPUDataObject(project); 491 pud.addPersistenceUnit(persistenceUnit); 492 pud.save(); 493 } 494 495 507 public static PUDataObject getPUDataObject(FileObject fo) throws InvalidPersistenceXmlException{ 508 Parameters.notNull("fo", fo); 510 DataObject dataObject = null; 511 try { 512 dataObject = DataObject.find(fo); 513 } catch (DataObjectNotFoundException ex) { 514 ErrorManager.getDefault().notify(ex); 515 } 516 if (!(dataObject instanceof PUDataObject)){ 517 throw new InvalidPersistenceXmlException(FileUtil.getFileDisplayName(fo)); 518 } 519 return (PUDataObject) dataObject; 520 } 521 522 535 public static PUDataObject getPUDataObject(Project project) throws InvalidPersistenceXmlException{ 536 Parameters.notNull("project", project); 538 FileObject puFileObject = getDDFile(project); 539 if (puFileObject == null) { 540 puFileObject = createPersistenceDDFile(project); 541 } 542 return getPUDataObject(puFileObject); 543 } 544 545 550 private static FileObject createPersistenceDDFile(Project project){ 551 final FileObject[] dd = new FileObject[1]; 552 try { 553 final FileObject persistenceLocation = PersistenceLocation.createLocation(project); 554 persistenceLocation.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 556 public void run() { 557 FileLock lock = null; 558 try { 559 dd[0] = FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource( 562 "org-netbeans-modules-j2ee-persistence/persistence-1.0.xml"), persistenceLocation, "persistence", "xml-jpa"); lock = dd[0].lock(); 564 dd[0].rename(lock, "persistence", "xml"); } catch (IOException ex) { 566 ErrorManager.getDefault().notify(ex); 567 } finally{ 568 if (lock != null){ 569 lock.releaseLock(); 570 } 571 } 572 } 573 }); 574 } catch (IOException ex) { 575 ErrorManager.getDefault().notify(ex); 576 } 577 return dd[0]; 578 } 579 580 592 public static boolean persistenceExists(Project project) throws InvalidPersistenceXmlException{ 593 Parameters.notNull("project", project); 595 if (getDDFile(project) == null){ 596 return false; 597 } 598 PUDataObject pud = getPUDataObject(project); 599 return pud.getPersistence().getPersistenceUnit().length > 0; 600 } 601 602 605 public static FileObject getDDFile(Project project){ 606 PersistenceScope[] persistenceScopes = PersistenceUtils.getPersistenceScopes(project); 607 for (int i = 0; i < persistenceScopes.length; i++) { 608 return persistenceScopes[i].getPersistenceXml(); 609 } 610 return null; 611 } 612 613 614 617 public static Provider[] getAllProviders() { 618 return new Provider[]{TOPLINK_PROVIDER, HIBERNATE_PROVIDER, KODO_PROVIDER}; 619 } 620 621 633 public static boolean makePortableIfPossible(Project project, PersistenceUnit persistenceUnit){ 634 635 Parameters.notNull("project", project); Parameters.notNull("persistenceUnit", persistenceUnit); 638 if (Util.isJavaSE(project)){ 639 return false; 640 } 641 642 Provider defaultProvider = getContainerManagedProvider(project); 643 644 if (defaultProvider == null){ 645 return false; 646 } 647 648 if (defaultProvider.getProviderClass().equals(persistenceUnit.getProvider()) 649 && persistenceUnit.getProperties().sizeProperty2() == 0){ 650 651 persistenceUnit.setProvider(null); 652 return true; 653 } 654 655 return false; 656 } 657 658 666 public static boolean isValidServerInstanceOrNone(Project project){ 667 Parameters.notNull("project", project); 668 ServerStatusProvider serverStatusProvider = project.getLookup().lookup(ServerStatusProvider.class); 669 if (serverStatusProvider == null) { 670 return true; 672 } 673 return serverStatusProvider.validServerInstancePresent(); 674 } 675 676 } 677 | Popular Tags |