Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Do you know if it is possible to setup mongodb instance in spring like any other db via datasource from jndi?

Thx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
99 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes it is possible, why relying in someone elses code when you can create your own JNDI factory? just create a class that implements javax.naming.spi.ObjectFactory and a bean that pulls mongo from the JNDI context, I configured this for spring data-mongo MongoTemplate object.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...