scala & mongodb : access denied

36 views
Skip to first unread message

lolveley

unread,
Oct 22, 2012, 3:21:55 PM10/22/12
to vcap...@cloudfoundry.org
hello

I have installed eclipse, the cloudfoundry plugin, the scala plugin,the vaadin plugin(for web developments) and the mongodb libraries.

I created a class like this :

Saisissez le cpackage com.example.acces_base_objet_1

import vaadin.scala.Application
import vaadin.scala.VerticalLayout
import com.mongodb.casbah.MongoConnection
import com.mongodb.casbah.commons.MongoDBObject
import vaadin.scala.Label
import vaadin.scala.Button

class Launcher extends Application {

  val label
=new Label
 
 
override def main = new VerticalLayout() {
   
      val coll
=MongoConnection()("mybd")("somecollection")
     
      val builder
=MongoDBObject.newBuilder
      builder
+="foo1" -> "bar"
     
var newobj=builder.result()
      coll
.save(newobj)
     
      val mongoColl
=MongoConnection()("mybd")("somecollection")
     
      val withFoo
=mongoColl.findOne()
     
      label
.value=withFoo
      add
(label)
     
     
//bouton pour faire joli
      add
(new Button{
        caption_
=("click me!")
     
})
   
 
}
}
the error (the access to the mongodb database is denied) comes from the parameters (the "()" after "val coll=MongoConnection"), which are the default ones.
do you know how to set up the good parameters in scala or in java?

thanks,

olivier

Jennifer Hickey

unread,
Oct 22, 2012, 6:10:27 PM10/22/12
to vcap...@cloudfoundry.org
You can use the cloudfoundry-runtime jar to programmatically connect to CF services.  This example shows how to use it to make a Mongo connection in a Scala app, and you can also check out the SBT config for bringing in the cloudfoundry-runtime dependency from the maven repo:


Jennifer

olivier SAINT-EVE

unread,
Oct 23, 2012, 7:31:46 AM10/23/12
to vcap...@cloudfoundry.org
hello

thx for ur answer.
I did not althought understood all this code :

**********************************************
trait MongoGridFSStorage extends Storage { self =>
  import com.mongodb.casbah.Imports._
  import com.mongodb.casbah.gridfs.Imports._
  val cloudEnvironment = new CloudEnvironment()
  val mongoServices = cloudEnvironment.getServiceInfos(classOf[MongoServiceInfo])
  lazy val mongo = mongoServices.asScala.toList match {
&nbs p;  case head :: _ => {
   val mongodb = MongoConnection(head.getHost(), head.getPort())(head.getDatabase())
   mongodb.authenticate(head.getUserName(),head.getPassword())
   mongodb
   }
      case _ => {
        val mongodb = MongoConnection("staff.mongohq.com", 10004)("bitshow")
        if(mongodb.authenticate("scalany","N8HANISMYHERO"))
          mongodb
        else throw new IllegalArgumentException("DEATH AND DESTRUCTION! PASSWORD FAILURE!")
     }
  } ********************************************* what does mean the second part (case _)?What are the parameters of "val mongodb = MongoConnection(...)" and "mongodb.authenticate(....)" mean? to my point of view, I only need to obtain the ids like in "case head ::..." but I don't know what to put in the second part of the program. olivier



Le 23/10/12 00:10, Jennifer Hickey a écrit :

Jennifer Hickey

unread,
Oct 23, 2012, 9:46:47 AM10/23/12
to vcap...@cloudfoundry.org
The code is looking for information about a Mongo service that you have bound to your application when deploying to Cloud Foundry.  If it finds this service, it initializes MongoConnection with the connection information specific to the CF service.   "Case _" is the fallback so you can create a local MongoConnection (or in this case, a connection to a different Mongo server) if you are not running the app on Cloud Foundry.

olivier SAINT-EVE

unread,
Oct 23, 2012, 9:52:28 AM10/23/12
to vcap...@cloudfoundry.org
thx!
I will soon code this, and if  have a problem I will send another mail.

olivier
Le 23/10/12 15:46, Jennifer Hickey a écrit :

olivier SAINT-EVE

unread,
Oct 24, 2012, 7:45:12 AM10/24/12
to vcap...@cloudfoundry.org
please see the joined document.
it seems "mongo" does not have a getUsername method, although it has a getPassword one...

I don't understand how it works.

olivier


Le 23/10/12 15:52, olivier SAINT-EVE a écrit :
Nouveau document texte (2).txt

olivier SAINT-EVE

unread,
Oct 24, 2012, 7:54:18 AM10/24/12
to vcap...@cloudfoundry.org
here is the code :
package com.example.vaadin_1

import vaadin.scala.Application
import org.cloudfoundry.runtime.env.CloudEnvironment
import org.cloudfoundry.runtime.env.MongoServiceInfo
import com.mongodb.casbah.MongoConnection


class Launcher extends Application {

   val cloudEnvironment = new CloudEnvironment()
   val mongoServices = cloudEnvironment.getServiceInfos(classOf[MongoServiceInfo])
   val mongo = mongoServices.get(0)
   val mongodb = MongoConnection(mongo.getHost(), mongo.getPort())("abc")
   mongodb.authenticate(mongo.getUserName(),mongo.getPassword())
 
 
}

Le 24/10/12 13:45, olivier SAINT-EVE a écrit :

olivier SAINT-EVE

unread,
Oct 24, 2012, 2:56:36 PM10/24/12
to vcap...@cloudfoundry.org
hello

please help me!
I can't obtain infos concerning the mongoDB database with this program:

********************************************************
class Vaadin_2Application extends Application {

  var label = new Label

  override val main = new VerticalLayout {


    val cloudEnvironment = new CloudEnvironment()
    val mongoServices = cloudEnvironment.getServiceInfos(classOf[MongoServiceInfo])

    label.value = mongoServices.toString()
    add(label)
  }


********************************************************
there is no crash at the launching of the site in cloudfoundry : the dependencies are satisfied, but nothing appears in the browser(only a little square).
before trying to get the mongoDB parameters I would like to see the JSON file.

olivier


Le 24/10/12 13:54, olivier SAINT-EVE a écrit :

Dave Syer

unread,
Oct 25, 2012, 6:36:12 AM10/25/12
to vcap...@cloudfoundry.org
On 24/10/12 12:45, olivier SAINT-EVE wrote:
> it seems "mongo" does not have a getUsername method

Try getUserName(). Your IDE should help you.

D.

--
Dave Syer
ds...@vmware.com

Reply all
Reply to author
Forward
0 new messages