Last updated April 17, 2009 17:16, by Peter Mount
Feedicon  

JNDI

The microKernel provides a simple in-memory JNDI server which components can use. Usually this is for third party code which expects a data source to be deployed within JNDI.

An additional feature is that the all beans deployed by the kernel are also accessible via JNDI by prefixing their deployed name with 'kernel/'. For example if you have a bean called myBean then you can access that bean from JNDI with the path kernel/myBean.

Binding a deployed bean into JNDI

In addition to the automatic jndi binding, you can get a bean to be deployed into an additional location using the @LocalBinding annotation.

For example, we have a bean called myApp and we want it bound as myApp/core in addition to kernel/myApp:

import uk.org.retep.kernel.annotations.Bean;
import uk.org.retep.kernel.annotations.LocalBinding;

@LocalBinding( "myApp/core")
@Bean( name = "myApp", lazyInit = false )
public class App
{

    private String myAppName;

    public String getMyAppName()
    {
        return myAppName;
    }
}

Binding an object into JNDI with XML

You can define a binding of any deployed object into JNDI via XML. Here is a simple example which will bind the deployed bean called someDeployedBean into JNDI with the path ds/MyDB:

    <bean name="myDB" class="uk.org.retep.kernel.naming.JNDIBinder" lazy-init="false">
        <property name="jndiName" value="ds/myDB"/>
        <property name="value" ref="someDeployedBean"/>
    </bean>

This is usually used when deploying DataSources.

DataSources

A common feature for an application is accessing a database via JDBC. To do this you would use a DataSource provided by your databases JDBC driver and use it to get a Connection.

To deploy a DataSource you need to write some XML into your applications's .xml file or into common.xml which defines the DataSource and deploys it. It is not advised to do this from within a Class as you usually want to be able to edit the JDBC connection details post compilation.

Deploy the DataSource

This simple example defines two beans, one called ds/myDB which is the actual DataSource for a PostgreSQL database. The second bean is 'myDB' which binds that DataSource into JNDI so that third party code can access it with the path ds/myDB.

    <!-- Bind the DataSource into JNDI as if it was annotated with @LocalBinding -->
    <bean name="myDB" class="uk.org.retep.kernel.naming.JNDIBinder" lazy-init="false">
        <property name="jndiName" value="ds/myDB"/>
        <property name="value" ref="ds/myDB"/>
    </bean>

    <!-- Deploy the DataSource into the kernel -->
    <bean name="ds/myDB" class="org.postgresql.ds.PGPoolingDataSource">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="5432"/>
        <property name="databaseName" value="mydb"/>
        <property name="user" value="myuser"/>
        <property name="password" value="mypassword"/>
    </bean>

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close