Skip to content

Commit

Permalink
Merge pull request #172 from aguibert/demon-config-api
Browse files Browse the repository at this point in the history
Demo config api
  • Loading branch information
Emily-Jiang authored Sep 25, 2017
2 parents 1b5d21e + 483d11f commit 1d21f2f
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 62 deletions.
23 changes: 23 additions & 0 deletions microservice-vote/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@
<artifactId>demo-bootstrap</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -206,6 +212,7 @@
<serverName>${wlpServerName}</serverName>
<assemblyInstallDirectory>${project.build.directory}</assemblyInstallDirectory>
<configFile>src/main/liberty/config/server.xml</configFile>
<serverEnv>${basedir}/src/main/liberty/config/server.env</serverEnv>
<packageFile>${package.file}</packageFile>
<include>${packaging.type}</include>
<bootstrapProperties>
Expand Down Expand Up @@ -331,6 +338,22 @@
<goal>install-server</goal>
</goals>
</execution>
<!-- Install the config feature into the liberty server -->
<execution>
<id>install-feature</id>
<phase>prepare-package</phase>
<goals>
<goal>install-feature</goal>
</goals>
<configuration>
<features>
<acceptLicense>true</acceptLicense>
<feature>mpConfig-1.0</feature>
</features>
<installDirectory>${project.build.directory}/wlp</installDirectory>
<serverName>${wlpServerName}</serverName>
</configuration>
</execution>
<execution>
<id>package-app</id>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package io.microprofile.showcase.vote.persistence.couch;

import javax.enterprise.context.Dependent;

