1
2
3
4
5
6
7
8
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
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 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
49 CommandLine commandLine = parseCommandLine(args);
50
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
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
144 }
145 }
146
147
148
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 }