KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarEMGenStrategyResolver


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.ejbjarproject;
21
22 import java.io.IOException JavaDoc;
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 /**
45  * An EntityManagerGenerationStrategyResolver implementation that resolves an
46  * appropriate generation strategy for classes in an EJB module.
47  *
48  * @author Erno Mononen
49  */

50 public class EjbJarEMGenStrategyResolver implements EntityManagerGenerationStrategyResolver{
51     
52     
53     /** Creates a new instance of EjbJarEMGenStrategyResolver */
54     public EjbJarEMGenStrategyResolver() {
55     }
56     
57     public Class JavaDoc<? extends EntityManagerGenerationStrategy> resolveStrategy(FileObject target) {
58         
59         Object JavaDoc j2eeModuleType = getJ2eeModuleType(target);
60         PersistenceUnit persistenceUnit = getPersistenceUnit(target);
61         
62         if (j2eeModuleType == null || !J2eeModule.EJB.equals(j2eeModuleType)) {
63             // handle only ejb module
64
return null;
65         }
66         
67         String JavaDoc jtaDataSource = persistenceUnit.getJtaDataSource();
68         String JavaDoc transactionType = persistenceUnit.getTransactionType();
69         boolean isInjectionTarget = isInjectionTarget(target);
70         boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && (transactionType != null && transactionType.equals("JTA")); //NO18N
71
boolean isJTA = (transactionType == null || transactionType.equals("JTA")); // JTA is default value for transaction type in non-J2SE projects
72

73         if (isContainerManaged && isInjectionTarget) { // Container-managed persistence context, managed class
74
return ContainerManagedJTAInjectableInEJB.class;
75         } else if (!isJTA) { // Application-managed resource-local persistence context
76
if (isInjectionTarget) { // session, MDB
77
return ApplicationManagedResourceTransactionInjectableInEJB.class;
78             } else { // other classes
79
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 JavaDoc {
93                     GenerationUtils genUtils = GenerationUtils.newInstance(parameter);
94                     result[0] = InjectionTargetQuery.isInjectionTarget(parameter, genUtils.getTypeElement());
95                 }
96             });
97         } catch (IOException JavaDoc ioe){
98             ErrorManager.getDefault().notify(ioe);
99         }
100         return result[0];
101     }
102     
103     /**
104      * @return the J2eeModule associated with the project of our target file or
105      * null if there was no associated J2eeModule.
106      */

107     protected Object JavaDoc 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             // TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file
129
Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml());
130             if(persistence != null){
131                 return persistence.getPersistenceUnit(0);
132             }
133         } catch (IOException JavaDoc ex) {
134             ErrorManager.getDefault().notify(ex);
135         }
136         return null;
137     }
138     
139     
140 }
141
Popular Tags