All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.facade.impl.ManagementAPIImpl Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * 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.
 * 
 * Modified by Matthieu Chaffotte - BonitaSoft S.A.
 **/
package org.ow2.bonita.facade.impl;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.ow2.bonita.definition.ClassData;
import org.ow2.bonita.deployment.Deployer;
import org.ow2.bonita.deployment.DeploymentRuntimeException;
import org.ow2.bonita.facade.def.element.BusinessArchive;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;
import org.ow2.bonita.facade.exception.DeploymentException;
import org.ow2.bonita.facade.exception.ProcessNotFoundException;
import org.ow2.bonita.facade.exception.UndeletableInstanceException;
import org.ow2.bonita.facade.exception.UndeletableProcessException;
import org.ow2.bonita.facade.internal.InternalManagementAPI;
import org.ow2.bonita.facade.uuid.ProcessDefinitionUUID;
import org.ow2.bonita.facade.uuid.ProcessInstanceUUID;
import org.ow2.bonita.runtime.ClassDataLoader;
import org.ow2.bonita.runtime.InternalInstance;
import org.ow2.bonita.services.Archiver;
import org.ow2.bonita.services.LargeDataRepository;
import org.ow2.bonita.services.Querier;
import org.ow2.bonita.services.Recorder;
import org.ow2.bonita.services.Repository;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.ClassDataTool;
import org.ow2.bonita.util.EnvTool;
import org.ow2.bonita.util.ExceptionManager;
import org.ow2.bonita.util.Misc;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes,
 *         Pierre Vigneras
 */
public class ManagementAPIImpl implements InternalManagementAPI {

  protected ManagementAPIImpl() {
  }