@Dependent
public class Credentials {

private String username;
private String password;
private String url;


public Credentials() {}

public Credentials(String username, String password, String url) {
this.url = url;
this.username = username;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.microprofile.showcase.vote.persistence.couch;

import javax.json.JsonObject;

import org.eclipse.microprofile.config.spi.Converter;

public class CredentialsConverter implements Converter<Credentials> {

@Override
public Credentials convert(String vcapServices) throws IllegalArgumentException {
Credentials creds;
try {
if ( (vcapServices.isEmpty() || vcapServices == null) )
return null;

JsonObject obj = VCAPServices.getCredentials(vcapServices, "cloudantNoSQLDB", "test-cloudantNoSQLDB-000");

String username = obj.getJsonString("username").getString();
String password = obj.getJsonString("password").getString();
String url = obj.getJsonString("url").getString();
creds = new Credentials(username, password, url);
} catch (InvalidCredentialsException e) {
throw new IllegalArgumentException(e);
}
return creds;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,41 @@

package io.microprofile.showcase.vote.persistence.couch;

import java.io.StringReader;
import java.util.Optional;

import javax.annotation.Resource;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class CredentialsProducer {


@Resource(lookup="cloudant/url")
protected String resourceUrl;

@Resource(lookup="cloudant/username")
protected String resourceUsername;

@Resource(lookup="cloudant/password")
protected String resourcePassword;

@Inject
@ConfigProperty(name="VCAP_SERVICES")
Optional <Credentials> cred;

@Produces
public Credentials newCredentials() {
Credentials credentials = null;
String vcap = System.getenv("VCAP_SERVICES");
if (vcap != null) {
credentials = parseVcap(vcap);
} else {
credentials = useEnv();
}

Credentials credentials = cred.orElse(null);
if (credentials == null) {
if ( (("${env.CLOUDANT_URL}").equals(resourceUrl)) && (("${env.CLOUDANT_USERNAME}").equals(resourceUsername))
&& ( ("${env.CLOUDANT_PASSWORD}").equals(resourcePassword) ) )
credentials = null;
else
credentials = new Credentials(resourceUsername, resourcePassword, resourceUrl);
}
return credentials;
}

private Credentials parseVcap(String vcapServices) {

JsonObject vcapServicesJson = Json.createReader(new StringReader(vcapServices)).readObject();
JsonArray cloudantObjectArray = vcapServicesJson.getJsonArray("cloudantNoSQLDB");
if (cloudantObjectArray == null) {
return useEnv();
}
JsonObject cloudantObject = cloudantObjectArray.getJsonObject(0);
JsonObject cloudantCredentials = cloudantObject.getJsonObject("credentials");
JsonString cloudantUsername = cloudantCredentials.getJsonString("username");

JsonString cloudantPassword = cloudantCredentials.getJsonString("password");
JsonString cloudantUrl = cloudantCredentials.getJsonString("url");

String username = cloudantUsername.getString();
String password = cloudantPassword.getString();
String url = cloudantUrl.getString();

return new Credentials(username, password, url);
}

private Credentials useEnv() {

String username = System.getenv("dbUsername");
String password = System.getenv("dbPassword");
String url = System.getenv("dbUrl");

if (username != null && password != null && url != null) {
return new Credentials(username, password, url);
} else
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.microprofile.showcase.vote.persistence.couch;

public class InvalidCredentialsException extends Exception {

private static final long serialVersionUID = 1L;

public InvalidCredentialsException() {
}

public InvalidCredentialsException(String message) {
super(message);
}

public InvalidCredentialsException(Throwable cause) {
super(cause);
}

public InvalidCredentialsException(String message, Throwable cause) {
super(message, cause);
}

public InvalidCredentialsException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.microprofile.showcase.vote.persistence.couch;

import java.io.StringReader;
import java.util.Iterator;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;

public class VCAPServices {

public static JsonObject getCredentials(String vcapServicesEnv, String serviceType, String serviceName) throws InvalidCredentialsException {
JsonObject credentials = null;
JsonArray vcapServiceArray = Json.createReader(new StringReader(vcapServicesEnv)).readArray();
Iterator<JsonValue> vsItr = vcapServiceArray.iterator();
while (vsItr.hasNext()) {
JsonObject vcapServices = (JsonObject) vsItr.next();
JsonArray serviceObjectArray = vcapServices.getJsonArray(serviceType);
if (serviceObjectArray == null)
continue;

JsonObject serviceObject = null;
if (serviceName == null || serviceName.isEmpty()) {
serviceObject = serviceObjectArray.getJsonObject(0);
} else {
Iterator<JsonValue> itr = serviceObjectArray.iterator();
while (itr.hasNext()) {
JsonObject object = (JsonObject) itr.next();
if (serviceName.equals(object.getJsonString("name").getString())) {
serviceObject = object;
}
}
}
checkNotNull(serviceObject);
credentials = serviceObject.getJsonObject("credentials");
checkNotNull(credentials);
if (serviceObject != null)
break;
}
return credentials;
}

private static void checkNotNull(Object object) throws InvalidCredentialsException {
if (object == null) {
throw new InvalidCredentialsException("Unable to parse VCAP_SERVICES");
}
}
}
8 changes: 5 additions & 3 deletions microservice-vote/src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
below or put a server.xml file in configDropins/overrides. Note: you may need to add Maven provided
dependencies for use at compile time if not using app accelerator dependencies. -->

<!--
<featureManager>
<feature><feature>
<feature>mpConfig-1.0</feature>
</featureManager>
-->

<httpEndpoint httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>

<jndiEntry jndiName="cloudant/url" value="${env.CLOUDANT_URL}"/>
<jndiEntry jndiName="cloudant/username" value="${env.CLOUDANT_USERNAME}"/>
<jndiEntry jndiName="cloudant/password" value="${env.CLOUDANT_PASSWORD}"/>

<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.microprofile.showcase.vote.persistence.couch.CredentialsConverter
12 changes: 5 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<version.payara>4.1.1.163</version.payara>
<version.tomee>7.0.1</version.tomee>
<version.wildfly>10.0.0.Final</version.wildfly>
<version.liberty>17.0.0.2</version.liberty>
<version.liberty>2017.9.+</version.liberty>

<!-- Dependencies -->
<version.microprofile>1.0.0</version.microprofile>
Expand Down Expand Up @@ -436,12 +436,10 @@
<artifactId>liberty-maven-plugin</artifactId>
<version>${version.liberty.plugin}</version>
<configuration>
<assemblyArtifact>
<groupId>com.ibm.websphere.appserver.runtime</groupId>
<artifactId>wlp-microProfile1</artifactId>
<version>${version.liberty}</version>
<type>zip</type>
</assemblyArtifact>
<install>
<type>webProfile7</type>
<version>${version.liberty}</version>
</install>
</configuration>
</plugin>
</plugins>
Expand Down

0 comments on commit 1d21f2f

Please sign in to comment.