1 19 20 package org.netbeans.modules.web.project; 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.modules.j2ee.common.queries.api.InjectionTargetQuery; 26 import org.netbeans.modules.j2ee.common.source.AbstractTask; 27 import org.netbeans.modules.j2ee.common.source.GenerationUtils; 28 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionInjectableInWeb; 29 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionNonInjectableInWeb; 30 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTAInjectableInEJB; 31 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTAInjectableInWeb; 32 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy; 33 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategyResolver; 34 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 35 import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; 36 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence; 37 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 38 import org.openide.ErrorManager; 39 import org.openide.filesystems.FileObject; 40 41 46 public class WebEMGenStrategyResolver implements EntityManagerGenerationStrategyResolver{ 47 48 49 50 public WebEMGenStrategyResolver() { 51 } 52 53 public Class <? extends EntityManagerGenerationStrategy> resolveStrategy(FileObject target) { 54 55 PersistenceUnit persistenceUnit = getPersistenceUnit(target); 56 String jtaDataSource = persistenceUnit.getJtaDataSource(); 57 String transactionType = persistenceUnit.getTransactionType(); 58 boolean isInjectionTarget = isInjectionTarget(target); 59 boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && (transactionType != null && transactionType.equals("JTA")); boolean isJTA = (transactionType == null || transactionType.equals("JTA")); 62 if (isContainerManaged) { if (isInjectionTarget) { return ContainerManagedJTAInjectableInEJB.class; 65 } else { return ContainerManagedJTAInjectableInWeb .class; 67 } 68 } else if (!isJTA){ if (isInjectionTarget) { return ApplicationManagedResourceTransactionInjectableInWeb.class; 71 } else { return ApplicationManagedResourceTransactionNonInjectableInWeb.class; 73 } 74 } 75 76 return null; 77 } 78 79 private boolean isInjectionTarget(FileObject target) { 80 final boolean[] result = new boolean[1]; 81 JavaSource source = JavaSource.forFileObject(target); 82 try{ 83 source.runModificationTask(new AbstractTask<WorkingCopy>(){ 84 public void run(WorkingCopy parameter) throws Exception { 85 GenerationUtils genUtils = GenerationUtils.newInstance(parameter); 86 result[0] = InjectionTargetQuery.isInjectionTarget(parameter, genUtils.getTypeElement()); 87 } 88 }); 89 } catch (IOException ioe){ 90 ErrorManager.getDefault().notify(ioe); 91 } 92 return result[0]; 93 } 94 95 96 private PersistenceUnit getPersistenceUnit(FileObject target) { 97 98 PersistenceScope persistenceScope = PersistenceScope.getPersistenceScope(target); 99 if (persistenceScope == null){ 100 return null; 101 } 102 103 try { 104 Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml()); 106 if(persistence != null){ 107 return persistence.getPersistenceUnit(0); 108 } 109 } catch (IOException ex) { 110 ErrorManager.getDefault().notify(ex); 111 } 112 return null; 113 } 114 115 116 117 } 118 | Popular Tags |