Home » geronimo-2.2-source-release » org.apache.geronimo.mavenplugins.geronimo.server » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *    http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.geronimo.mavenplugins.geronimo.server;
   21   
   22   import java.io.File;
   23   import java.io.IOException;
   24   import java.util.Map;
   25   import java.util.HashMap;
   26   import java.util.Enumeration;
   27   import java.util.zip.ZipFile;
   28   import java.util.zip.ZipEntry;
   29   
   30   import org.apache.maven.artifact.Artifact;
   31   
   32   import org.apache.maven.plugin.MojoExecutionException;
   33   import org.apache.maven.plugin.MojoFailureException;
   34   
   35   import org.codehaus.plexus.util.FileUtils;
   36   
   37   import org.apache.tools.ant.taskdefs.Expand;
   38   import org.apache.tools.ant.taskdefs.Chmod;
   39   
   40   import org.apache.geronimo.mavenplugins.geronimo.AssemblyConfig;
   41   import org.apache.geronimo.mavenplugins.geronimo.reporting.ReportingMojoSupport;
   42   
   43   /**
   44    * Common assembly install support.
   45    *
   46    * @version $Rev: 703199 $ $Date: 2008-10-09 09:14:09 -0700 (Thu, 09 Oct 2008) $
   47    */
   48   public abstract class InstallerMojoSupport
   49       extends ReportingMojoSupport
   50   {
   51       /**
   52        * Enable forced install refresh.
   53        *
   54        * @parameter expression="${refresh}" default-value="false"
   55        */
   56       protected boolean refresh = false;
   57   
   58       /**
   59        * List of assembly artifact configurations.  Artifacts need to point to ZIP archives.
   60        *
   61        * @parameter
   62        */
   63       protected AssemblyConfig[] assemblies = null;
   64   
   65       /**
   66        * Identifer of the assembly configuration to use.
   67        *
   68        * @parameter expression="${assemblyId}"
   69        */
   70       protected String assemblyId = null;
   71   
   72       /**
   73        * The default assemblyId to use when no assemblyId configured.
   74        *
   75        * @parameter
   76        */
   77       protected String defaultAssemblyId = null;
   78   
   79       /**
   80        * A file which points to a specific assembly ZIP archive.
   81        * If this parameter is set, then it will be used instead of from the
   82        * assemblies configuration.
   83        *
   84        * @parameter expression="${assemblyArchive}"
   85        */
   86       protected File assemblyArchive = null;
   87   
   88       /**
   89        * Directory to install the assembly into.
   90        *
   91        * @parameter expression="${installDirectory}" default-value="${project.build.directory}"
   92        * @required
   93        */
   94       protected File installDirectory = null;
   95   
   96       /**
   97        * The directory where the assembly has been installed to.
   98        *
   99        * Normally this value is detected,
  100        * but if it is set, then it is assumed to be the location where a pre-installed assembly exists
  101        * and no installation will be done.
  102        *
  103        * @parameter expression="${geronimoHome}"
  104        */
  105       protected File geronimoHome;
  106       
  107       protected static enum InstallType {
  108           FROM_ARTIFACT,
  109           FROM_FILE,
  110           ALREADY_EXISTS
  111       }
  112       
  113       protected InstallType installType;
  114   
  115       private File discoverGeronimoHome(final File archive) throws IOException, MojoExecutionException {
  116           log.debug("Attempting to discover geronimoHome...");
  117   
  118           File dir = null;
  119   
  120           try {
  121               ZipFile zipFile = new ZipFile(archive);
  122               
  123               Enumeration n = zipFile.entries();
  124               while (n.hasMoreElements()) {
  125                   ZipEntry entry = (ZipEntry)n.nextElement();
  126                   // look for bin/server.jar under a single directory                     
  127                   if (entry.getName().endsWith("bin/server.jar") && entry.getName().split("/").length == 3) {
  128                       File file = new File(installDirectory, entry.getName());
  129                       dir = file.getParentFile().getParentFile();
  130                       break;
  131                   }
  132               }
  133   
  134               zipFile.close();
  135           }
  136           catch (IOException e) {
  137               throw new MojoExecutionException("Failed to determine geronimoHome while scanning archive for 'bin/server.jar'", e);
  138           }
  139   
  140           if (dir == null) {
  141               throw new MojoExecutionException("Archive does not contain a Geronimo assembly: " + archive);
  142           }
  143   
  144           return dir.getCanonicalFile();
  145       }
  146   
  147       protected void init() throws MojoExecutionException, MojoFailureException {
  148           super.init();
  149           
  150           try {
  151               // First check if geronimoHome is set, if it is, then we can skip this
  152               if (geronimoHome != null) {
  153                   geronimoHome = geronimoHome.getCanonicalFile();
  154                   
  155                   // Quick sanity check
  156                   File file = new File(geronimoHome, "bin/server.jar");
  157                   if (!file.exists()) {
  158                       throw new MojoExecutionException("When geronimoHome is set, it must point to a directory that contains 'bin/server.jar'");
  159                   }
  160                   log.info("Using pre-installed assembly: " + geronimoHome);
  161   
  162                   installType = InstallType.ALREADY_EXISTS;
  163               }
  164               else {
  165                   if (assemblyArchive != null) {
  166                       assemblyArchive = assemblyArchive.getCanonicalFile();
  167                       
  168                       log.info("Using non-artifact based assembly archive: " + assemblyArchive);
  169   
  170                       installType = InstallType.FROM_FILE;
  171                   }
  172                   else {
  173                       Artifact artifact = getAssemblyArtifact();
  174   
  175                       if (!"zip".equals(artifact.getType())) {
  176                           throw new MojoExecutionException("Assembly file does not look like a ZIP archive");
  177                       }
  178   
  179                       log.info("Using assembly artifact: " + artifact);
  180   
  181                       assemblyArchive = artifact.getFile();
  182   
  183                       installType = InstallType.FROM_ARTIFACT;
  184                   }
  185   
  186                   geronimoHome = discoverGeronimoHome(assemblyArchive);
  187                   log.info("Using geronimoHome: " + geronimoHome);
  188               }
  189           }
  190           catch (java.io.IOException e) {
  191               throw new MojoExecutionException(e.getMessage(), e);
  192           }
  193       }
  194   
  195       /**
  196        * Selects the assembly artifact tp be used for installation.
  197        *
  198        * @return The assembly artifact selected to be installed.
  199        *
  200        * @throws MojoExecutionException   Failed to select assembly artifact
  201        */
  202       protected Artifact getAssemblyArtifact() throws MojoExecutionException {
  203           AssemblyConfig config;
  204   
  205           if (assemblies == null || assemblies.length == 0) {
  206               throw new MojoExecutionException("At least one assembly configuration must be specified");
  207           }
  208           else if (assemblies.length > 1 && assemblyId == null && defaultAssemblyId == null) {
  209               throw new MojoExecutionException("Must specify assemblyId (or defaultAssemblyId) when more than on assembly configuration is given");
  210           }
  211           else if (assemblies.length == 1) {
  212               config = assemblies[0];
  213           }
  214           else {
  215               if (assemblyId == null) {
  216                   assemblyId = defaultAssemblyId;
  217               }
  218   
  219               log.debug("Searching for assembly config for id: " + assemblyId);
  220   
  221               // Make sure there are no duplicate ids
  222               Map idMap = new HashMap();
  223   
  224               for (int i=0; i < assemblies.length; i++) {
  225                   String id = assemblies[i].getId();
  226   
  227                   if (id == null) {
  228                       throw new MojoExecutionException("Missing id for assembly configuration: " + assemblies[i]);
  229                   }
  230   
  231                   if (idMap.containsKey(id)) {
  232                       throw new MojoExecutionException("Duplicate assembly id: " + id);
  233                   }
  234   
  235                   idMap.put(id, assemblies[i]);
  236               }
  237   
  238               config = (AssemblyConfig) idMap.get(assemblyId);
  239               if (config == null) {
  240                   throw new MojoExecutionException("Missing assembly configuration for id: " + assemblyId);
  241               }
  242           }
  243   
  244           log.info("Using assembly configuration: " + config.getId());
  245           Artifact artifact = getArtifact(config);
  246   
  247           if (artifact.getFile() == null) {
  248               throw new MojoExecutionException("Assembly artifact does not have an attached file: " + artifact);
  249           }
  250   
  251           return artifact;
  252       }
  253   
  254       /**
  255        * Performs assembly installation unless the install type is pre-existing.
  256        *
  257        * @throws Exception
  258        */
  259       protected void installAssembly() throws Exception {
  260           if (installType == InstallType.ALREADY_EXISTS) {
  261               log.info("Installation type is pre-existing; skipping installation");
  262               return;
  263           }
  264   
  265           // Check if there is a newer archive or missing marker to trigger assembly install
  266           File installMarker = new File(geronimoHome, ".installed");
  267   
  268           if (!refresh) {
  269               if (!installMarker.exists()) {
  270                   refresh = true;
  271               }
  272               else if (assemblyArchive.lastModified() > installMarker.lastModified()) {
  273                   log.debug("Detected new assembly archive");
  274                   refresh = true;
  275               }
  276           }
  277           else {
  278               log.debug("User requested installation refresh");
  279           }
  280   
  281           if (refresh) {
  282               if (geronimoHome.exists()) {
  283                   log.info("Uninstalling: " + geronimoHome);
  284                   FileUtils.forceDelete(geronimoHome);
  285               }
  286           }
  287   
  288           // Install the assembly
  289           if (!installMarker.exists()) {
  290               log.info("Installing assembly...");
  291   
  292               FileUtils.forceMkdir(geronimoHome);
  293               
  294               Expand unzip = (Expand)createTask("unzip");
  295               unzip.setSrc(assemblyArchive);
  296               unzip.setDest(installDirectory.getCanonicalFile());
  297               unzip.execute();
  298   
  299               // Make scripts executable, since Java unzip ignores perms
  300               Chmod chmod = (Chmod)createTask("chmod");
  301               chmod.setPerm("ugo+rx");
  302               chmod.setDir(geronimoHome);
  303               chmod.setIncludes("bin/*");
  304               chmod.setExcludes("bin/*.bat");
  305               chmod.execute();
  306   
  307               installMarker.createNewFile();
  308           }
  309           else {
  310               log.info("Re-using previously installed assembly");
  311           }
  312       }
  313   }

Home » geronimo-2.2-source-release » org.apache.geronimo.mavenplugins.geronimo.server » [javadoc | source]