1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
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
80 /**
81 * Creates a new <code>DataSourceFactory</code> object.
82 */
83 public DataSourceFactory() {
84 }
85
86
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 }