Coverage Report - org.mule.module.boot.MuleBootstrap
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleBootstrap
0%
0/54
0%
0/32
2.429
 
 1  
 /*
 2  
  * $Id: MuleBootstrap.java 12277 2008-07-10 15:21:25Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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 org.mule.util.ClassUtils;
 14  
 
 15  
 import java.io.File;
 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.WrapperListener;
 25  
 import org.tanukisoftware.wrapper.WrapperManager;
 26  
 import org.tanukisoftware.wrapper.WrapperSimpleApp;
 27  
 
 28  
 /**
 29  
  * Determine which is the main class to run and delegate control to the Java Service
 30  
  * Wrapper.  Configure the classpath based on 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  
     public static final String MAIN_CLASS_MULE_SERVER = "org.mule.module.boot.MuleServerWrapper";
 40  
 
 41  0
     public static final String CLI_OPTIONS[][] = {
 42  
             {"main", "true", "Main Class"},
 43  
             {"version", "false", "Show product and version information"}
 44  
     };
 45  
 
 46  
     public static void main(String[] args) throws Exception
 47  
     {
 48  
         // Parse any command line options based on the list above.
 49  0
         CommandLine commandLine = parseCommandLine(args);
 50  
         // Any unrecognized arguments get passed through to the next class (e.g., to Knopflerfish).
 51  0
         String[] remainingArgs = commandLine.getArgs();
 52  
 
 53  0
         String mainClassName = commandLine.getOptionValue("main");
 54  0
         if (commandLine.hasOption("version"))
 55  
         {
 56  0
             prepareBootstrapPhase();
 57  0
             WrapperManager.start(new VersionWrapper(), remainingArgs);
 58  
         }
 59  0
         else if (mainClassName == null || mainClassName.equals(MAIN_CLASS_MULE_SERVER))
 60  
         {
 61  0
             prepareBootstrapPhase();
 62  0
             System.out.println("Starting the Mule Server...");
 63  0
             WrapperManager.start((WrapperListener) Class.forName(MAIN_CLASS_MULE_SERVER).newInstance(), remainingArgs);
 64  
         }
 65  
         else
 66  
         {
 67  
             // Add the main class name as the first argument to the Wrapper.
 68  0
             String[] appArgs = new String[remainingArgs.length + 1];
 69  0
             appArgs[0] = mainClassName;
 70  0
             System.arraycopy(remainingArgs, 0, appArgs, 1, remainingArgs.length);
 71  0
             prepareBootstrapPhase();
 72  0
             System.out.println("Starting class " + mainClassName + "...");
 73  0
             WrapperSimpleApp.main(appArgs);
 74  
         }
 75  0
     }
 76  
     
 77  
     private static void prepareBootstrapPhase() throws Exception
 78  
     {
 79  0
         File muleHome = lookupMuleHome();
 80  0
         File muleBase = lookupMuleBase();
 81  
 
 82  0
         if (muleBase == null)
 83  
         {
 84  0
             muleBase = muleHome;
 85  
         }
 86  
 
 87  0
         MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
 88  
         
 89  0
         setSystemMuleVersion();
 90  0
         requestLicenseAcceptance();        
 91  0
     }
 92  
     
 93  
     private static File lookupMuleHome() throws Exception
 94  
     {
 95  0
         File muleHome = null;
 96  0
         String muleHomeVar = System.getProperty("mule.home");
 97  
         
 98  0
         if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
 99  
         {
 100  0
             muleHome = new File(muleHomeVar).getCanonicalFile();
 101  
         }
 102  
 
 103  0
         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
 104  
         {
 105  0
             throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
 106  
         }
 107  0
         return muleHome;
 108  
     }
 109  
     
 110  
     private static File lookupMuleBase() throws Exception
 111  
     {
 112  0
         File muleBase = null;
 113  0
         String muleBaseVar = System.getProperty("mule.base");
 114  
         
 115  0
         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
 116  
         {
 117  0
             muleBase = new File(muleBaseVar).getCanonicalFile();
 118  
         }
 119  0
         return muleBase;
 120  
     }    
 121  
     
 122  
     private static void requestLicenseAcceptance() throws Exception
 123  
     {
 124  0
         if (!LicenseHandler.isLicenseAccepted() && !LicenseHandler.getAcceptance())
 125  
         {
 126  0
             WrapperManager.stop(-1);
 127  
         }        
 128  0
     }
 129  
     
 130  
     private static void setSystemMuleVersion()
 131  
     {
 132  
         try
 133  
         {
 134  0
             URL mavenPropertiesUrl = ClassUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
 135  0
             Properties mavenProperties = new Properties();
 136  0
             mavenProperties.load(mavenPropertiesUrl.openStream());
 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  0
         }
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Parse any command line arguments using the Commons CLI library.
 149  
      */
 150  
     private static CommandLine parseCommandLine(String[] args) throws ParseException
 151  
     {
 152  0
         Options options = new Options();
 153  0
         for (int i = 0; i < CLI_OPTIONS.length; i++)
 154  
         {
 155  0
             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
 156  
         }
 157  0
         return new BasicParser().parse(options, args, true);
 158  
     }
 159  
 
 160  
 }