-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
SessionBeanComponentDescriptionFactory.java
292 lines (263 loc) · 15.4 KB
/
SessionBeanComponentDescriptionFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.ejb3.deployment.processors;
import static org.jboss.as.ejb3.deployment.processors.AnnotatedEJBComponentDescriptionDeploymentUnitProcessor.getEjbJarDescription;
import java.lang.reflect.Modifier;
import java.util.List;
import javax.ejb.SessionBean;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import org.jboss.as.ee.metadata.MetadataCompleteMarker;
import org.jboss.as.ee.structure.EJBAnnotationPropertyReplacement;
import org.jboss.as.ejb3.component.session.SessionBeanComponentDescription;
import org.jboss.as.ejb3.component.singleton.SingletonComponentDescription;
import org.jboss.as.ejb3.component.stateful.StatefulComponentDescription;
import org.jboss.as.ejb3.component.stateless.StatelessComponentDescription;
import org.jboss.as.ejb3.deployment.EjbJarDescription;
import org.jboss.as.ejb3.logging.EjbLogger;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.EjbDeploymentMarker;
import org.jboss.as.server.deployment.annotation.CompositeIndex;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.metadata.ejb.spec.EjbType;
import org.jboss.metadata.ejb.spec.EnterpriseBeanMetaData;
import org.jboss.metadata.ejb.spec.GenericBeanMetaData;
import org.jboss.metadata.ejb.spec.SessionBean32MetaData;
import org.jboss.metadata.ejb.spec.SessionBeanMetaData;
import org.jboss.metadata.ejb.spec.SessionType;
import org.jboss.metadata.property.PropertyReplacer;
import org.jboss.msc.service.ServiceName;
/**
* User: jpai
*/
public class SessionBeanComponentDescriptionFactory extends EJBComponentDescriptionFactory {
private static final DotName STATELESS_ANNOTATION = DotName.createSimple(Stateless.class.getName());
private static final DotName STATEFUL_ANNOTATION = DotName.createSimple(Stateful.class.getName());
private static final DotName SINGLETON_ANNOTATION = DotName.createSimple(Singleton.class.getName());
private static final DotName SESSION_BEAN_INTERFACE = DotName.createSimple(SessionBean.class.getName());
private final boolean defaultSlsbPoolAvailable;
public SessionBeanComponentDescriptionFactory(final boolean appclient, final boolean defaultSlsbPoolAvailable) {
super(appclient);
this.defaultSlsbPoolAvailable = defaultSlsbPoolAvailable;
}
/**
* Process annotations and merge any available metadata at the same time.
*/
@Override
protected void processAnnotations(final DeploymentUnit deploymentUnit, final CompositeIndex compositeIndex) throws DeploymentUnitProcessingException {
if (MetadataCompleteMarker.isMetadataComplete(deploymentUnit)) {
return;
}
// Find and process any @Stateless bean annotations
final List<AnnotationInstance> slsbAnnotations = compositeIndex.getAnnotations(STATELESS_ANNOTATION);
if (!slsbAnnotations.isEmpty()) {
processSessionBeans(deploymentUnit, slsbAnnotations, SessionBeanComponentDescription.SessionBeanType.STATELESS);
}
// Find and process any @Stateful bean annotations
final List<AnnotationInstance> sfsbAnnotations = compositeIndex.getAnnotations(STATEFUL_ANNOTATION);
if (!sfsbAnnotations.isEmpty()) {
processSessionBeans(deploymentUnit, sfsbAnnotations, SessionBeanComponentDescription.SessionBeanType.STATEFUL);
}
// Find and process any @Singleton bean annotations
final List<AnnotationInstance> sbAnnotations = compositeIndex.getAnnotations(SINGLETON_ANNOTATION);
if (!sbAnnotations.isEmpty()) {
processSessionBeans(deploymentUnit, sbAnnotations, SessionBeanComponentDescription.SessionBeanType.SINGLETON);
}
}
@Override
protected void processBeanMetaData(final DeploymentUnit deploymentUnit, final EnterpriseBeanMetaData enterpriseBeanMetaData) throws DeploymentUnitProcessingException {
if (enterpriseBeanMetaData.isSession()) {
assert enterpriseBeanMetaData instanceof SessionBeanMetaData : enterpriseBeanMetaData + " is not a SessionBeanMetaData";
processSessionBeanMetaData(deploymentUnit, (SessionBeanMetaData) enterpriseBeanMetaData);
}
}
private void processSessionBeans(final DeploymentUnit deploymentUnit, final List<AnnotationInstance> sessionBeanAnnotations, final SessionBeanComponentDescription.SessionBeanType annotatedSessionBeanType) {
final EjbJarDescription ejbJarDescription = getEjbJarDescription(deploymentUnit);
final ServiceName deploymentUnitServiceName = deploymentUnit.getServiceName();
PropertyReplacer propertyReplacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
// process these session bean annotations and create component descriptions out of it
for (final AnnotationInstance sessionBeanAnnotation : sessionBeanAnnotations) {
final AnnotationTarget target = sessionBeanAnnotation.target();
if (!(target instanceof ClassInfo)) {
// Let's just WARN and move on. No need to throw an error
EjbLogger.DEPLOYMENT_LOGGER.warn(EjbLogger.ROOT_LOGGER.annotationOnlyAllowedOnClass(sessionBeanAnnotation.name().toString(), target).getMessage());
continue;
}
final ClassInfo sessionBeanClassInfo = (ClassInfo) target;
// skip if it's not a valid class for session bean
if (!assertSessionBeanClassValidity(sessionBeanClassInfo)) {
continue;
}
final String ejbName = sessionBeanClassInfo.name().local();
final AnnotationValue nameValue = sessionBeanAnnotation.value("name");
final String beanName = (nameValue == null || nameValue.asString().isEmpty()) ? ejbName : propertyReplacer.replaceProperties(nameValue.asString());
final SessionBeanMetaData beanMetaData = getEnterpriseBeanMetaData(deploymentUnit, beanName, SessionBeanMetaData.class);
final SessionBeanComponentDescription.SessionBeanType sessionBeanType;
final String beanClassName;
if (beanMetaData != null) {
beanClassName = override(sessionBeanClassInfo.name().toString(), beanMetaData.getEjbClass());
sessionBeanType = override(annotatedSessionBeanType, descriptionOf(((SessionBeanMetaData) beanMetaData).getSessionType()));
} else {
beanClassName = sessionBeanClassInfo.name().toString();
sessionBeanType = annotatedSessionBeanType;
}
final SessionBeanComponentDescription sessionBeanDescription;
switch (sessionBeanType) {
case STATELESS:
sessionBeanDescription = new StatelessComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData, defaultSlsbPoolAvailable);
break;
case STATEFUL:
sessionBeanDescription = new StatefulComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData);
// If passivation is disabled for the SFSB, either via annotation or via DD, then setup the component
// description appropriately
final boolean passivationCapableAnnotationValue = sessionBeanAnnotation.value("passivationCapable") == null ? true : sessionBeanAnnotation.value("passivationCapable").asBoolean();
final Boolean passivationCapableDeploymentDescriptorValue;
if ((beanMetaData instanceof SessionBean32MetaData)) {
passivationCapableDeploymentDescriptorValue = ((SessionBean32MetaData) beanMetaData).isPassivationCapable();
} else {
passivationCapableDeploymentDescriptorValue = null;
}
final boolean passivationApplicable = override(passivationCapableDeploymentDescriptorValue, passivationCapableAnnotationValue);
((StatefulComponentDescription) sessionBeanDescription).setPassivationApplicable(passivationApplicable);
break;
case SINGLETON:
if (sessionBeanClassInfo.interfaceNames().contains(SESSION_BEAN_INTERFACE)) {
EjbLogger.ROOT_LOGGER.singletonCantImplementSessionBean(beanClassName);
}
sessionBeanDescription = new SingletonComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, beanMetaData);
break;
default:
throw EjbLogger.ROOT_LOGGER.unknownSessionBeanType(sessionBeanType.name());
}
addComponent(deploymentUnit, sessionBeanDescription);
final AnnotationValue mappedNameValue = sessionBeanAnnotation.value("mappedName");
if (mappedNameValue != null && !mappedNameValue.asString().isEmpty()) {
EjbLogger.ROOT_LOGGER.mappedNameNotSupported(mappedNameValue != null ? mappedNameValue.asString() : "",
ejbName);
}
}
EjbDeploymentMarker.mark(deploymentUnit);
}
private static SessionBeanComponentDescription.SessionBeanType descriptionOf(final SessionType sessionType) {
if (sessionType == null)
return null;
switch (sessionType) {
case Stateless:
return SessionBeanComponentDescription.SessionBeanType.STATELESS;
case Stateful:
return SessionBeanComponentDescription.SessionBeanType.STATEFUL;
case Singleton:
return SessionBeanComponentDescription.SessionBeanType.SINGLETON;
default:
throw EjbLogger.ROOT_LOGGER.unknownSessionBeanType(sessionType.name());
}
}
/**
* Returns true if the passed <code>sessionBeanClass</code> meets the requirements set by the EJB3 spec about
* bean implementation classes. The passed <code>sessionBeanClass</code> must not be an interface and must be public
* and not final and not abstract. If it passes these requirements then this method returns true. Else it returns false.
*
* @param sessionBeanClass The session bean class
* @return
*/
private static boolean assertSessionBeanClassValidity(final ClassInfo sessionBeanClass) {
final short flags = sessionBeanClass.flags();
final String className = sessionBeanClass.name().toString();
// must *not* be an interface
if (Modifier.isInterface(flags)) {
EjbLogger.DEPLOYMENT_LOGGER.sessionBeanClassCannotBeAnInterface(className);
return false;
}
// bean class must be public, must *not* be abstract or final
if (!Modifier.isPublic(flags) || Modifier.isAbstract(flags) || Modifier.isFinal(flags)) {
EjbLogger.DEPLOYMENT_LOGGER.sessionBeanClassMustBePublicNonAbstractNonFinal(className);
return false;
}
// valid class
return true;
}
private void processSessionBeanMetaData(final DeploymentUnit deploymentUnit, final SessionBeanMetaData sessionBean) throws DeploymentUnitProcessingException {
final EjbJarDescription ejbJarDescription = getEjbJarDescription(deploymentUnit);
final CompositeIndex compositeIndex = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
final String beanName = sessionBean.getName();
SessionType sessionType = sessionBean.getSessionType();
if (sessionType == null && sessionBean instanceof GenericBeanMetaData) {
final GenericBeanMetaData bean = (GenericBeanMetaData) sessionBean;
if (bean.getEjbType() == EjbType.SESSION) {
sessionType = determineSessionType(sessionBean.getEjbClass(), compositeIndex);
if (sessionType == null) {
throw EjbLogger.ROOT_LOGGER.sessionTypeNotSpecified(beanName);
}
} else {
//it is not a session bean, so we ignore it
return;
}
} else if (sessionType == null) {
sessionType = determineSessionType(sessionBean.getEjbClass(), compositeIndex);
if (sessionType == null) {
throw EjbLogger.ROOT_LOGGER.sessionTypeNotSpecified(beanName);
}
}
final String beanClassName = sessionBean.getEjbClass();
final SessionBeanComponentDescription sessionBeanDescription;
switch (sessionType) {
case Stateless:
sessionBeanDescription = new StatelessComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, sessionBean, defaultSlsbPoolAvailable);
break;
case Stateful:
sessionBeanDescription = new StatefulComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, sessionBean);
if (sessionBean instanceof SessionBean32MetaData && ((SessionBean32MetaData) sessionBean).isPassivationCapable() != null) {
((StatefulComponentDescription) sessionBeanDescription).setPassivationApplicable(((SessionBean32MetaData) sessionBean).isPassivationCapable());
}
break;
case Singleton:
sessionBeanDescription = new SingletonComponentDescription(beanName, beanClassName, ejbJarDescription, deploymentUnit, sessionBean);
break;
default:
throw EjbLogger.ROOT_LOGGER.unknownSessionBeanType(sessionType.name());
}
addComponent(deploymentUnit, sessionBeanDescription);
}
private SessionType determineSessionType(final String ejbClass, final CompositeIndex compositeIndex) {
if(ejbClass == null) {
return null;
}
final ClassInfo info = compositeIndex.getClassByName(DotName.createSimple(ejbClass));
if (info == null) {
return null;
}
if(info.annotationsMap().get(STATEFUL_ANNOTATION) != null) {
return SessionType.Stateful;
} else if(info.annotationsMap().get(STATELESS_ANNOTATION) != null) {
return SessionType.Stateless;
} else if(info.annotationsMap().get(SINGLETON_ANNOTATION) != null) {
return SessionType.Singleton;
}
return null;
}
}