简体   繁体   English

从XML文件配置JNDI InitialContext

[英]Configuring JNDI InitialContext from XML file

I'd like to define JNDI lookups in my app's own XML config file (somewhat similar to Tomcat's context.xml ), and then reference them in my app. 我想在应用程序自己的XML配置文件(有点类似于Tomcat的context.xml )中定义JNDI查找,然后在我的应用程序中引用它们。 Then, under the hood, the app uses JNDI to look the objects up with the information stored in the XML file. 然后,在后台,该应用程序使用JNDI来查找对象以及存储在XML文件中的信息。

For instance, given the following snippet from my app's XML config file: 例如,给定我的应用程序的XML配置文件中的以下代码段:

<app>
    <!-- lots of stuff -->

    <dataSource name="jdbc/myDB"
        maxActive="50" maxIdle="30" maxWait="10000"
        username="mysqluser" password="mysqlpassword" 
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/myDB"/>

    <!-- lots of stuff -->
</app>

Then, in my app: 然后,在我的应用中:

public class DataSourceProvider {
    public DataSource lookupDataSource(String name) {
        DataSource dataSource = null;
        try {
            Context context = new InitialContext();
            dataSource = (DataSource) context.lookup("Database");
        } catch (NamingException e) {
            // Handle...
        }

        return dataSource;
    }
}

DataSourceProvider dsp = new DataSourceProvider();
DataSource myDB = dsp.lookupDataSource("myDB");

How do I link these two concepts together? 如何将这两个概念联系在一起? Say the <dataSource/> XML element gets read into a DataSourceVO object. 假设<dataSource/> XML元素被读取到DataSourceVO对象中。 How do I configure the InitialContext instance from the DataSourceVO instance, such that we can perform the JNDI lookup? 如何从DataSourceVO实例配置InitialContext实例,以便我们可以执行JNDI查找? Thanks in advance! 提前致谢!

Update : I found this example: 更新 :我发现了这个例子:

try{   
    Hashtable env = new Hashtable();   
    env.put(Context.INITIAL_CONTEXT_FACTORY,   
            "com.sun.jndi.ldap.LdapCtxFactory");   
    env.put(Context.SECURITY_AUTHENTICATION, "simple");

    String securityPrincipal = domain + "\\" + user;

    env.put(Context.SECURITY_PRINCIPAL, securityPrincipal);   
    env.put(Context.SECURITY_CREDENTIALS, password);   
    env.put(Context.PROVIDER_URL, "ldap://" + domainController);   

    ctx = new InitialDirContext(env);   
} catch (AuthenticationException ex) { 
    ex.printStackTrace();
} catch(NamingException nex){   
    nex.printStackTrace();   
}

Is this what I'm looking for? 是我要找的东西吗? That way, I could read my XML into a Hashtable (or something similar) and then create the InitialContext from that table? 这样,我可以将我的XML读入Hashtable (或类似的东西),然后从该表创建InitialContext吗? Is that all there is to it?!? 这就是全部吗?!?

Try Spring http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/jdbc.html#jdbc-connections , see 14.3.1 DataSource 尝试使用Spring http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/jdbc.html#jdbc-connections ,请参阅14.3.1数据源

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>

If Spring is not an option then you can configure the data source using Properties 如果没有选择Spring,则可以使用Properties配置数据源。

Properties props = new Properties();

... load from properties file or XML 

BasicDataSource ds = new BasicDataSource();
for(Entry e : props.entrySet()) {
    ds.addConnectionProperty((String)e.getKey(), (String)e.getValue());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM