/** * {@inheritDoc} */ @Override public void cleanupRequestResources() { super.cleanupRequestResources(); if (noTxSession != null) { if (noTxSession.isOpen()) { Connection conn = noTxSession.close(); if (conn != null) { try { conn.close(); } catch (SQLException ex) { LOG.warn("The provided non transactional connection could not be correctly closed.", ex); } } } noTxSession = null; } }
/** * {@inheritDoc} */ @Override public void performPendingOperations() { Collection<IEntity> entitiesToUpdate = sessionUnitOfWork.getEntitiesRegisteredForUpdate(); Collection<IEntity> entitiesToDelete = sessionUnitOfWork.getEntitiesRegisteredForDeletion(); if (entitiesToUpdate != null) { List<IEntity> uowEntitiesToUpdate = cloneInUnitOfWork(new ArrayList<>(entitiesToUpdate)); for (IEntity uowEntity : uowEntitiesToUpdate) { // Must force insertion registerForUpdate(uowEntity); } } if (entitiesToDelete != null) { List<IEntity> uowEntitiesToDelete = cloneInUnitOfWork(new ArrayList<>(entitiesToDelete)); for (IEntity uowEntity : uowEntitiesToDelete) { // Must force deletion registerForDeletion(uowEntity); } } clearPendingOperations(); }
/** * {@inheritDoc} */ @Override public final void beginUnitOfWork() { if (isUnitOfWorkSuspended()) { return; } if (isUnitOfWorkActive()) { throw new BackendException("Cannot begin a new unit of work. Another one is already active."); } doBeginUnitOfWork(); }
@Override protected void doInTransactionWithoutResult(TransactionStatus status) { IEntity entityClone = cloneInUnitOfWork(entity); resetTransientEntity(entityClone); merge(entityClone, EMergeMode.MERGE_EAGER); } });
private void linkSessionArtifacts() { if (getApplicationSession() != null && getEntityFactory() != null) { ((ApplicationSessionAwareProxyEntityFactory) getEntityFactory()) .setApplicationSession(getApplicationSession()); ((BasicApplicationSession) getApplicationSession()) .setEntityFactory(getEntityFactory()); } } }
Class<? extends IEntity> entityContract = getComponentContract(entity); E alreadyMergedEntity = (E) alreadyMerged.get(entityContract, entity.getId()); if (alreadyMergedEntity != null) { checkBadMergeUsage(entity); registeredEntity = (E) getRegisteredEntity(getComponentContract(entity), entity.getId()); boolean newlyRegistered = false; if (registeredEntity == null) { if (!isInitialized(entity)) { return mergeUninitializedEntity(entity); sessionUnitOfWork.register(registeredEntity, getDirtyProperties(entity)); } else { sessionUnitOfWork.register(registeredEntity, null); (mergeMode == EMergeMode.MERGE_LAZY || mergeMode == EMergeMode.MERGE_CLEAN_LAZY) && !isInitialized(entity))) { alreadyMerged.register(entityContract, entity.getId(), registeredEntity); return registeredEntity; } else if (mergeMode == EMergeMode.MERGE_EAGER) { sessionUnitOfWork.register(registeredEntity, getDirtyProperties(entity)); if (isInitialized(registeredEntity)) { eventsBlocked = registeredEntity.blockEvents(); IComponentDescriptor<?> entityDescriptor = getEntityFactory().getComponentDescriptor( getComponentContract(entity)); Map<String, Object> entityProperties = entity.straightGetProperties(); Map<String, Object> registeredEntityProperties = registeredEntity.straightGetProperties();
Class<? extends IEntity> entityContract = getComponentContract(entity); IComponentDescriptor<?> entityDescriptor = getEntityFactory().getComponentDescriptor(entityContract); E uowEntity = (E) alreadyCloned.get(entityContract, entity.getId()); if (uowEntity != null) { return uowEntity; uowEntity = performUowEntityCloning(entity); boolean eventsBlocked = false; try { if (isInitialized(uowEntity)) { eventsBlocked = uowEntity.blockEvents(); if (isInitialized(entity)) { dirtyProperties = unitOfWork.getParentDirtyProperties(entity, sessionUnitOfWork); if (dirtyProperties == null) { if (isInitialized(entity)) { Map<String, Object> entityProperties = entity.straightGetProperties(); for (Map.Entry<String, Object> property : entityProperties.entrySet()) { IPropertyDescriptor propertyDescriptor = entityDescriptor.getPropertyDescriptor(propertyName); if (propertyValue instanceof IEntity) { if (isInitialized(propertyValue)) { uowEntity.straightSetProperty(propertyName, cloneInUnitOfWork((IEntity) propertyValue, alreadyCloned, eventsToRelease)); } else { uowEntity.straightSetProperty(propertyName, cloneUninitializedProperty(uowEntity, propertyValue)); if (isInitialized(propertyValue)) {
return true; if (!isAccessGranted(action)) { return false; final Map<String, Object> actionContext = getInitialActionContext(); if (context != null) { context.putAll(actionContext); int currentExecutorsCount = getRunningExecutors().size(); int maxExecutorsCount = getAsyncExecutorsMaxCount(context); if (maxExecutorsCount >= 0 && currentExecutorsCount >= maxExecutorsCount) { throw new ActionBusinessException( + currentExecutorsCount, "async.count.exceeded", currentExecutorsCount); executeAsynchronously(action, context); return true; IActionMonitoringPlugin amp = getActionMonitoringPlugin(); try { if (amp != null) { return executeTransactionally(action, context); return executeBackend(action, context); } finally { if (amp != null) {
/** * Creates a slave backend controller, starts it and assign it the same * application session. * <p/> * {@inheritDoc} */ @Override public AbstractBackendController createBackendController() { AbstractBackendController slaveBackendController = (AbstractBackendController) getSlaveControllerFactory() .createBackendController(); // Start the slave controller slaveBackendController.start(getLocale(), getClientTimeZone()); // Use the same application session slaveBackendController.setApplicationSession(getApplicationSession()); slaveBackendController.sessionUnitOfWork = sessionUnitOfWork; slaveBackendController.workspaceConnectors = workspaceConnectors; slaveBackendController.moduleConnectors = moduleConnectors; slaveBackendController.masterController = this; return slaveBackendController; }
slaveContext.putAll(slaveBackendController.getInitialActionContext()); slaveBackendController.recordUowMergedEntities(); slaveBackendController.executeTransactionally(action, slaveContext); } else { action.execute(slaveBackendController, slaveContext); Map<String, Object> mergeContext = new HashMap<>(); mergeContext.put(COMPLETED_CONTROLLER_KEY, slaveBackendController); slaveBackendController.executeLater(new BackendAction() { @Override public boolean execute(IActionHandler actionHandler, Map<String, Object> innerContext) { Map<String, Object> exceptionContext = new HashMap<>(); exceptionContext.put(EXCEPTION_KEY, ex); slaveBackendController.executeLater(new BackendAction() { @Override public boolean execute(IActionHandler actionHandler, Map<String, Object> innerContext) { slaveBackendController.cleanupRequestResources(); slaveBackendController.stop(); BackendControllerHolder.setThreadBackendController(null); action = null;
component = (IComponent) unwrapProxy(componentOrProxy); Class<? extends IComponent> componentContract = getComponentContract(component); if (clearedEntities.contains(component)) { return; if (!dryRun) { if (component instanceof IEntity) { registerForDeletion((IEntity) component); IComponentDescriptor<?> componentDescriptor = getEntityFactory().getComponentDescriptor(componentContract); for (Map.Entry<String, Object> property : component.straightGetProperties().entrySet()) { String propertyName = property.getKey(); getAccessorFactory().createPropertyAccessor(propertyName, componentContract).getValue(component); if (propertyDescriptor instanceof IReferencePropertyDescriptor && propertyValue instanceof IEntity) { if (((IRelationshipEndPropertyDescriptor) propertyDescriptor).isComposition()) { cleanRelationshipsOnDeletion((IEntity) propertyValue, dryRun, clearedEntities, integrityViolations); } else { if (((IRelationshipEndPropertyDescriptor) propertyDescriptor).getReverseRelationEnd() != null) { getAccessorFactory().createPropertyAccessor(reversePropertyDescriptor.getName(), getComponentContract(((IComponent) propertyValue))).setValue(propertyValue, null); Collection<Object> reverseCollection = getAccessorFactory().createPropertyAccessor( reversePropertyDescriptor.getName(), getComponentContract(((IComponent) propertyValue))) .getValue(propertyValue); ((ICollectionPropertyDescriptor<?>) reversePropertyDescriptor).preprocessRemover( propertyValue, reverseCollection, component); } else {
/** * Directly delegates execution to the action after having completed its * execution context with the controller's initial context. * <p> * {@inheritDoc} */ public boolean execute(IAction action, Map<String, Object> context) { if (action == null) { return true; } SecurityHelper.checkAccess(getApplicationSession().getSubject(), action, getTranslationProvider(), getLocale()); Map<String, Object> actionContext = getInitialActionContext(); if (context != null) { context.putAll(actionContext); } return action.execute(this, context); }
/** * {@inheritDoc} */ @Override public <E extends IEntity> List<E> cloneInUnitOfWork(List<E> entities) { if (!isUnitOfWorkActive()) { throw new BackendException("Cannot use a unit of work that has not begun."); } List<E> uowEntities = new ArrayList<>(); IEntityRegistry alreadyCloned = getUowEntityRegistry(); Set<IEntity> eventsToRelease = new LinkedHashSet<>(); boolean wasDirtyTrackingEnabled = isDirtyTrackingEnabled(); try { setDirtyTrackingEnabled(false); for (E entity : entities) { uowEntities.add(cloneInUnitOfWork(entity, alreadyCloned, eventsToRelease)); } } finally { for (IEntity entity : eventsToRelease) { try { entity.releaseEvents(); } catch (Throwable t) { LOG.error("An unexpected exception occurred when releasing events after a merge", t); } } setDirtyTrackingEnabled(wasDirtyTrackingEnabled); } return uowEntities; }
public Map<String, Object> getDirtyProperties(IEntity entity, boolean includeComputed) { Map<String, Object> dirtyProperties; if (isUnitOfWorkActive()) { dirtyProperties = unitOfWork.getDirtyProperties(entity); } else { boolean include = true; if (!includeComputed) { IComponentDescriptor<?> entityDescriptor = getEntityFactory().getComponentDescriptor( getComponentContract(entity)); IPropertyDescriptor propertyDescriptor = entityDescriptor.getPropertyDescriptor(property.getKey()); include = (propertyDescriptor != null && !propertyDescriptor.isComputed()); Object propertyValue = property.getValue(); Object currentProperty = entity.straightGetProperty(property.getKey()); if ((currentProperty != null && !(currentProperty instanceof Collection<?>) && areEqualWithoutInitializing( currentProperty, property.getValue())) || (currentProperty == null && propertyValue == null)) { clearPropertyDirtyState(currentProperty);
/** * Perform login boolean. * * @param username * the username * @param password * the password * @return the boolean */ @Override public boolean performLogin(String username, String password) { Subject subject = null; String lcName = getLoginContextName(); if (lcName != null) { subject = performJAASLogin(username, password); } if (subject == null) { LOG.info("User {} failed to log in for session {}.", username, getApplicationSession().getId()); return false; } LOG.info("User {} logged in for session {}.", username, getApplicationSession().getId()); loggedIn(subject); return true; }
/** * {@inheritDoc} */ @Override public boolean isAccessGranted(ISecurable securable) { if (SecurityHelper.isSubjectGranted(getApplicationSession().getSubject(), securable)) { if (customSecurityPlugin != null) { try { pushToSecurityContext(securable); Map<String, Object> securityContext = new HashMap<>(); if (getApplicationSession() != null && getApplicationSession().getPrincipal() != null) { securityContext.put(SecurityContextConstants.USER_ROLES, SecurityHelper.getRoles(getApplicationSession().getSubject())); securityContext.put(SecurityContextConstants.USER_ID, getApplicationSession().getUsername()); Map<String, Object> sessionProperties = getApplicationSession().getCustomValues(); sessionProperties.putAll(getApplicationSession().getPrincipal().getCustomProperties()); securityContext.put(SecurityContextConstants.SESSION_PROPERTIES, sessionProperties); } securityContext.putAll(getSecurityContext()); return customSecurityPlugin.isAccessGranted(securable, securityContext); } finally { restoreLastSecurityContextSnapshot(); } } return true; } return false; }
Object propertyValue = property.getValue(); if (propertyValue instanceof IEntity) { if (isInitialized(propertyValue)) { uowComponent.straightSetProperty(propertyName, cloneInUnitOfWork((IEntity) propertyValue, alreadyCloned, eventsToRelease)); } else { uowComponent.straightSetProperty(propertyName, cloneUninitializedProperty(uowComponent, propertyValue)); cloneComponentInUnitOfWork((IComponent) propertyValue, alreadyCloned, eventsToRelease)); IComponent uowOwningComponent; if (owningComponent instanceof IEntity) { uowOwningComponent = cloneInUnitOfWork((IEntity) owningComponent, alreadyCloned, eventsToRelease); } else { uowOwningComponent = cloneComponentInUnitOfWork(owningComponent, alreadyCloned, eventsToRelease);
/** * Gets whether the entity property is dirty (has changes that need to be * updated to the persistent store). * * @param entity * the entity to test. * @param propertyName * the entity property to test. * @return true if the entity is dirty. */ @Override public boolean isDirty(IEntity entity, String propertyName) { if (entity == null) { return false; } Map<String, Object> entityDirtyProperties = getDirtyProperties(entity); if (entityDirtyProperties != null && entityDirtyProperties.containsKey(propertyName)) { IPropertyDescriptor propertyDescriptor = getEntityFactory().getComponentDescriptor(getComponentContract(entity)) .getPropertyDescriptor(propertyName); return propertyDescriptor != null && !propertyDescriptor.isComputed(); } return false; }
/** * Constructs a new {@code AsyncActionExecutor} instance. * * @param action * the action to execute asynchronously. * @param context * the action context. * @param group * the thread group. * @param slaveBackendController * the slave backend controller used to execute the action. */ public AsyncActionExecutor(IAction action, Map<String, Object> context, ThreadGroup group, AbstractBackendController slaveBackendController) { super(group, /* "Jspresso Asynchronous Action Runner " + */ action.getClass().getSimpleName() + "[" + slaveBackendController.getApplicationSession().getId() + "][" + slaveBackendController.getApplicationSession().getUsername() + "]"); this.action = action; this.context = context; this.slaveBackendController = slaveBackendController; this.progressionData = new HashMap<>(); }
/** * {@inheritDoc} */ @Override public void loggedIn(Subject subject) { getApplicationSession().setSubject(subject); String userPreferredLanguageCode = (String) getApplicationSession().getPrincipal().getCustomProperty( UserPrincipal.LANGUAGE_PROPERTY); if (userPreferredLanguageCode != null) { getApplicationSession().setLocale(LocaleUtils.toLocale(userPreferredLanguageCode)); } if (getUserPreferencesStore() != null) { getUserPreferencesStore().setStorePath(getApplicationSession().getUsername()); } }