-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from aguibert/demon-config-api
Demo config api
- Loading branch information
Showing
9 changed files
with
168 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
28 changes: 28 additions & 0 deletions
28
...e/src/main/java/io/microprofile/showcase/vote/persistence/couch/CredentialsConverter.java
This file contains 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
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; | ||
} | ||
|
||
} |
This file contains 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
27 changes: 27 additions & 0 deletions
27
...ain/java/io/microprofile/showcase/vote/persistence/couch/InvalidCredentialsException.java
This file contains 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
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); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...vice-vote/src/main/java/io/microprofile/showcase/vote/persistence/couch/VCAPServices.java
This file contains 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
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"); | ||
} | ||
} | ||
} |
This file contains 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
1 change: 1 addition & 0 deletions
1
...vice-vote/src/main/webapp/META-INF/services/org.eclipse.microprofile.config.spi.Converter
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.microprofile.showcase.vote.persistence.couch.CredentialsConverter |
This file contains 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