KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > action > UseDatabaseGenerator


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.ejbcore.action;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.MethodTree;
24 import java.io.IOException JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.Collections JavaDoc;
27 import javax.lang.model.element.Modifier;
28 import javax.lang.model.element.TypeElement;
29 import org.netbeans.api.java.source.ElementHandle;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.api.java.source.WorkingCopy;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer;
35 import org.netbeans.modules.j2ee.common.method.MethodModel;
36 import org.netbeans.modules.j2ee.common.method.MethodModelSupport;
37 import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery;
38 import org.netbeans.modules.j2ee.common.source.AbstractTask;
39 import org.netbeans.modules.j2ee.dd.api.common.ResourceRef;
40 import org.netbeans.modules.j2ee.deployment.common.api.Datasource;
41 import org.netbeans.modules.j2ee.ejbcore.Utils;
42 import org.netbeans.modules.j2ee.ejbcore._RetoucheUtil;
43 import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres.ServiceLocatorStrategy;
44 import org.openide.filesystems.FileObject;
45
46 /**
47  *
48  * @author Martin Adamek
49  */

50 public final class UseDatabaseGenerator {
51     
52     public UseDatabaseGenerator() {
53     }
54
55     public void generate(final FileObject fileObject, final ElementHandle<TypeElement> elementHandle, final Datasource datasource,
56             final boolean createServerResources, String JavaDoc serviceLocator) throws IOException JavaDoc {
57         Project project = FileOwnerQuery.getOwner(fileObject);
58         ServiceLocatorStrategy serviceLocatorStrategy = (serviceLocator == null) ? null :
59             ServiceLocatorStrategy.create(project, fileObject, serviceLocator);
60         EnterpriseReferenceContainer erc = project.getLookup().lookup(EnterpriseReferenceContainer.class);
61         String JavaDoc className = elementHandle.getQualifiedName();
62         if (Utils.isJavaEE5orHigher(project) && serviceLocatorStrategy == null &&
63                 InjectionTargetQuery.isInjectionTarget(fileObject, className)) {
64             boolean isStatic = InjectionTargetQuery.isStaticReferenceRequired(fileObject, className);
65             String JavaDoc jndiName = datasource.getJndiName();
66             String JavaDoc fieldName = Utils.jndiNameToCamelCase(jndiName, true, null);
67             _RetoucheUtil.generateAnnotatedField(fileObject, className, "javax.annotation.Resource", fieldName,
68                     "javax.sql.DataSource", Collections.singletonMap("name", jndiName), isStatic);
69         } else {
70             String JavaDoc jndiName = generateJNDILookup(datasource.getJndiName(), erc,
71                     fileObject, className, datasource.getUrl(), createServerResources);
72             if (jndiName != null) {
73                 generateLookupMethod(fileObject, className, jndiName, serviceLocatorStrategy);
74             }
75         }
76         if (serviceLocator != null) {
77             erc.setServiceLocatorName(serviceLocator);
78         }
79     }
80     
81     private String JavaDoc generateJNDILookup(String JavaDoc jndiName, EnterpriseReferenceContainer enterpriseReferenceContainer,
82             FileObject fileObject, String JavaDoc className, String JavaDoc nodeName, boolean createServerResources) throws IOException JavaDoc {
83         String JavaDoc result = null;
84         ResourceRef ref = enterpriseReferenceContainer.createResourceRef(className);
85         if (ref != null) {
86             if (createServerResources) {
87                 ref.setDescription(nodeName);
88             }
89             ref.setResRefName(jndiName);
90             ref.setResAuth(org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_AUTH_CONTAINER);
91             ref.setResSharingScope(org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_SHARING_SCOPE_SHAREABLE);
92             ref.setResType(javax.sql.DataSource JavaDoc.class.getName());
93             result = enterpriseReferenceContainer.addResourceRef(ref, fileObject, className);
94         }
95         return result;
96     }
97     
98     private void generateLookupMethod(FileObject fileObject, final String JavaDoc className, final String JavaDoc jndiName,
99             final ServiceLocatorStrategy slStrategy) throws IOException JavaDoc {
100         JavaSource javaSource = JavaSource.forFileObject(fileObject);
101         final String JavaDoc body = slStrategy == null ? getLookupCode(jndiName) : getLookupCode(jndiName, slStrategy, fileObject, className);
102         javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
103             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
104                 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
105                 TypeElement typeElement = workingCopy.getElements().getTypeElement(className);
106                 String JavaDoc methodName = "get" + Utils.jndiNameToCamelCase(jndiName, false, null); //NO18N
107
MethodModel methodModel = MethodModel.create(
108                         methodName,
109                         javax.sql.DataSource JavaDoc.class.getName(),
110                         body,
111                         Collections.<MethodModel.Variable>emptyList(),
112                         Collections.singletonList(javax.naming.NamingException JavaDoc.class.getName()),
113                         Collections.singleton(Modifier.PRIVATE)
114                         );
115                 MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel);
116                 ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
117                 ClassTree modifiedClassTree = workingCopy.getTreeMaker().addClassMember(classTree, methodTree);
118                 workingCopy.rewrite(classTree, modifiedClassTree);
119             }
120         }).commit();
121     }
122     
123     private String JavaDoc getLookupCode(String JavaDoc jndiName, ServiceLocatorStrategy serviceLocatorStrategy, FileObject fileObject, String JavaDoc className) {
124         String JavaDoc jdbcLookupString = serviceLocatorStrategy.genDataSource(jndiName, fileObject, className);
125         return "return (javax.sql.DataSource) " + jdbcLookupString + ";\n"; // NOI18N
126
}
127     
128     private String JavaDoc getLookupCode(String JavaDoc jndiName) {
129         return MessageFormat.format(
130                 "javax.naming.Context c = new javax.naming.InitialContext();\n" + // NOI18N
131
"return (javax.sql.DataSource) c.lookup(\"java:comp/env/{0}\");\n", // NOI18N
132
new Object JavaDoc[] { jndiName });
133     }
134     
135 }
136
Popular Tags