geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kristian Köhler <Kristian.Koeh...@dmc.de>
Subject AW: How to register a Datasource in GlobalJNDI Namespace?
Date Thu, 27 Sep 2007 04:46:29 GMT
Thanks David and Tomasz!

Works great. :-)

Kristian

> -----Ursprüngliche Nachricht-----
> Von: David Jencks [mailto:david_jencks@yahoo.com]
> Gesendet: Mittwoch, 26. September 2007 16:15
> An: user@geronimo.apache.org
> Betreff: Re: How to register a Datasource in GlobalJNDI Namespace?
> 
> 
> 
> On Sep 26, 2007, at 3:33 AM, Kristian Köhler 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?
> 
> You can but its a pain.  You need to find the  
> ManagedConnectionFactoryWrapper for your pool and call 
> $getResource()  
> on it to get the datasource.
> 
> Instead you might try the more specific connection factory binder  
> dain wrote a long time ago that has been languishing in the 
> sandbox.   
> This will let you get the actual datasource out of jndi.  I haven't  
> tested it but someone has recently got something similar and a  
> related class for admin objects to work.
> 
> thanks
> david jencks
> 
> /*
> * Licensed to the Apache Software Foundation (ASF) under one
> * or more contributor license agreements.  See the NOTICE file
> * distributed with this work for additional information
> * regarding copyright ownership.  The ASF licenses this file
> * to you under the Apache License, Version 2.0 (the
> * "License"); you may not use this file except in compliance
> * with the License.  You may obtain a copy of the License at
> *
> *  http://www.apache.org/licenses/LICENSE-2.0
> *
> * Unless required by applicable law or agreed to in writing,
> * software distributed under the License is distributed on an
> * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> * KIND, either express or implied.  See the License for the
> * specific language governing permissions and limitations
> * under the License.
> */
> 
> 
> package org.apache.geronimo.connector.globaljndi;
> 
> 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;
> 
> import javax.naming.Name;
> import javax.naming.NameParser;
> import javax.naming.NamingException;
> import javax.resource.ResourceException;
> 
> import java.util.Collections;
> import java.util.Map;
> 
> /**
> * @version $Rev$ $Date$
> */
> public class ResourceBindings extends KernelContextGBean implements  
> GBeanLifecycle {
>      private final Class type;
> 
>      public ResourceBindings(Kernel kernel, String nameInNamespace,  
> Class type) throws NamingException {
>          super(nameInNamespace, new AbstractNameQuery(null,  
> Collections.EMPTY_MAP, ConnectionFactorySource.class.getName()),  
> kernel);
>          this.type = type;
>      }
> 
>      public ResourceBindings(Kernel kernel, String nameInNamespace,  
> ClassLoader classLoader, String type) throws NamingException,  
> ClassNotFoundException {
>          super(nameInNamespace, new AbstractNameQuery(null,  
> Collections.EMPTY_MAP, ConnectionFactorySource.class.getName()),  
> kernel);
>          this.type = classLoader.loadClass(type);
>      }
> 
>      protected Map 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) 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());
>      }
> 
>      public static final GBeanInfo GBEAN_INFO;
> 
>      public static GBeanInfo getGBeanInfo() {
>          return ResourceBindings.GBEAN_INFO;
>      }
> 
>      static {
>          GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic 
> (ResourceBindings.class, "JdbcBindings");
>          builder.addAttribute("type", String.class, true);
>          builder.setConstructor(new String[]{"kernel",  
> "nameInNamespace", "classLoader", "type"});
>          GBEAN_INFO = builder.getBeanInfo();
>      }
> }
> 

---
http://gaswerk.sourceforge.net

Mime
View raw message