1 24 25 package org.objectweb.cjdbc.scenario.templates; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.util.ResourceBundle ; 30 import java.util.StringTokenizer ; 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 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 standAlone = System 52 .getProperty("standalone"); 53 protected static final boolean standaloneTest = !(Boolean.valueOf(standAlone) 54 .booleanValue()); 55 56 static String [] tokenize(String param) 57 { 58 if (standaloneTest) 59 return null; 60 61 int counter = 0; 62 StringTokenizer tokenizer = new StringTokenizer (param, ","); 63 String [] params = new String [tokenizer.countTokens()]; 64 while (tokenizer.hasMoreTokens()) 65 { 66 String pa = tokenizer.nextToken(); 67 params[counter] = (pa.equals("NULL")) ? "" : pa; 68 counter++; 69 } 70 return params; 71 } 72 73 protected static final ResourceBundle bundle = (standaloneTest) 75 ? null 76 : ResourceBundle 77 .getBundle("sqlinjection"); 78 79 protected static final String jdbcurl = (standaloneTest) 81 ? null 82 : bundle 83 .getString("jdbc.url"); 84 protected static final String [] urls = tokenize(jdbcurl); 85 protected static final String jdbcuser = (standaloneTest) 87 ? null 88 : bundle 89 .getString("jdbc.user"); 90 protected static final String [] users = tokenize(jdbcuser); 91 protected static final String jdbcpassword = (standaloneTest) 93 ? null 94 : bundle 95 .getString("jdbc.password"); 96 protected static final String [] passwords = tokenize((jdbcpassword)); 97 protected static final String jdbcdriverclass = (standaloneTest) 99 ? null 100 : bundle 101 .getString("jdbc.driver.class"); 102 protected static final String [] classes = tokenize(jdbcdriverclass); 103 104 protected static final String cjdbcurl = (standaloneTest) 106 ? "jdbc:cjdbc://localhost:25322/myDB" 107 : bundle 108 .getString("cjdbc.url"); 109 protected static final String cjdbcpassword = (standaloneTest) 110 ? null 111 : bundle 112 .getString("cjdbc.password"); 113 protected static final String cjdbcuser = (standaloneTest) 114 ? null 115 : bundle 116 .getString("cjdbc.user"); 117 118 protected static final String threadcount = (standaloneTest) 120 ? null 121 : bundle 122 .getString("thread.count"); 123 protected static final String threadstartwaittimerange = (standaloneTest) 124 ? null 125 : bundle 126 .getString("thread.start.wait.time.range"); 127 protected static final String unitruncount = (standaloneTest) 128 ? null 129 : bundle 130 .getString("unit.run.count"); 131 protected static final String threadwaittime = (standaloneTest) 132 ? null 133 : bundle 134 .getString("thread.wait.time"); 135 protected static final String jointhreadtimeout = (standaloneTest) 136 ? null 137 : bundle 138 .getString("join.thread.timeout"); 139 140 protected static final String dropTables = (standaloneTest) 141 ? null 142 : bundle 143 .getString("drop.tables"); 144 145 protected static final String createTables = (standaloneTest) 146 ? null 147 : bundle 148 .getString("create.tables"); 149 150 protected static final String keyIndex = (standaloneTest) 151 ? null 152 : bundle 153 .getString("key.index"); 154 155 158 public Connection getConnection() throws Exception 159 { 160 if (standaloneTest) 161 return super.getCJDBCConnection(); 162 else 163 return DriverManager.getConnection(cjdbcurl, cjdbcuser, cjdbcpassword); 164 } 165 166 169 public Connection getBackendConnection(int index) throws Exception 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 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 e) 206 { 207 e.printStackTrace(); 208 fail("Could not start controller"); 209 tearDown(); 210 } 211 } 212 213 216 protected void tearDown() 217 { 218 if (!standaloneTest) 219 return; 220 221 hm.stopAll(); 222 cm.stopAll(); 223 } 224 225 } | Popular Tags |