1
2
3
4
5
6
7
8
9
10
11 package org.mule.module.boot;
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 import org.tanukisoftware.wrapper.WrapperSimpleApp;
26
27
28
29
30
31
32
33
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 CLI_OPTIONS[][] = {
40 {"main", "true", "Main Class"},
41 {"production", "false", "Modify the system class loader for production use (as in Mule 2.x)"},
42 {"version", "false", "Show product and version information"}
43 };
44
45 public static void main(String[] args) throws Exception
46 {
47
48 CommandLine commandLine = parseCommandLine(args);
49
50 String[] remainingArgs = commandLine.getArgs();
51
52 prepareBootstrapPhase(commandLine);
53
54 String mainClassName = commandLine.getOptionValue("main");
55 if (commandLine.hasOption("version"))
56 {
57 WrapperManager.start(new VersionWrapper(), remainingArgs);
58 }
59 else if (mainClassName == null || mainClassName.equals(MuleServerWrapper.class.getName()))
60 {
61 System.out.println("Starting the Mule Server...");
62 WrapperManager.start(new MuleServerWrapper(), remainingArgs);
63 }
64 else
65 {
66
67 String[] appArgs = new String[remainingArgs.length + 1];
68 appArgs[0] = mainClassName;
69 System.arraycopy(remainingArgs, 0, appArgs, 1, remainingArgs.length);
70 System.out.println("Starting class " + mainClassName + "...");
71 WrapperSimpleApp.main(appArgs);
72 }
73 }
74
75 private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception
76 {
77 boolean production = commandLine.hasOption("production");
78 prepareBootstrapPhase(production);
79 }
80
81 private static void prepareBootstrapPhase(boolean production) throws Exception
82 {
83 File muleHome = lookupMuleHome();
84 File muleBase = lookupMuleBase();
85 if (muleBase == null)
86 {
87 muleBase = muleHome;
88 }
89
90 if (production)
91 {
92 MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
93 }
94
95 setSystemMuleVersion();
96 }
97
98 public static File lookupMuleHome() throws Exception
99 {
100 File muleHome = null;
101 String muleHomeVar = System.getProperty("mule.home");
102
103 if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
104 {
105 muleHome = new File(muleHomeVar).getCanonicalFile();
106 }
107
108 if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
109 {
110 throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
111 }
112 return muleHome;
113 }
114
115 public static File lookupMuleBase() throws Exception
116 {
117 File muleBase = null;
118 String muleBaseVar = System.getProperty("mule.base");
119
120 if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
121 {
122 muleBase = new File(muleBaseVar).getCanonicalFile();
123 }
124 return muleBase;
125 }
126
127 private static void setSystemMuleVersion()
128 {
129 InputStream propertiesStream = null;
130 try
131 {
132 URL mavenPropertiesUrl = MuleBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
133 propertiesStream = mavenPropertiesUrl.openStream();
134
135 Properties mavenProperties = new Properties();
136 mavenProperties.load(propertiesStream);
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
144 }
145 finally
146 {
147 if (propertiesStream != null)
148 {
149 try
150 {
151 propertiesStream.close();
152 }
153 catch (IOException iox)
154 {
155
156 }
157 }
158 }
159 }
160
161
162
163
164 private static CommandLine parseCommandLine(String[] args) throws ParseException
165 {
166 Options options = new Options();
167 for (int i = 0; i < CLI_OPTIONS.length; i++)
168 {
169 options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
170 }
171 return new BasicParser().parse(options, args, true);
172 }
173 }