Coverage Report - org.mule.modules.boot.MuleBootstrap
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleBootstrap
0%
0/56
0%
0/36
2.222
 
 1  
 /*
 2  
  * $Id: MuleBootstrap.java 10545 2008-01-25 21:44:53Z 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.modules.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. <p/> MuleBootstrap class is responsible for constructing Mule's classpath
 31  
  * from the Mule home folder.
 32  
  * 
 33  
  * IMPORTANT NOTE:
 34  
  * <p>When using external functionality from packages outside of <code>./lib/boot</code>
 35  
  * you must not use them within <code>main(String args[])</code>. Although this will safely
 36  
  * work with Sun's VM it'll fail on e.g. JRockit. In general, it is recommended to test
 37  
  * against JRockit in addition to Sun's VM when making changes within this module.</p> 
 38  
  */
 39  
 public final class MuleBootstrap
 40  
 {
 41  
     private static final String MULE_MODULE_BOOT_POM_FILE_PATH = "META-INF/maven/org.mule.modules/mule-module-boot/pom.properties";
 42  
     
 43  
     public static final String MULE_SERVER_WRAPPER = "org.mule.modules.boot.MuleServerWrapper";
 44  
     public static final String MULE_VERSION_WRAPPER = "org.mule.modules.boot.VersionWrapper";
 45  
     
 46  0
     public static final String CLI_OPTIONS[][] = {
 47  
         {"builder", "true", "Configuration Builder Type"},
 48  
         {"config", "true", "Configuration File"},
 49  
         {"main", "true", "Main Class"},
 50  
         {"mode", "true", "Run Mode"},
 51  
         {"version", "false", "Show product and version information"},
 52  
         {"props", "true", "Startup Properties"}
 53  
     };
 54  
     
 55  
     private MuleBootstrap()
 56  0
     {
 57  
         // utility class only
 58  0
     }
 59  
 
 60  
     public static void main(String args[]) throws Exception
 61  
     {
 62  0
         prepareBootstrapPhase();
 63  
         
 64  0
         CommandLine commandLine = parseCommandLine(args);
 65  
 
 66  0
         String mainClassName = commandLine.getOptionValue("main");
 67  0
         if (commandLine.hasOption("version"))
 68  
         {
 69  0
             mainClassName = MULE_VERSION_WRAPPER;
 70  
         }
 71  0
         else if (mainClassName == null)
 72  
         {
 73  0
             mainClassName = MULE_SERVER_WRAPPER;
 74  
         }
 75  
         
 76  0
         if (mainClassName.equals(MULE_SERVER_WRAPPER) || mainClassName.equals(MULE_VERSION_WRAPPER))
 77  
         {
 78  0
             WrapperManager.start((WrapperListener) Class.forName(mainClassName).newInstance(), args);
 79  
         }
 80  
         else
 81  
         {
 82  0
             wrapperMain(mainClassName, args);
 83  
         }    
 84  0
     }
 85  
     
 86  
     private static void prepareBootstrapPhase() throws Exception
 87  
     {
 88  0
         File muleHome = lookupMuleHome();
 89  0
         File muleBase = lookupMuleBase();
 90  
 
 91  0
         if (muleBase == null)
 92  
         {
 93  0
             muleBase = muleHome;
 94  
         }
 95  
 
 96  0
         MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
 97  
         
 98  0
         setSystemMuleVersion();
 99  0
         requestLicenseAcceptance();           
 100  
 
 101  0
         MuleBootstrapUtils.addExternalJarFilesToClasspath(muleHome, null);
 102  0
     }
 103  
     
 104  
     private static File lookupMuleHome() throws Exception
 105  
     {
 106  0
         File muleHome = null;
 107  0
         String muleHomeVar = System.getProperty("mule.home");
 108  
         
 109  0
         if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
 110  
         {
 111  0
             muleHome = new File(muleHomeVar).getCanonicalFile();
 112  
         }
 113  
 
 114  0
         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
 115  
         {
 116  0
             throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
 117  
         }
 118  0
         return muleHome;
 119  
     }
 120  
     
 121  
     private static File lookupMuleBase() throws Exception
 122  
     {
 123  0
         File muleBase = null;
 124  0
         String muleBaseVar = System.getProperty("mule.base");
 125  
         
 126  0
         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
 127  
         {
 128  0
             muleBase = new File(muleBaseVar).getCanonicalFile();
 129  
         }
 130  0
         return muleBase;
 131  
     }
 132  
 
 133  
     private static void requestLicenseAcceptance() throws Exception
 134  
     {
 135  0
         if (!LicenseHandler.isLicenseAccepted() && !LicenseHandler.getAcceptance())
 136  
         {
 137  0
             WrapperManager.stop(-1);
 138  
         }        
 139  0
     }
 140  
     
 141  
     private static void setSystemMuleVersion()
 142  
     {
 143  
         try
 144  
         {
 145  0
             URL mavenPropertiesUrl = ClassUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
 146  0
             Properties mavenProperties = new Properties();
 147  0
             mavenProperties.load(mavenPropertiesUrl.openStream());
 148  
             
 149  0
             System.setProperty("mule.version", mavenProperties.getProperty("version"));
 150  0
             System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
 151  
         }
 152  0
         catch (Exception ignore)
 153  
         {
 154  
             // ignore
 155  0
         }
 156  0
     }    
 157  
     
 158  
     private static CommandLine parseCommandLine(String[] args) throws ParseException
 159  
     {
 160  0
         Options options = new Options();
 161  0
         for (int i = 0; i < CLI_OPTIONS.length; i++)
 162  
         {
 163  0
             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
 164  
         }
 165  0
         return new BasicParser().parse(options, args, true);
 166  
     }
 167  
     
 168  
     private static void wrapperMain(String mainClassName, String[] args) throws Exception
 169  
     {
 170  0
         String[] appArgs = new String[args.length + 1];
 171  0
         appArgs[0] = mainClassName;
 172  0
         System.arraycopy(args, 0, appArgs, 1, args.length);
 173  0
         System.out.println("Starting class " + mainClassName + "...");
 174  0
         WrapperSimpleApp.main(appArgs);
 175  0
     }    
 176  
 }