geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tomasz Mazan <win...@wp.pl>
Subject Re: How to register a Datasource in GlobalJNDI Namespace?
Date Wed, 26 Sep 2007 14:41:36 GMT


Kristian Köhler-2 wrote:
> 
> Hi
> 
> i use a custom deployment mechanism to install my application into
> Geronimo 2.0.1. I have no special deployment plan for that kind of
> application. One component out of my application wants to access the
> DataSource registered within Geronimo Application server. The component
> tries to lookup the db via a JNDI lookup call.
> 
> My idea is to register the DB within the Global JNDI Namespace and that
> access it from within my application. I configured the pool via deployment
> plan and registered a GBeanBinding GBean for the registration in the
> Global Namespace.
> 
> My problem is: What's the name of the DBPool to access/ to link to with
> the GBeanBinding? Is this a passable way to do this?
> 
> Here is the plan:
> 
> ----- 8< -----
> 
> <connector
> xmlns="http://geronimo.apache.org/xml/ns/j2ee/connector-${geronimoSchemaVersion}">
> 
>     <resourceadapter>
>         <outbound-resourceadapter>
>             <connection-definition>
>                
> <connectionfactory-interface>javax.sql.DataSource</connectionfactory-interface>
>                 <connectiondefinition-instance>
>                  <name>Datasource</name>
>                   ...
>                 </connectiondefinition-instance>
>             </connection-definition>
>         </outbound-resourceadapter>
>     </resourceadapter>
> 
>     <gbean name="MyDatabaseBinding"
> class="org.apache.geronimo.gjndi.binding.GBeanBinding">
>         <attribute name="name">Database</attribute>
>         <attribute name="abstractNameQuery">?name=Datasource</attribute>
>         <reference name="Context">
>             <name>JavaContext</name>
>         </reference>
>     </gbean>
> 
> ----- 8< -----
> 
> 
> Thanks!
> 
> Kristian
> 
> 

Here you got java code

package org.apache.geronimo.connector.globaljndi;
 
import java.util.Collections;
import java.util.Map;

import javax.naming.Name;
import javax.naming.NameParser;
import javax.naming.NamingException;
import javax.resource.ResourceException;

import org.apache.geronimo.connector.outbound.ConnectionFactorySource;
import org.apache.geronimo.gbean.AbstractName;
import org.apache.geronimo.gbean.AbstractNameQuery;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.gjndi.KernelContextGBean;
import org.apache.geronimo.kernel.Kernel;

public class ConnectionFactoryBindings extends KernelContextGBean implements
GBeanLifecycle {
	
	/**
	 * Serial UID
	 */
	private static final long serialVersionUID = -7456461926486650621L;
	
	/**
	 * GBeanInfo
	 */
    public static final GBeanInfo GBEAN_INFO;
    
	/**
	 * @return gbean info
	 */
    public static GBeanInfo getGBeanInfo() {
        return ConnectionFactoryBindings.GBEAN_INFO;
    }
 
    static {
        GBeanInfoBuilder builder =
GBeanInfoBuilder.createStatic(ConnectionFactoryBindings.class,
"ConnectionFactory binding monitor");
        builder.addAttribute("type", String.class, true);
		builder.addAttribute("regularExpression", String.class, true);        
        builder.setConstructor(new String[]{"kernel", "nameInNamespace",
"classLoader", "type", "regularExpression"});
        GBEAN_INFO = builder.getBeanInfo();
    }	
	
	/**
	 * Class monitored with this gbean
	 */
	@SuppressWarnings("unchecked")
	private final Class type;
	
	/**
	 * Regular expression to match
	 */
	private final String regExp;
 
	/**
	 * Default constructor
	 * 
	 * @param kernel kernel object *magic*
	 * @param nameInNamespace namespace to publish objects
	 * @param classLoader the classloader *magic*
	 * @param type full classname
	 * @param regularExpression to match monitored object's names
	 * @throws NamingException on NamingException
	 * @throws ClassNotFoundException if classname not found
	 */
    public ConnectionFactoryBindings(Kernel kernel, String nameInNamespace,
ClassLoader classLoader, String type, String regularExpression)
	throws NamingException, ClassNotFoundException {
		super(nameInNamespace, new AbstractNameQuery(null, Collections.EMPTY_MAP,
ConnectionFactorySource.class.getName()), kernel);
        this.type = classLoader.loadClass(type);
        this.regExp = regularExpression;
    }
 
	/**
	 * {@inheritDoc}}
	 * @see
org.apache.geronimo.gjndi.KernelContextGBean#createBindings(org.apache.geronimo.gbean.AbstractName,
java.lang.Object)
	 */
    protected Map<Name, Object> createBindings(AbstractName abstractName,
Object value) throws NamingException {
        if (value instanceof ConnectionFactorySource) {
            ConnectionFactorySource connectionFactorySource =
(ConnectionFactorySource) value;
 
            String name = (String) abstractName.getName().get("name");
			if (name == null || !name.matches(this.regExp)) {
				return null;
			} 
			 
            Object resource = null;
            try {
                resource = connectionFactorySource.$getResource();
            } catch (ResourceException e) {
                throw (NamingException) new NamingException("Could not
obtain connection factory from gbean").initCause(e);
            }
            if (!type.isInstance(resource)) {
            	return null;
			}
 
            NameParser parser = getNameParser();
            Name jndiName = parser.parse(name);
            
            return Collections.singletonMap(jndiName, resource);
        }
        throw new NamingException("Value is not a ConnectionFactorySource:
abstractName=" + abstractName + " valueType=" + value.getClass().getName());
    }

}

and fragment of deployment plan

<dep:gbean name="ConnectionFactoryBindings"
class="org.apache.geronimo.connector.globaljndi.ConnectionFactoryBindings">
		<dep:attribute name="nameInNamespace">jms:conn</dep:attribute>	
		<dep:attribute name="type">javax.jms.ConnectionFactory</dep:attribute>
		<dep:attribute
name="regularExpression">^JmsDispatcher.*ConnectionFactory$</dep:attribute>			
	</dep:gbean>
-- 
View this message in context: http://www.nabble.com/How-to-register-a-Datasource-in-GlobalJNDI-Namespace--tf4521379s134.html#a12902470
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.


Mime
View raw message