Last active
November 23, 2015 22:36
-
-
Save jalex19100/65362a7d54c0152708d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Also: https://maven.apache.org/plugin-developers/cookbook/add-svn-revision-to-manifest.html | |
| // pom.xml pieces | |
| <plugin> | |
| <groupId>org.codehaus.mojo</groupId> | |
| <artifactId>versions-maven-plugin</artifactId> | |
| <version>${versions-maven-plugin.version}</version> | |
| </plugin> | |
| <plugin> | |
| <groupId>com.google.code.maven-svn-revision-number-plugin</groupId> | |
| <artifactId>svn-revision-number-maven-plugin</artifactId> | |
| <version>${svn-revision-number-maven-plugin.version}</version> | |
| </plugin> | |
| <resources> | |
| <resource> | |
| <directory>src/main/resources</directory> | |
| <filtering>true</filtering> | |
| </resource> | |
| </resources> | |
| <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | |
| <manifestEntries> | |
| <Main-Class>com.aep.monitoring.cli.Client</Main-Class> | |
| <Specification-Title>${project.name}</Specification-Title> | |
| <Specification-Version>${project.parent.version}</Specification-Version> | |
| <Implementation-Version>[Build #${BUILD_TAG} | r${SVN_REVISION} | ID: ${BUILD_ID} | Tag: | |
| ${app-svn-rev.repository}/${app-svn-rev.path}] | |
| </Implementation-Version> | |
| </manifestEntries> | |
| </transformer> | |
| // version.properties: | |
| version=[Build #${BUILD_TAG} | r${SVN_REVISION} | ID: ${BUILD_ID} | Tag: ${app-svn-rev.repository}/${app-svn-rev.path}] | |
| // VersionResource.java: | |
| package resources; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.net.InetAddress; | |
| import java.util.Collections; | |
| import java.util.Map; | |
| import java.util.Properties; | |
| import javax.annotation.Nullable; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.core.Context; | |
| import javax.ws.rs.core.MediaType; | |
| import org.apache.commons.lang3.StringUtils; | |
| import org.apache.commons.logging.Log; | |
| import org.apache.commons.logging.LogFactory; | |
| import com.google.common.base.Predicate; | |
| import com.google.common.collect.Maps; | |
| /** | |
| * Class: VersionResource | |
| */ | |
| @Path("/version") | |
| @Produces({MediaType.APPLICATION_JSON, TEXT_JSON}) | |
| public class VersionResource { | |
| public static final String TEXT_JSON = "text/json"; | |
| private static final Log log = LogFactory.getLog(VersionResource.class); | |
| @Context | |
| HttpServletRequest request; | |
| @GET | |
| @Path("/number") | |
| @Produces({MediaType.APPLICATION_JSON, }) | |
| public Map<String, String> getVersionNumber() { | |
| return Maps.filterKeys(getVersionProperties(), new Predicate<String>() { | |
| @Override | |
| public boolean apply(@Nullable final String s) { | |
| return StringUtils.equalsIgnoreCase("version", s); | |
| } | |
| }); | |
| } | |
| @GET | |
| @Path("/list") | |
| @Produces({MediaType.APPLICATION_JSON, TEXT_JSON}) | |
| public Map<String, String> getVersionProperties() { | |
| try { | |
| final InputStream stream = getResourceAsStream(); | |
| if (null != stream) { | |
| try { | |
| final Properties properties = new Properties(); | |
| properties.load(stream); | |
| properties.setProperty("node", request.getServerName()); | |
| return properties.isEmpty() ? Collections.<String, String>emptyMap() : asSortedMap(Maps.fromProperties(properties)); | |
| } finally { | |
| stream.close(); | |
| } | |
| } | |
| } catch (final IOException e) { | |
| log.error("problem reading version.properties", e); | |
| } | |
| return Collections.emptyMap(); | |
| } | |
| @GET | |
| @Path("/verbose") | |
| @Produces({MediaType.APPLICATION_JSON, TEXT_JSON}) | |
| public Map<String, String> getVerboseVersionProperties() { | |
| final Map<String, String> result = getVersionProperties(); | |
| result.put("env.host", System.getenv("HOSTNAME")); | |
| result.put("weblogic.Name", System.getenv("weblogic.Name")); | |
| try { | |
| result.put("net.host", InetAddress.getLocalHost().getHostName()); | |
| } catch (final IOException e) { | |
| log.error("problem calling InetAddress.getLocalHost()", e); | |
| } | |
| return result; | |
| } | |
| protected InputStream getResourceAsStream() throws IOException { | |
| return getClass().getResourceAsStream("/version.properties"); | |
| } | |
| private Map<String, String> asSortedMap(final Map<String, String> map) { | |
| final Map<String, String> result = Maps.newTreeMap(); | |
| result.putAll(map); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment