KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > tapestry > contrib > mckoi > McKoiDB


1 /**
2  * Redistribution and use in source and binary forms, with or without
3  * modification, are permitted provided that the following conditions
4  * are met:
5  *
6  * 1. Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above
9  * copyright notice, this list of conditions and the following
10  * disclaimer in the documentation and/or other materials provided
11  * with the distribution.
12  * 3. The name of the author may not be used to endorse or promote
13  * products derived from this software without specific prior
14  * written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */

28
29 package net.sf.tapestry.contrib.mckoi;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36
37 import org.jboss.system.ServiceMBeanSupport;
38
39 import com.mckoi.database.control.DBController;
40 import com.mckoi.database.control.DBSystem;
41 import com.mckoi.database.control.DefaultDBConfig;
42 import com.mckoi.database.control.TCPJDBCServer;
43
44 /**
45  * An MBean used to start and stop an embedded instance of
46  * <a HREF="http://www.mckoi.com/database">McKoi Database</a>.
47  *
48  * @author Howard Lewis Ship
49  * @version $Id: McKoiDB.java,v 1.1 2003/05/05 10:36:06 toby Exp $
50  * @since 1.0.8
51  *
52  **/

53
54 public class McKoiDB extends ServiceMBeanSupport implements McKoiDBMBean
55 {
56     private String JavaDoc _configPath;
57     private DBSystem _database;
58     private TCPJDBCServer _server;
59
60     public String JavaDoc getConfigPath()
61     {
62         return _configPath;
63     }
64
65     public void setConfigPath(String JavaDoc path)
66     {
67         log.debug("Config path set to: " + path);
68         _configPath = path;
69     }
70
71     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
72     {
73         if (name != null)
74             return name;
75             
76         return new ObjectName JavaDoc(":service=McKoiDB");
77     }
78
79     public String JavaDoc getName()
80     {
81         return "McKoiDB"; }
82
83     public void startService() throws Exception JavaDoc
84     {
85         if (_configPath == null)
86             throw new NullPointerException JavaDoc("McKoiDB: configPath not specified.");
87
88         log.debug("Config path: " + _configPath);
89
90         File JavaDoc configFile = new File JavaDoc(_configPath).getAbsoluteFile();
91         File JavaDoc rootFile = configFile.getParentFile();
92
93         DefaultDBConfig config = new DefaultDBConfig(rootFile);
94
95         try
96         {
97             config.loadFromFile(configFile);
98         }
99         catch (IOException JavaDoc ex)
100         {
101             log.error("Unable to initialize McKoi database.", ex);
102         }
103
104         DBController controller = DBController.getDefault();
105         _database = controller.startDatabase(config);
106
107         _server = new TCPJDBCServer(_database);
108
109         _server.start();
110
111         log.info(_server);
112
113     }
114
115     public void stopService()
116     {
117         _server.stop();
118         _database.close();
119     }
120 }
Popular Tags