1 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 ; 25 import java.text.MessageFormat ; 26 import java.util.Collections ; 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 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 serviceLocator) throws IOException { 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 className = elementHandle.getQualifiedName(); 62 if (Utils.isJavaEE5orHigher(project) && serviceLocatorStrategy == null && 63 InjectionTargetQuery.isInjectionTarget(fileObject, className)) { 64 boolean isStatic = InjectionTargetQuery.isStaticReferenceRequired(fileObject, className); 65 String jndiName = datasource.getJndiName(); 66 String 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 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 generateJNDILookup(String jndiName, EnterpriseReferenceContainer enterpriseReferenceContainer, 82 FileObject fileObject, String className, String nodeName, boolean createServerResources) throws IOException { 83 String 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 .class.getName()); 93 result = enterpriseReferenceContainer.addResourceRef(ref, fileObject, className); 94 } 95 return result; 96 } 97 98 private void generateLookupMethod(FileObject fileObject, final String className, final String jndiName, 99 final ServiceLocatorStrategy slStrategy) throws IOException { 100 JavaSource javaSource = JavaSource.forFileObject(fileObject); 101 final String body = slStrategy == null ? getLookupCode(jndiName) : getLookupCode(jndiName, slStrategy, fileObject, className); 102 javaSource.runModificationTask(new AbstractTask<WorkingCopy>() { 103 public void run(WorkingCopy workingCopy) throws IOException { 104 workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 105 TypeElement typeElement = workingCopy.getElements().getTypeElement(className); 106 String methodName = "get" + Utils.jndiNameToCamelCase(jndiName, false, null); MethodModel methodModel = MethodModel.create( 108 methodName, 109 javax.sql.DataSource .class.getName(), 110 body, 111 Collections.<MethodModel.Variable>emptyList(), 112 Collections.singletonList(javax.naming.NamingException .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 getLookupCode(String jndiName, ServiceLocatorStrategy serviceLocatorStrategy, FileObject fileObject, String className) { 124 String jdbcLookupString = serviceLocatorStrategy.genDataSource(jndiName, fileObject, className); 125 return "return (javax.sql.DataSource) " + jdbcLookupString + ";\n"; } 127 128 private String getLookupCode(String jndiName) { 129 return MessageFormat.format( 130 "javax.naming.Context c = new javax.naming.InitialContext();\n" + "return (javax.sql.DataSource) c.lookup(\"java:comp/env/{0}\");\n", new Object [] { jndiName }); 133 } 134 135 } 136 | Popular Tags |