Search in sources :

Example 16 with StopWatch

use of org.apache.commons.lang.time.StopWatch in project jspwiki by apache.

the class ReferenceManager method initialize.

/**
 *  Initializes the entire reference manager with the initial set of pages
 *  from the collection.
 *
 *  @param pages A collection of all pages you want to be included in the reference
 *               count.
 *  @since 2.2
 *  @throws ProviderException If reading of pages fail.
 */
public void initialize(Collection pages) throws ProviderException {
    log.debug("Initializing new ReferenceManager with " + pages.size() + " initial pages.");
    StopWatch sw = new StopWatch();
    sw.start();
    log.info("Starting cross reference scan of WikiPages");
    // 
    try {
        // 
        // Unserialize things.  The loop below cannot be combined with
        // the other loop below, simply because engine.getPage() has
        // side effects such as loading initializing the user databases,
        // which in turn want all of the pages to be read already...
        // 
        // Yes, this is a kludge.  We know.  Will be fixed.
        // 
        long saved = unserializeFromDisk();
        for (Iterator it = pages.iterator(); it.hasNext(); ) {
            WikiPage page = (WikiPage) it.next();
            unserializeAttrsFromDisk(page);
        }
        // 
        // Now we must check if any of the pages have been changed
        // while we were in the electronic la-la-land, and update
        // the references for them.
        // 
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // Skip attachments
            } else {
                // Refresh with the latest copy
                page = m_engine.getPage(page.getName());
                if (page.getLastModified() == null) {
                    log.fatal("Provider returns null lastModified.  Please submit a bug report.");
                } else if (page.getLastModified().getTime() > saved) {
                    updatePageReferences(page);
                }
            }
        }
    } catch (Exception e) {
        log.info("Unable to unserialize old refmgr information, rebuilding database: " + e.getMessage());
        buildKeyLists(pages);
        // Scan the existing pages from disk and update references in the manager.
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // We cannot build a reference list from the contents
            // of attachments, so we skip them.
            } else {
                updatePageReferences(page);
                serializeAttrsToDisk(page);
            }
        }
        serializeToDisk();
    }
    sw.stop();
    log.info("Cross reference scan done in " + sw);
    WikiEventUtils.addWikiEventListener(m_engine.getPageManager(), WikiPageEvent.PAGE_DELETED, this);
}
Also used : Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConcurrentModificationException(java.util.ConcurrentModificationException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 17 with StopWatch

use of org.apache.commons.lang.time.StopWatch in project jspwiki by apache.

the class ReferenceManager method unserializeAttrsFromDisk.

/**
 *  Reads the serialized data from the disk back to memory.
 *  Returns the date when the data was last written on disk
 */
private synchronized long unserializeAttrsFromDisk(WikiPage p) throws IOException, ClassNotFoundException {
    ObjectInputStream in = null;
    long saved = 0L;
    try {
        StopWatch sw = new StopWatch();
        sw.start();
        // 
        // Find attribute cache, and check if it exists
        // 
        File f = new File(m_engine.getWorkDir(), SERIALIZATION_DIR);
        f = new File(f, getHashFileName(p.getName()));
        if (!f.exists()) {
            return 0L;
        }
        log.debug("Deserializing attributes for " + p.getName());
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
        long ver = in.readLong();
        if (ver != serialVersionUID) {
            log.debug("File format has changed; cannot deserialize.");
            return 0L;
        }
        saved = in.readLong();
        String name = in.readUTF();
        if (!name.equals(p.getName())) {
            log.debug("File name does not match (" + name + "), skipping...");
            // Not here
            return 0L;
        }
        long entries = in.readLong();
        for (int i = 0; i < entries; i++) {
            String key = in.readUTF();
            Object value = in.readObject();
            p.setAttribute(key, value);
            log.debug("   attr: " + key + "=" + value);
        }
        in.close();
        sw.stop();
        log.debug("Read serialized data for " + name + " successfully in " + sw);
        p.setHasMetadata();
    } catch (NoSuchAlgorithmException e) {
        log.fatal("No MD5!?!");
    } finally {
        if (in != null)
            in.close();
    }
    return saved;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 18 with StopWatch

use of org.apache.commons.lang.time.StopWatch in project jspwiki by apache.

the class ReferenceManager method unserializeFromDisk.

/**
 *  Reads the serialized data from the disk back to memory.
 *  Returns the date when the data was last written on disk
 */
@SuppressWarnings("unchecked")
private synchronized long unserializeFromDisk() throws IOException, ClassNotFoundException {
    ObjectInputStream in = null;
    long saved = 0L;
    try {
        StopWatch sw = new StopWatch();
        sw.start();
        File f = new File(m_engine.getWorkDir(), SERIALIZATION_FILE);
        in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f)));
        long ver = in.readLong();
        if (ver != serialVersionUID) {
            throw new IOException("File format has changed; I need to recalculate references.");
        }
        saved = in.readLong();
        m_refersTo = (Map) in.readObject();
        m_referredBy = (Map) in.readObject();
        in.close();
        m_unmutableReferredBy = Collections.unmodifiableMap(m_referredBy);
        m_unmutableRefersTo = Collections.unmodifiableMap(m_refersTo);
        sw.stop();
        log.debug("Read serialized data successfully in " + sw);
    } finally {
        if (in != null)
            in.close();
    }
    return saved;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 19 with StopWatch

