KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > testlet > AbstractVdbTestLet


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.testlet;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
33 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
34
35 /**
36  * This class defines a AbstractVdbTestLet
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
39  * @version 1.0
40  */

41 public abstract class AbstractVdbTestLet
42     extends
43       AbstractTestLet
44 {
45
46   protected VirtualDatabase vdb;
47
48   /**
49    * Creates a new <code>AbstractVdbTestLet</code> object
50    *
51    * @param vdb virtual database
52    */

53   public AbstractVdbTestLet(VirtualDatabase vdb)
54   {
55     super();
56     this.vdb = vdb;
57   }
58
59   /**
60    * Get a sql connection
61    *
62    * @param url of the backend to get a connection from
63    * @return <code>Connection</code>
64    * @throws SQLException if fails
65    */

66   public Connection JavaDoc getDatabaseConnection(String JavaDoc url) throws SQLException JavaDoc
67   {
68     Properties JavaDoc props = new Properties JavaDoc();
69     props.put("user", "test");
70     props.put("password", "");
71     Connection JavaDoc con = DriverManager.getConnection(url, props);
72     return con;
73   }
74   
75   /**
76    * Retrieve a connection on C-JDBC with the given properties
77    *
78    * @param props additionnal properties for the connection
79    * @return <code>java.sql.Connection</code> object to C-JDBC
80    * @throws Exception if fails
81    */

82   public Connection JavaDoc getCJDBCConnection(Properties JavaDoc props) throws Exception JavaDoc
83   {
84     Properties JavaDoc p = new Properties JavaDoc();
85     p.put("user", "user");
86     p.put("password", "");
87     p.putAll(props);
88     Class.forName("org.objectweb.cjdbc.driver.Driver");
89     Connection JavaDoc con = DriverManager.getConnection("jdbc:cjdbc://localhost/myDB",p);
90     return con;
91   }
92   
93   /**
94    * Return a connection on the backend of the virtual database at given index
95    *
96    * @param index of the backend in the arraylist of backends of the vdb
97    * @return <code>Connection</code>
98    * @throws Exception if fails
99    */

100   public Connection JavaDoc getBackendConnection(int index) throws Exception JavaDoc
101   {
102     return getDatabaseConnection(getBackend(index).getURL());
103   }
104   
105   /**
106    * Get a backend object
107    *
108    * @param index of the backend in the array list of backends of the virtual database
109    * @return <code>DatabaseBackend</code>
110    * @throws Exception if fails
111    */

112   public DatabaseBackend getBackend(int index) throws Exception JavaDoc
113   {
114     DatabaseBackend b = ((DatabaseBackend)vdb.getBackends().get(index));
115     System.out.println("Accessing backend:"+b.getName());
116     return b;
117   }
118 }
Popular Tags