List of usage examples for org.apache.commons.lang.time DurationFormatUtils formatDurationHMS
public static String formatDurationHMS(long durationMillis)
Formats the time gap as a string.
The format used is ISO8601-like: H:m:s.S.
From source file:org.kuali.rice.ken.util.PerformanceLog.java
/** * This method logs the duration information for a given message. * @param message/*from w w w. j av a2 s .c o m*/ * @param duration */ public static void logDuration(String message, long duration) { LOG.info(message + ": " + DurationFormatUtils.formatDurationHMS(duration) + " (" + duration + " ms)"); }
From source file:org.kuali.rice.test.ClearDatabaseLifecycle.java
protected void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource) { Assert.assertNotNull("DataSource could not be located.", dataSource); try {//from w w w.j a v a 2s. c o m StopWatch s = new StopWatch(); s.start(); new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { String schemaName = statement.getConnection().getMetaData().getUserName().toUpperCase(); LOG.info("Clearing tables for schema " + schemaName); if (StringUtils.isBlank(schemaName)) { Assert.fail("Empty schema name given"); } final List<String> reEnableConstraints = new ArrayList<String>(); DatabaseMetaData metaData = statement.getConnection().getMetaData(); Map<String, List<String[]>> exportedKeys = indexExportedKeys(metaData, schemaName); final ResultSet resultSet = metaData.getTables(null, schemaName, null, new String[] { "TABLE" }); final StringBuilder logStatements = new StringBuilder(); while (resultSet.next()) { String tableName = resultSet.getString("TABLE_NAME"); if (shouldTableBeCleared(tableName)) { if (!isUsingDerby(metaData) && isUsingOracle(metaData)) { List<String[]> exportedKeyNames = exportedKeys.get(tableName); if (exportedKeyNames != null) { for (String[] exportedKeyName : exportedKeyNames) { final String fkName = exportedKeyName[0]; final String fkTableName = exportedKeyName[1]; final String disableConstraint = "ALTER TABLE " + fkTableName + " DISABLE CONSTRAINT " + fkName; logStatements.append("Disabling constraints using statement ->" + disableConstraint + "<-\n"); statement.addBatch(disableConstraint); reEnableConstraints.add("ALTER TABLE " + fkTableName + " ENABLE CONSTRAINT " + fkName); } } } else if (isUsingMySQL(metaData)) { statement.addBatch("SET FOREIGN_KEY_CHECKS = 0"); } String deleteStatement = "DELETE FROM " + tableName; logStatements.append( "Clearing contents using statement ->" + deleteStatement + "<-\n"); statement.addBatch(deleteStatement); } } for (final String constraint : reEnableConstraints) { logStatements .append("Enabling constraints using statement ->" + constraint + "<-\n"); statement.addBatch(constraint); } if (isUsingMySQL(metaData)) { statement.addBatch("SET FOREIGN_KEY_CHECKS = 1"); } LOG.info(logStatements); int[] results = statement.executeBatch(); for (int index = 0; index < results.length; index++) { if (results[index] == Statement.EXECUTE_FAILED) { Assert.fail("Execution of database clear statement failed."); } } resultSet.close(); LOG.info("Tables successfully cleared for schema " + schemaName); return null; } }); } }); s.stop(); LOG.info("Time to clear tables: " + DurationFormatUtils.formatDurationHMS(s.getTime())); } catch (Exception e) { LOG.error(e); throw new RuntimeException(e); } }
From source file:org.rundeck.api.domain.RundeckEvent.java
/** * @return the duration of the event, as a "short" human-readable string : "0:03:34.187" (or null if the dates are * invalid)/* w w w . j a v a 2s . com*/ */ public String getShortDuration() { Long durationInMillis = getDurationInMillis(); return durationInMillis != null ? DurationFormatUtils.formatDurationHMS(durationInMillis) : null; }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
@Override public Result call() throws Exception { Date startDate = new Date(); console.printMessage("Starting wysiwyg contents purge for component instance id " + componentId + ", at " + DateUtil.formatAsISO8601WithUtcOffset(startDate)); Result result = purgeWysiwyg(); String message = "Finishing wysiwyg contents purge for component instance id " + componentId; if (result.getNbWysiwygContentPurged() > 0L) { message += " with " + result.getNbWysiwygContentPurged() + " wysiwyg content(s) purged"; } else {//from w w w . j a va 2s.co m message += " with no wysiwyg content purged"; } if (result.getNbWysiwygFilenameRenamed() > 0L) { message += " and " + result.getNbWysiwygFilenameRenamed() + " wysiwyg content file name(s) renamed"; } else { message += " and no wysiwyg content file name renamed"; } Date endDate = new Date(); console.printMessage(message + ", at " + DateUtil.formatAsISO8601WithUtcOffset(endDate) + " (duration of " + DurationFormatUtils.formatDurationHMS(endDate.getTime() - startDate.getTime()) + ")"); return result; }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
private Result purgeWysiwyg() { final String headerLogPart = "instanceId=" + componentId; Result result = new Result(); long startIdentification = System.currentTimeMillis(); Map<String, List<SimpleDocument>> wysiwygByForeignIds = loadComponentWysiwygIndexedByForeignIds(); console.printMessage(headerLogPart + " - identifying foreign ids (" + wysiwygByForeignIds.size() + ") and corresponding wysiwyg documents in " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - startIdentification)); for (Map.Entry<String, List<SimpleDocument>> wysiwygForForeignId : wysiwygByForeignIds.entrySet()) { long startForeignId = System.currentTimeMillis(); String commonLogPart = headerLogPart + ", foreignId=" + wysiwygForForeignId.getKey(); console.printMessage(commonLogPart + " - verifying wysiwyg contents ..."); // Getting documents ordered by their names ForeignIdProcessContext context = new ForeignIdProcessContext(wysiwygForForeignId.getValue()); int nbContents = context.getWysiwygOfForeignId().size(); if (nbContents > 0) { // Removing firstly the empty ones. removeEmptyOnes(commonLogPart, context); // For each language, removing duplicates removeDuplicatesForEachLanguage(commonLogPart, context); // Removing duplicates between all languages removeDuplicatesBetweenAllLanguages(commonLogPart, context); // Renaming wysiwyg content file names according to the language renameFilenames(commonLogPart, context); }/* www. j a v a 2 s. c o m*/ result.addNbWysiwygContentPurged(context.getNbRemovedContent()); result.addNbWysiwygFilenameRenamed(context.getNbRenamedContent()); console.printMessage( commonLogPart + " - " + context.getNbRemovedContent() + "/" + nbContents + " purged."); console.printMessage( commonLogPart + " - " + context.getNbRenamedContent() + "/" + nbContents + " renamed."); console.printMessage(commonLogPart + " - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - startForeignId)); } return result; }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
/** * Removes all empty wysiwyg./*from w w w .java2s . c o m*/ * @param context the context. */ private void removeEmptyOnes(String commonLogPart, ForeignIdProcessContext context) { long start = System.currentTimeMillis(); commonLogPart += " - empty content"; for (WysiwygContent wysiwygContent : new ArrayList<WysiwygContent>(context.getWysiwygOfForeignId())) { if (isPhysicalFileExistingWithEmptyContent(commonLogPart, wysiwygContent.getAttachment())) { service.removeContent(wysiwygContent.getAttachment(), wysiwygContent.getLanguage()); console.printMessage(commonLogPart + " - doc=" + wysiwygContent.getNodeName() + ", lang=" + wysiwygContent.getLanguage() + " - removed"); context.remove(wysiwygContent); } } context.resetIndexation(); console.printMessage(commonLogPart + " - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
/** * Removes all wysiwyg duplication for each language. * @param context the context./* w w w.ja v a 2 s. c o m*/ */ private void removeDuplicatesForEachLanguage(String commonLogPart, ForeignIdProcessContext context) { long start = System.currentTimeMillis(); commonLogPart += " - duplicates for a language"; for (String language : new ArrayList<String>(context.getWysiwygIndexedByLangs().keySet())) { List<WysiwygContent> wysiwygContentsForCurrentLanguage = context.getWysiwygIndexedByLangs() .get(language); if (wysiwygContentsForCurrentLanguage.size() > 1) { List<WysiwygContent> wysiwygContentsToRemove = new ArrayList<WysiwygContent>( wysiwygContentsForCurrentLanguage); WysiwygContent firstWysiwygContent = wysiwygContentsToRemove.remove(0); WysiwygContent firstContent = firstWysiwygContent; for (WysiwygContent wysiwygContent : wysiwygContentsForCurrentLanguage) { if (!wysiwygContent.isContentEqualTo(commonLogPart, firstContent)) { wysiwygContentsToRemove.clear(); break; } } removeWysiwygContents(commonLogPart, context, firstWysiwygContent, wysiwygContentsToRemove); } } console.printMessage(commonLogPart + " - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
/** * Removes all wysiwyg duplication between all languages. * Method// www. j ava 2 s . c o m * {@link #removeDuplicatesForEachLanguage(String, WysiwygDocumentPurger.ForeignIdProcessContext)} * must be called before. * @param context the context. */ private void removeDuplicatesBetweenAllLanguages(String commonLogPart, ForeignIdProcessContext context) { long start = System.currentTimeMillis(); commonLogPart += " - duplicates between all languages"; if (context.getWysiwygIndexedByLangs().size() > 1) { WysiwygContent defaultWysiwygContent = null; WysiwygContent firstContent = null; List<WysiwygContent> wysiwygContentsToRemove = new ArrayList<WysiwygContent>( context.getWysiwygIndexedByLangs().size()); for (Map.Entry<String, List<WysiwygContent>> languageWysiwygContents : context .getWysiwygIndexedByLangs().entrySet()) { if (!languageWysiwygContents.getValue().isEmpty()) { // It must exist at most one SimpleDocument per language, otherwise treatments stops here. if (languageWysiwygContents.getValue().size() > 1) { console.printWarning(commonLogPart + " - contains several JCR master nodes with different language contents, ..."); return; } WysiwygContent currentWysiwygContent = languageWysiwygContents.getValue().get(0); if (defaultWysiwygContent == null || !defaultWysiwygContent.getLanguage().equals(ConverterUtil.defaultLanguage) && currentWysiwygContent.getLanguage().equals(ConverterUtil.defaultLanguage)) { defaultWysiwygContent = currentWysiwygContent; if (firstContent == null) { firstContent = defaultWysiwygContent; } } if (firstContent.isContentEqualTo(commonLogPart, currentWysiwygContent)) { wysiwygContentsToRemove.add(currentWysiwygContent); } else { // Contents between languages are differents, stopping the treatment of this method. return; } } } removeWysiwygContents(commonLogPart, context, defaultWysiwygContent, wysiwygContentsToRemove); } console.printMessage(commonLogPart + " - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
/** * Renames all wysiwyg file name according to the language. * @param context the context.//from w w w .ja v a 2 s.c o m */ private void renameFilenames(String commonLogPart, ForeignIdProcessContext context) { long start = System.currentTimeMillis(); commonLogPart += " - wysiwyg content file name"; try { for (WysiwygContent wysiwygContent : new ArrayList<WysiwygContent>(context.getWysiwygOfForeignId())) { String fileNameLanguage = ConverterUtil .checkLanguage(ConverterUtil.extractLanguage(wysiwygContent.getAttachment().getFilename())); if (!wysiwygContent.getLanguage().equals(fileNameLanguage)) { File contentToRename = new File(wysiwygContent.getAttachment().getAttachmentPath()); if (contentToRename.exists() && contentToRename.isFile()) { wysiwygContent.getAttachment().setFilename(ConverterUtil.replaceLanguage( wysiwygContent.getAttachment().getFilename(), wysiwygContent.getLanguage())); File contentRenamed = new File(wysiwygContent.getAttachment().getAttachmentPath()); console.printMessage(commonLogPart + " - doc=" + wysiwygContent.getNodeName() + ", lang=" + wysiwygContent.getLanguage() + " - " + contentToRename.getName() + " renaming to " + contentRenamed.getName()); if (contentRenamed.exists()) { console.printWarning(commonLogPart + " - doc=" + wysiwygContent.getNodeName() + ", lang=" + wysiwygContent.getLanguage() + " aborted because " + contentRenamed.getName() + " already exists! Nothing is renamed and a manual intervention " + "must be performed."); return; } service.updateAttachment(wysiwygContent.getAttachment(), contentToRename); FileUtils.deleteQuietly(contentToRename); context.addRenamedContent(); } } } } finally { console.printMessage(commonLogPart + " - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); } }
From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java
/** * Common treatments to remove content./* w ww. j av a2 s . com*/ * @param commonLogPart * @param context * @param wysiwygContentToKeep * @param wysiwygContentsToRemove */ private void removeWysiwygContents(String commonLogPart, ForeignIdProcessContext context, WysiwygContent wysiwygContentToKeep, List<WysiwygContent> wysiwygContentsToRemove) { long start = System.currentTimeMillis(); if (wysiwygContentToKeep != null) { wysiwygContentsToRemove.remove(wysiwygContentToKeep); if (!wysiwygContentsToRemove.isEmpty()) { console.printMessage(commonLogPart + " - doc=" + wysiwygContentToKeep.getNodeName() + ", lang=" + wysiwygContentToKeep.getLanguage() + " - content kept"); for (WysiwygContent wysiwygContentToRemove : wysiwygContentsToRemove) { service.removeContent(wysiwygContentToRemove.getAttachment(), wysiwygContentToRemove.getLanguage()); console.printMessage(commonLogPart + " - doc=" + wysiwygContentToRemove.getNodeName() + ", lang=" + wysiwygContentToRemove.getLanguage() + " - content removed"); context.remove(wysiwygContentToRemove); } context.resetIndexation(); } } console.printMessage(commonLogPart + " - removeWysiwygContents method - duration of " + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); }