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

org.fabric3.runtime.ant.task.Fabric3Task Maven / Gradle / Ivy

/*
 * Fabric3
 * Copyright (c) 2009-2012 Metaform Systems
 *
 * Fabric3 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version, with the
 * following exception:
 *
 * Linking this software statically or dynamically with other
 * modules is making a combined work based on this software.
 * Thus, the terms and conditions of the GNU General Public
 * License cover the whole combination.
 *
 * As a special exception, the copyright holders of this software
 * give you permission to link this software with independent
 * modules to produce an executable, regardless of the license
 * terms of these independent modules, and to copy and distribute
 * the resulting executable under terms of your choice, provided
 * that you also meet, for each linked independent module, the
 * terms and conditions of the license of that module. An
 * independent module is a module which is not derived from or
 * based on this software. If you modify this software, you may
 * extend this exception to your version of the software, but
 * you are not obligated to do so. If you do not wish to do so,
 * delete this exception statement from your version.
 *
 * Fabric3 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 General Public License for more details.
 *
 * You should have received a copy of the
 * GNU General Public License along with Fabric3.
 * If not, see .
*/
package org.fabric3.runtime.ant.task;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileList;
import org.apache.tools.ant.types.FileSet;
import org.w3c.dom.Document;

import org.fabric3.host.Names;
import org.fabric3.host.RuntimeMode;
import org.fabric3.host.contribution.ContributionException;
import org.fabric3.host.contribution.ContributionService;
import org.fabric3.host.contribution.ContributionSource;
import org.fabric3.host.contribution.FileContributionSource;
import org.fabric3.host.domain.DeploymentException;
import org.fabric3.host.domain.Domain;
import org.fabric3.host.monitor.MonitorEventDispatcherFactory;
import org.fabric3.host.runtime.BootConfiguration;
import org.fabric3.host.runtime.BootstrapFactory;
import org.fabric3.host.runtime.BootstrapHelper;
import org.fabric3.host.runtime.BootstrapService;
import org.fabric3.host.runtime.ComponentRegistration;
import org.fabric3.host.runtime.Fabric3Runtime;
import org.fabric3.host.runtime.HiddenPackages;
import org.fabric3.host.runtime.HostInfo;
import org.fabric3.host.classloader.MaskingClassLoader;
import org.fabric3.host.runtime.RuntimeConfiguration;
import org.fabric3.host.runtime.RuntimeCoordinator;
import org.fabric3.host.runtime.ScanResult;
import org.fabric3.host.runtime.ShutdownException;
import org.fabric3.host.util.FileHelper;
import org.fabric3.runtime.ant.api.TestRunner;
import org.fabric3.runtime.ant.monitor.AntMonitorEventDispatcher;
import org.fabric3.runtime.ant.monitor.AntMonitorEventDispatcherFactory;

