View Javadoc

1   /*
2    * $Id: DataSourceFactory.java 119 2004-10-24 08:52:43Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.factory;
24  
25  import javax.sql.DataSource;
26  
27  import org.apache.commons.dbcp.BasicDataSource;
28  import org.talika.tarsis.context.Context;
29  import org.talika.tarsis.service.ServiceException;
30  
31  /**
32   * <code>DataSource</code> factory implementation of <code>Factory</code> interface.<br>
33   * <br>
34   * Creates a <code>DataSource</code> object from given parameters. <code>DataSource</code>
35   * instance is unique for all life-cycle of factory.<br>
36   * <br>
37   * Parameters given to create <code>DataSource</code> are:
38   * <ul>
39   * <li>driverName - Name of JDBC driver.</li>
40   * <li>connectionUrl - URL for database connections.</li>
41   * <li>user - User name for database connections.</li>
42   * <li>password - User assword for database connections.<li>
43   * </ul>
44   *
45   * @author  Jose M. Palomar
46   * @version $Revision: 119 $
47   * @see javax.sql.DataSource
48   */
49  public final class DataSourceFactory extends FactoryService {
50  
51      // Constants
52  
53      // Fields
54      /**
55       * Name of JDBC driver for <code>DataSource</code>.
56       */
57      private String driverName;
58  
59      /**
60       * URL for database connections of <code>DataSource</code>.
61       */
62      private String connectionUrl;
63  
64      /**
65       * User name for database connections of <code>DataSource</code>.
66       */
67      private String user;
68  
69      /**
70       * User password for database connections of <code>DataSource</code>.
71       */
72      private String password;
73  
74      /**
75       * <code>DataSource</code> instance.
76       */
77      private DataSource ds;
78  
79      // Constructors
80      /**
81       * Creates a new <code>DataSourceFactory</code> object.
82       */
83      public DataSourceFactory() {
84      }
85  
86      // Methods
87      /**
88       * Called by the framework to indicate that is being placed into service.<br>
89       * <br>
90       * It tries to initialize <code>DataSource</code> instance.
91       *
92       * @param context Context context that initialized service.
93       * @throws ServiceException if an exception has occurred that interferes with the
94       * services's normal operation
95       * @see org.talika.tarsis.service.Service#init(Context)
96       */
97      public void init(Context context) throws ServiceException {
98          super.init(context);
99          try {
100             BasicDataSource basicDs = new BasicDataSource();
101 
102             basicDs.setDriverClassName(driverName);
103             basicDs.setUrl(connectionUrl);
104             basicDs.setUsername(user);
105             basicDs.setPassword(password);
106 
107             this.ds = basicDs;
108         }
109         catch (Throwable t) {
110             throw new ServiceException("Error instantiating data source", t);
111         }
112     }
113 
114     /**
115      * Sets connection URL parameter.
116      *
117      * @param connectionUrl String URL for database connections.
118      */
119     public void setConnectionUrl(String connectionUrl) {
120         this.connectionUrl = connectionUrl;
121     }
122 
123     /**
124      * Sets driver name parameter.
125      *
126      * @param driverName String JDBC driver for database connections.
127      */
128     public void setDriverName(String driverName) {
129         this.driverName = driverName;
130     }
131 
132     /**
133      * Sets password parameter.
134      *
135      * @param password String user password for database connections.
136      */
137     public void setPassword(String password) {
138         this.password = password;
139     }
140 
141     /**
142      * Sets user parameter.
143      *
144      * @param user String user name for database connections.
145      */
146     public void setUser(String user) {
147         this.user = user;
148     }
149 
150     /**
151      * Returns name of service.
152      *
153      * @return String name of service.
154      * @see org.talika.tarsis.service.Service#getName()
155      */
156     public String getName() {
157         return "DataSourceFactory";
158     }
159 
160     /**
161      * Returns <code>DataSource</code> instance created by this factory.<br>
162      * Calling this method returns allways same <code>DataSource</code> instance.
163      *
164      * @return Object <code>DataSource</code> instance created by this factory.
165      * @see org.talika.tarsis.factory.Factory#getInstance()
166      */
167     public Object getInstance() {
168         return getDataSource();
169     }
170 
171     /**
172      * Returns <code>DataSource</code> instance created by this factory.
173      *
174      * @return DataSource <code>DataSource</code> instance created by this factory.
175      */
176     public DataSource getDataSource() {
177         return ds;
178     }
179 
180 }