View Javadoc

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