001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.project;
043:
044: import java.io.IOException;
045: import javax.lang.model.element.TypeElement;
046: import org.netbeans.api.java.source.JavaSource;
047: import org.netbeans.api.java.source.Task;
048: import org.netbeans.api.java.source.WorkingCopy;
049: import org.netbeans.modules.j2ee.common.queries.api.InjectionTargetQuery;
050: import org.netbeans.modules.j2ee.core.api.support.java.SourceUtils;
051: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionInjectableInWeb;
052: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionNonInjectableInWeb;
053: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTAInjectableInWeb;
054: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy;
055: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategyResolver;
056: import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
057: import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata;
058: import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence;
059: import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
060: import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTANonInjectableInWeb;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.Exceptions;
063:
064: /**
065: * An implementation of EntityManagerGenerationStrategyResolver for web projects.
066: *
067: * @author Erno Mononen
068: */
069: public class WebEMGenStrategyResolver implements
070: EntityManagerGenerationStrategyResolver {
071:
072: /** Creates a new instance of WebEMGenStrategyResolver */
073: public WebEMGenStrategyResolver() {
074: }
075:
076: public Class<? extends EntityManagerGenerationStrategy> resolveStrategy(
077: FileObject target) {
078:
079: PersistenceUnit persistenceUnit = getPersistenceUnit(target);
080: String jtaDataSource = persistenceUnit.getJtaDataSource();
081: String transactionType = persistenceUnit.getTransactionType();
082: boolean isInjectionTarget = isInjectionTarget(target);
083: boolean isJTA = (transactionType == null || transactionType
084: .equals("JTA")); // JTA is default value for transaction type in non-J2SE projects
085: boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource
086: .equals(""))
087: && isJTA; //NO18N
088:
089: if (isContainerManaged) { // Container-managed persistence context
090: if (isInjectionTarget) { // servlet, JSF managed bean ...
091: return ContainerManagedJTAInjectableInWeb.class;
092: } else { // other classes
093: return ContainerManagedJTANonInjectableInWeb.class;
094: }
095: } else if (!isJTA) { // Application-managed persistence context (Resource-transaction)
096: if (isInjectionTarget) { // servlet, JSF managed bean ...
097: return ApplicationManagedResourceTransactionInjectableInWeb.class;
098: } else { // other classes
099: return ApplicationManagedResourceTransactionNonInjectableInWeb.class;
100: }
101: }
102:
103: return null;
104: }
105:
106: private boolean isInjectionTarget(FileObject target) {
107: final boolean[] result = new boolean[1];
108: JavaSource source = JavaSource.forFileObject(target);
109: try {
110: source.runModificationTask(new Task<WorkingCopy>() {
111: public void run(WorkingCopy parameter) throws Exception {
112: parameter
113: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
114: TypeElement typeElement = SourceUtils
115: .getPublicTopLevelElement(parameter);
116: result[0] = InjectionTargetQuery.isInjectionTarget(
117: parameter, typeElement);
118: }
119: });
120: } catch (IOException ioe) {
121: Exceptions.printStackTrace(ioe);
122: }
123: return result[0];
124: }
125:
126: private PersistenceUnit getPersistenceUnit(FileObject target) {
127:
128: PersistenceScope persistenceScope = PersistenceScope
129: .getPersistenceScope(target);
130: if (persistenceScope == null) {
131: return null;
132: }
133:
134: try {
135: // TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file
136: Persistence persistence = PersistenceMetadata.getDefault()
137: .getRoot(persistenceScope.getPersistenceXml());
138: if (persistence != null) {
139: return persistence.getPersistenceUnit(0);
140: }
141: } catch (IOException ex) {
142: Exceptions.printStackTrace(ex);
143: }
144: return null;
145: }
146:
147: }
|