View Javadoc

1   /*
2    * Copyright 2010 Capgemini and others. Licensed under the Apache License,
3    * Version 2.0 (the "License"); you may not use this file except in compliance
4    * with the License. You may obtain a copy of the License at
5    * 
6    * http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
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   * Retrieve POM informations for WAR archive.
30   * 
31   * @author Nicolas Richeton (Capgemini)
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  	 * {@inheritDoc}
46  	 * 
47  	 * @see net.sf.appstatus.core.property.IPropertyProvider#getCategory()
48  	 */
49  	public String getCategory() {
50  		return CATEGORY;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 * 
56  	 * @see net.sf.appstatus.core.property.IPropertyProvider#getProperties()
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  	 * {@inheritDoc}
93  	 * 
94  	 * @see net.sf.appstatus.core.IServletContextAware#setServletContext(javax.servlet.ServletContext)
95  	 */
96  	public void setServletContext(ServletContext servletContext) {
97  		this.servletContext = servletContext;
98  	}
99  
100 }