Lines 40-46
Link Here
|
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
|
42 |
|
43 |
package org.netbeans.modules.j2ee.common.project; |
43 |
package org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator; |
44 |
|
44 |
|
45 |
import java.io.IOException; |
45 |
import java.io.IOException; |
46 |
import javax.lang.model.element.TypeElement; |
46 |
import javax.lang.model.element.TypeElement; |
Lines 55-161
Link Here
|
55 |
import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; |
55 |
import org.netbeans.modules.j2ee.persistence.dd.PersistenceMetadata; |
56 |
import org.netbeans.modules.j2ee.persistence.dd.common.Persistence; |
56 |
import org.netbeans.modules.j2ee.persistence.dd.common.Persistence; |
57 |
import org.netbeans.modules.j2ee.persistence.dd.common.PersistenceUnit; |
57 |
import org.netbeans.modules.j2ee.persistence.dd.common.PersistenceUnit; |
58 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionInjectableInWeb; |
|
|
59 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ApplicationManagedResourceTransactionNonInjectableInWeb; |
60 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTAInjectableInEJB; |
61 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.ContainerManagedJTANonInjectableInWeb; |
62 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategy; |
63 |
import org.netbeans.modules.j2ee.persistence.spi.entitymanagergenerator.EntityManagerGenerationStrategyResolver; |
64 |
import org.openide.filesystems.FileObject; |
58 |
import org.openide.filesystems.FileObject; |
65 |
import org.openide.util.Exceptions; |
59 |
import org.openide.util.Exceptions; |
66 |
|
60 |
|
67 |
/** |
61 |
/** |
68 |
* General implementation of EntityManagerGenerationStrategyResolver. Any project |
62 |
* Factory class providing default implementation of EntityManagerGenerationStrategyResolver. |
69 |
* type which need to use {@link EntityManagerGenerationStrategyResolver} should |
63 |
* Any project type which need to use {@link EntityManagerGenerationStrategyResolver} and |
70 |
* either put the instance to the project lookup or better register it using |
64 |
* want to reuse this default implementation should put the instance either to the project |
71 |
* {@link org.netbeans.spi.project.ProjectServiceProvider}. |
65 |
* lookup or better register it using {@link org.netbeans.spi.project.ProjectServiceProvider}. |
72 |
* |
66 |
* |
|
|
67 |
* @see EntityManagerGenerationStrategyResolver |
73 |
* @author Martin Janicek |
68 |
* @author Martin Janicek |
|
|
69 |
* |
70 |
* @since 1.39 |
74 |
*/ |
71 |
*/ |
75 |
public class EMGenStrategyResolverImpl implements EntityManagerGenerationStrategyResolver { |
72 |
public final class EntityManagerGenerationStrategyResolverFactory { |
76 |
|
73 |
|
77 |
private Project project; |
74 |
private EntityManagerGenerationStrategyResolverFactory() { |
78 |
|
|
|
79 |
|
80 |
public EMGenStrategyResolverImpl(Project project) { |
81 |
this.project = project; |
82 |
} |
75 |
} |
83 |
|
76 |
|
84 |
@Override |
77 |
/** |
85 |
public Class<? extends EntityManagerGenerationStrategy> resolveStrategy(final FileObject target) { |
78 |
* For the given project creates default implementation of the {@link EntityManagerGenerationStrategyResolver}. |
|
|
79 |
* |
80 |
* @param project project for which we need to create default implementation |
81 |
* @return default implementation |
82 |
* |
83 |
* @since 1.39 |
84 |
*/ |
85 |
public static EntityManagerGenerationStrategyResolver createInstance(Project project) { |
86 |
return new EMGenStrategyResolverImpl(project); |
87 |
} |
86 |
|
88 |
|
87 |
PersistenceUnit persistenceUnit = getPersistenceUnit(target); |
89 |
private static class EMGenStrategyResolverImpl implements EntityManagerGenerationStrategyResolver { |
88 |
String jtaDataSource = persistenceUnit.getJtaDataSource(); |
|
|
89 |
String transactionType = persistenceUnit.getTransactionType(); |
90 |
boolean isInjectionTarget = isInjectionTarget(target); |
91 |
boolean isJTA = (transactionType == null || transactionType.equals("JTA")); // JTA is default value for transaction type in non-J2SE projects |
92 |
boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && isJTA; //NO18N |
93 |
|
90 |
|
94 |
ContainerClassPathModifier modifier = project.getLookup().lookup(ContainerClassPathModifier.class); |
91 |
private Project project; |
95 |
if (modifier != null) { |
|
|
96 |
modifier.extendClasspath(target, |
97 |
new String[] { |
98 |
ContainerClassPathModifier.API_ANNOTATION, |
99 |
ContainerClassPathModifier.API_PERSISTENCE, |
100 |
ContainerClassPathModifier.API_TRANSACTION |
101 |
}); |
102 |
|
92 |
|
|
|
93 |
|
94 |
private EMGenStrategyResolverImpl(Project project) { |
95 |
this.project = project; |
103 |
} |
96 |
} |
104 |
|
97 |
|
105 |
if (isContainerManaged) { // Container-managed persistence context |
98 |
@Override |
106 |
if (isInjectionTarget) { // servlet, JSF managed bean ... |
99 |
public Class<? extends EntityManagerGenerationStrategy> resolveStrategy(final FileObject target) { |
107 |
return ContainerManagedJTAInjectableInEJB.class; |
100 |
|
108 |
} else { // other classes |
101 |
PersistenceUnit persistenceUnit = getPersistenceUnit(target); |
109 |
return ContainerManagedJTANonInjectableInWeb.class; |
102 |
String jtaDataSource = persistenceUnit.getJtaDataSource(); |
|
|
103 |
String transactionType = persistenceUnit.getTransactionType(); |
104 |
boolean isInjectionTarget = isInjectionTarget(target); |
105 |
boolean isJTA = (transactionType == null || transactionType.equals("JTA")); // JTA is default value for transaction type in non-J2SE projects |
106 |
boolean isContainerManaged = (jtaDataSource != null && !jtaDataSource.equals("")) && isJTA; //NO18N |
107 |
|
108 |
ContainerClassPathModifier modifier = project.getLookup().lookup(ContainerClassPathModifier.class); |
109 |
if (modifier != null) { |
110 |
modifier.extendClasspath(target, |
111 |
new String[] { |
112 |
ContainerClassPathModifier.API_ANNOTATION, |
113 |
ContainerClassPathModifier.API_PERSISTENCE, |
114 |
ContainerClassPathModifier.API_TRANSACTION |
115 |
}); |
116 |
|
110 |
} |
117 |
} |
111 |
} else if (!isJTA) { // Application-managed persistence context (Resource-transaction) |
118 |
|
112 |
if (isInjectionTarget) { // servlet, JSF managed bean ... |
119 |
if (isContainerManaged) { // Container-managed persistence context |
113 |
return ApplicationManagedResourceTransactionInjectableInWeb.class; |
120 |
if (isInjectionTarget) { // servlet, JSF managed bean ... |
114 |
} else { // other classes |
121 |
return ContainerManagedJTAInjectableInEJB.class; |
115 |
return ApplicationManagedResourceTransactionNonInjectableInWeb.class; |
122 |
} else { // other classes |
|
|
123 |
return ContainerManagedJTANonInjectableInWeb.class; |
124 |
} |
125 |
} else if (!isJTA) { // Application-managed persistence context (Resource-transaction) |
126 |
if (isInjectionTarget) { // servlet, JSF managed bean ... |
127 |
return ApplicationManagedResourceTransactionInjectableInWeb.class; |
128 |
} else { // other classes |
129 |
return ApplicationManagedResourceTransactionNonInjectableInWeb.class; |
130 |
} |
116 |
} |
131 |
} |
117 |
} |
|
|
118 |
|
132 |
|
119 |
return null; |
|
|
120 |
} |
121 |
|
122 |
private boolean isInjectionTarget(FileObject target) { |
123 |
final boolean[] result = new boolean[1]; |
124 |
JavaSource source = JavaSource.forFileObject(target); |
125 |
if (source == null) { |
126 |
return false; |
127 |
} |
128 |
try { |
129 |
source.runModificationTask(new Task<WorkingCopy>() { |
130 |
@Override |
131 |
public void run(WorkingCopy parameter) throws Exception { |
132 |
parameter.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); |
133 |
TypeElement typeElement = SourceUtils.getPublicTopLevelElement(parameter); |
134 |
result[0] = InjectionTargetQuery.isInjectionTarget(parameter, typeElement); |
135 |
} |
136 |
}); |
137 |
} catch (IOException ioe) { |
138 |
Exceptions.printStackTrace(ioe); |
139 |
} |
140 |
return result[0]; |
141 |
} |
142 |
|
143 |
|
144 |
private PersistenceUnit getPersistenceUnit(FileObject target) { |
145 |
PersistenceScope persistenceScope = PersistenceScope.getPersistenceScope(target); |
146 |
if (persistenceScope == null) { |
147 |
return null; |
133 |
return null; |
148 |
} |
134 |
} |
149 |
|
135 |
|
150 |
try { |
136 |
private boolean isInjectionTarget(FileObject target) { |
151 |
// TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file |
137 |
final boolean[] result = new boolean[1]; |
152 |
Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml()); |
138 |
JavaSource source = JavaSource.forFileObject(target); |
153 |
if (persistence != null) { |
139 |
if (source == null) { |
154 |
return persistence.getPersistenceUnit(0); |
140 |
return false; |
155 |
} |
141 |
} |
156 |
} catch (IOException ex) { |
142 |
try { |
157 |
Exceptions.printStackTrace(ex); |
143 |
source.runModificationTask(new Task<WorkingCopy>() { |
|
|
144 |
@Override |
145 |
public void run(WorkingCopy parameter) throws Exception { |
146 |
parameter.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); |
147 |
TypeElement typeElement = SourceUtils.getPublicTopLevelElement(parameter); |
148 |
result[0] = InjectionTargetQuery.isInjectionTarget(parameter, typeElement); |
149 |
} |
150 |
}); |
151 |
} catch (IOException ioe) { |
152 |
Exceptions.printStackTrace(ioe); |
153 |
} |
154 |
return result[0]; |
158 |
} |
155 |
} |
159 |
return null; |
156 |
|
|
|
157 |
|
158 |
private PersistenceUnit getPersistenceUnit(FileObject target) { |
159 |
PersistenceScope persistenceScope = PersistenceScope.getPersistenceScope(target); |
160 |
if (persistenceScope == null) { |
161 |
return null; |
162 |
} |
163 |
|
164 |
try { |
165 |
// TODO: fix ASAP! 1st PU is taken, needs to find the one which realy owns given file |
166 |
Persistence persistence = PersistenceMetadata.getDefault().getRoot(persistenceScope.getPersistenceXml()); |
167 |
if (persistence != null) { |
168 |
return persistence.getPersistenceUnit(0); |
169 |
} |
170 |
} catch (IOException ex) { |
171 |
Exceptions.printStackTrace(ex); |
172 |
} |
173 |
return null; |
174 |
} |
160 |
} |
175 |
} |
161 |
} |
176 |
} |