Skip to content

Commit

Permalink
Merge pull request payara#3341 from jGauravGupta/PAYARA4-2905
Browse files Browse the repository at this point in the history
PAYARA-2905 Prevent creation of databases used for Timer Service when only non-persistent timers are present - Payara4
  • Loading branch information
MeroRai authored Nov 15, 2018
2 parents 392afcc + 3431a26 commit a7da356
Show file tree
Hide file tree
Showing 22 changed files with 807 additions and 540 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] [Payara Foundation and/or its affiliates]

package com.sun.ejb;

import com.sun.ejb.containers.EJBContextImpl;
import com.sun.ejb.containers.EJBTimerServiceWrapper;
import com.sun.ejb.containers.EJBTimerService;
import com.sun.enterprise.container.common.spi.EjbNamingReferenceManager;
import com.sun.enterprise.deployment.EjbReferenceDescriptor;
Expand All @@ -50,13 +50,10 @@
import org.glassfish.enterprise.iiop.api.GlassFishORBHelper;
import javax.inject.Inject;
import org.jvnet.hk2.annotations.Service;

import javax.inject.Provider;
import javax.naming.Context;
import com.sun.enterprise.util.Utility;

import org.omg.CORBA.ORB;

import javax.naming.NamingException;

/**
Expand Down Expand Up @@ -200,6 +197,7 @@ public Object resolveEjbReference(EjbReferenceDescriptor ejbRefDesc, Context con
return resolved ? jndiObj : EJBUtils.resolveEjbRefObject(ejbRefDesc, jndiObj);
}

@Override
public boolean isEjbReferenceCacheable(EjbReferenceDescriptor ejbRefDesc) {
// Ejb-ref is only eligible for caching if it refers to the legacy
// Home view and it is resolved to an ejb within the same application.
Expand All @@ -211,6 +209,7 @@ public boolean isEjbReferenceCacheable(EjbReferenceDescriptor ejbRefDesc) {
}


@Override
public Object getEJBContextObject(String contextType) {

ComponentInvocation currentInv = invMgr.getCurrentInvocation();
Expand All @@ -228,16 +227,10 @@ public Object getEJBContextObject(String contextType) {

Object returnObject = ejbInv.context;

if( contextType.equals("javax.ejb.TimerService") ) {
if (EJBTimerService.getEJBTimerService() == null ) {
throw new IllegalStateException("EJB Timer Service not " +
"available");
}
returnObject = new EJBTimerServiceWrapper
(EJBTimerService.getEJBTimerService(), (EJBContextImpl) ejbInv.context);
if (contextType.equals("javax.ejb.TimerService")) {
returnObject = EJBTimerService.getEJBTimerServiceWrapper((EJBContextImpl) ejbInv.context);
}


return returnObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,20 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] [Payara Foundation and/or its affiliates]

package com.sun.ejb.containers;

import com.sun.ejb.EjbInvocation;
import com.sun.ejb.spi.container.StatefulEJBContext;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.EjbSessionDescriptor;
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;

import javax.ejb.EJBException;
import javax.ejb.SessionContext;
import javax.ejb.TimerService;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;
import javax.xml.rpc.handler.MessageContext;
import com.sun.ejb.EJBUtils;
import java.util.*;


import static com.sun.ejb.containers.StatefulSessionContainer.EEMRefInfo;
import static com.sun.ejb.containers.StatefulSessionContainer.EEMRefInfoKey;

/**
* Implementation of EJBContext for SessionBeans
Expand Down Expand Up @@ -92,10 +83,12 @@ public void setInstanceKey(Object instanceKey) {
this.instanceKey = instanceKey;
}

@Override
public String toString() {
return ejbName + "; id: " + instanceKey;
}

@Override
public TimerService getTimerService() throws IllegalStateException {

// Instance key is first set between after setSessionContext and
Expand All @@ -104,10 +97,10 @@ public TimerService getTimerService() throws IllegalStateException {
throw new IllegalStateException("Operation not allowed");
}

EJBTimerService timerService = EJBTimerService.getValidEJBTimerService();
return new EJBTimerServiceWrapper(timerService, this);
return EJBTimerService.getEJBTimerServiceWrapper(this);
}

@Override
public UserTransaction getUserTransaction()
throws IllegalStateException {
// The state check ensures that an exception is thrown if this
Expand All @@ -120,6 +113,7 @@ public UserTransaction getUserTransaction()
return ((BaseContainer) getContainer()).getUserTransaction();
}

@Override
public MessageContext getMessageContext() {
InvocationManager invManager = EjbContainerUtilImpl.getInstance().getInvocationManager();
try {
Expand All @@ -138,6 +132,7 @@ public MessageContext getMessageContext() {
}
}

@Override
public <T> T getBusinessObject(Class<T> businessInterface)
throws IllegalStateException {

Expand Down Expand Up @@ -202,6 +197,7 @@ public <T> T getBusinessObject(Class<T> businessInterface)
return businessObject;
}

@Override
public Class getInvokedBusinessInterface()
throws IllegalStateException {

Expand Down Expand Up @@ -235,6 +231,7 @@ public Class getInvokedBusinessInterface()
return businessInterface;
}

@Override
public boolean wasCancelCalled() {

try {
Expand Down Expand Up @@ -262,6 +259,7 @@ public boolean wasCancelCalled() {
"outside an ejb invocation");
}

@Override
protected void checkAccessToCallerSecurity()
throws IllegalStateException {
if (state == BeanState.CREATED) {
Expand All @@ -270,6 +268,7 @@ protected void checkAccessToCallerSecurity()

}

@Override
public void checkTimerServiceMethodAccess()
throws IllegalStateException {
// checks that apply to both stateful AND stateless
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ public enum ContainerType {
protected boolean isWebServiceEndpoint = false;

private boolean isTimedObject_ = false;
private boolean isPersistenceTimer;

/*****************************************
* Data members for Local views *
Expand Down Expand Up @@ -793,6 +794,7 @@ protected BaseContainer(ContainerType type, EjbDescriptor ejbDesc, ClassLoader l
// ignore. Will happen for EJB 3.0 session beans
}

isPersistenceTimer = false;
if ( ejbDescriptor.isTimedObject() ) {

warnIfNotFullProfile("use of persistent EJB Timer Service");
Expand All @@ -802,6 +804,7 @@ protected BaseContainer(ContainerType type, EjbDescriptor ejbDesc, ClassLoader l
// Can be a @Timeout or @Schedule or TimedObject
if (ejbTimeoutMethodDesc != null) {
Method method = ejbTimeoutMethodDesc.getMethod(ejbDescriptor);
isPersistenceTimer = true; // timers defined in runtime
processEjbTimeoutMethod(method);

ejbTimeoutMethod = method;
Expand All @@ -818,14 +821,17 @@ protected BaseContainer(ContainerType type, EjbDescriptor ejbDesc, ClassLoader l
ejbClass.getName(), schd.getTimeoutMethod().getFormattedString()));
}

if ( _logger.isLoggable(Level.FINE) ) {
_logger.log(Level.FINE, "... processing " + method );
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "... processing {0}", method);
}
if (!isPersistenceTimer) {
isPersistenceTimer = schd.getPersistent();
}
processEjbTimeoutMethod(method);

List<ScheduledTimerDescriptor> list = schedules.get(method);
if (list == null) {
list = new ArrayList<ScheduledTimerDescriptor>();
list = new ArrayList<>();
schedules.put(method, list);
}
list.add(schd);
Expand All @@ -836,7 +842,7 @@ protected BaseContainer(ContainerType type, EjbDescriptor ejbDesc, ClassLoader l
if( !isStatefulSession ) {
// EJBTimerService should be accessed only if needed
// not to cause it to be loaded if it's not used.
EJBTimerService timerService = EJBTimerService.getEJBTimerService();
EJBTimerService timerService = EJBTimerService.getEJBTimerService(isPersistenceTimer);
if( timerService != null ) {
timerService.timedObjectCount();
}
Expand Down Expand Up @@ -2237,7 +2243,7 @@ void addSchedule(TimerPrimaryKey timerId, EJBTimerSchedule ts) {
}

/**
* Check timeout method and set it accessable
* Check timeout method and set it accessible
*/
private void processEjbTimeoutMethod(Method method) throws Exception {
Class[] params = method.getParameterTypes();
Expand Down Expand Up @@ -2493,7 +2499,7 @@ protected void cancelTimers(Object key) {
if ( isTimedObject() ) {
// EJBTimerService should be accessed only if needed
// not to cause it to be loaded if it's not used.
EJBTimerService timerService = EJBTimerService.getEJBTimerService();
EJBTimerService timerService = EJBTimerService.getEJBTimerService(isPersistenceTimer);
if ( timerService != null ) {
timerService.cancelTimersByKey(getContainerId(), key);
}
Expand All @@ -2502,7 +2508,7 @@ protected void cancelTimers(Object key) {

private void stopTimers() {
if ( isTimedObject() ) {
EJBTimerService ejbTimerService = EJBTimerService.getEJBTimerService();
EJBTimerService ejbTimerService = EJBTimerService.getEJBTimerService(isPersistenceTimer);
if ( ejbTimerService != null ) {
ejbTimerService.stopTimers(getContainerId());
}
Expand Down Expand Up @@ -4024,7 +4030,7 @@ public void startApplication(boolean deploy) {
if ( isTimedObject_ ) {
// EJBTimerService should be accessed only if needed
// not to cause it to be loaded if it's not used.
EJBTimerService timerService = EJBTimerService.getEJBTimerService();
EJBTimerService timerService = EJBTimerService.getEJBTimerService(isPersistenceTimer);
if (timerService != null) {
boolean deploy0 = deploy; //avoid modifying param
if (deploy0 && ejbDescriptor.getApplication().getKeepStateResolved()) {
Expand Down
Loading

0 comments on commit a7da356

Please sign in to comment.