1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 21 22 import java.io.IOException ; 23 import org.netbeans.api.java.source.JavaSource; 24 import org.netbeans.api.java.source.WorkingCopy; 25 import org.netbeans.api.project.FileOwnerQuery; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery; 28 import org.netbeans.modules.j2ee.common.source.AbstractTask; 29 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 30 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 31 import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; 32 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence; 33 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 34 import org.openide.ErrorManager; 35 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 36 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; 37 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionInjectableInEJB; 38 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionNonInjectableInEJB; 39 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTAInjectableInEJB; 40 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy; 41 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategyResolver; 42 import org.openide.filesystems.FileObject; 43 44 50 public class EjbJarEMGenStrategyResolver implements EntityManagerGenerationStrategyResolver{ 51 52 53 54 public EjbJarEMGenStrategyResolver() { 55 } 56 57 public Class <? extends EntityManagerGenerationStrategy> resolveStrategy(FileObject target) { 58 59 Object j2eeModuleType = getJ2eeModuleType(target); 60 PersistenceUnit persistenceUnit = getPersistenceUnit(target); 61 62 if (j2eeModuleType == null || !J2eeModule.EJB.equals(j2eeModuleType)) { 63 return null; 65 } 66 67 String jtaDataSource = persistenceUnit.getJtaDataSource(); 68 String transactionType = persistenceUnit.getTransactionType(); 69 boolean isInjectionTarget = isInjectionTarget(target); 70 boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && (transactionType != null && transactionType.equals("JTA")); boolean isJTA = (transactionType == null || transactionType.equals("JTA")); 73 if (isContainerManaged && isInjectionTarget) { return ContainerManagedJTAInjectableInEJB.class; 75 } else if (!isJTA) { if (isInjectionTarget) { return ApplicationManagedResourceTransactionInjectableInEJB.class; 78 } else { return ApplicationManagedResourceTransactionNonInjectableInEJB.class; 80 } 81 } 82 83 return null; 84 85 } 86 87 private boolean isInjectionTarget(FileObject target) { 88 final boolean[] result = new boolean[1]; 89 JavaSource source = JavaSource.forFileObject(target); 90 try{ 91 source.runModificationTask(new AbstractTask<WorkingCopy>(){ 92 public void run(WorkingCopy parameter) throws Exception { 93 GenerationUtils genUtils = GenerationUtils.newInstance(parameter); 94 result[0] = InjectionTargetQuery.isInjectionTarget(parameter, genUtils.getTypeElement()); 95 } 96 }); 97 } catch (IOException ioe){ 98 ErrorManager.getDefault().notify(ioe); 99 } 100 return result[0]; 101 } 102 103 107 protected Object getJ2eeModuleType(FileObject target){ 108 J2eeModule result = null; 109 Project project = FileOwnerQuery.getOwner(target); 110 J2eeModuleProvider j2eeModuleProvider = null; 111 if (project != null){ 112 j2eeModuleProvider = (J2eeModuleProvider) project.getLookup().lookup(J2eeModuleProvider.class); 113 } 114 if (j2eeModuleProvider != null) { 115 result = j2eeModuleProvider.getJ2eeModule(); 116 } 117 return result != null ? result.getModuleType() : null; 118 119 } 120 121 private PersistenceUnit getPersistenceUnit(FileObject target) { 122 PersistenceScope persistenceScope = PersistenceScope.getPersistenceScope(target); 123 if (persistenceScope == null){ 124 return null; 125 } 126 127 try { 128 Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml()); 130 if(persistence != null){ 131 return persistence.getPersistenceUnit(0); 132 } 133 } catch (IOException ex) { 134 ErrorManager.getDefault().notify(ex); 135 } 136 return null; 137 } 138 139 140 } 141 | Popular Tags |