View Javadoc
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  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      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          CommandLine commandLine = parseCommandLine(args);
45          // Any unrecognized arguments get passed through to the next class (e.g., to the OSGi Framework).
46          String[] remainingArgs = commandLine.getArgs();
47  
48          prepareBootstrapPhase(commandLine);
49          
50          String mainClassName = commandLine.getOptionValue("main");
51          if (commandLine.hasOption("version"))
52          {
53              WrapperManager.start(new VersionWrapper(), remainingArgs);
54          }
55          else if (mainClassName == null || mainClassName.equals(MuleServerWrapper.class.getName()))
56          {
57              System.out.println("Starting the Mule Server...");
58              WrapperManager.start(new MuleServerWrapper(), remainingArgs);
59          }
60          else
61          {
62              // Add the main class name as the first argument to the Wrapper.
63              String[] appArgs = new String[remainingArgs.length + 1];
64              appArgs[0] = mainClassName;
65              System.arraycopy(remainingArgs, 0, appArgs, 1, remainingArgs.length);
66              System.out.println("Starting class " + mainClassName + "...");
67              WrapperSimpleApp.main(appArgs);
68          }
69      }
70  
71      private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception
72      {
73          boolean production = commandLine.hasOption("production");                
74          prepareBootstrapPhase(production);
75      }
76      
77      private static void prepareBootstrapPhase(boolean production) throws Exception
78      {
79          File muleHome = lookupMuleHome();
80          File muleBase = lookupMuleBase();
81          if (muleBase == null)
82          {
83              muleBase = muleHome;
84          }
85  
86          if (production)
87          {            
88              MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
89          }
90          
91          setSystemMuleVersion();
92      }
93      
94      public static File lookupMuleHome() throws Exception
95      {
96          File muleHome = null;
97          String muleHomeVar = System.getProperty("mule.home");
98          
99          if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
100         {
101             muleHome = new File(muleHomeVar).getCanonicalFile();
102         }
103 
104         if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
105         {
106             throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
107         }
108         return muleHome;
109     }
110     
111     public static File lookupMuleBase() throws Exception
112     {
113         File muleBase = null;
114         String muleBaseVar = System.getProperty("mule.base");
115         
116         if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
117         {
118             muleBase = new File(muleBaseVar).getCanonicalFile();
119         }
120         return muleBase;
121     }
122 
123     private static void setSystemMuleVersion()
124     {
125         InputStream propertiesStream = null;
126         try
127         {
128             URL mavenPropertiesUrl = MuleBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
129             propertiesStream = mavenPropertiesUrl.openStream();
130             
131             Properties mavenProperties = new Properties();
132             mavenProperties.load(propertiesStream);
133             
134             System.setProperty("mule.version", mavenProperties.getProperty("version"));
135             System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
136         }
137         catch (Exception ignore)
138         {
139             // ignore;
140         }
141         finally
142         {
143             if (propertiesStream != null)
144             {
145                 try
146                 {
147                     propertiesStream.close();
148                 }
149                 catch (IOException iox)
150                 {
151                     // ignore
152                 }
153             }
154         }
155     }
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         Options options = new Options();
163         for (int i = 0; i < CLI_OPTIONS.length; i++)
164         {
165             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
166         }
167         return new BasicParser().parse(options, args, true);
168     }
169 }