View Javadoc

1   /*
2    * $Id: MuleContainerBootstrap.java 19455 2010-09-08 16:00:24Z aperepel $
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.reboot;
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  
26  /**
27   * Determine which is the main class to run and delegate control to the Java Service
28   * Wrapper.  If OSGi is not being used to boot with, configure the classpath based on
29   * the libraries in $MULE_HOME/lib/*
30   * <p/>
31   * Note: this class is intentionally kept free of any external library dependencies and
32   * therefore repeats a few utility methods.
33   */
34  public class MuleContainerBootstrap
35  {
36      private static final String MULE_MODULE_BOOT_POM_FILE_PATH = "META-INF/maven/org.mule.module/mule-module-boot/pom.properties";
37   
38      public static final String CLI_OPTIONS[][] = {
39          {"main", "true", "Main Class"},
40          {"production", "false", "Modify the system class loader for production use (as in Mule 2.x)"},
41          {"version", "false", "Show product and version information"}
42      };
43  
44      public static void main(String[] args) throws Exception
45      {
46          // Parse any command line options based on the list above.
47          CommandLine commandLine = parseCommandLine(args);
48          // Any unrecognized arguments get passed through to the next class (e.g., to the OSGi Framework).
49          String[] remainingArgs = commandLine.getArgs();
50  
51          prepareBootstrapPhase(commandLine);
52          
53          System.out.println("Starting the Mule Container...");
54          WrapperManager.start(new MuleContainerWrapper(), remainingArgs);
55      }
56  
57      private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception
58      {
59          boolean production = commandLine.hasOption("production");
60          prepareBootstrapPhase(production);
61      }
62      
63      private static void prepareBootstrapPhase(boolean production) throws Exception
64      {
65          File muleHome = lookupMuleHome();
66          File muleBase = lookupMuleBase();
67          if (muleBase == null)
68          {
69              muleBase = muleHome;
70          }
71          
72          setSystemMuleVersion();
73      }
74      
75      public static File lookupMuleHome() throws Exception
76      {
77          File muleHome = null;
78          String muleHomeVar = System.getProperty("mule.home");
79          
80          if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
81          {
82              muleHome = new File(muleHomeVar).getCanonicalFile();
83          }
84  
85          if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
86          {
87              throw new IllegalArgumentException("Either the system property mule.home is not set or does not contain a valid directory.");
88          }
89          return muleHome;
90      }
91      
92      public static File lookupMuleBase() throws Exception
93      {
94          File muleBase = null;
95          String muleBaseVar = System.getProperty("mule.base");
96          
97          if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
98          {
99              muleBase = new File(muleBaseVar).getCanonicalFile();
100         }
101         return muleBase;
102     }
103 
104     private static void setSystemMuleVersion()
105     {
106         InputStream propertiesStream = null;
107         try
108         {
109             URL mavenPropertiesUrl = MuleContainerBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleContainerWrapper.class);
110             propertiesStream = mavenPropertiesUrl.openStream();
111             
112             Properties mavenProperties = new Properties();
113             mavenProperties.load(propertiesStream);
114             
115             System.setProperty("mule.version", mavenProperties.getProperty("version"));
116             System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
117         }
118         catch (Exception ignore)
119         {
120             // ignore;
121         }
122         finally
123         {
124             if (propertiesStream != null)
125             {
126                 try
127                 {
128                     propertiesStream.close();
129                 }
130                 catch (IOException iox)
131                 {
132                     // ignore
133                 }
134             }
135         }
136     }
137 
138     /**
139      * Parse any command line arguments using the Commons CLI library.
140      */
141     private static CommandLine parseCommandLine(String[] args) throws ParseException
142     {
143         Options options = new Options();
144         for (int i = 0; i < CLI_OPTIONS.length; i++)
145         {
146             options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
147         }
148         return new BasicParser().parse(options, args, true);
149     }
150 }