@Test public void testExecute_withoutDataSource() throws Exception { PruneHistoryCommand pruneHistoryCmd = new PruneHistoryCommand(); pruneHistoryCmd.bundleContext = Mockito.mock( BundleContext.class ); Mockito.when( pruneHistoryCmd.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" )).thenReturn( new ArrayList<ServiceReference<DataSource>>( 0 )); ByteArrayOutputStream os = new ByteArrayOutputStream(); pruneHistoryCmd.out = new PrintStream( os, true, "UTF-8" ); pruneHistoryCmd.execute(); Assert.assertEquals( os.toString( "UTF-8" ).trim(), "No data source was found to prune the commands history." ); }
@Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; }
PruneHistoryCommand implements Action { @Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; } }
PruneHistoryCommand implements Action { @Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; }  }
PruneHistoryCommand implements Action { @Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; }  @Override Object execute(); }
PruneHistoryCommand implements Action { @Override public Object execute() throws Exception { if( this.daysToKeep < 0 ) { this.out.println( "[ WARNING ] The daysToKeep argument must be equal or greater than 0. Operation cancelled." ); return null; } Collection<ServiceReference<DataSource>> dataSources = this.bundleContext.getServiceReferences( DataSource.class, "(dataSourceName=roboconf-dm-db)" ); if( dataSources.isEmpty()) { this.out.println( "No data source was found to prune the commands history." ); } else { this.out.println( "Pruning the commands history." ); if( this.daysToKeep > 0 ) this.out.println( "Only the last " + this.daysToKeep + " day" + (this.daysToKeep == 1 ? "" : "s") +" will be kept." ); else this.out.println( "All the entries will be deleted." ); ServiceReference<DataSource> sr = dataSources.iterator().next(); DataSource dataSource = this.bundleContext.getService( sr ); Connection conn = null; PreparedStatement ps = null; try { long startLimitInMilliSeconds = System.currentTimeMillis() - 1000L * 24 * 3600 * this.daysToKeep; StringBuilder sb = new StringBuilder(); sb.append( "DELETE FROM commands_history WHERE start < ?" ); conn = dataSource.getConnection(); ps = conn.prepareStatement( sb.toString()); ps.setString( 1, String.valueOf( startLimitInMilliSeconds )); ps.executeUpdate(); this.out.println( "Pruning done." ); } catch( SQLException e ) { this.logger.severe( "An error occurred while pruning the commands history." ); Utils.logException( this.logger, e ); e.printStackTrace(); } finally { Utils.closeStatement( ps, this.logger ); Utils.closeConnection( conn, this.logger ); this.bundleContext.ungetService( sr ); } } return null; }  @Override Object execute();  }