KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > templates > SQLInjectionTemplate


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.templates;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.objectweb.cjdbc.controller.core.Controller;
33 import org.objectweb.cjdbc.controller.virtualdatabase.VirtualDatabase;
34 import org.objectweb.cjdbc.scenario.tools.components.ComponentInterface;
35 import org.objectweb.cjdbc.scenario.tools.components.backend.DatabaseManager;
36 import org.objectweb.cjdbc.scenario.tools.components.controller.ControllerManager;
37
38 /**
39  * This class defines a SQLInjectionTemplate
40  *
41  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
42  * @version 1.0
43  */

44 public class SQLInjectionTemplate extends Template
45 {
46   protected ControllerManager cm = new ControllerManager();
47   protected DatabaseManager hm = new DatabaseManager();
48   protected ComponentInterface hm1 = null, hm2 = null, hm3 = null;
49   protected Controller controller = null;
50   protected VirtualDatabase mainVdb;
51   static final String JavaDoc standAlone = System
52                                                     .getProperty("standalone");
53   protected static final boolean standaloneTest = !(Boolean.valueOf(standAlone)
54                                                     .booleanValue());
55
56   static String JavaDoc[] tokenize(String JavaDoc param)
57   {
58     if (standaloneTest)
59       return null;
60
61     int counter = 0;
62     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(param, ",");
63     String JavaDoc[] params = new String JavaDoc[tokenizer.countTokens()];
64     while (tokenizer.hasMoreTokens())
65     {
66       String JavaDoc pa = tokenizer.nextToken();
67       params[counter] = (pa.equals("NULL")) ? "" : pa;
68       counter++;
69     }
70     return params;
71   }
72
73   // Get Bundle
74
protected static final ResourceBundle JavaDoc bundle = (standaloneTest)
75                                                                      ? null
76                                                                      : ResourceBundle
77                                                                          .getBundle("sqlinjection");
78
79   // jdbc urls
80
protected static final String JavaDoc jdbcurl = (standaloneTest)
81                                                                      ? null
82                                                                      : bundle
83                                                                          .getString("jdbc.url");
84   protected static final String JavaDoc[] urls = tokenize(jdbcurl);
85   // jdbc users
86
protected static final String JavaDoc jdbcuser = (standaloneTest)
87                                                                      ? null
88                                                                      : bundle
89                                                                          .getString("jdbc.user");
90   protected static final String JavaDoc[] users = tokenize(jdbcuser);
91   // jdbc passwords
92
protected static final String JavaDoc jdbcpassword = (standaloneTest)
93                                                                      ? null
94                                                                      : bundle
95                                                                          .getString("jdbc.password");
96   protected static final String JavaDoc[] passwords = tokenize((jdbcpassword));
97   // jdbc classes
98
protected static final String JavaDoc jdbcdriverclass = (standaloneTest)
99                                                                      ? null
100                                                                      : bundle
101                                                                          .getString("jdbc.driver.class");
102   protected static final String JavaDoc[] classes = tokenize(jdbcdriverclass);
103
104   // cjdbc info
105
protected static final String JavaDoc cjdbcurl = (standaloneTest)
106                                                                      ? "jdbc:cjdbc://localhost:25322/myDB"
107                                                                      : bundle
108                                                                          .getString("cjdbc.url");
109   protected static final String JavaDoc cjdbcpassword = (standaloneTest)
110                                                                      ? null
111                                                                      : bundle
112                                                                          .getString("cjdbc.password");
113   protected static final String JavaDoc cjdbcuser = (standaloneTest)
114                                                                      ? null
115                                                                      : bundle
116                                                                          .getString("cjdbc.user");
117
118   // sql injection info
119
protected static final String JavaDoc threadcount = (standaloneTest)
120                                                                      ? null
121                                                                      : bundle
122                                                                          .getString("thread.count");
123   protected static final String JavaDoc threadstartwaittimerange = (standaloneTest)
124                                                                      ? null
125                                                                      : bundle
126                                                                          .getString("thread.start.wait.time.range");
127   protected static final String JavaDoc unitruncount = (standaloneTest)
128                                                                      ? null
129                                                                      : bundle
130                                                                          .getString("unit.run.count");
131   protected static final String JavaDoc threadwaittime = (standaloneTest)
132                                                                      ? null
133                                                                      : bundle
134                                                                          .getString("thread.wait.time");
135   protected static final String JavaDoc jointhreadtimeout = (standaloneTest)
136                                                                      ? null
137                                                                      : bundle
138                                                                          .getString("join.thread.timeout");
139
140   protected static final String JavaDoc dropTables = (standaloneTest)
141                                                                      ? null
142                                                                      : bundle
143                                                                          .getString("drop.tables");
144
145   protected static final String JavaDoc createTables = (standaloneTest)
146                                                                      ? null
147                                                                      : bundle
148                                                                          .getString("create.tables");
149
150   protected static final String JavaDoc keyIndex = (standaloneTest)
151                                                                      ? null
152                                                                      : bundle
153                                                                          .getString("key.index");
154
155   /**
156    * @see org.objectweb.cjdbc.scenario.templates.Template#getCJDBCConnection()
157    */

158   public Connection JavaDoc getConnection() throws Exception JavaDoc
159   {
160     if (standaloneTest)
161       return super.getCJDBCConnection();
162     else
163       return DriverManager.getConnection(cjdbcurl, cjdbcuser, cjdbcpassword);
164   }
165
166   /**
167    * @see org.objectweb.cjdbc.scenario.templates.Template#getHypersonicConnection(int)
168    */

169   public Connection JavaDoc getBackendConnection(int index) throws Exception JavaDoc
170   {
171     if (standaloneTest)
172       return super.getHypersonicConnection(index);
173     else
174     {
175       Class.forName(classes[index]);
176       {
177         System.out.println("Connecting to:"+urls[index] +";"+users[index]+";"+passwords[index]);
178       return DriverManager.getConnection(urls[index], users[index],
179           passwords[index]);
180       }
181     }
182   }
183
184   /**
185    * @see junit.framework.TestCase#setUp()
186    */

187   protected void setUp()
188   {
189     if (!standaloneTest)
190       return;
191     try
192     {
193       hm1 = hm.start("9001");
194       hm1.loadDatabase("database-raidb1-user.template");
195       hm2 = hm.start("9002");
196       hm2.loadDatabase("database-raidb1-user.template");
197       hm3 = hm.start("9003");
198       hm.loaddatabase("9003");
199       controller = (Controller) cm.start("25322").getProcess();
200       cm.loaddatabase("" + controller.getPortNumber(),
201           "hsqldb-raidb1-variablepool-waitforall.xml");
202       mainVdb = controller.getVirtualDatabase("myDB");
203       mainVdb.enableAllBackends();
204     }
205     catch (Exception JavaDoc e)
206     {
207       e.printStackTrace();
208       fail("Could not start controller");
209       tearDown();
210     }
211   }
212
213   /**
214    * @see junit.framework.TestCase#tearDown()
215    */

216   protected void tearDown()
217   {
218     if (!standaloneTest)
219       return;
220
221     hm.stopAll();
222     cm.stopAll();
223   }
224
225 }
Popular Tags