KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > action > EntityManagerGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
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 /**
47  * Generates appropriate code for retrieving and invoking <code>javax.persistence.EntityManager</code>.
48  * The generated code depends on the target class' enviroment.
49  *
50  * TODO: move this class to different package if anybody else wants to use it
51  * @author Martin Adamek, Erno Mononen
52  */

53
54 public final class EntityManagerGenerator {
55     
56     /**
57      * The fully qualified name of the target class.
58      */

59     private final String JavaDoc fqn;
60     /**
61      * The target java source file.
62      */

63     private final JavaSource targetSource;
64     /**
65      * The file object of the target source file.
66      */

67     private final FileObject targetFo;
68     
69     /**
70      * The project to which the target file belongs.
71      */

72     private final Project project;
73     
74     /**
75      * Creates a new EntityManagerGenerator.
76      * @param targetFo the file object of the target java source file.
77      * @param fqn the fully qualified name of the target java class.
78      */

79     public EntityManagerGenerator(FileObject targetFo, String JavaDoc fqn) {
80         this.fqn = fqn;
81         this.targetFo = targetFo;
82         this.targetSource = JavaSource.forFileObject(targetFo);
83         this.project = FileOwnerQuery.getOwner(targetFo);
84     }
85     
86     /**
87      * Generates the code needed for retrieving and invoking
88      * <code>javax.persistence.EntityManager</code>. The generated code depends
89      * on the target class' environment.
90      * @param options the options for the generation.
91      * @return the modified file object of the target java class.
92      */

93     public FileObject generate(final GenerationOptions options) throws IOException JavaDoc{
94         
95         final Class JavaDoc<? 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 JavaDoc {
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 JavaDoc<? extends EntityManagerGenerationStrategy> getStrategy(){
129
130         EntityManagerGenerationStrategyResolver resolver = project.getLookup().lookup(EntityManagerGenerationStrategyResolver.class);
131         if (resolver != null){
132             return resolver.resolveStrategy(targetFo);
133         }
134         
135         // must be a java se project (we don't want it to implement the EntityManagerGenerationStrategyResolver SPI)
136
return ApplicationManagedResourceTransactionInJ2SE.class;
137     }
138     
139     private EntityManagerGenerationStrategy instantiateStrategy(Class JavaDoc<? 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 JavaDoc iae){
152             throw new RuntimeException JavaDoc(iae); //TODO
153
} catch (InstantiationException JavaDoc ie){
154             throw new RuntimeException JavaDoc(ie); //TODO
155
}
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             // TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file
169
Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml());
170             if(persistence != null){
171                 return persistence.getPersistenceUnit(0);
172             }
173         } catch (IOException JavaDoc ex) {
174             ErrorManager.getDefault().notify(ex);
175         }
176         return null;
177     }
178     
179 }
Popular Tags