Skip to content

Commit 01e4974

Browse files
committed
Stackify API Common
1 parent 031ae39 commit 01e4974

15 files changed

Lines changed: 1259 additions & 0 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.classpath
2+
.project
3+
.settings
4+
target
5+
coverage.ec

pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>com.stackify</groupId>
6+
<artifactId>stackify-parent-pom</artifactId>
7+
<version>1.0.2</version>
8+
</parent>
9+
10+
<groupId>com.stackify</groupId>
11+
<artifactId>stackify-api-common</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
<name>Stackify API Common</name>
14+
<scm>
15+
<connection>scm:git:git@bitbucket.org:stackify/stackify-api-common.git</connection>
16+
<developerConnection>scm:git:git@bitbucket.org:stackify/stackify-api-common.git</developerConnection>
17+
<url>https://bitbucket.org/stackify/stackify-api-common</url>
18+
</scm>
19+
20+
<dependencies>
21+
22+
<!-- Compile time dependencies -->
23+
24+
<dependency>
25+
<groupId>com.stackify</groupId>
26+
<artifactId>stackify-api</artifactId>
27+
<version>1.0.0-SNAPSHOT</version>
28+
</dependency>
29+
30+
<!-- Runtime dependencies -->
31+
32+
<!-- Test dependencies -->
33+
34+
<dependency>
35+
<groupId>junit</groupId>
36+
<artifactId>junit</artifactId>
37+
<version>4.11</version>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.mockito</groupId>
43+
<artifactId>mockito-all</artifactId>
44+
<version>1.9.5</version>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>org.powermock</groupId>
50+
<artifactId>powermock-api-mockito</artifactId>
51+
<version>1.5</version>
52+
<scope>test</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.powermock</groupId>
57+
<artifactId>powermock-module-junit4</artifactId>
58+
<version>1.5</version>
59+
<scope>test</scope>
60+
</dependency>
61+
62+
</dependencies>
63+
64+
<repositories>
65+
<repository>
66+
<id>stackify-release-repo</id>
67+
<name>Stackify Release Repository</name>
68+
<url>http://dev-ubuntu1/nexus/content/repositories/releases/</url>
69+
</repository>
70+
<repository>
71+
<id>stackify-snapshot-repo</id>
72+
<name>Stackify Snapshot Repository</name>
73+
<url>http://dev-ubuntu1/nexus/content/repositories/snapshots/</url>
74+
</repository>
75+
</repositories>
76+
77+
<distributionManagement>
78+
<site>
79+
<id>stackify-release-site</id>
80+
<name>Stackify Release Site</name>
81+
<url>dav:http://dev-ubuntu1/nexus/content/repositories/release-sites/${project.groupId}/${project.artifactId}/${project.version}/</url>
82+
</site>
83+
</distributionManagement>
84+
85+
<profiles>
86+
<profile>
87+
<id>snapshot</id>
88+
<distributionManagement>
89+
<site>
90+
<id>stackify-release-site</id>
91+
<name>Stackify Release Site</name>
92+
<url>dav:http://dev-ubuntu1/nexus/content/repositories/snapshot-sites/${project.groupId}/${project.artifactId}/${project.version}/</url>
93+
</site>
94+
</distributionManagement>
95+
</profile>
96+
</profiles>
97+
98+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2013 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common;
17+
18+
import java.io.InputStream;
19+
import java.util.Properties;
20+
21+
import com.stackify.api.ApiClient;
22+
23+
/**
24+
* Utility class for api client details and building an ApiClient object
25+
*
26+
* @author Eric Martin
27+
*/
28+
public class ApiClients {
29+
30+
/**
31+
* Creates an api client object with information from the specified properties file
32+
* @param propertyFileName The file name of the properties file containing api client details (could be null)
33+
* @return The ApiClient object
34+
*/
35+
public static ApiClient getApiClient(final Class<?> apiClass, final String propertyFileName) {
36+
37+
String name = null;
38+
String version = null;
39+
40+
// get the name and version from the properties stream
41+
42+
InputStream propertiesStream = null;
43+
44+
try {
45+
propertiesStream = apiClass.getResourceAsStream(propertyFileName);
46+
47+
if (propertiesStream != null) {
48+
Properties props = new Properties();
49+
props.load(propertiesStream);
50+
name = (String) props.get("api-client.name");
51+
version = (String) props.get("api-client.version");
52+
}
53+
} catch (Throwable t) {
54+
// do nothing
55+
} finally {
56+
if (propertiesStream != null) {
57+
try {
58+
propertiesStream.close();
59+
} catch (Throwable t) {
60+
// do nothing
61+
}
62+
}
63+
}
64+
65+
// build the api client
66+
67+
ApiClient.Builder apiClientBuilder = ApiClient.newBuilder();
68+
apiClientBuilder.name(name);
69+
apiClientBuilder.version(version);
70+
71+
return apiClientBuilder.build();
72+
}
73+
74+
/**
75+
* Hidden to prevent construction
76+
*/
77+
private ApiClients() {
78+
}
79+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2013 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common;
17+
18+
import java.net.InetAddress;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
22+
import com.stackify.api.EnvironmentDetail;
23+
24+
/**
25+
* Utility class for retrieving environment details and building an EnvironmentDetail object
26+
*
27+
* @author Eric Martin
28+
*/
29+
public class EnvironmentDetails {
30+
31+
/**
32+
* Creates an environment details object with system information
33+
* @param application The configured application name
34+
* @param location The configured application location
35+
* @param environment The configured application environment
36+
* @return The EnvironmentDetail object
37+
*/
38+
public static EnvironmentDetail getEnvironmentDetail(final String application, final String location, final String environment) {
39+
40+
// lookup the hostname
41+
42+
String hostName = null;
43+
44+
try {
45+
InetAddress addr = InetAddress.getLocalHost();
46+
hostName = addr.getHostName();
47+
} catch (Throwable t) {
48+
// Do nothing
49+
}
50+
51+
// lookup the current path
52+
53+
Path currentRelativePath = Paths.get("");
54+
String currentPath = currentRelativePath.toAbsolutePath().toString();
55+
56+
// build the environment details
57+
58+
EnvironmentDetail.Builder environmentBuilder = EnvironmentDetail.newBuilder();
59+
environmentBuilder.deviceName(hostName);
60+
environmentBuilder.appLocation(currentPath);
61+
environmentBuilder.configuredAppName(application);
62+
environmentBuilder.configuredLocationName(location);
63+
environmentBuilder.configuredEnvironmentName(environment);
64+
65+
return environmentBuilder.build();
66+
}
67+
68+
/**
69+
* Hidden to prevent construction
70+
*/
71+
private EnvironmentDetails() {
72+
}
73+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2013 Stackify
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stackify.api.common.http;
17+
18+
import java.io.BufferedReader;
19+
import java.io.InputStream;
20+
import java.io.InputStreamReader;
21+
import java.net.HttpURLConnection;
22+
23+
/**
24+
* Utility class for cleaning up an HttpURLConnection object
25+
*
26+
* @author Eric Martin
27+
*/
28+
public class HttpUrlConnections {
29+
30+
/**
31+
* Reads and closes the input and error streams for a connection
32+
* @param connection The connection
33+
*/
34+
public static void readAndCloseInputStreams(final HttpURLConnection connection) {
35+
36+
if (connection != null) {
37+
38+
// read and close the input stream
39+
40+
try {
41+
readAndCloseInputStream(connection.getInputStream());
42+
} catch (Throwable t) {
43+
// do nothing
44+
}
45+
46+
// read and close the error stream
47+
48+
try {
49+
readAndCloseInputStream(connection.getErrorStream());
50+
} catch (Throwable t) {
51+
// do nothing
52+
}
53+
}
54+
}
55+
56+
/**
57+
* Reads all contents from the input stream and closes it
58+
* @param stream The input stream
59+
*/
60+
public static void readAndCloseInputStream(final InputStream stream) {
61+
if (stream != null) {
62+
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
63+
try {
64+
while (reader.readLine() != null) {
65+
// do nothing
66+
}
67+
} catch (Throwable t) {
68+
// do nothing
69+
} finally {
70+
try {
71+
reader.close();
72+
} catch (Throwable t) {
73+
// do nothing
74+
}
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Hidden to prevent construction
81+
*/
82+
private HttpUrlConnections() {
83+
}
84+
}

0 commit comments

Comments
 (0)