KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebEMGenStrategyResolver


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.web.project;
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.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 /**
42  * An implementation of EntityManagerGenerationStrategyResolver for web projects.
43  *
44  * @author Erno Mononen
45  */

46 public class WebEMGenStrategyResolver implements EntityManagerGenerationStrategyResolver{
47     
48     
49     /** Creates a new instance of WebEMGenStrategyResolver */
50     public WebEMGenStrategyResolver() {
51     }
52     
53     public Class JavaDoc<? extends EntityManagerGenerationStrategy> resolveStrategy(FileObject target) {
54         
55         PersistenceUnit persistenceUnit = getPersistenceUnit(target);
56         String JavaDoc jtaDataSource = persistenceUnit.getJtaDataSource();
57         String JavaDoc transactionType = persistenceUnit.getTransactionType();
58         boolean isInjectionTarget = isInjectionTarget(target);
59         boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && (transactionType != null && transactionType.equals("JTA")); //NO18N
60
boolean isJTA = (transactionType == null || transactionType.equals("JTA")); // JTA is default value for transaction type in non-J2SE projects
61

62         if (isContainerManaged) { // Container-managed persistence context
63
if (isInjectionTarget) { // servlet, JSF managed bean ...
64
return ContainerManagedJTAInjectableInEJB.class;
65             } else { // other classes
66
return ContainerManagedJTAInjectableInWeb .class;
67             }
68         } else if (!isJTA){ // Application-managed persistence context (Resource-transaction)
69
if (isInjectionTarget) { // servlet, JSF managed bean ...
70
return ApplicationManagedResourceTransactionInjectableInWeb.class;
71             } else { // other classes
72
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 JavaDoc {
85                     GenerationUtils genUtils = GenerationUtils.newInstance(parameter);
86                     result[0] = InjectionTargetQuery.isInjectionTarget(parameter, genUtils.getTypeElement());
87                 }
88             });
89         } catch (IOException JavaDoc 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             // TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file
105
Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml());
106             if(persistence != null){
107                 return persistence.getPersistenceUnit(0);
108             }
109         } catch (IOException JavaDoc ex) {
110             ErrorManager.getDefault().notify(ex);
111         }
112         return null;
113     }
114     
115     
116     
117 }
118
Popular Tags