View Javadoc
1   /***
2    * License Agreement.
3    * 
4    * JSPA (POJO-SP)
5    * 
6    * Copyright (C) 2009 HRX Pty Ltd
7    * 
8    * This file is part of JSPA.
9    * 
10   * JSPA is free software: you can redistribute it and/or modify it under the
11   * terms of the GNU Lesser General Public License version 3 as published by the
12   * Free Software Foundation.
13   * 
14   * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16   * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17   * 
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20   */
21  package com.hrx.rasp.util.dao.ejb;
22  
23  import javax.naming.InitialContext;
24  import javax.naming.NamingException;
25  import javax.sql.DataSource;
26  
27  import com.hrx.rasp.util.dao.DAOManager;
28  import com.hrx.rasp.util.dao.DAOManagerImpl;
29  import com.hrx.rasp.util.dao.jdbc.WrapperDataSource;
30  
31  /***
32   * <p>
33   * The Factory is responsible to provide new instances of DAOManager, There is
34   * one DAOManagerFactory instance per server.
35   * <p>
36   * 
37   * 
38   * @author dan.stoica <dan.stoica@acslink.net.au>
39   * 
40   */
41  public class DAOManagerFactory
42  {
43  	public final static String DEFAULT_DATA_SOURCE_NAME = "jdbc/pojo-sp";
44  
45  	/***
46  	 * It does a JNDI lookup for DataSource than initialise and return a new
47  	 * instance of the DAOManager
48  	 * 
49  	 * @param dataSourceMappedName
50  	 * @return
51  	 * @throws NamingException
52  	 */
53  	public static final DAOManager create(String dataSourceMappedName) throws NamingException
54  	{
55  		DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/" + dataSourceMappedName);
56  		if (ds == null)
57  		{
58  			throw new NamingException("The datasource can not be null");
59  		}
60  		return create(dataSourceMappedName, ds);
61  	}
62  
63  	/***
64  	 * Initialise and return a new DAOManager Instance using the specified
65  	 * DataSource
66  	 * 
67  	 * @param ds
68  	 * @return
69  	 * @throws NamingException
70  	 */
71  	public static final DAOManager create(String name, DataSource ds) throws NamingException
72  	{
73  		DataSource daoDS = new WrapperDataSource(name, ds);
74  		DAOManager dm = new DAOManagerImpl(daoDS);
75  		return dm;
76  	}
77  
78  	/***
79  	 * Initialise and return a new DAOManager Instance using the specified
80  	 * DataSource
81  	 * 
82  	 * @param ds
83  	 * @return
84  	 * @throws NamingException
85  	 */
86  	public static final DAOManager create(DataSource ds) throws NamingException
87  	{
88  		return create(DEFAULT_DATA_SOURCE_NAME, ds);
89  	}
90  }