1 21 22 package org.apache.derbyTesting.functionTests.util; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.security.PrivilegedActionException ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.sql.*; 29 import java.io.*; 30 31 import org.apache.derby.iapi.reference.JDBC30Translation; 32 33 34 42 public class TestRoutines { 43 44 48 public static void installRoutines() throws SQLException { 49 50 Connection conn = DriverManager.getConnection("jdbc:default:connection"); 51 52 TestRoutines.installRoutines(conn); 53 54 } 55 56 61 public static void installRoutines(Connection conn) throws SQLException { 62 63 Statement s = conn.createStatement(); 64 65 s.execute("CREATE PROCEDURE TESTROUTINE.SET_SYSTEM_PROPERTY(IN PROPERTY_KEY VARCHAR(32000), IN PROPERTY_VALUE VARCHAR(32000)) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.setSystemProperty' language java parameter style java"); 67 68 s.execute("CREATE PROCEDURE TESTROUTINE.SLEEP(IN SLEEP_TIME_MS BIGINT) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.sleep' language java parameter style java"); 70 71 s.execute("CREATE FUNCTION TESTROUTINE.HAS_SECURITY_MANAGER() RETURNS INT NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.hasSecurityManager' language java parameter style java"); 72 73 s.execute("CREATE FUNCTION TESTROUTINE.READ_FILE(FILE_NAME VARCHAR(60), ENCODING VARCHAR(60)) RETURNS VARCHAR(32000) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.readFile' language java parameter style java"); 74 s.close(); 75 } 76 77 78 82 public static void setSystemProperty(final String key, final String value) { 83 84 AccessController.doPrivileged(new PrivilegedAction () { 89 public Object run() { 90 System.setProperty(key, value); 91 return null; } 93 }); 94 95 } 96 100 public static void sleep(long ms) throws InterruptedException { 101 102 Thread.sleep(ms); 103 } 104 105 110 public static int hasSecurityManager() 111 { 112 return System.getSecurityManager() == null ? 0 : 1; 113 } 114 115 122 public static String readFile(final String fileName, final String encoding) 123 throws PrivilegedActionException , IOException 124 { 125 126 FileInputStream fin = (FileInputStream) 131 AccessController.doPrivileged(new PrivilegedExceptionAction () { 132 public Object run() throws FileNotFoundException { 133 return new FileInputStream(fileName); } 135 }); 136 137 InputStreamReader isr = new InputStreamReader( 138 new BufferedInputStream(fin, 32*1024), encoding); 139 140 StringBuffer sb = new StringBuffer (); 141 for (;;) 142 { 143 int ci = isr.read(); 144 if (ci < 0) 145 break; 146 147 if (ci <= 0x7f) 148 { 149 sb.append((char) ci); 150 } 151 else 152 { 153 sb.append("\\u"); 154 String hex = Integer.toHexString(ci); 155 156 switch (hex.length()) 157 { 158 case 2: 159 sb.append("00"); 160 break; 161 case 3: 162 sb.append("0"); 163 break; 164 } 165 sb.append(hex); 166 } 167 } 168 169 return sb.toString(); 170 } 171 } 172 173 174 175 | Popular Tags |