blob: 4fcf0f41260e389b51c9b640dbdc6b99d39aaa98 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator;
import java.text.MessageFormat;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.TypeParameterTree;
import java.util.Collections;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeKind;
import org.netbeans.modules.j2ee.persistence.action.GenerationOptions.*;
/**
* Generates the code needed for invoking an <code>EntityManager</code> in J2EE 1.4
* web components with an application-managed persistence unit.
*
* @author Erno Mononen
*/
public final class ApplicationManagedResourceTransactionNonInjectableInWeb extends EntityManagerGenerationStrategySupport {
public ClassTree generate(){
ClassTree modifiedClazz = getClassTree();
String body = "";
FieldInfo em = getEntityManagerFieldInfo();
if (!em.isExisting()){
FieldInfo emf = getEntityManagerFactoryFieldInfo();
if (!emf.isExisting()){
modifiedClazz = getTreeMaker().insertClassMember(getClassTree(), getIndexForField(getClassTree()), createEntityManagerFactory(emf.getName()));
}
body += getEmInitCode(em, emf);
}
body += getMethodBody(em);
ModifiersTree methodModifiers = getTreeMaker().Modifiers(
getGenerationOptions().getModifiers(),
Collections.<AnnotationTree>emptyList()
);
MethodTree newMethod = getTreeMaker().Method(
methodModifiers,
computeMethodName(),
getTreeMaker().PrimitiveType(TypeKind.VOID),
Collections.<TypeParameterTree>emptyList(),
getParameterList(),
Collections.<ExpressionTree>emptyList(),
"{ " + body + "}",
null
);
return getTreeMaker().addClassMember(modifiedClazz, importFQNs(newMethod));
}
private String getMethodBody(FieldInfo em){
String text =
"try '{'\n" +
" {0}.getTransaction().begin();\n" +
generateCallLines(em.getName()) +
" {0}.getTransaction().commit();\n" +
"} catch(Exception e) '{'\n" +
" java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,\"exception caught\", e);\n" +
" {0}.getTransaction().rollback();\n" +
"} finally '{'\n" +
" {0}.close();\n" +
"}";
return MessageFormat.format(text, em.getName());
}
}