MongoDb来自jndi

时间:2010-11-02 09:14:19

标签: java mongodb jndi nosql

你知道是否有可能在jndi中通过datasource设置mongodb实例和其他数据库一样?

THX

5 个答案:

答案 0 :(得分:11)

是的,有可能,为什么在你可以创建自己的JNDI工厂时依赖别人的代码呢? 只需创建一个实现javax.naming.spi.ObjectFactory的类和一个从JNDI上下文中提取mongo的bean,我将其配置为spring data-mongo MongoTemplate对象。

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);
    }
}

}

Spring bean:

@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>

答案 1 :(得分:3)

重用Juan Melo的ObjectFactory接口(CustomMongoJNDIFactory)的自定义实现,也可以使用Spring的jee命名空间的jndi-lookup标签和{{1}中相应的Tomcat配置进行配置},像这样:

context.xml file

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

答案 2 :(得分:2)

为此,您需要一个用于MongoDB的JDBC驱动程序impl。我只找到一个,它在MongoDB页面中被称为“实验性的”:GitHub JDBC Driver for MongoDB

为了解决这个限制,您可以设置一些Spring bean并为您的应用程序DAO创建一个MongoDB实现(这样,您就不需要更改DAO接口及其客户端组件)。

本文可能有所帮助:

答案 3 :(得分:0)

如果您的意思是像具有JDBC访问权限的常规RDBMS,那么答案就是否定。

答案 4 :(得分:-1)

还有另一项努力为MongoDB提供JDBC驱动程序impl。这里:

https://sourceforge.net/projects/mongojdbcdriver

任何措施都不完整,但希望很快提供Java开发人员熟悉的JDBC实现。