1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.appstatus.core.property.impl;
15
16 import java.io.InputStream;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Properties;
20
21 import javax.servlet.ServletContext;
22
23 import net.sf.appstatus.core.IServletContextAware;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31
32
33
34 public class WarMavenVersionProvider extends AbstractPropertyProvider implements IServletContextAware {
35
36 private static final String ARTIFACT_ID = "artifactId";
37 private static final String CATEGORY = "maven";
38 private static final String GROUP_ID = "groupId";
39 private static Logger logger = LoggerFactory.getLogger(WarMavenVersionProvider.class);
40 private static final String NOT_AVAILABLE = "Not available";
41 private static final String VERSION = "version";
42 private ServletContext servletContext = null;
43
44
45
46
47
48
49 public String getCategory() {
50 return CATEGORY;
51 }
52
53
54
55
56
57
58 public Map<String, String> getProperties() {
59 InputStream url = null;
60 Properties pomProperties = null;
61 Map<String, String> prop = new HashMap<String, String>();
62 if (this.servletContext != null) {
63 try {
64 url = servletContext.getResourceAsStream(servletContext
65 .getResourcePaths(
66 (String) servletContext.getResourcePaths("/META-INF/maven/").iterator().next())
67 .iterator().next()
68 + "pom.properties");
69 if (url != null) {
70 pomProperties = new Properties();
71 pomProperties.load(url);
72 url.close();
73 }
74 } catch (Exception e) {
75 logger.info("Error getting maven information from /META-INF/maven/*. Not a maven war.");
76 }
77 if (pomProperties == null) {
78 prop.put(VERSION, NOT_AVAILABLE);
79 prop.put(GROUP_ID, NOT_AVAILABLE);
80 prop.put(ARTIFACT_ID, NOT_AVAILABLE);
81 } else {
82 prop.put(VERSION, pomProperties.getProperty(VERSION));
83 prop.put(GROUP_ID, pomProperties.getProperty(GROUP_ID));
84 prop.put(ARTIFACT_ID, pomProperties.getProperty(ARTIFACT_ID));
85 }
86 }
87
88 return prop;
89 }
90
91
92
93
94
95
96 public void setServletContext(ServletContext servletContext) {
97 this.servletContext = servletContext;
98 }
99
100 }