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