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

org.ow2.bonita.util.BusinessArchiveFactory Maven / Gradle / Ivy

/**
 * Copyright (C) 2006  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.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.ow2.bonita.facade.def.element.BusinessArchive;
import org.ow2.bonita.facade.def.element.impl.BusinessArchiveImpl;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;

public final class BusinessArchiveFactory {

  private BusinessArchiveFactory() { }

  public static void generateBusinessArchiveFile(final File barFile, final ProcessDefinition process, final Map resources, final Class< ? >... classes) throws IOException, ClassNotFoundException {
    Misc.checkArgsNotNull(barFile);
    byte[] barContent = getBusinessArchiveContent(process, resources, classes);
    generateBusinessArchiveFile(barFile, barContent);
  }

  public static void generateBusinessArchiveFile(final File barFile, final BusinessArchive businessArchive) throws IOException {
    Misc.checkArgsNotNull(barFile, businessArchive);
    byte[] barContent = Misc.generateJar(businessArchive.getResources());
    generateBusinessArchiveFile(barFile, barContent);
  }

  private static void generateBusinessArchiveFile(final File barFile, final byte[] barContent) throws IOException {
    Misc.checkArgsNotNull(barFile, barContent);
    Misc.write(barFile, barContent);
  }

  public static BusinessArchive getBusinessArchive(final URL businessArchiveFileUrl) throws URISyntaxException, IOException, ClassNotFoundException {
    return getBusinessArchive(new File(businessArchiveFileUrl.toURI()));
  }

  public static BusinessArchive getBusinessArchive(final File businessArchiveFile) throws IOException, ClassNotFoundException {
    if (!businessArchiveFile.exists()) {
      throw new FileNotFoundException("File " + businessArchiveFile + "doesn't exists.");
    }
    byte[] fileAsBytes = Misc.getAllContentFrom(businessArchiveFile);
    Map resources = Misc.getResourcesFromZip(fileAsBytes);

    Map newResources = new HashMap();

    for (Map.Entry resource : resources.entrySet()) {
      if (resource.getKey().endsWith(".xpdl")) {
        File xpdlFile = File.createTempFile("xpdl", null);
        Misc.getFile(xpdlFile, resource.getValue());
        ProcessDefinition clientProcess = ProcessBuilder.createProcessFromXpdlFile(xpdlFile.toURL());
        newResources.put(BusinessArchiveImpl.PROCESS_RESOURCE_NAME, Misc.serialize(clientProcess));
      } else {
        newResources.put(resource.getKey(), resource.getValue());
      }
    }
    return new BusinessArchiveImpl(newResources);
  }

  public static BusinessArchive getBusinessArchive(final ProcessDefinition process, final Class< ? >... classes) throws IOException, ClassNotFoundException {
    Misc.checkArgsNotNull(process);
    return new BusinessArchiveImpl(process, null, classes);
  }

  public static BusinessArchive getBusinessArchive(final ProcessDefinition process, final Map resources , final Class< ? >... classes) throws IOException, ClassNotFoundException {
    Misc.checkArgsNotNull(process);
    return new BusinessArchiveImpl(process, resources, classes);
  }

  private static byte[] getBusinessArchiveContent(final ProcessDefinition process, final Map resources , final Class< ? >... classes) throws IOException, ClassNotFoundException {
    BusinessArchive businessArchive = getBusinessArchive(process, resources, classes);
    return Misc.generateJar(businessArchive.getResources());
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy