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