2010-11-02 6 views
5

Savez-vous s'il est possible de configurer l'instance mongodb au printemps comme n'importe quelle autre base de données via jndi?MongoDb via jndi

Thx

Répondre

0

Si vous voulez dire, comme SGBDR doté d'un accès JDBC, alors la réponse est non.

2

Pour ce faire, vous aurez besoin d'un pilote JDBC impl pour MongoDB. J'ai seulement trouvé un, et il est référé comme "expérimental" de la page MongoDB: GitHub JDBC Driver for MongoDB. Pour contourner cette limitation, vous pouvez configurer des beans Spring et créer une implémentation MongoDB pour votre DAO d'application (vous n'aurez donc pas besoin de modifier l'interface DAO et ses composants clients).

Ces articles peuvent aider:

11

Oui, il est possible, pourquoi se fondant dans le code quelqu'un elses quand vous pouvez créer votre propre usine JNDI? juste créer une classe qui implémente javax.naming.spi.ObjectFactory et un bean qui tire mongo du contexte JNDI, je l'ai configuré pour l'objet data-mongo MongoTemplate de printemps.

public class CustomMongoJNDIFactory implements ObjectFactory { 

public Object getObjectInstance(Object obj, Name name, Context nameCtx, 
     Hashtable<?, ?> environment) throws Exception { 

    validateProperty(obj, "Invalid JNDI object reference"); 

    MongoTemplate mongoTemplate = null; 
    String db = null; 
    String host = null; 
    String username = null; 
    String password = null; 
    int port = 27017; 

    Reference ref = (Reference) obj; 
    Enumeration<RefAddr> props = ref.getAll(); 
    while (props.hasMoreElements()) { 
     RefAddr addr = (RefAddr) props.nextElement(); 
     String propName = addr.getType(); 
     String propValue = (String) addr.getContent(); 
     if (propName.equals("db")) { 
      db = propValue; 
     } else if (propName.equals("host")) { 
      host = propValue; 
     } else if (propName.equals("username")) { 
      username = propValue; 
     } else if (propName.equals("password")) { 
      password = propValue; 
     } else if (name.equals("port")) { 
      try { 
       port = Integer.parseInt(propValue); 
      } catch (NumberFormatException e) { 
       throw new NamingException("Invalid port value " + propValue); 
      } 
     } 

    } 

    // validate properties 
    validateProperty(db, "Invalid or empty mongo database name"); 
    validateProperty(host, "Invalid or empty mongo host"); 
    validateProperty(username, "Invalid or empty mongo username"); 
    validateProperty(password, "Invalid or empty mongo password"); 

    //create mongo template 
    mongoTemplate = new MongoTemplate(new Mongo(host, port), db, 
      new UserCredentials(username, password)); 

    return mongoTemplate; 
} 


/** 
* Validate internal String properties 
* 
* @param property 
* @param errorMessage 
* @throws NamingException 
*/ 
private void validateProperty(String property, String errorMessage) 
     throws NamingException { 
    if (property == null || property.trim().equals("")) { 
     throw new NamingException(errorMessage); 
    } 
} 

/** 
* Validate internal Object properties 
* 
* @param property 
* @param errorMessage 
* @throws NamingException 
*/ 
private void validateProperty(Object property, String errorMessage) 
     throws NamingException { 
    if (property == null) { 
     throw new NamingException(errorMessage); 
    } 
} 

}

bean Spring:

@Configuration 
@Qualifier("mongoTemplate") 
public class CustomMongoTemplate { 


public @Bean MongoTemplate mongoTemplate() throws Exception { 
    Context initCtx = new InitialContext(); 
    Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
    return (MongoTemplate) envCtx.lookup("bean/MyMongoBean"); 
    } 
} 

context.xml:

<Resource name="bean/MyMongoBean" auth="Container" 
     type="org.springframework.data.mongodb.core.MongoTemplate" 
     factory="com.package.CustomMongoJNDIFactory" 
     host="" db="" username="" password=""/> 

Web.xml

<resource-env-ref> 
    <description>Mongo JNDI configuration</description> 
    <resource-env-ref-name>comp/env/bean/MyMongoBean</resource-env-ref-name> 
    <resource-env-ref-type>org.springframework.data.mongodb.core.MongoTemplate</resource-env-ref-type> 
</resource-env-ref> 
3

Réutiliser Juan la mise en œuvre personnalisée Melo de l'interface ObjectFactory (CustomMongoJNDIFactory), il peut également être configuré en utilisant balise de recherche JNDI espace de noms de Spring jee et correspondant config Tomcat dans context.xml file, comme ceci:

spring-mongodb-persistence-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> 

    <jee:jndi-lookup id="mongoTemplate" jndi-name="java:/comp/env/jndi/MongoDB" expected-type="org.springframework.data.mongodb.core.MongoTemplate" /> 

    <mongo:repositories base-package="com.package.repository.mongodb" /> 

</beans> 

context.xml:

<Resource name="jndi/MongoDB" 
    auth="Container" 
    type="org.springframework.data.mongodb.core.MongoTemplate" 
    factory="com.package.mongo.CustomMongoJNDIFactory" 
    username="test" 
    password="test" 
    host="localhost" 
    port="27017" 
    db="test" />