Skip to content

Commit

Permalink
Merge pull request #23 from sweettypdevassy/issue_1
Browse files Browse the repository at this point in the history
Created CloudantProducer.java
  • Loading branch information
sweettypdevassy authored Nov 15, 2024
2 parents 9f7d73b + d0e5221 commit 4ee3b5e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<http.port>9080</http.port>
<https.port>9443</https.port>
</properties>

<dependencies>
Expand All @@ -29,6 +31,11 @@
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibm.cloud</groupId>
<artifactId>cloudant</artifactId>
<version>0.9.2</version>
</dependency>
<!-- Test Dependencies-->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -66,8 +73,8 @@
</assemblyArtifact>
<serverName>CloudantServer</serverName>
<bootstrapProperties>
<default.http.port>9080</default.http.port>
<default.https.port>9443</default.https.port>
<default.http.port>${http.port}</default.http.port>
<default.https.port>${https.port}</default.https.port>
<app.context.root>/</app.context.root>
</bootstrapProperties>
</configuration>
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/io/openliberty/sample/cloudant/CloudantProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.sample.cloudant;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.GetDatabaseInformationOptions;
import com.ibm.cloud.cloudant.v1.model.PutDatabaseOptions;
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
import com.ibm.cloud.sdk.core.service.exception.NotFoundException;

@ApplicationScoped
public class CloudantProducer {

@Inject
@ConfigProperty(name = "cloudant.host", defaultValue = "localhost")
String host;

@Inject
@ConfigProperty(name = "cloudant.port", defaultValue = "5984")
String port;

@Inject
@ConfigProperty(name = "cloudant.username")
String username;

@Inject
@ConfigProperty(name = "cloudant.password")
String password;


@Inject
@ConfigProperty(name = "cloudant.dbname")
String dbname;

@Produces
public Cloudant createCloudant() {
BasicAuthenticator authenticator = new BasicAuthenticator.Builder()
.username(username)
.password(password)
.build();

Cloudant service = new Cloudant(Cloudant.DEFAULT_SERVICE_NAME, authenticator);
service.setServiceUrl("http://" + host + ":" + port);

// Check if the database exists, if not, create it
GetDatabaseInformationOptions dbInfoOptions = new GetDatabaseInformationOptions.Builder()
.db(dbname)
.build();

try {
service.getDatabaseInformation(dbInfoOptions).execute();
System.out.println("connected to existing database " + dbname );
} catch (NotFoundException e) {
PutDatabaseOptions dbOptions = new PutDatabaseOptions.Builder()
.db(dbname)
.build();
try{
service.putDatabase(dbOptions).execute();
System.out.println("Created new database " + dbname );
}catch(Exception c){
c.printStackTrace(System.out);
}

}

return service;
}
}

3 changes: 3 additions & 0 deletions src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<httpEndpoint host="*" httpPort="${default.http.port}" httpsPort="${default.https.port}" id="defaultHttpEndpoint"/>

<webApplication location="CloudantSample.war" contextRoot="/"/>
<variable name="cloudant.username" value="admin"/>
<variable name="cloudant.password" value="password"/>
<variable name="cloudant.dbname" value="testdb"/>

</server>

0 comments on commit 4ee3b5e

Please sign in to comment.