use of org.apache.commons.lang.time.StopWatch in project motech by motech.

the class MdsBundleWatcher method lockAndGetSchema.

private SchemaHolder lockAndGetSchema() {
    LOGGER.info("Retrieving MDS schema");
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    TransactionTemplate trxTemplate = new TransactionTemplate(transactionManager);
    final SchemaHolder schemaHolder = trxTemplate.execute(new TransactionCallback<SchemaHolder>() {

        @Override
        public SchemaHolder doInTransaction(TransactionStatus status) {
            schemaChangeLockManager.acquireLock(MdsBundleWatcher.class.getName() + " - start refreshing bundles");
            SchemaHolder result = entityService.getSchema();
            schemaChangeLockManager.releaseLock(MdsBundleWatcher.class.getName() + " - start refreshing bundles");
            return result;
        }
    });
    stopWatch.stop();
    LOGGER.info("Schema retrieved in {} ms", stopWatch.getTime());
    return schemaHolder;
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 20 with StopWatch

use of org.apache.commons.lang.time.StopWatch in project motech by motech.

the class MdsBundleWatcher method start.

// called by the initializer after the initial entities bundle was generated
public void start() {
    LOGGER.info("Scanning for MDS annotations");
    bundlesToRefresh = new ArrayList<>();
    StopWatch stopWatch = new StopWatch();
    SchemaHolder schemaHolder = lockAndGetSchema();
    final List<MDSProcessorOutput> mdsProcessorOutputs = processInstalledBundles(schemaHolder);
    stopWatch.start();
    new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            schemaChangeLockManager.acquireLock(MdsBundleWatcher.class.getName() + " - start annotation processing");
            for (MDSProcessorOutput output : mdsProcessorOutputs) {
                processAnnotationScanningResults(output);
            }
            schemaChangeLockManager.releaseLock(MdsBundleWatcher.class.getName() + " - start annotation processing");
        }
    });
    stopWatch.stop();
    LOGGER.info("Annotation processing finished in {} ms", stopWatch.getTime());
    // classes it exposes
    if (!bundlesToRefresh.isEmpty()) {
        LOGGER.info("Starting bundle refresh process");
        schemaHolder = lockAndGetSchema();
        LOGGER.info("Refreshing bundles: {}", bundlesToRefresh);
        StopWatchHelper.restart(stopWatch);
        refreshBundles(bundlesToRefresh, schemaHolder);
        stopWatch.stop();
        LOGGER.info("Bundle refresh finished in {} ms", stopWatch.getTime());
    } else {
        LOGGER.info("No bundles to refresh, proceeding");
    }
    bundleContext.addBundleListener(this);
}
Also used : MDSProcessorOutput(org.motechproject.mds.annotations.internal.MDSProcessorOutput) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SchemaHolder(org.motechproject.mds.dto.SchemaHolder) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StopWatch(org.apache.commons.lang.time.StopWatch)

Aggregations

StopWatch (org.apache.commons.lang.time.StopWatch)48 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 File (java.io.File)4 Path (java.nio.file.Path)4 HashMap (java.util.HashMap)4 FileNotFoundException (java.io.FileNotFoundException)3 ObjectOutputStream (java.io.ObjectOutputStream)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Map (java.util.Map)3 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)3 YarnApplicationReport (com.continuuity.weave.internal.yarn.YarnApplicationReport)2 BufferedInputStream (java.io.BufferedInputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 HashSet (java.util.HashSet)2