private void loadDriversExplicitly(P6SpyLoadableOptions spyOptions) { final Collection<String> driverNames = spyOptions.getDriverNames(); if (null != driverNames) { for (String driverName : driverNames) { try { // you really only need to load the driver if it is not a // type 4 driver! P6Util.forName(driverName).newInstance(); } catch (Exception e) { String err = "Error registering driver names: " + driverNames + " \nCaused By: " + e.toString(); P6LogQuery.error(err); throw new P6DriverNotFoundError(err); } } } }
final Properties properties = new Properties(); properties.load(in); options = P6Util.getPropertiesMap(properties); } finally { if (null != in) {
private String computeIncludeExcludePattern() { final String excludes = P6Util.joinNullSafe(optionsRepository.getSet(String.class, EXCLUDE_LIST), "|"); final String includes = P6Util.joinNullSafe(optionsRepository.getSet(String.class, INCLUDE_LIST), "|"); if (excludes.length() == 0 && includes.length() == 0) { return null; } final StringBuilder sb = new StringBuilder("(?mis)^"); if (excludes.length() > 0 ) { sb.append("(?!.*(").append(excludes).append(").*)"); } if (includes.length() > 0 ) { sb.append("(.*(").append(includes).append(").*)"); } else { // make sure to match any string here, if no explicit includes specified sb.append("(.*)"); } return sb.append("$").toString(); }
/** * Formats a log message for the logging module * * @param connectionId the id of the connection * @param now the current ime expressing in milliseconds * @param elapsed the time in milliseconds that the operation took to complete * @param category the category of the operation * @param prepared the SQL statement with all bind variables replaced with actual values * @param sql the sql statement executed * @param url the database url where the sql statement executed * @return the formatted log message */ @Override public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql, final String url) { return now + "|" + elapsed + "|" + category + "|connection " + connectionId + "|url " + url + "|" + P6Util.singleLine(prepared) + "|" + P6Util.singleLine(sql); } }
private URL locate() { String propsFileName = System.getProperty(OPTIONS_FILE_PROPERTY, DEFAULT_OPTIONS_FILE); if (null == propsFileName || propsFileName.isEmpty()) { propsFileName = DEFAULT_OPTIONS_FILE; } return P6Util.locateFile(propsFileName); }
final List<String> collection = P6Util.parseCSVList(csv);
/** * Locates a file on the file system or on the classpath. * <p> * Search order: * <ol> * <li>current working directory (for relative path) or any directory (for absolute path)</li> * <li>class path</li> * </ol> * * @param file the relative path of the file to locate * @return A URL to the file or null if not found */ public static URL locateFile(String file) { File fp; URL result = null; try { // try to find relative to current working directory first fp = new File(file); if (fp.exists()) { result = fp.toURI().toURL(); } // next try to load from context class loader if (result == null) { result = locateOnClassPath(file); } } catch (Exception e) { } return result; }
.replaceAll(Pattern.quote(CATEGORY), category) .replaceAll(Pattern.quote(EFFECTIVE_SQL), Matcher.quoteReplacement(prepared)) .replaceAll(Pattern.quote(EFFECTIVE_SQL_SINGLELINE), Matcher.quoteReplacement(P6Util.singleLine(prepared))) .replaceAll(Pattern.quote(SQL), Matcher.quoteReplacement(sql)) .replaceAll(Pattern.quote(SQL_SINGLE_LINE), Matcher.quoteReplacement(P6Util.singleLine(sql))) .replaceAll(Pattern.quote(URL), url);
private URL locate() { String propsFileName = System.getProperty(OPTIONS_FILE_PROPERTY, DEFAULT_OPTIONS_FILE); if (null == propsFileName || propsFileName.isEmpty()) { propsFileName = DEFAULT_OPTIONS_FILE; } return P6Util.locateFile(propsFileName); }
final List<String> collection = P6Util.parseCSVList(csv);
/** * Locates a file on the file system or on the classpath. * <p> * Search order: * <ol> * <li>current working directory (for relative path) or any directory (for absolute path)</li> * <li>class path</li> * </ol> * * @param file the relative path of the file to locate * @return A URL to the file or null if not found */ public static URL locateFile(String file) { File fp; URL result = null; try { // try to find relative to current working directory first fp = new File(file); if (fp.exists()) { result = fp.toURI().toURL(); } // next try to load from context class loader if (result == null) { result = locateOnClassPath(file); } } catch (Exception e) { } return result; }
@Override public void setDriverlist(final String driverlist) { optionsRepository.setSet(String.class, DRIVER_NAMES, driverlist); // setting effective string optionsRepository.set(String.class, DRIVERLIST, P6Util.joinNullSafe(optionsRepository.getSet(String.class, DRIVER_NAMES), ",")); }
@Override public Connection createConnection() throws SQLException { Driver driver; try { driver = (Driver) P6Util.forName(getDriverClassName()).newInstance(); } catch (Exception e) { throw new RuntimeException(e); } return new DriverConnectionFactory(driver, getUrl(), connectionProperties).createConnection(); } };
@Test public void testSingleLine() { Assert.assertEquals("abc efg", P6Util.singleLine("abc\nefg")); Assert.assertEquals("abc efg", P6Util.singleLine("abc\n\nefg")); Assert.assertEquals("abc efg", P6Util.singleLine("abc\r\n\nefg")); } @Test
@Test public void testGetPropertiesMap() throws IOException { final String string = "key1=\nkey2=val2"; final Properties properties = new Properties(); properties.load(new ByteArrayInputStream(string.getBytes())); final Map<String, String> map = P6Util.getPropertiesMap(properties); Assert.assertTrue(map.containsKey("key1")); Assert.assertEquals("", map.get("key1")); Assert.assertTrue(map.containsKey("key2")); Assert.assertEquals("val2", map.get("key2")); Assert.assertFalse(map.containsKey("key3")); Assert.assertNull(map.get("key3")); } }
@Test public void testJoinNullSafe() { Assert.assertEquals("", P6Util.joinNullSafe(null, null)); Assert.assertEquals("", P6Util.joinNullSafe(null, "")); Assert.assertEquals("", P6Util.joinNullSafe(null, ",")); Assert.assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), null)); Assert.assertEquals("", P6Util.joinNullSafe(Collections.<String>emptyList(), ",")); Assert.assertEquals("foo", P6Util.joinNullSafe(Arrays.asList("foo"), ",")); Assert.assertEquals("foobar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), null)); Assert.assertEquals("foo,bar", P6Util.joinNullSafe(Arrays.asList("foo", "bar"), ",")); Assert.assertEquals("foo|bar|aaa", P6Util.joinNullSafe(Arrays.asList("foo", "bar", "aaa"), "|")); }
@Before public void setUpFramework() throws Exception { // clean table plz (we need to make sure that all the configured factories will be re-loaded) new DefaultJdbcEventListenerFactory().clearCache(); Collection<String> driverNames = P6SpyOptions.getActiveInstance().getDriverNames(); String user = P6TestOptions.getActiveInstance().getUser(); String password = P6TestOptions.getActiveInstance().getPassword(); String url = P6TestOptions.getActiveInstance().getUrl(); if (driverNames != null && !driverNames.isEmpty()) { for (String driverName : driverNames) { P6Util.forName(driverName); } } Driver driver = DriverManager.getDriver(url); if (log.isDebugEnabled()) { log.debug("FRAMEWORK USING DRIVER == " + driver.getClass().getName() + " FOR URL " + url); } connection = DriverManager.getConnection(url, user, password).unwrap(ConnectionWrapper.class); P6TestUtil.printAllDrivers(); P6TestUtil.setupTestData(url, user, password); }
/** * Formats a log message for the logging module * * @param connectionId the id of the connection * @param now the current ime expressing in milliseconds * @param elapsed the time in milliseconds that the operation took to complete * @param category the category of the operation * @param prepared the SQL statement with all bind variables replaced with actual values * @param sql the sql statement executed * @param url the database url where the sql statement executed * @return the formatted log message */ @Override public String formatMessage(final int connectionId, final String now, final long elapsed, final String category, final String prepared, final String sql, final String url) { return now + "|" + elapsed + "|" + category + "|connection " + connectionId + "|url " + url + "|" + P6Util.singleLine(prepared) + "|" + P6Util.singleLine(sql); } }