Coverage Report - org.mule.module.reboot.MuleContainerBootstrap
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleContainerBootstrap
0%
0/46
0%
0/24
2.286
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.reboot;
 8  
 
 9  
 import java.io.File;
 10  
 import java.io.IOException;
 11  
 import java.io.InputStream;
 12  
 import java.net.URL;
 13  
 import java.util.Date;
 14  
 import java.util.Properties;
 15  
 
 16  
 import org.apache.commons.cli.BasicParser;
 17  
 import org.apache.commons.cli.CommandLine;
 18  
 import org.apache.commons.cli.Options;
 19  
 import org.apache.commons.cli.ParseException;
 20  
 import org.tanukisoftware.wrapper.WrapperManager;
 21  
 
 22  
 /**
 23  
  * Determine which is the main class to run and delegate control to the Java Service
 24  
  * Wrapper.  If OSGi is not being used to boot with, configure the classpath based on
 25  
  * the libraries in $MULE_HOME/lib/*
 26  
  * <p/>
 27  
  * Note: this class is intentionally kept free of any external library dependencies and
 28  
  * therefore repeats a few utility methods.
 29  
  */
 30  0
 public class MuleContainerBootstrap
 31  
 {
 32  
     private static final String MULE_MODULE_BOOT_POM_FILE_PATH = "META-INF/maven/org.mule.module/mule-module-boot/pom.properties";
 33  
  
 34  0
     public static final String CLI_OPTIONS[][] = {
 35  
         {"main", "true", "Main Class"},
 36  
         {"production", "false", "Modify the system class loader for production use (as in Mule 2.x)"},
 37  
         {"version", "false", "Show product and version information"}
 38  
     };
 39  
 
 40  
     public static void main(String[] args) throws Exception
 41  
     {
 42  
         // Parse any command line options based on the list above.
 43  0
         CommandLine commandLine = parseCommandLine(args);
 44  
         // Any unrecognized arguments get passed through to the next class (e.g., to the OSGi Framework).
 45  0
         String[] remainingArgs = commandLine.getArgs();
 46  
 
 47  0
         prepareBootstrapPhase(commandLine);
 48  
         
 49  0
         System.out.println("Starting the Mule Container...");
 50  0
         WrapperManager.start(new MuleContainerWrapper(), remainingArgs);
 51  0
     }
 52  
 
 53  
     private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception
 54  
     {
 55  0
         boolean production = commandLine.hasOption("production");
 56  0
         prepareBootstrapPhase(production);
 57  0
     }
 58  
     
 59  
     private static void prepareBootstrapPhase(boolean production) throws Exception
 60  
     {
 61  0
         File muleHome = lookupMuleHome();
 62  0
         File muleBase = lookupMuleBase();
 63  0
         if (muleBase == null)
 64  
         {
 65  0
             muleBase = muleHome;
 66  
         }
 67  
         
 68  0
         setSystemMuleVersion();
 69  0
     }
 70  
     
 71  
     public static File lookupMuleHome() throws Exception
 72  
     {
 73  0
         File muleHome = null;
 74  0
         String muleHomeVar = System.getProperty("mule.home");
 75  
         
 76  0
         if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
 77  
         {
 78  0
             muleHome = new File(muleHomeVar).getCanonicalFile();
 79  
         }
 80  
 
 81  0
         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
 82  
         {
 83  0
             throw new IllegalArgumentException("Either the system property mule.home is not set or does not contain a valid directory.");
 84  
         }
 85  0
         return muleHome;
 86  
     }
 87  
     
 88  
     public static File lookupMuleBase() throws Exception
 89  
     {
 90  0
         File muleBase = null;
 91  0
         String muleBaseVar = System.getProperty("mule.base");
 92  
         
 93  0
         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
 94  
         {
 95  0
             muleBase = new File(muleBaseVar).getCanonicalFile();
 96  
         }
 97  0
         return muleBase;
 98  
     }
 99  
 
 100  
     private static void setSystemMuleVersion()
 101  
     {
 102  0
         InputStream propertiesStream = null;
 103  
         try
 104  
         {
 105  0
             URL mavenPropertiesUrl = MuleContainerBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleContainerWrapper.class);
 106  0
             propertiesStream = mavenPropertiesUrl.openStream();
 107  
             
 108  0
             Properties mavenProperties = new Properties();
 109  0
             mavenProperties.load(propertiesStream);
 110  
             
 111  0
             System.setProperty("mule.version", mavenProperties.getProperty("version"));
 112  0
             System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
 113  
         }
 114  0
         catch (Exception ignore)
 115  
         {
 116  
             // ignore;
 117  
         }
 118  
         finally
 119  
         {
 120  0
             if (propertiesStream != null)
 121  
             {
 122  
                 try
 123  
                 {
 124  0
                     propertiesStream.close();
 125  
                 }
 126  0
                 catch (IOException iox)
 127  
                 {
 128  
                     // ignore
 129  0
                 }
 130  
             }
 131  
         }
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Parse any command line arguments using the Commons CLI library.
 136  
      */
 137  
     private static CommandLine parseCommandLine(String[] args) throws ParseException
 138  
     {
 139  0
         Options options = new Options();
 140  0
         for (int i = 0; i < CLI_OPTIONS.length; i++)
 141  
         {
 142  0
             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
 143  
         }
 144  0
         return new BasicParser().parse(options, args, true);
 145  
     }
 146  
 }