KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jdbc > DerbyDatabase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import javax.management.MBeanRegistration JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import org.jboss.system.ServiceMBeanSupport;
34
35 /**
36  * Integration with <a HREF="http://incubator.apache.org/derby/index.html">Derby</a>.
37  *
38  * <p>Starts Derby database in-VM.
39  *
40  * @jmx.mbean name="jboss:service=Derby"
41  * extends="org.jboss.system.ServiceMBean"
42  *
43  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
44  * @version $Revision: 37459 $
45  */

46 public class DerbyDatabase
47    extends ServiceMBeanSupport
48    implements DerbyDatabaseMBean, MBeanRegistration JavaDoc
49 {
50    /**
51     * Default password: <code>empty string</code>.
52     */

53    private static final String JavaDoc DEFAULT_PASSWORD = "";
54    
55    /**
56     * Default user: <code>sa</code>.
57     */

58    private static final String JavaDoc DEFAULT_USER = "sa";
59    
60    /**
61     * JDBC Driver class: <code>org.apache.derby.jdbc.EmbeddedDriver</code>.
62     */

63    private static final String JavaDoc JDBC_DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
64    
65    /**
66     * JDBC URL common prefix: <code>jdbc:derby:</code>.
67     */

68    private static final String JavaDoc JDBC_URL_PREFIX = "jdbc:derby:";
69
70    /**
71     * Default data subdir: <code>derby</code>.
72     */

73    private static final String JavaDoc DERBY_DATA_DIR = "derby";
74    
75    /**
76     * Default database name: <code>default</code>.
77     */

78    private static final String JavaDoc DEFAULT_DATABASE_NAME = "default";
79    
80    /**
81     * Database name.
82     */

83    String JavaDoc name = DEFAULT_DATABASE_NAME;
84
85    /**
86     * Database user.
87     */

88    private String JavaDoc user = DEFAULT_USER;
89    
90    /**
91     * Database password.
92     */

93    private String JavaDoc password = DEFAULT_PASSWORD;
94    
95    /**
96     * Hold a connection for in process derby.
97     */

98    private Connection JavaDoc connection;
99
100    /**
101     * Set the database name.
102     *
103     * @jmx.managed-attribute
104     */

105    public void setDatabase(String JavaDoc name)
106    {
107       if (name == null)
108       {
109          name = DEFAULT_DATABASE_NAME;
110       }
111
112       this.name = name;
113    }
114
115    /**
116     * Get the database name.
117     *
118     * @jmx.managed-attribute
119     */

120    public String JavaDoc getDatabase()
121    {
122       return name;
123    }
124
125    /**
126     * @return the password
127     *
128     * @jmx.managed-attribute
129     */

130    public String JavaDoc getPassword()
131    {
132       return password;
133    }
134
135    /**
136     * @return the user
137     *
138     * @jmx.managed-attribute
139     */

140    public String JavaDoc getUser()
141    {
142       return user;
143    }
144
145    /**
146     * @param password
147     *
148     * @jmx.managed-attribute
149     */

150    public void setPassword(String JavaDoc password)
151    {
152       if (password == null)
153       {
154          password = DEFAULT_PASSWORD;
155       }
156
157       this.password = password;
158    }
159
160    /**
161     * @param user
162     *
163     * @jmx.managed-attribute
164     */

165    public void setUser(String JavaDoc user)
166    {
167       if (user == null)
168       {
169          user = DEFAULT_USER;
170       }
171
172       this.user = user;
173    }
174
175    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
176       throws MalformedObjectNameException JavaDoc
177    {
178       return name == null ? OBJECT_NAME : name;
179    }
180
181    protected void startService() throws Exception JavaDoc
182    {
183       String JavaDoc dbURL = JDBC_URL_PREFIX + System.getProperty("jboss.server.data.dir") +
184          '/' + DerbyDatabase.DERBY_DATA_DIR +
185          '/' + name + ";create=true";
186       log.info("starting derby " + dbURL);
187
188       // hold a connection so hypersonic does not close the database
189
connection = getConnection(dbURL);
190    }
191
192    protected void stopService() throws Exception JavaDoc
193    {
194       try
195       {
196          getConnection("jdbc:derby:;shutdown=true");
197          log.error("According to the docs, should have caught an exception!");
198       }
199       catch(SQLException JavaDoc e)
200       {
201          log.info("Derby shutdown successfully.", e);
202       }
203
204       connection = null;
205    }
206
207    /**
208     * Get the connection.
209     *
210     * @param dbURL jdbc url.
211     *
212     * @return the connection, allocate one if needed.
213     *
214     * @throws Exception
215     */

216    private synchronized Connection JavaDoc getConnection(String JavaDoc dbURL) throws Exception JavaDoc
217    {
218       if (connection == null)
219       {
220          ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
221          
222          Class.forName(JDBC_DRIVER_CLASS, true, cl).newInstance();
223          
224          connection = DriverManager.getConnection(dbURL, user, password);
225       }
226       
227       return connection;
228    }
229 }
230
Popular Tags