All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stratio.provider.mongodb.MongodbClientFactory.scala Maven / Gradle / Ivy

There is a newer version: 0.8.7
Show newest version
package com.stratio.provider.mongodb

import javax.net.ssl.SSLSocketFactory

import com.mongodb.ServerAddress
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.{MongoClient, MongoClientOptions}

/**
 * Different client configurations to Mongodb database
 */
object MongodbClientFactory {

  type Client = MongoClient

  def createClient(host : String) : Client = MongoClient(host)

  def createClient(host : String, port : Int, user: String, database: String, password :String) : Client = {

    val credentials : List[MongoCredential]= List(MongoCredential.createCredential(user, database, password.toCharArray))
    val hostPort = new ServerAddress(host, port)
    MongoClient(hostPort, credentials)
  }

  def createClient(hostPort : List[ServerAddress], credentials : List[MongoCredential]) : Client = MongoClient(hostPort, credentials)

  def createClient(
    hostPort : List[ServerAddress],
    credentials : List[MongoCredential],
    optionSSLOptions: Option[MongodbSSLOptions]) : Client = {

    optionSSLOptions match{
      case Some(sslOptions) =>

        if(sslOptions.keyStore.nonEmpty) {
          System.setProperty("javax.net.ssl.keyStore", sslOptions.keyStore.get)
          if (sslOptions.keyStorePassword.nonEmpty)
            System.setProperty("javax.net.ssl.keyStorePassword", sslOptions.keyStorePassword.get)
        }
        if(sslOptions.trustStore.nonEmpty) {
          System.setProperty("javax.net.ssl.trustStore", sslOptions.trustStore)
          if (sslOptions.trustStorePassword.nonEmpty)
            System.setProperty("javax.net.ssl.trustStorePassword", sslOptions.trustStorePassword.get)
        }

        val options = new MongoClientOptions.Builder().socketFactory(SSLSocketFactory.getDefault()).build()
        MongoClient(hostPort, credentials, options)

      case _ => MongoClient(hostPort, credentials)
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy