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