Coverage Report - org.mule.module.boot.MuleBootstrap
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleBootstrap
0%
0/57
0%
0/32
2.714
 
 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.boot;
 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  
 import org.tanukisoftware.wrapper.WrapperSimpleApp;
 22  
 
 23  
 /**
 24  
  * Determine which is the main class to run and delegate control to the Java Service
 25  
  * Wrapper.  If OSGi is not being used to boot with, configure the classpath based on
 26  
  * the libraries in $MULE_HOME/lib/*
 27  
  * <p/>
 28  
  * Note: this class is intentionally kept free of any external library dependencies and
 29  
  * therefore repeats a few utility methods.
 30  
  */
 31  0
 public class MuleBootstrap
 32  
 {
 33  
     private static final String MULE_MODULE_BOOT_POM_FILE_PATH = "META-INF/maven/org.mule.module/mule-module-boot/pom.properties";
 34  
  
 35  0
     public static final String CLI_OPTIONS[][] = {
 36  
         {"main", "true", "Main Class"},
 37  
         {"production", "false", "Modify the system class loader for production use (as in Mule 2.x)"},
 38  
         {"version", "false", "Show product and version information"}
 39  
     };
 40  
 
 41  
     public static void main(String[] args) throws Exception
 42  
     {
 43  
         // Parse any command line options based on the list above.
 44  0
         CommandLine commandLine = parseCommandLine(args);
 45  
         // Any unrecognized arguments get passed through to the next class (e.g., to the OSGi Framework).
 46  0
         String[] remainingArgs = commandLine.getArgs();
 47  
 
 48  0
         prepareBootstrapPhase(commandLine);
 49  
         
 50  0
         String mainClassName = commandLine.getOptionValue("main");
 51  0
         if (commandLine.hasOption("version"))
 52  
         {
 53  0
             WrapperManager.start(new VersionWrapper(), remainingArgs);
 54  
         }
 55  0
         else if (mainClassName == null || mainClassName.equals(MuleServerWrapper.class.getName()))
 56  
         {
 57  0
             System.out.println("Starting the Mule Server...");
 58  0
             WrapperManager.start(new MuleServerWrapper(), remainingArgs);
 59  
         }
 60  
         else
 61  
         {
 62  
             // Add the main class name as the first argument to the Wrapper.
 63  0
             String[] appArgs = new String[remainingArgs.length + 1];
 64  0
             appArgs[0] = mainClassName;
 65  0
             System.arraycopy(remainingArgs, 0, appArgs, 1, remainingArgs.length);
 66  0
             System.out.println("Starting class " + mainClassName + "...");
 67  0
             WrapperSimpleApp.main(appArgs);
 68  
         }
 69  0
     }
 70  
 
 71  
     private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception
 72  
     {
 73  0
         boolean production = commandLine.hasOption("production");                
 74  0
         prepareBootstrapPhase(production);
 75  0
     }
 76  
     
 77  
     private static void prepareBootstrapPhase(boolean production) throws Exception
 78  
     {
 79  0
         File muleHome = lookupMuleHome();
 80  0
         File muleBase = lookupMuleBase();
 81  0
         if (muleBase == null)
 82  
         {
 83  0
             muleBase = muleHome;
 84  
         }
 85  
 
 86  0
         if (production)
 87  
         {            
 88  0
             MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
 89  
         }
 90  
         
 91  0
         setSystemMuleVersion();
 92  0
     }
 93  
     
 94  
     public static File lookupMuleHome() throws Exception
 95  
     {
 96  0
         File muleHome = null;
 97  0
         String muleHomeVar = System.getProperty("mule.home");
 98  
         
 99  0
         if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
 100  
         {
 101  0
             muleHome = new File(muleHomeVar).getCanonicalFile();
 102  
         }
 103  
 
 104  0
         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
 105  
         {
 106  0
             throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
 107  
         }
 108  0
         return muleHome;
 109  
     }
 110  
     
 111  
     public static File lookupMuleBase() throws Exception
 112  
     {
 113  0
         File muleBase = null;
 114  0
         String muleBaseVar = System.getProperty("mule.base");
 115  
         
 116  0
         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
 117  
         {
 118  0
             muleBase = new File(muleBaseVar).getCanonicalFile();
 119  
         }
 120  0
         return muleBase;
 121  
     }
 122  
 
 123  
     private static void setSystemMuleVersion()
 124  
     {
 125  0
         InputStream propertiesStream = null;
 126  
         try
 127  
         {
 128  0
             URL mavenPropertiesUrl = MuleBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
 129  0
             propertiesStream = mavenPropertiesUrl.openStream();
 130  
             
 131  0
             Properties mavenProperties = new Properties();
 132  0
             mavenProperties.load(propertiesStream);
 133  
             
 134  0
             System.setProperty("mule.version", mavenProperties.getProperty("version"));
 135  0
             System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
 136  
         }
 137  0
         catch (Exception ignore)
 138  
         {
 139  
             // ignore;
 140  
         }
 141  
         finally
 142  
         {
 143  0
             if (propertiesStream != null)
 144  
             {
 145  
                 try
 146  
                 {
 147  0
                     propertiesStream.close();
 148  
                 }
 149  0
                 catch (IOException iox)
 150  
                 {
 151  
                     // ignore
 152  0
                 }
 153  
             }
 154  
         }
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Parse any command line arguments using the Commons CLI library.
 159  
      */
 160  
     private static CommandLine parseCommandLine(String[] args) throws ParseException
 161  
     {
 162  0
         Options options = new Options();
 163  0
         for (int i = 0; i < CLI_OPTIONS.length; i++)
 164  
         {
 165  0
             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
 166  
         }
 167  0
         return new BasicParser().parse(options, args, true);
 168  
     }
 169  
 }