Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DataSourceFactory.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Marek Prochazka.
00022  * Contributor(s): 
00023  */
00024 package org.objectweb.cjdbc.driver;
00025 
00026 import java.util.Hashtable;
00027 
00028 import javax.naming.Context;
00029 import javax.naming.Name;
00030 import javax.naming.Reference;
00031 import javax.naming.spi.ObjectFactory;
00032 
00033 /**
00034  * <code>DataSource</code> factory for to implement <code>Referenceable</code>.
00035  * The factory serves for the {@link DataSource},<code>PooledDataSource</code>,
00036  * and <code>XADataSource</code> classes.
00037  * 
00038  * @author <a href="mailto:Marek.Prochazka@inrialpes.fr">Marek Prochazka</a>
00039  * @version 1.0
00040  */
00041 public class DataSourceFactory implements ObjectFactory
00042 {
00043   /** DataSource classnames. */
00044   protected final String dataSourceClassName = "org.objectweb.cjdbc.driver.DataSource";
00045   protected final String poolDataSourceName  = "org.objectweb.cjdbc.driver.PoolDataSource";
00046   protected final String xaDataSourceName    = "org.objectweb.cjdbc.driver.XADataSource";
00047 
00048   /**
00049    * Gets an instance of the requested <code>DataSource</code> object.
00050    * 
00051    * @param objRef object containing location or reference information that is
00052    *          used to create an object instance (could be <code>null</code>).
00053    * @param name name of this object relative to specified <code>nameCtx</code>
00054    *          (could be <code>null</code>).
00055    * @param nameCtx name context (could ne null if the default initial context
00056    *          is used).
00057    * @param env environment to use (could be null if default is used)
00058    * @return a newly created instance of C-JDBC DataSource, <code>null</code>
00059    *         if an error occurred.
00060    * @throws Exception if an error occurs when creating the object instance.
00061    */
00062   public Object getObjectInstance(Object objRef, Name name, Context nameCtx,
00063       Hashtable env) throws Exception
00064   {
00065     // Check the requested object class
00066     Reference ref = (Reference) objRef;
00067     String className = ref.getClassName();
00068     if ((className == null)
00069         || !(className.equals(dataSourceClassName)
00070             | className.equals(poolDataSourceName) | className
00071             .equals(xaDataSourceName)))
00072     {
00073       // Wrong class
00074       return null;
00075     }
00076     DataSource ds = null;
00077     try
00078     {
00079       ds = (DataSource) Class.forName(className).newInstance();
00080     }
00081     catch (Exception e)
00082     {
00083       throw new RuntimeException("Error when creating C-JDBC" + className
00084           + " instance: " + e);
00085     }
00086 
00087     ds.setUrl((String) ref.get(DataSource.URL_PROPERTY).getContent());
00088     ds.setUser((String) ref.get(DataSource.USER_PROPERTY).getContent());
00089     ds.setPassword((String) ref.get(DataSource.PASSWORD_PROPERTY).getContent());
00090     return ds;
00091   }
00092 }

Generated on Mon Apr 11 22:01:31 2005 for C-JDBC by  doxygen 1.3.9.1