/
EJBComponentCreateService.java
433 lines (361 loc) · 18.6 KB
/
EJBComponentCreateService.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* 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.component;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagementType;
import javax.transaction.TransactionManager;
import javax.transaction.TransactionSynchronizationRegistry;
import javax.transaction.UserTransaction;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import org.jboss.as.core.security.ServerSecurityManager;
import org.jboss.as.ee.component.BasicComponentCreateService;
import org.jboss.as.ee.component.ComponentConfiguration;
import org.jboss.as.ee.component.ViewConfiguration;
import org.jboss.as.ee.component.ViewDescription;
import org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory;
import org.jboss.as.ejb3.component.messagedriven.MessageDrivenComponentDescription;
import org.jboss.as.ejb3.deployment.ApplicationExceptions;
import org.jboss.as.ejb3.remote.EJBRemoteTransactionsRepository;
import org.jboss.as.ejb3.security.EJBSecurityMetaData;
import org.jboss.as.ejb3.subsystem.ApplicationSecurityDomainService.ApplicationSecurityDomain;
import org.jboss.as.ejb3.subsystem.ApplicationSecurityDomainService.Registration;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.invocation.InterceptorFactory;
import org.jboss.invocation.Interceptors;
import org.jboss.invocation.proxy.MethodIdentifier;
import org.jboss.msc.inject.Injector;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.wildfly.extension.requestcontroller.ControlPoint;
import org.wildfly.security.auth.server.SecurityDomain;
/**
* @author Jaikiran Pai
*/
public class EJBComponentCreateService extends BasicComponentCreateService {
private final Map<MethodTransactionAttributeKey, TransactionAttributeType> txAttrs;
private final Map<MethodTransactionAttributeKey, Integer> txTimeouts;
private final TransactionManagementType transactionManagementType;
private final ApplicationExceptions applicationExceptions;
private final Map<String, ServiceName> viewServices;
private final EJBSecurityMetaData securityMetaData;
private final TimerService timerService;
private final Map<Method, InterceptorFactory> timeoutInterceptors;
private final Method timeoutMethod;
private final ServiceName ejbLocalHome;
private final ServiceName ejbHome;
private final ServiceName ejbObject;
private final ServiceName ejbLocalObject;
private final String applicationName;
private final String earApplicationName;
private final String moduleName;
private final String distinctName;
private final String policyContextID;
private final InjectedValue<EJBRemoteTransactionsRepository> ejbRemoteTransactionsRepository = new InjectedValue<EJBRemoteTransactionsRepository>();
private final InjectedValue<TransactionManager> transactionManagerInjectedValue = new InjectedValue<>();
private final InjectedValue<UserTransaction> userTransactionInjectedValue = new InjectedValue<>();
private final InjectedValue<TransactionSynchronizationRegistry> transactionSynchronizationRegistryValue = new InjectedValue<TransactionSynchronizationRegistry>();
private final InjectedValue<ServerSecurityManager> serverSecurityManagerInjectedValue = new InjectedValue<>();
private final InjectedValue<ControlPoint> controlPoint = new InjectedValue<>();
private final InjectedValue<AtomicBoolean> exceptionLoggingEnabled = new InjectedValue<>();
private final InjectedValue<ApplicationSecurityDomain> applicationSecurityDomain = new InjectedValue<>();
private final ShutDownInterceptorFactory shutDownInterceptorFactory;
private Registration registration;
/**
* Construct a new instance.
*
* @param componentConfiguration the component configuration
*/
public EJBComponentCreateService(final ComponentConfiguration componentConfiguration, final ApplicationExceptions applicationExceptions) {
super(componentConfiguration);
this.applicationExceptions = applicationExceptions;
final EJBComponentDescription ejbComponentDescription = (EJBComponentDescription) componentConfiguration.getComponentDescription();
this.transactionManagementType = ejbComponentDescription.getTransactionManagementType();
this.timerService = ejbComponentDescription.getTimerService();
this.policyContextID = ejbComponentDescription.getPolicyContextID();
// CMTTx
if (transactionManagementType.equals(TransactionManagementType.CONTAINER)) {
this.txAttrs = new HashMap<MethodTransactionAttributeKey, TransactionAttributeType>();
this.txTimeouts = new HashMap<MethodTransactionAttributeKey, Integer>();
} else {
this.txAttrs = null;
this.txTimeouts = null;
}
// Setup the security metadata for the bean
this.securityMetaData = new EJBSecurityMetaData(componentConfiguration);
if (ejbComponentDescription.isTimerServiceRequired()) {
Map<Method, InterceptorFactory> timeoutInterceptors = new IdentityHashMap<Method, InterceptorFactory>();
for (Method method : componentConfiguration.getDefinedComponentMethods()) {
if ((ejbComponentDescription.getTimeoutMethod() != null && ejbComponentDescription.getTimeoutMethod().equals(method)) ||
ejbComponentDescription.getScheduleMethods().containsKey(method)) {
final InterceptorFactory interceptorFactory = Interceptors.getChainedInterceptorFactory(componentConfiguration.getAroundTimeoutInterceptors(method));
timeoutInterceptors.put(method, interceptorFactory);
}
}
this.timeoutInterceptors = timeoutInterceptors;
} else {
timeoutInterceptors = Collections.emptyMap();
}
List<ViewConfiguration> views = componentConfiguration.getViews();
if (views != null) {
for (ViewConfiguration view : views) {
//TODO: Move this into a configurator
final EJBViewConfiguration ejbView = (EJBViewConfiguration) view;
final MethodIntf viewType = ejbView.getMethodIntf();
for (Method method : view.getProxyFactory().getCachedMethods()) {
// TODO: proxy factory exposes non-public methods, is this a bug in the no-interface view?
if (!Modifier.isPublic(method.getModifiers()))
continue;
final Method componentMethod = getComponentMethod(componentConfiguration, method.getName(), method.getParameterTypes());
if (componentMethod != null) {
this.processTxAttr(ejbComponentDescription, viewType, componentMethod);
} else {
this.processTxAttr(ejbComponentDescription, viewType, method);
}
}
}
}
this.timeoutMethod = ejbComponentDescription.getTimeoutMethod();
// FIXME: TODO: a temporary measure until EJBTHREE-2120 is fully resolved, let's create tx attribute map
// for the component methods. Once the issue is resolved, we should get rid of this block and just rely on setting
// up the tx attributes only for the views exposed by this component
// AS7-899: We only want to process public methods of the proper sub-class. (getDefinedComponentMethods returns all in random order)
// TODO: use ClassReflectionIndex (low prio, because we store the result without class name) (which is a bug: AS7-905)
Set<Method> lifeCycle = new HashSet<>(componentConfiguration.getLifecycleMethods());
for (Method method : componentConfiguration.getComponentClass().getMethods()) {
this.processTxAttr(ejbComponentDescription, MethodIntf.BEAN, method);
lifeCycle.remove(method);
}
//now handle non-public lifecycle methods declared on the bean class itself
//see WFLY-4127
for(Method method : lifeCycle) {
if(method.getDeclaringClass().equals(componentConfiguration.getComponentClass())) {
this.processTxAttr(ejbComponentDescription, MethodIntf.BEAN, method);
}
}
final HashMap<String, ServiceName> viewServices = new HashMap<String, ServiceName>();
for (ViewDescription view : componentConfiguration.getComponentDescription().getViews()) {
viewServices.put(view.getViewClassName(), view.getServiceName());
}
this.viewServices = viewServices;
final EjbHomeViewDescription localHome = ejbComponentDescription.getEjbLocalHomeView();
this.ejbLocalHome = localHome == null ? null : ejbComponentDescription.getEjbLocalHomeView().getServiceName();
final EjbHomeViewDescription home = ejbComponentDescription.getEjbHomeView();
this.ejbHome = home == null ? null : home.getServiceName();
final EJBViewDescription ejbObject = ejbComponentDescription.getEjbRemoteView();
this.ejbObject = ejbObject == null ? null : ejbObject.getServiceName();
final EJBViewDescription ejbLocalObject = ejbComponentDescription.getEjbLocalView();
this.ejbLocalObject = ejbLocalObject == null ? null : ejbLocalObject.getServiceName();
this.applicationName = componentConfiguration.getApplicationName();
this.earApplicationName = componentConfiguration.getComponentDescription().getModuleDescription().getEarApplicationName();
this.moduleName = componentConfiguration.getModuleName();
this.distinctName = componentConfiguration.getComponentDescription().getModuleDescription().getDistinctName();
this.shutDownInterceptorFactory = ejbComponentDescription.getShutDownInterceptorFactory();
}
@Override
public synchronized void start(final StartContext context) throws StartException {
super.start(context);
ApplicationSecurityDomain applicationSecurityDomain = getApplicationSecurityDomain();
Function<String, Registration> securityFunction = applicationSecurityDomain != null ? applicationSecurityDomain.getSecurityFunction() : null;
if (securityFunction != null) {
final DeploymentUnit deploymentUnit = getDeploymentUnitInjector().getValue();
final String deploymentName = deploymentUnit.getParent() == null ? deploymentUnit.getName() : deploymentUnit.getParent().getName() + "." + deploymentUnit.getName();
registration = securityFunction.apply(deploymentName);
}
}
@Override
public synchronized void stop(final StopContext context) {
super.stop(context);
if (registration != null) {
registration.cancel();
}
}
@Override
protected boolean requiresInterceptors(final Method method, final ComponentConfiguration componentConfiguration) {
if (super.requiresInterceptors(method, componentConfiguration)) {
return true;
}
final EJBComponentDescription ejbComponentDescription = (EJBComponentDescription) componentConfiguration.getComponentDescription();
if ((ejbComponentDescription.getTimeoutMethod() != null && ejbComponentDescription.getTimeoutMethod().equals(method)) ||
ejbComponentDescription.getScheduleMethods().containsKey(method)) {
return true;
}
return false;
}
private static Method getComponentMethod(final ComponentConfiguration componentConfiguration, final String name, final Class<?>[] parameterTypes) {
try {
return componentConfiguration.getComponentClass().getMethod(name, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}
/**
* @return
* @deprecated {@link EJBUtilities} is deprecated post 7.2.0.Final version.
*/
@Deprecated
protected EJBUtilities getEJBUtilities() {
// constructs
final DeploymentUnit deploymentUnit = getDeploymentUnitInjector().getValue();
final ServiceController<EJBUtilities> serviceController = (ServiceController<EJBUtilities>) deploymentUnit.getServiceRegistry().getRequiredService(EJBUtilities.SERVICE_NAME);
return serviceController.getValue();
}
Map<MethodTransactionAttributeKey, TransactionAttributeType> getTxAttrs() {
return txAttrs;
}
Map<MethodTransactionAttributeKey, Integer> getTxTimeouts() {
return txTimeouts;
}
TransactionManagementType getTransactionManagementType() {
return transactionManagementType;
}
ApplicationExceptions getApplicationExceptions() {
return this.applicationExceptions;
}
protected void processTxAttr(final EJBComponentDescription ejbComponentDescription, final MethodIntf methodIntf, final Method method) {
if (this.getTransactionManagementType().equals(TransactionManagementType.BEAN)) {
// it's a BMT bean
return;
}
MethodIntf defaultMethodIntf = (ejbComponentDescription instanceof MessageDrivenComponentDescription) ? MethodIntf.MESSAGE_ENDPOINT : MethodIntf.BEAN;
TransactionAttributeType txAttr = ejbComponentDescription.getTransactionAttributes().getAttribute(methodIntf, method, defaultMethodIntf);
if(txAttr != null) {
txAttrs.put(new MethodTransactionAttributeKey(methodIntf, MethodIdentifier.getIdentifierForMethod(method)), txAttr);
}
Integer txTimeout = ejbComponentDescription.getTransactionTimeouts().getAttribute(methodIntf, method, defaultMethodIntf);
if (txTimeout != null) {
txTimeouts.put(new MethodTransactionAttributeKey(methodIntf, MethodIdentifier.getIdentifierForMethod(method)), txTimeout);
}
}
public Map<String, ServiceName> getViewServices() {
return viewServices;
}
public EJBSecurityMetaData getSecurityMetaData() {
return this.securityMetaData;
}
public Map<Method, InterceptorFactory> getTimeoutInterceptors() {
return timeoutInterceptors;
}
public TimerService getTimerService() {
return timerService;
}
public Method getTimeoutMethod() {
return timeoutMethod;
}
public ServiceName getEjbHome() {
return ejbHome;
}
public ServiceName getEjbLocalHome() {
return ejbLocalHome;
}
public ServiceName getEjbObject() {
return ejbObject;
}
public ServiceName getEjbLocalObject() {
return ejbLocalObject;
}
public String getApplicationName() {
return applicationName;
}
public String getEarApplicationName() {
return this.earApplicationName;
}
public String getDistinctName() {
return distinctName;
}
public String getModuleName() {
return moduleName;
}
public Injector<EJBRemoteTransactionsRepository> getEJBRemoteTransactionsRepositoryInjector() {
return this.ejbRemoteTransactionsRepository;
}
EJBRemoteTransactionsRepository getEJBRemoteTransactionsRepository() {
// remote tx repo is applicable only for remote views, hence the optionalValue
return this.ejbRemoteTransactionsRepository.getOptionalValue();
}
Injector<TransactionManager> getTransactionManagerInjector() {
return this.transactionManagerInjectedValue;
}
TransactionManager getTransactionManager() {
return this.transactionManagerInjectedValue.getValue();
}
Injector<UserTransaction> getUserTransactionInjector() {
return this.userTransactionInjectedValue;
}
UserTransaction getUserTransaction() {
return this.userTransactionInjectedValue.getValue();
}
Injector<TransactionSynchronizationRegistry> getTransactionSynchronizationRegistryInjector() {
return transactionSynchronizationRegistryValue;
}
TransactionSynchronizationRegistry getTransactionSynchronizationRegistry() {
return transactionSynchronizationRegistryValue.getOptionalValue();
}
ServerSecurityManager getServerSecurityManager() {
return this.serverSecurityManagerInjectedValue.getOptionalValue();
}
Injector<ServerSecurityManager> getServerSecurityManagerInjector() {
return this.serverSecurityManagerInjectedValue;
}
public ControlPoint getControlPoint() {
return this.controlPoint.getOptionalValue();
}
public Injector<ControlPoint> getControlPointInjector() {
return this.controlPoint;
}
public String getPolicyContextID() {
return this.policyContextID;
}
InjectedValue<AtomicBoolean> getExceptionLoggingEnabledInjector() {
return exceptionLoggingEnabled;
}
public AtomicBoolean getExceptionLoggingEnabled() {
return exceptionLoggingEnabled.getValue();
}
Injector<ApplicationSecurityDomain> getApplicationSecurityDomainInjector() {
return applicationSecurityDomain;
}
public ApplicationSecurityDomain getApplicationSecurityDomain() {
return applicationSecurityDomain.getOptionalValue();
}
public SecurityDomain getSecurityDomain() {
ApplicationSecurityDomain applicationSecurityDomain = getApplicationSecurityDomain();
return applicationSecurityDomain != null ? applicationSecurityDomain.getSecurityDomain() : null;
}
public ShutDownInterceptorFactory getShutDownInterceptorFactory() {
return shutDownInterceptorFactory;
}
}