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

org.ow2.petals.ant.task.ConfigureComponentTask Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/**
 * Copyright (c) 2009-2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/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, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/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/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.ant.task;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.apache.tools.ant.BuildException;
import org.ow2.petals.ant.AbstractConfigureArchiveAntTask;
import org.ow2.petals.ant.util.ZipUtil;
import org.ow2.petals.ant.util.ZipUtil.ZipEntryCallback;
import org.ow2.petals.jbi.descriptor.JBIDescriptorException;
import org.ow2.petals.jbi.descriptor.original.JBIDescriptorBuilder;
import org.ow2.petals.jbi.descriptor.original.generated.Component;
import org.ow2.petals.jbi.descriptor.original.generated.Jbi;

/**
 * Ant task : petals-configure-component. 
 * 

* This task pre-configure a JBI component (service engine of binding component). *

* * @author Roland Naudin - EBM WebSourcing */ public class ConfigureComponentTask extends AbstractConfigureArchiveAntTask { /** * SharedLibrary class. * * @author rnaudin * */ public static final class SharedLibrary { private String file; public SharedLibrary() { super(); } public SharedLibrary(final String file) { super(); this.file = file; } public String getFile() { return file; } public void setFile(final String file) { this.file = file; } } /** * Inner structure. * * @author rnaudin * */ private static final class _SharedLibrary { public String identification; public String version; } /** * The aimed identification of the component. */ private String identification; private final List sharedLibraries = new ArrayList(); /** * Location of the file that contains Shared Libraries properties */ private String slProperties; /** * A Shared Library to include in the component configuration. * * @return */ public SharedLibrary createSharedLibrary() { final SharedLibrary sharedLibrary = new SharedLibrary(); this.sharedLibraries.add(sharedLibrary); return sharedLibrary; } @Override public void execute() throws BuildException { super.execute(); final List<_SharedLibrary> _sharedLibraries = new ArrayList<_SharedLibrary>(); try { this.loadSharedLibrariesProperties(); for (final SharedLibrary sharedLibrary : this.sharedLibraries) { final URL slUrl = validateFileParameter(sharedLibrary.getFile(), "sharedlibrary.file"); final File slFile = downloadURL(slUrl); try { final Jbi slJbi = JBIDescriptorBuilder.getInstance().buildJavaJBIDescriptorFromArchive(slFile); final _SharedLibrary _sharedLibrary = new _SharedLibrary(); _sharedLibrary.identification = slJbi.getSharedLibrary().getIdentification().getName(); _sharedLibrary.version = slJbi.getSharedLibrary().getVersion(); _sharedLibraries.add(_sharedLibrary); } finally { slFile.delete(); } } final URL url = validateFileParameter(this.file, "file"); final File componentFile = downloadURL(url); try { final ZipFile zipComponentFile = new ZipFile(componentFile); try { final ZipOutputStream zipOutputFile = new ZipOutputStream(new FileOutputStream( this.outputFile)); ZipUtil.copyAndUpdateZipFile(zipComponentFile, zipOutputFile, new ZipEntryCallback() { public InputStream onZipEntry(final ZipEntry zipEntry, final InputStream zipEntryInputStream) throws IOException, JBIDescriptorException { if (JBIDescriptorBuilder.JBI_DESCRIPTOR_RESOURCE_IN_ARCHIVE.equals(zipEntry.getName())) { InputStream resultStream = null; try { final Jbi componentJbi = JBIDescriptorBuilder.getInstance() .buildJavaJBIDescriptor(zipEntryInputStream); final List componentSLs = componentJbi .getComponent().getSharedLibraryList(); componentSLs.clear(); for (final _SharedLibrary _sharedLibrary : _sharedLibraries) { final Component.SharedLibrary sharedLibrary = new Component.SharedLibrary(); sharedLibrary .setContent(_sharedLibrary.identification); sharedLibrary.setVersion(_sharedLibrary.version); componentSLs.add(sharedLibrary); } if (ConfigureComponentTask.this.identification != null) { componentJbi .getComponent() .getIdentification() .setName( ConfigureComponentTask.this.identification); } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); JBIDescriptorBuilder.getInstance().writeXMLJBIdescriptor(componentJbi, baos); resultStream = new ByteArrayInputStream(baos .toByteArray()); } finally { zipEntryInputStream.close(); } return resultStream; } else { return zipEntryInputStream; } } }); zipOutputFile.flush(); zipOutputFile.close(); } finally { zipComponentFile.close(); } } finally { componentFile.delete(); } } catch (IOException e) { throw new BuildException(e); } catch (JBIDescriptorException e) { throw new BuildException(e); } log("Component configuration set"); } /** * Identification getter. * * @return */ public String getIdentification() { return identification; } /** * SlProperties getter. * * @return */ public String getSlProperties() { return slProperties; } /** * Identification setter. * * @param identification */ public void setIdentification(final String identification) { this.identification = identification; } /** * SlProperties setter. * * @param slProperties */ public void setSlProperties(final String slProperties) { this.slProperties = slProperties; } /** * Load the Shared Libraries properties. * * @throws IOException */ private void loadSharedLibrariesProperties() throws IOException { final Properties props = new Properties(); if (this.slProperties != null) { final InputStream isProperties = new FileInputStream(this.slProperties); try { props.load(isProperties); } finally { isProperties.close(); } } for (final Object value : props.values()) { final SharedLibrary sl = new SharedLibrary((String) value); this.sharedLibraries.add(sl); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy