1 19 20 package org.netbeans.modules.j2ee.persistence.action; 21 22 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategyResolver; 23 import com.sun.source.tree.ClassTree; 24 import com.sun.source.tree.CompilationUnitTree; 25 import com.sun.source.tree.Tree; 26 import java.io.IOException ; 27 import org.netbeans.api.java.source.JavaSource; 28 import org.netbeans.api.java.source.JavaSource.Phase; 29 import org.netbeans.api.java.source.TreeMaker; 30 import org.netbeans.api.java.source.WorkingCopy; 31 import org.netbeans.api.project.FileOwnerQuery; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.modules.j2ee.persistence.util.AbstractTask; 34 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionInJ2SE; 35 import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy; 36 import org.netbeans.modules.j2ee.persistence.api.PersistenceScope; 37 import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; 38 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence; 39 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit; 40 import org.openide.DialogDisplayer; 41 import org.openide.ErrorManager; 42 import org.openide.NotifyDescriptor; 43 import org.openide.filesystems.FileObject; 44 import org.openide.util.NbBundle; 45 46 53 54 public final class EntityManagerGenerator { 55 56 59 private final String fqn; 60 63 private final JavaSource targetSource; 64 67 private final FileObject targetFo; 68 69 72 private final Project project; 73 74 79 public EntityManagerGenerator(FileObject targetFo, String fqn) { 80 this.fqn = fqn; 81 this.targetFo = targetFo; 82 this.targetSource = JavaSource.forFileObject(targetFo); 83 this.project = FileOwnerQuery.getOwner(targetFo); 84 } 85 86 93 public FileObject generate(final GenerationOptions options) throws IOException { 94 95 final Class <? extends EntityManagerGenerationStrategy> strategyClass = getStrategy(); 96 97 if (strategyClass == null){ 98 NotifyDescriptor d = new NotifyDescriptor.Message( 99 NbBundle.getMessage(EntityManagerGenerator.class, "ERR_NotSupportedAMJTA"), NotifyDescriptor.INFORMATION_MESSAGE); 100 DialogDisplayer.getDefault().notify(d); 101 102 return targetFo; 103 } 104 105 AbstractTask task = new AbstractTask<WorkingCopy>() { 106 107 public void run(WorkingCopy workingCopy) throws Exception { 108 109 workingCopy.toPhase(Phase.RESOLVED); 110 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 111 TreeMaker make = workingCopy.getTreeMaker(); 112 113 for (Tree typeDeclaration : cut.getTypeDecls()){ 114 if (Tree.Kind.CLASS == typeDeclaration.getKind()){ 115 ClassTree clazz = (ClassTree) typeDeclaration; 116 EntityManagerGenerationStrategy strategy = instantiateStrategy(strategyClass, workingCopy, make, clazz, options); 117 workingCopy.rewrite(clazz, strategy.generate()); 118 } 119 } 120 } 121 }; 122 123 targetSource.runModificationTask(task).commit(); 124 125 return targetFo; 126 } 127 128 private Class <? extends EntityManagerGenerationStrategy> getStrategy(){ 129 130 EntityManagerGenerationStrategyResolver resolver = project.getLookup().lookup(EntityManagerGenerationStrategyResolver.class); 131 if (resolver != null){ 132 return resolver.resolveStrategy(targetFo); 133 } 134 135 return ApplicationManagedResourceTransactionInJ2SE.class; 137 } 138 139 private EntityManagerGenerationStrategy instantiateStrategy(Class <? extends EntityManagerGenerationStrategy> strategy, WorkingCopy workingCopy, 140 TreeMaker make, ClassTree clazz, GenerationOptions options){ 141 142 EntityManagerGenerationStrategy result = null; 143 144 try{ 145 result = strategy.newInstance(); 146 result.setClassTree(clazz); 147 result.setWorkingCopy(workingCopy); 148 result.setGenerationOptions(options); 149 result.setTreeMaker(make); 150 result.setPersistenceUnit(getPersistenceUnit()); 151 } catch (IllegalAccessException iae){ 152 throw new RuntimeException (iae); } catch (InstantiationException ie){ 154 throw new RuntimeException (ie); } 156 157 return result; 158 } 159 160 private PersistenceUnit getPersistenceUnit() { 161 PersistenceScope persistenceScope = PersistenceScope.getPersistenceScope(targetFo); 162 163 if (persistenceScope == null){ 164 return null; 165 } 166 167 try { 168 Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml()); 170 if(persistence != null){ 171 return persistence.getPersistenceUnit(0); 172 } 173 } catch (IOException ex) { 174 ErrorManager.getDefault().notify(ex); 175 } 176 return null; 177 } 178 179 } | Popular Tags |