Coloured | Raw |
1// Copyright 2007, Tim Vernum 2// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 3// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 4// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 5// Software is furnished to do so, subject to the following conditions: 6// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 8// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 9// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 10// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 12package logging_interceptor; 13 14import java.sql.Connection; 15import java.sql.ResultSet; 16import java.sql.SQLException; 17import java.sql.Statement; 18 19import net.sf.tie.InterceptionInjector; 20import net.sf.tie.InterceptorStack; 21import net.sf.tie.MethodInterceptorStack; 22import net.sf.tie.ProxyInjector; 23 24public class WiringItUp 25{ 26 public MethodInterceptorStack createStack() 27 { 28 return InterceptorStack.singleton(new LoggingInterceptor()); 29 } 30 31 public InterceptionInjector createInjector() 32 { 33 MethodInterceptorStack stack = createStack(); 34 return new ProxyInjector(stack); 35 } 36 37 public Statement getStatement(Connection connection) throws SQLException 38 { 39 InterceptionInjector injector = createInjector(); 40 Statement statement = connection.createStatement(); 41 return injector.wrapObject(Statement.class, statement); 42 } 43 44 public int getNumberOfOrders() throws SQLException 45 { 46 Connection connection = DatabaseManager.getActiveConnection(); 47 Statement statement = getStatement(connection); 48 try 49 { 50 ResultSet results = statement.executeQuery("SELECT COUNT(*) FROM orders"); 51 return DatabaseManager.getIntegerResult(results); 52 } 53 finally 54 { 55 statement.close(); 56 } 57 } 58}
// Copyright 2007, Tim Vernum // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package logging_interceptor; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import net.sf.tie.InterceptionInjector; import net.sf.tie.InterceptorStack; import net.sf.tie.MethodInterceptorStack; import net.sf.tie.ProxyInjector; public class WiringItUp { public MethodInterceptorStack createStack() { return InterceptorStack.singleton(new LoggingInterceptor()); } public InterceptionInjector createInjector() { MethodInterceptorStack stack = createStack(); return new ProxyInjector(stack); } public Statement getStatement(Connection connection) throws SQLException { InterceptionInjector injector = createInjector(); Statement statement = connection.createStatement(); return injector.wrapObject(Statement.class, statement); } public int getNumberOfOrders() throws SQLException { Connection connection = DatabaseManager.getActiveConnection(); Statement statement = getStatement(connection); try { ResultSet results = statement.executeQuery("SELECT COUNT(*) FROM orders"); return DatabaseManager.getIntegerResult(results); } finally { statement.close(); } } }