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