-
Notifications
You must be signed in to change notification settings - Fork 2
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 #23 from sweettypdevassy/issue_1
Created CloudantProducer.java
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 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
81 changes: 81 additions & 0 deletions
81
src/main/java/io/openliberty/sample/cloudant/CloudantProducer.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,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; | ||
} | ||
} | ||
|
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