1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.launcher; |
8 | |
|
9 | |
import org.mule.api.MuleRuntimeException; |
10 | |
import org.mule.config.PreferredObjectSelector; |
11 | |
import org.mule.config.i18n.MessageFactory; |
12 | |
import org.mule.module.launcher.descriptor.ApplicationDescriptor; |
13 | |
import org.mule.module.launcher.descriptor.DescriptorParser; |
14 | |
import org.mule.module.launcher.descriptor.EmptyApplicationDescriptor; |
15 | |
import org.mule.module.launcher.descriptor.PropertiesDescriptorParser; |
16 | |
import org.mule.module.reboot.MuleContainerBootstrapUtils; |
17 | |
import org.mule.util.FileUtils; |
18 | |
import org.mule.util.FilenameUtils; |
19 | |
import org.mule.util.PropertiesUtils; |
20 | |
|
21 | |
import java.io.File; |
22 | |
import java.io.IOException; |
23 | |
import java.util.Collection; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Map; |
27 | |
import java.util.Properties; |
28 | |
|
29 | |
import javax.imageio.spi.ServiceRegistry; |
30 | |
|
31 | |
import org.apache.commons.collections.MultiMap; |
32 | |
import org.apache.commons.collections.map.MultiValueMap; |
33 | |
import org.apache.commons.io.filefilter.WildcardFileFilter; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class DefaultAppBloodhound implements AppBloodhound |
39 | |
{ |
40 | |
|
41 | |
|
42 | 0 | protected Map<String, DescriptorParser> parserRegistry = new HashMap<String, DescriptorParser>(); |
43 | |
public static final String SYSTEM_PROPERTY_OVERRIDE = "-O"; |
44 | |
|
45 | |
public DefaultAppBloodhound() |
46 | 0 | { |
47 | |
|
48 | 0 | parserRegistry.put("properties", new PropertiesDescriptorParser()); |
49 | |
|
50 | 0 | final Iterator<DescriptorParser> it = ServiceRegistry.lookupProviders(DescriptorParser.class); |
51 | |
|
52 | 0 | MultiMap overrides = new MultiValueMap(); |
53 | 0 | while (it.hasNext()) |
54 | |
{ |
55 | 0 | final DescriptorParser parser = it.next(); |
56 | 0 | overrides.put(parser.getSupportedFormat(), parser); |
57 | 0 | } |
58 | 0 | mergeParserOverrides(overrides); |
59 | 0 | } |
60 | |
|
61 | |
public ApplicationDescriptor fetch(String appName) throws IOException |
62 | |
{ |
63 | 0 | final File appsDir = MuleContainerBootstrapUtils.getMuleAppsDir(); |
64 | 0 | File appDir = new File(appsDir, appName); |
65 | 0 | if (!appDir.exists()) |
66 | |
{ |
67 | 0 | throw new MuleRuntimeException( |
68 | |
MessageFactory.createStaticMessage( |
69 | |
String.format("Application directory does not exist: '%s'", appDir))); |
70 | |
} |
71 | |
|
72 | |
@SuppressWarnings("unchecked") |
73 | 0 | Collection<File> deployFiles = FileUtils.listFiles(appDir, new WildcardFileFilter("mule-deploy.*"), null); |
74 | 0 | if (deployFiles.size() > 1) |
75 | |
{ |
76 | |
|
77 | 0 | throw new MuleRuntimeException( |
78 | |
MessageFactory.createStaticMessage( |
79 | |
String.format("More than one mule-deploy descriptors found in application '%s'", appName))); |
80 | |
} |
81 | |
|
82 | |
ApplicationDescriptor desc; |
83 | |
|
84 | |
|
85 | 0 | if (deployFiles.isEmpty()) |
86 | |
{ |
87 | 0 | desc = new EmptyApplicationDescriptor(appName); |
88 | |
} |
89 | |
else |
90 | |
{ |
91 | |
|
92 | 0 | final File descriptorFile = deployFiles.iterator().next(); |
93 | 0 | final String ext = FilenameUtils.getExtension(descriptorFile.getName()); |
94 | 0 | final DescriptorParser descriptorParser = parserRegistry.get(ext); |
95 | |
|
96 | 0 | if (descriptorParser == null) |
97 | |
{ |
98 | |
|
99 | 0 | throw new MuleRuntimeException( |
100 | |
MessageFactory.createStaticMessage( |
101 | |
String.format("Unsupported deployment descriptor format for app '%s': %s", appName, ext))); |
102 | |
} |
103 | |
|
104 | 0 | desc = descriptorParser.parse(descriptorFile); |
105 | |
|
106 | 0 | desc.setAppName(appName); |
107 | |
} |
108 | |
|
109 | |
|
110 | 0 | final File appPropsFile = new File(appDir, ApplicationDescriptor.DEFAULT_APP_PROPERTIES_RESOURCE); |
111 | 0 | setApplicationProperties(desc, appPropsFile); |
112 | 0 | return desc; |
113 | |
|
114 | |
} |
115 | |
|
116 | |
public void setApplicationProperties(ApplicationDescriptor desc, File appPropsFile) throws IOException |
117 | |
{ |
118 | |
|
119 | 0 | Map<String, String> m = new HashMap<String, String>(); |
120 | |
|
121 | 0 | if (appPropsFile.exists() && appPropsFile.canRead()) |
122 | |
{ |
123 | 0 | final Properties props = PropertiesUtils.loadProperties(appPropsFile.toURI().toURL()); |
124 | 0 | for (Object key : props.keySet()) |
125 | |
{ |
126 | 0 | m.put(key.toString(), props.getProperty(key.toString())); |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | Properties sysProps = System.getProperties(); |
132 | 0 | for (Map.Entry<Object, Object> entry : sysProps.entrySet()) |
133 | |
{ |
134 | 0 | String key = entry.getKey().toString(); |
135 | 0 | if (key.startsWith(SYSTEM_PROPERTY_OVERRIDE)) |
136 | |
{ |
137 | 0 | m.put(key.substring(SYSTEM_PROPERTY_OVERRIDE.length()), entry.getValue().toString()); |
138 | |
} |
139 | 0 | } |
140 | 0 | desc.setAppProperties(m); |
141 | 0 | } |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected void mergeParserOverrides(MultiMap overrides) |
149 | |
{ |
150 | 0 | PreferredObjectSelector<DescriptorParser> selector = new PreferredObjectSelector<DescriptorParser>(); |
151 | |
|
152 | 0 | for (Map.Entry<String, DescriptorParser> entry : parserRegistry.entrySet()) |
153 | |
{ |
154 | |
@SuppressWarnings("unchecked") |
155 | 0 | final Collection<DescriptorParser> candidates = (Collection<DescriptorParser>) overrides.get(entry.getKey()); |
156 | |
|
157 | 0 | if (candidates != null) |
158 | |
{ |
159 | 0 | parserRegistry.put(entry.getKey(), selector.select(candidates.iterator())); |
160 | |
} |
161 | 0 | } |
162 | |
|
163 | 0 | } |
164 | |
} |