/**
 * Launches a Fabric3 instance from the Ant runtime distribution.
 * 

* To define the task, create a taskdef pointing to the Fabric3 ant runtime distribution /lib directory entry as follows: *

 *
 *  <taskdef name="fabric3" classname="org.fabric3.runtime.ant.task.Fabric3Task">
 *       <classpath>
 *           <fileset dir="<path to distribution>/fabric3-runtime-ant-1.6-bin/lib">
 *               <include name="*.jar"/>
 *           </fileset>
 *       </classpath>
 *   </taskdef>
 * 
* This Task may be configured with contribution sub-elements which are FileLists pointing to contribution jars or * contributionSet sub-elements which are FileSet filters for calculating sets of contributions as follows: *
 *
 *  <fabric3>
 *     <contribution dir="lib" files="mycontribution.jar"/>
 *     <contributionSet dir="build">
 *        <include name="..."/>
 *     </contributionSet>
 *  </fabric3>
 * 
* * @version $Rev: 11083 $ $Date: 2012-06-21 10:38:50 +0000 (Thu, 21 Jun 2012) $ */ public class Fabric3Task extends Task { private List contributions = new ArrayList(); private List contributionSets = new ArrayList(); private Fabric3Runtime runtime; private RuntimeCoordinator coordinator; public void addContribution(FileList contribution) { this.contributions.add(contribution); } public void addContributionSet(FileSet contribution) { this.contributionSets.add(contribution); } @Override public void execute() throws BuildException { log("Starting Fabric3 Runtime ..."); startRuntime(); deployContributions(); executeTests(); log("Stopping Fabric3 Runtime ..."); stopRuntime(); } private void startRuntime() throws BuildException { try { File installDirectory = BootstrapHelper.getInstallDirectory(Fabric3Task.class); // calculate config directories based on the mode the runtime is booted in File rootRuntimesDir = BootstrapHelper.getDirectory(installDirectory, "runtimes"); File runtimeDir = BootstrapHelper.getDirectory(rootRuntimesDir, "vm"); File configDir = BootstrapHelper.getDirectory(runtimeDir, "config"); File extensionsDir = new File(installDirectory, "extensions"); File bootDir = BootstrapHelper.getDirectory(installDirectory, "boot"); File hostDir = BootstrapHelper.getDirectory(installDirectory, "host"); // create the classloaders for booting the runtime ClassLoader systemClassLoader = getClass().getClassLoader(); // mask hidden JDK and system classpath packages ClassLoader maskingClassLoader = new MaskingClassLoader(systemClassLoader, HiddenPackages.getPackages()); ClassLoader hostLoader = BootstrapHelper.createClassLoader(maskingClassLoader, hostDir); ClassLoader bootLoader = BootstrapHelper.createClassLoader(hostLoader, bootDir); BootstrapService bootstrapService = BootstrapFactory.getService(bootLoader); // load the system configuration Document systemConfig = bootstrapService.loadSystemConfig(configDir); URI domainName = bootstrapService.parseDomainName(systemConfig); String environment = bootstrapService.parseEnvironment(systemConfig); List deployDirs = bootstrapService.parseDeployDirectories(systemConfig); // create the HostInfo and runtime HostInfo hostInfo = BootstrapHelper.createHostInfo("ant", RuntimeMode.VM, domainName, environment, runtimeDir, configDir, extensionsDir, deployDirs); // clear out the tmp directory FileHelper.cleanDirectory(hostInfo.getTempDir()); MBeanServer mBeanServer = MBeanServerFactory.createMBeanServer("fabric3"); AntMonitorEventDispatcher runtimeDispatcher = new AntMonitorEventDispatcher(this); AntMonitorEventDispatcher appDispatcher = new AntMonitorEventDispatcher(this); RuntimeConfiguration runtimeConfig = new RuntimeConfiguration(hostInfo, mBeanServer, runtimeDispatcher, appDispatcher); runtime = bootstrapService.createDefaultRuntime(runtimeConfig); Map exportedPackages = new HashMap(); exportedPackages.put("org.fabric3.runtime.ant.api", Names.VERSION); URL systemComposite = new File(bootDir, "system.composite").toURI().toURL(); ScanResult result = bootstrapService.scanRepository(hostInfo); BootConfiguration configuration = new BootConfiguration(); List registrations = new ArrayList(); AntMonitorEventDispatcherFactory factory = new AntMonitorEventDispatcherFactory(this); ComponentRegistration registration = new ComponentRegistration("MonitorEventDispatcherFactory", MonitorEventDispatcherFactory.class, factory, true); registrations.add(registration); configuration.addRegistrations(registrations); configuration.setRuntime(runtime); configuration.setHostClassLoader(hostLoader); configuration.setBootClassLoader(bootLoader); configuration.setSystemCompositeUrl(systemComposite); configuration.setSystemConfig(systemConfig); configuration.setExtensionContributions(result.getExtensionContributions()); configuration.setUserContributions(result.getUserContributions()); configuration.setExportedPackages(exportedPackages); // boot the runtime coordinator = bootstrapService.createCoordinator(configuration); coordinator.start(); } catch (Exception e) { throw new BuildException(e); } } private void stopRuntime() throws BuildException { try { coordinator.shutdown(); } catch (ShutdownException e) { throw new BuildException(e); } } private void deployContributions() throws BuildException { List sources = new ArrayList(); for (FileList list : contributions) { Project project = list.getProject(); File dir = list.getDir(project); for (String file : list.getFiles(project)) { FileContributionSource source = createContributionSource(dir, file); sources.add(source); } } for (FileSet set : contributionSets) { Project project = set.getProject(); File dir = set.getDir(project); FileScanner scanner = set.getDirectoryScanner(project); set.setupDirectoryScanner(scanner, project); scanner.scan(); String[] files = scanner.getIncludedFiles(); for (String file : files) { FileContributionSource source = createContributionSource(dir, file); sources.add(source); } } try { ContributionService contributionService = runtime.getComponent(ContributionService.class, Names.CONTRIBUTION_SERVICE_URI); Domain domain = runtime.getComponent(Domain.class, Names.APPLICATION_DOMAIN_URI); List uris = contributionService.store(sources); List installed = contributionService.install(uris); for (URI contribution : installed) { log("Installed: " + contribution); } domain.include(installed); } catch (ContributionException e) { throw new BuildException(e); } catch (DeploymentException e) { throw new BuildException(e); } } private FileContributionSource createContributionSource(File dir, String file) throws BuildException { try { File contributionFile = new File(dir, file); URI uri = URI.create(contributionFile.getName()); URL url = contributionFile.toURI().toURL(); long timestamp = System.currentTimeMillis(); return new FileContributionSource(uri, url, timestamp, false); } catch (MalformedURLException e) { throw new BuildException(e); } } private void executeTests() { TestRunner runner = runtime.getComponent(TestRunner.class, TestRunner.TEST_RUNNER_URI); runner.executeTests(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy