1
2
3
4
5
6
7
8
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
30
31
32
33
34
35
36
37
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 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 {
57
58 }
59
60 public static void main(String args[]) throws Exception
61 {
62 prepareBootstrapPhase();
63
64 CommandLine commandLine = parseCommandLine(args);
65
66 String mainClassName = commandLine.getOptionValue("main");
67 if (commandLine.hasOption("version"))
68 {
69 mainClassName = MULE_VERSION_WRAPPER;
70 }
71 else if (mainClassName == null)
72 {
73 mainClassName = MULE_SERVER_WRAPPER;
74 }
75
76 if (mainClassName.equals(MULE_SERVER_WRAPPER) || mainClassName.equals(MULE_VERSION_WRAPPER))
77 {
78 WrapperManager.start((WrapperListener) Class.forName(mainClassName).newInstance(), args);
79 }
80 else
81 {
82 wrapperMain(mainClassName, args);
83 }
84 }
85
86 private static void prepareBootstrapPhase() throws Exception
87 {
88 File muleHome = lookupMuleHome();
89 File muleBase = lookupMuleBase();
90
91 if (muleBase == null)
92 {
93 muleBase = muleHome;
94 }
95
96 MuleBootstrapUtils.addLocalJarFilesToClasspath(muleHome, muleBase);
97
98 setSystemMuleVersion();
99 requestLicenseAcceptance();
100
101 MuleBootstrapUtils.addExternalJarFilesToClasspath(muleHome, null);
102 }
103
104 private static File lookupMuleHome() throws Exception
105 {
106 File muleHome = null;
107 String muleHomeVar = System.getProperty("mule.home");
108
109 if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%"))
110 {
111 muleHome = new File(muleHomeVar).getCanonicalFile();
112 }
113
114 if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory())
115 {
116 throw new IllegalArgumentException("Either MULE_HOME is not set or does not contain a valid directory.");
117 }
118 return muleHome;
119 }
120
121 private static File lookupMuleBase() throws Exception
122 {
123 File muleBase = null;
124 String muleBaseVar = System.getProperty("mule.base");
125
126 if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%"))
127 {
128 muleBase = new File(muleBaseVar).getCanonicalFile();
129 }
130 return muleBase;
131 }
132
133 private static void requestLicenseAcceptance() throws Exception
134 {
135 if (!LicenseHandler.isLicenseAccepted() && !LicenseHandler.getAcceptance())
136 {
137 WrapperManager.stop(-1);
138 }
139 }
140
141 private static void setSystemMuleVersion()
142 {
143 try
144 {
145 URL mavenPropertiesUrl = ClassUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleServerWrapper.class);
146 Properties mavenProperties = new Properties();
147 mavenProperties.load(mavenPropertiesUrl.openStream());
148
149 System.setProperty("mule.version", mavenProperties.getProperty("version"));
150 System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime());
151 }
152 catch (Exception ignore)
153 {
154
155 }
156 }
157
158 private static CommandLine parseCommandLine(String[] args) throws ParseException
159 {
160 Options options = new Options();
161 for (int i = 0; i < CLI_OPTIONS.length; i++)
162 {
163 options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]);
164 }
165 return new BasicParser().parse(options, args, true);
166 }
167
168 private static void wrapperMain(String mainClassName, String[] args) throws Exception
169 {
170 String[] appArgs = new String[args.length + 1];
171 appArgs[0] = mainClassName;
172 System.arraycopy(args, 0, appArgs, 1, args.length);
173 System.out.println("Starting class " + mainClassName + "...");
174 WrapperSimpleApp.main(appArgs);
175 }
176 }