  public ProcessDefinition deploy(final BusinessArchive businessArchive) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(businessArchive);
    try {
      return Deployer.deploy(businessArchive);
    } catch (final Exception e) {
      throw new DeploymentException(e.getMessage(), e);
    }
  }

  public void deployClass(final byte[] clazz) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(clazz);
    String className = getClassName(clazz);
    deployClass(className, clazz);
  }
  
  private String getClassName(final byte[] clazz) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(clazz);
    String className;
    try {
      className = ClassDataTool.visitClass(clazz).getClassName();
    } catch (final ArrayIndexOutOfBoundsException e) {
      String message = ExceptionManager.getInstance().getFullMessage("bai_MAPII_4");
      throw new DeploymentException(message);
    }
    return className;
  }
  
  private void deployClass(final String className, final byte[] clazz) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(className, clazz);
    final LargeDataRepository ldr = EnvTool.getLargeDataRepository();
    
    final ClassData globalClassData = ldr.getData(ClassData.class, Misc.getGlobalClassDataCategories(), className);
    if (globalClassData != null) {
      // this class was already deployed
      String message = ExceptionManager.getInstance().getFullMessage("bai_MAPII_5");
      throw new DeploymentException(message, className);
    }
    ldr.storeData(Misc.getGlobalClassDataCategories(), className, new ClassData(className, clazz), true);
  }

  public void deployClassesInJar(final byte[] classesArchive) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(classesArchive);
    try {
      Map classResources = Misc.getResourcesFromZip(classesArchive);
      final Collection classes = new ArrayList();
      for (final byte[] classData : classResources.values()) {
        classes.add(classData);
      }
      this.deployClasses(classes);
    } catch (final IOException e) {
      throw new DeploymentException(e.getMessage(), e);
    }
  }

  public void deployClasses(final Collection classes) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(classes);
    for (final byte[] data : classes) {
      this.deployClass(data);
    }
  }

  public void undeploy(final ProcessDefinitionUUID processUUID) throws DeploymentException {
    disable(processUUID);
  }

  public void removeClass(final String className) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(className);

    final LargeDataRepository ldr = EnvTool.getLargeDataRepository();
    ClassData cd = ldr.getData(ClassData.class, Misc.getGlobalClassDataCategories(), className);
    if (cd == null) {
      String message = ExceptionManager.getInstance().getFullMessage("bai_MAPII_6");
      throw new DeploymentException(message, className);
    }
    Querier querier = EnvTool.getAllQueriers();
    // check that no process is still using this class
    //for all process having a dependency on this class, if the class is not available at process level, we can't remove it
    final Set dependentProcesses = querier.getDependentProcesses(className);
    if (dependentProcesses != null && !dependentProcesses.isEmpty()) {
      for (final ProcessDefinitionUUID processUUID : dependentProcesses) {
        final ClassData classData = ldr.getData(ClassData.class, Misc.getProcessClassDataCategories(processUUID), className);
        if (classData == null) {
          //class doesn't exist at process level
          //It means, it is a common class and it is used by a process: can't remove it.
          String message = ExceptionManager.getInstance().getFullMessage("bai_MAPII_7");
          throw new DeploymentException(message, className, processUUID);
        }
      }
    }
    ldr.deleteData(Misc.getGlobalClassDataCategories(), className);
    ClassDataLoader.removeCommonClass(className);
  }

  public void removeClasses(final String[] classNames) throws DeploymentException {
    if (classNames != null) {
      for (final String className : classNames) {
        this.removeClass(className);
      }
    }
  }

  public void replaceClass(final String className, final byte[] newClazz) throws DeploymentException {
    final String newClassName = getClassName(newClazz);
    if (!newClassName.equals(className)) {
      throw new DeploymentException("Unable to replace class: " + className + " by: " + newClassName + ".");
    }
    this.removeClass(className);
    this.deployClass(className, newClazz);
  }

  public void deleteProcess(final ProcessDefinitionUUID processUUID) throws ProcessNotFoundException, 
  UndeletableProcessException, UndeletableInstanceException {
    FacadeUtil.checkArgsNotNull(processUUID);

    final Querier journal = EnvTool.getJournalQueriers();
    final Querier history = EnvTool.getHistoryQueriers();
    final Repository repository = EnvTool.getRepository();

    ProcessDefinition processDef = journal.getProcess(processUUID);
    boolean inJournal = processDef != null;
    if (processDef == null) {
      processDef = history.getProcess(processUUID);
    }

    if (processDef == null) {
      throw new ProcessNotFoundException("bai_MAPII_9", processUUID);
    }

    final RuntimeAPIImpl runtimeAPI = new RuntimeAPIImpl();
    try {
      runtimeAPI.deleteAllProcessInstances(processUUID);
      repository.removeProcessVersion(processDef.getName(), processDef.getVersion());
    } catch (final ProcessNotFoundException e) {
      throw new BonitaRuntimeException(e);
    }

    if (inJournal) {
      final Set instances = repository.getInstances(processUUID);
      if (!instances.isEmpty()) {
        final ProcessInstanceUUID instanceUUID = instances.iterator().next().getUUID();
        throw new UndeletableProcessException("bai_MAPII_10", processUUID, instanceUUID);
      }
      Deployer.removeProcessDependencies(processDef);
      final Recorder recorder = EnvTool.getRecorder();
      recorder.remove(processDef);
    }

    if (!inJournal) {
      final Archiver archiver = EnvTool.getArchiver();
      archiver.remove(processDef);
    }
    
    EnvTool.getLargeDataRepository().deleteData(Misc.getBusinessArchiveCategories(), processUUID.getValue());

  }

  public void deleteAllProcesses() throws UndeletableInstanceException,
  RemoteException, UndeletableProcessException {
    Querier querier = EnvTool.getAllQueriers();
    for (ProcessDefinition processDefinition : querier.getProcesses()) {
      try {
        deleteProcess(processDefinition.getUUID());
      } catch (ProcessNotFoundException e) {
        //nothing
      }
    }
  }

  public String getLoggedUser() throws RemoteException {
    return EnvTool.getUserId();
  }

  public void addMetaData(String key, String value) throws RemoteException {
    EnvTool.getRepository().storeMetaData(key, value);
  }

  public void deleteMetaData(String key) throws RemoteException {
    EnvTool.getRepository().deleteMetaData(key);
  }

  public String getMetaData(String key) throws RemoteException {
    return EnvTool.getRepository().getMetaData(key);
  }
  
  public void archive(ProcessDefinitionUUID processUUID) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(processUUID);
    try {
      Deployer.archiveProcess(processUUID, EnvTool.getUserId());
    } catch (final DeploymentRuntimeException e) {
      throw new DeploymentException(e.getMessage(), e);
    }
  }

  public void disable(ProcessDefinitionUUID processUUID) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(processUUID);
    try {
      Deployer.disableProcess(processUUID);
    } catch (final DeploymentRuntimeException e) {
      throw new DeploymentException(e.getMessage(), e);
    }
  }

  public void enable(ProcessDefinitionUUID processUUID) throws DeploymentException {
    FacadeUtil.checkArgsNotNull(processUUID);
    try {
      Deployer.enableProcess(processUUID);
    } catch (final DeploymentRuntimeException e) {
      throw new DeploymentException(e.getMessage(), e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy