org.bonitasoft.engine.api.impl.CommandAPIImpl Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library 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
* version 2.1 of the License.
* This library 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
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.api.impl;
import static org.bonitasoft.engine.classloader.ClassLoaderIdentifier.identifier;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bonitasoft.engine.api.CommandAPI;
import org.bonitasoft.engine.api.impl.transaction.CustomTransactions;
import org.bonitasoft.engine.api.impl.transaction.command.DeleteSCommand;
import org.bonitasoft.engine.api.impl.transaction.command.GetCommands;
import org.bonitasoft.engine.builder.BuilderFactory;
import org.bonitasoft.engine.classloader.ClassLoaderService;
import org.bonitasoft.engine.classloader.SClassLoaderException;
import org.bonitasoft.engine.command.*;
import org.bonitasoft.engine.command.CommandUpdater.CommandField;
import org.bonitasoft.engine.command.model.SCommand;
import org.bonitasoft.engine.command.model.SCommandCriterion;
import org.bonitasoft.engine.command.model.SCommandUpdateBuilder;
import org.bonitasoft.engine.command.model.SCommandUpdateBuilderFactory;
import org.bonitasoft.engine.commons.exceptions.SBonitaException;
import org.bonitasoft.engine.dependency.DependencyService;
import org.bonitasoft.engine.dependency.SDependencyAlreadyExistsException;
import org.bonitasoft.engine.dependency.SDependencyException;
import org.bonitasoft.engine.dependency.SDependencyNotFoundException;
import org.bonitasoft.engine.dependency.model.ScopeType;
import org.bonitasoft.engine.exception.*;
import org.bonitasoft.engine.recorder.model.EntityUpdateDescriptor;
import org.bonitasoft.engine.search.SearchCommands;
import org.bonitasoft.engine.search.SearchOptions;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.search.descriptor.SearchEntitiesDescriptor;
import org.bonitasoft.engine.service.ModelConvertor;
import org.bonitasoft.engine.service.ServiceAccessor;
import org.bonitasoft.engine.service.ServiceAccessorSingleton;
import org.bonitasoft.engine.transaction.UserTransactionService;
/**
* @author Zhang Bole
* @author Matthieu Chaffotte
* @author Celine Souchet
* @author Emmanuel Duchastenier
* @author Laurent Vaills
*/
public class CommandAPIImpl implements CommandAPI {
protected static ServiceAccessor getServiceAccessor() {
return ServiceAccessorSingleton.getInstance();
}
@Override
public void addDependency(final String name, final byte[] jar) throws CreationException {
final ServiceAccessor serviceAccessor = getServiceAccessor();
final DependencyService dependencyService = serviceAccessor.getDependencyService();
final ClassLoaderService classLoaderService = serviceAccessor.getClassLoaderService();
try {
dependencyService.createMappedDependency(name, jar, name + ".jar", serviceAccessor.getTenantId(),
ScopeType.TENANT);
classLoaderService
.refreshClassLoaderAfterUpdate(identifier(ScopeType.TENANT, serviceAccessor.getTenantId()));
} catch (final SDependencyAlreadyExistsException e) {
throw new AlreadyExistsException(e);
} catch (final SDependencyException | SClassLoaderException sbe) {
throw new CreationException(sbe);
}
}
@Override
public void removeDependency(final String name) throws DependencyNotFoundException, DeletionException {
final ServiceAccessor serviceAccessor = getServiceAccessor();
final DependencyService dependencyService = serviceAccessor.getDependencyService();
final ClassLoaderService classLoaderService = serviceAccessor.getClassLoaderService();
try {
dependencyService.deleteDependency(name);
classLoaderService
.refreshClassLoaderAfterUpdate(identifier(ScopeType.TENANT, serviceAccessor.getTenantId()));
} catch (final SDependencyNotFoundException e) {
throw new DependencyNotFoundException(e);
} catch (final SBonitaException e) {
throw new DeletionException(e);
}
}
@Override
public CommandDescriptor register(final String name, final String description, final String implementation)
throws CreationException {
CommandDescriptor existingCommandDescriptor = null;
try {
existingCommandDescriptor = getCommand(name);
} catch (final CommandNotFoundException notFoundE) {
// Nothing to do : no command with that name exists.
}
if (existingCommandDescriptor != null) {
throw new AlreadyExistsException("A command with name \"" + name + "\" already exists");
}
final ServiceAccessor serviceAccessor = getServiceAccessor();
final CommandService commandService = serviceAccessor.getCommandService();
final SCommand sCommand = SCommand.builder()
.name(name)
.description(description)
.implementation(implementation).isSystem(false).build();
try {
commandService.create(sCommand);
return ModelConvertor.toCommandDescriptor(sCommand);
} catch (final SBonitaException sbe) {
throw new CreationException(sbe);
}
}
private RuntimeCommand fetchRuntimeCommand(final SCommandFetcher commandFetcher,
final boolean transactionManagedManually)
throws SCommandNotFoundException, SCommandParameterizationException {
final ServiceAccessor serviceAccessor = getServiceAccessor();
try {
final SCommand sCommand;
if (transactionManagedManually) {
sCommand = commandFetcher.fetchInTransaction(serviceAccessor.getUserTransactionService(),
serviceAccessor.getCommandService());
} else {
sCommand = commandFetcher.fetch(serviceAccessor.getCommandService());
}
final String runtimeCommandClassName = sCommand.getImplementation();
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
return (RuntimeCommand) contextClassLoader.loadClass(runtimeCommandClassName).newInstance();
} catch (final ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new SCommandParameterizationException(e);
}
}
@Override
public Serializable execute(final String commandName, final Map parameters)
throws CommandNotFoundException,
CommandParameterizationException, CommandExecutionException {
return execute(new SCommandFetcherByName(commandName), parameters);
}
@Override
public Serializable execute(final long commandId, final Map parameters)
throws CommandNotFoundException,
CommandParameterizationException, CommandExecutionException {
return execute(new SCommandFetcherById(commandId), parameters);
}
private Serializable execute(final SCommandFetcher commandFetcher, final Map parameters)
throws CommandNotFoundException,
CommandParameterizationException, CommandExecutionException {
return executeCommand(commandFetcher, parameters, false);
}
@Override
@CustomTransactions
public Serializable executeWithUserTransactions(final String commandName,
final Map parameters) throws CommandNotFoundException,
CommandParameterizationException, CommandExecutionException {
return executeWithUserTransactions(new SCommandFetcherByName(commandName), parameters);
}
@Override
@CustomTransactions
public Serializable executeWithUserTransactions(final long commandId, final Map parameters)
throws CommandNotFoundException,
CommandParameterizationException, CommandExecutionException {
return executeWithUserTransactions(new SCommandFetcherById(commandId), parameters);
}
private Serializable executeWithUserTransactions(final SCommandFetcher commandFetcher,
final Map parameters)
throws CommandNotFoundException, CommandParameterizationException, CommandExecutionException {
return executeCommand(commandFetcher, parameters, true);
}
private Serializable executeCommand(final SCommandFetcher commandFetcher,
final Map parameters,
final boolean transactionManagedManually)
throws CommandNotFoundException, CommandParameterizationException, CommandExecutionException {
final ServiceAccessor serviceAccessor = getServiceAccessor();
try {
final RuntimeCommand runtimeCommand = fetchRuntimeCommand(commandFetcher, transactionManagedManually);
return runtimeCommand.execute(parameters, serviceAccessor);
} catch (final SCommandExecutionException scee) {
throw new CommandExecutionException(scee);
} catch (final SCommandParameterizationException scpe) {
throw new CommandParameterizationException(scpe);
} catch (final SCommandNotFoundException e) {
throw new CommandNotFoundException(e);
}
}
@Override
public void unregister(final long commandId) throws CommandNotFoundException, DeletionException {
final CommandService commandService = getServiceAccessor().getCommandService();
final DeleteSCommand deleteCommand = new DeleteSCommand(commandService, commandId);
unregister(deleteCommand);
}
@Override
public void unregister(final String name) throws CommandNotFoundException, DeletionException {
if (name == null) {
// FIXME: throw IllegalArgumentException instead, and make bonita interceptor catch all exceptions and wrap it into BonitaRuntimeException:
throw new DeletionException("Command name can not be null!");
}
final CommandService commandService = getServiceAccessor().getCommandService();
final DeleteSCommand deleteCommand = new DeleteSCommand(commandService, name);
unregister(deleteCommand);
}
private void unregister(final DeleteSCommand deleteCommand) throws CommandNotFoundException, DeletionException {
try {
deleteCommand.execute();
} catch (final SCommandNotFoundException scnfe) {
throw new CommandNotFoundException(scnfe);
} catch (final SBonitaException sbe) {
throw new DeletionException(sbe);
}
}
@Override
public void unregisterAll() throws DeletionException {
final CommandService commandService = getServiceAccessor().getCommandService();
try {
commandService.deleteAll();
} catch (final SCommandDeletionException sde) {
throw new DeletionException(sde);
}
}
@Override
public CommandDescriptor get(final long commandId) throws CommandNotFoundException {
return getCommand(new SCommandFetcherById(commandId));
}
@Override
public CommandDescriptor getCommand(final String commandName) throws CommandNotFoundException {
return getCommand(new SCommandFetcherByName(commandName));
}
private CommandDescriptor getCommand(final SCommandFetcher commandFetcher) throws CommandNotFoundException {
final CommandService commandService = getServiceAccessor().getCommandService();
try {
final SCommand sCommand = commandFetcher.fetch(commandService);
return ModelConvertor.toCommandDescriptor(sCommand);
} catch (final SBonitaException e) {
throw new CommandNotFoundException(e);
}
}
@Override
public List getAllCommands(final int startIndex, final int maxResults,
final CommandCriterion sort) {
SCommandCriterion sCommandCriterion;
if (CommandCriterion.NAME_ASC.equals(sort)) {
sCommandCriterion = SCommandCriterion.NAME_ASC;
} else {
sCommandCriterion = SCommandCriterion.NAME_DESC;
}
final CommandService commandService = getServiceAccessor().getCommandService();
try {
final List commands = commandService.getAllCommands(startIndex, maxResults, sCommandCriterion);
return ModelConvertor.toCommandDescriptors(commands);
} catch (final SBonitaException e) {
throw new RetrieveException(e);
}
}
@Override
public void update(final long commandId, final CommandUpdater updateDescriptor) throws UpdateException {
update(new SCommandFetcherById(commandId), updateDescriptor);
}
@Override
public void update(final String commandName, final CommandUpdater updateDescriptor) throws UpdateException {
update(new SCommandFetcherByName(commandName), updateDescriptor);
}
private void update(final SCommandFetcher commandFetcher, final CommandUpdater updateDescriptor)
throws UpdateException {
if (updateDescriptor == null || updateDescriptor.getFields().isEmpty()) {
throw new UpdateException("The update descriptor does not contain field updates");
}
final SCommandUpdateBuilderFactory fact = BuilderFactory.get(SCommandUpdateBuilderFactory.class);
final SCommandUpdateBuilder commandUpdateBuilder = fact.createNewInstance();
final CommandService commandService = getServiceAccessor().getCommandService();
try {
final EntityUpdateDescriptor changeDescriptor = getCommandUpdateDescriptor(updateDescriptor,
commandUpdateBuilder);
final SCommand sCommand = commandFetcher.fetch(commandService);
commandService.update(sCommand, changeDescriptor);
} catch (final SCommandNotFoundException | SCommandUpdateException e) {
throw new UpdateException(e);
}
}
private EntityUpdateDescriptor getCommandUpdateDescriptor(final CommandUpdater updateDescriptor,
final SCommandUpdateBuilder commandUpdateBuilder) {
final Map fields = updateDescriptor.getFields();
for (final Entry field : fields.entrySet()) {
final String value = (String) field.getValue();
switch (field.getKey()) {
case NAME:
commandUpdateBuilder.updateName(value);
break;
case DESCRIPTION:
commandUpdateBuilder.updateDescription(value);
break;
default:
throw new IllegalStateException();
}
}
return commandUpdateBuilder.done();
}
@Override
public List getUserCommands(final int startIndex, final int maxResults,
final CommandCriterion sort) {
final CommandService commandService = getServiceAccessor().getCommandService();
try {
final GetCommands getCommands = new GetCommands(commandService, startIndex, maxResults, sort);
getCommands.execute();
return ModelConvertor.toCommandDescriptors(getCommands.getResult());
} catch (final SBonitaException e) {
throw new RetrieveException(e);
}
}
@Override
public SearchResult searchCommands(final SearchOptions searchOptions) throws SearchException {
final ServiceAccessor serviceAccessor = getServiceAccessor();
final CommandService commandService = serviceAccessor.getCommandService();
final SearchEntitiesDescriptor searchEntitiesDescriptor = serviceAccessor.getSearchEntitiesDescriptor();
final SearchCommands searchCommands = new SearchCommands(commandService,
searchEntitiesDescriptor.getSearchCommandDescriptor(), searchOptions);
try {
searchCommands.execute();
return searchCommands.getResult();
} catch (final SBonitaException sbe) {
throw new SearchException(sbe);
}
}
// Utility classes to factorize how we fetch a TenantCommand
private abstract static class SCommandFetcher {
abstract SCommand fetch(final CommandService commandService) throws SCommandNotFoundException;
SCommand fetchInTransaction(final UserTransactionService userTransactionService,
final CommandService commandService) throws SCommandNotFoundException {
try {
return userTransactionService.executeInTransaction(() -> fetch(commandService));
} catch (final Exception e) {
throw new SCommandNotFoundException(e);
}
}
}
private static class SCommandFetcherByName extends SCommandFetcher {
private final String commandName;
public SCommandFetcherByName(final String commandName) {
this.commandName = commandName;
}
@Override
SCommand fetch(final CommandService commandService) throws SCommandNotFoundException {
return commandService.get(commandName);
}
}
private static class SCommandFetcherById extends SCommandFetcher {
private final long commandId;
public SCommandFetcherById(final long commandId) {
this.commandId = commandId;
}
@Override
SCommand fetch(final CommandService commandService) throws SCommandNotFoundException {
return commandService.get(commandId);
}
}
}