1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.reboot; |
8 | |
|
9 | |
import java.io.File; |
10 | |
import java.io.IOException; |
11 | |
import java.io.InputStream; |
12 | |
import java.net.URL; |
13 | |
import java.util.Date; |
14 | |
import java.util.Properties; |
15 | |
|
16 | |
import org.apache.commons.cli.BasicParser; |
17 | |
import org.apache.commons.cli.CommandLine; |
18 | |
import org.apache.commons.cli.Options; |
19 | |
import org.apache.commons.cli.ParseException; |
20 | |
import org.tanukisoftware.wrapper.WrapperManager; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class MuleContainerBootstrap |
31 | |
{ |
32 | |
private static final String MULE_MODULE_BOOT_POM_FILE_PATH = "META-INF/maven/org.mule.module/mule-module-boot/pom.properties"; |
33 | |
|
34 | 0 | public static final String CLI_OPTIONS[][] = { |
35 | |
{"main", "true", "Main Class"}, |
36 | |
{"production", "false", "Modify the system class loader for production use (as in Mule 2.x)"}, |
37 | |
{"version", "false", "Show product and version information"} |
38 | |
}; |
39 | |
|
40 | |
public static void main(String[] args) throws Exception |
41 | |
{ |
42 | |
|
43 | 0 | CommandLine commandLine = parseCommandLine(args); |
44 | |
|
45 | 0 | String[] remainingArgs = commandLine.getArgs(); |
46 | |
|
47 | 0 | prepareBootstrapPhase(commandLine); |
48 | |
|
49 | 0 | System.out.println("Starting the Mule Container..."); |
50 | 0 | WrapperManager.start(new MuleContainerWrapper(), remainingArgs); |
51 | 0 | } |
52 | |
|
53 | |
private static void prepareBootstrapPhase(CommandLine commandLine) throws Exception |
54 | |
{ |
55 | 0 | boolean production = commandLine.hasOption("production"); |
56 | 0 | prepareBootstrapPhase(production); |
57 | 0 | } |
58 | |
|
59 | |
private static void prepareBootstrapPhase(boolean production) throws Exception |
60 | |
{ |
61 | 0 | File muleHome = lookupMuleHome(); |
62 | 0 | File muleBase = lookupMuleBase(); |
63 | 0 | if (muleBase == null) |
64 | |
{ |
65 | 0 | muleBase = muleHome; |
66 | |
} |
67 | |
|
68 | 0 | setSystemMuleVersion(); |
69 | 0 | } |
70 | |
|
71 | |
public static File lookupMuleHome() throws Exception |
72 | |
{ |
73 | 0 | File muleHome = null; |
74 | 0 | String muleHomeVar = System.getProperty("mule.home"); |
75 | |
|
76 | 0 | if (muleHomeVar != null && !muleHomeVar.trim().equals("") && !muleHomeVar.equals("%MULE_HOME%")) |
77 | |
{ |
78 | 0 | muleHome = new File(muleHomeVar).getCanonicalFile(); |
79 | |
} |
80 | |
|
81 | 0 | if (muleHome == null || !muleHome.exists() || !muleHome.isDirectory()) |
82 | |
{ |
83 | 0 | throw new IllegalArgumentException("Either the system property mule.home is not set or does not contain a valid directory."); |
84 | |
} |
85 | 0 | return muleHome; |
86 | |
} |
87 | |
|
88 | |
public static File lookupMuleBase() throws Exception |
89 | |
{ |
90 | 0 | File muleBase = null; |
91 | 0 | String muleBaseVar = System.getProperty("mule.base"); |
92 | |
|
93 | 0 | if (muleBaseVar != null && !muleBaseVar.trim().equals("") && !muleBaseVar.equals("%MULE_BASE%")) |
94 | |
{ |
95 | 0 | muleBase = new File(muleBaseVar).getCanonicalFile(); |
96 | |
} |
97 | 0 | return muleBase; |
98 | |
} |
99 | |
|
100 | |
private static void setSystemMuleVersion() |
101 | |
{ |
102 | 0 | InputStream propertiesStream = null; |
103 | |
try |
104 | |
{ |
105 | 0 | URL mavenPropertiesUrl = MuleContainerBootstrapUtils.getResource(MULE_MODULE_BOOT_POM_FILE_PATH, MuleContainerWrapper.class); |
106 | 0 | propertiesStream = mavenPropertiesUrl.openStream(); |
107 | |
|
108 | 0 | Properties mavenProperties = new Properties(); |
109 | 0 | mavenProperties.load(propertiesStream); |
110 | |
|
111 | 0 | System.setProperty("mule.version", mavenProperties.getProperty("version")); |
112 | 0 | System.setProperty("mule.reference.version", mavenProperties.getProperty("version") + '-' + (new Date()).getTime()); |
113 | |
} |
114 | 0 | catch (Exception ignore) |
115 | |
{ |
116 | |
|
117 | |
} |
118 | |
finally |
119 | |
{ |
120 | 0 | if (propertiesStream != null) |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | propertiesStream.close(); |
125 | |
} |
126 | 0 | catch (IOException iox) |
127 | |
{ |
128 | |
|
129 | 0 | } |
130 | |
} |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private static CommandLine parseCommandLine(String[] args) throws ParseException |
138 | |
{ |
139 | 0 | Options options = new Options(); |
140 | 0 | for (int i = 0; i < CLI_OPTIONS.length; i++) |
141 | |
{ |
142 | 0 | options.addOption(CLI_OPTIONS[i][0], "true".equalsIgnoreCase(CLI_OPTIONS[i][1]), CLI_OPTIONS[i][2]); |
143 | |
} |
144 | 0 | return new BasicParser().parse(options, args, true); |
145 | |
} |
146 | |
} |