Skip to content

Commit

Permalink
Make the DirectoryWatcher instances lazy initialized to fix the Logging.
Browse files Browse the repository at this point in the history
Now, the logs related to DirectoryWatcher appears on Provisiond's log.
  • Loading branch information
agalue committed Dec 22, 2014
1 parent 7f62770 commit d06568b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void close() throws IOException {
try {
stop();
}
catch ( InterruptedException e ) {
catch (InterruptedException e) {
LOG.warn( "request to stop failed, guess its time to stop being polite!" );
}
}
Expand All @@ -127,7 +127,7 @@ public void run() {
LOG.debug("registering create watcher on " + m_path.toAbsolutePath().toString());
m_path.register( watcher, StandardWatchEventKinds.ENTRY_MODIFY);
LOG.debug( "watcher registration complete for " + m_path.toAbsolutePath().toString() );
synchronized ( this ) {
synchronized (this) {
this.notifyAll();
}
for ( ;; ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,150 +48,141 @@
* <p>FasterFilesystemForeignSourceRepository class.</p>
*/
public class FasterFilesystemForeignSourceRepository extends FilesystemForeignSourceRepository implements InitializingBean {


/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(FasterFilesystemForeignSourceRepository.class);


/** The foreign sources watcher. */
private DirectoryWatcher<ForeignSource> m_foreignSources;
private DirectoryWatcher<Requisition> m_requisitions;

/** The requisitions watcher. */
private DirectoryWatcher<Requisition> m_requisitions;

/**
* <p>Constructor for FilesystemForeignSourceRepository.</p>
* Instantiates a new faster filesystem foreign source repository.
*
* @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.
* @throws ForeignSourceRepositoryException the foreign source repository exception
*/
public FasterFilesystemForeignSourceRepository() throws ForeignSourceRepositoryException {
super();
}

@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();

m_foreignSources = new DirectoryWatcher<ForeignSource>(new File(m_foreignSourcePath), fsLoader());
m_requisitions = new DirectoryWatcher<Requisition>(new File(m_requisitionPath), reqLoader());
}

/**
* <p>getActiveForeignSourceNames</p>
*
* @return a {@link java.util.Set} object.
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getActiveForeignSourceNames()
*/
@Override
public Set<String> getActiveForeignSourceNames() {
m_readLock.lock();
try {
Set<String> activeForeignSourceNames = new LinkedHashSet<String>();
activeForeignSourceNames.addAll(m_foreignSources.getBaseNamesWithExtension(".xml"));
activeForeignSourceNames.addAll(m_requisitions.getBaseNamesWithExtension(".xml"));
return activeForeignSourceNames;
Set<String> activeForeignSourceNames = new LinkedHashSet<String>();
activeForeignSourceNames.addAll(getForeignSourcesDirectoryWatcher().getBaseNamesWithExtension(".xml"));
activeForeignSourceNames.addAll(getRequisitionsDirectoryWatcher().getBaseNamesWithExtension(".xml"));
return activeForeignSourceNames;

} finally {
m_readLock.unlock();
}
}

/**
* <p>getForeignSourceCount</p>
*
* @return a int.
* @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getForeignSourceCount()
*/
@Override
public int getForeignSourceCount() throws ForeignSourceRepositoryException {
m_readLock.lock();
try {
return m_foreignSources.getBaseNamesWithExtension(".xml").size();
return getForeignSourcesDirectoryWatcher().getBaseNamesWithExtension(".xml").size();
} finally {
m_readLock.unlock();
}
}

/**
* <p>getForeignSources</p>
*
* @return a {@link java.util.Set} object.
* @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.

/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getForeignSources()
*/
@Override
public Set<ForeignSource> getForeignSources() throws ForeignSourceRepositoryException {
m_readLock.lock();
try {
Set<ForeignSource> foreignSources = new LinkedHashSet<ForeignSource>();
for(String baseName : m_foreignSources.getBaseNamesWithExtension(".xml")) {
try {
ForeignSource contents = m_foreignSources.getContents(baseName+".xml");
foreignSources.add(contents);
} catch (FileNotFoundException e) {
LOG.info("Unable to load foreignSource {}: It must have been deleted by another thread", baseName, e);
}
}
return foreignSources;
Set<ForeignSource> foreignSources = new LinkedHashSet<ForeignSource>();
for(String baseName : getForeignSourcesDirectoryWatcher().getBaseNamesWithExtension(".xml")) {
try {
ForeignSource contents = getForeignSourcesDirectoryWatcher().getContents(baseName+".xml");
foreignSources.add(contents);
} catch (FileNotFoundException e) {
LOG.info("Unable to load foreignSource {}: It must have been deleted by another thread", baseName, e);
}
}
return foreignSources;
} finally {
m_readLock.unlock();
}
}

/** {@inheritDoc} */
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getForeignSource(java.lang.String)
*/
@Override
public ForeignSource getForeignSource(final String foreignSourceName) throws ForeignSourceRepositoryException {
if (foreignSourceName == null) {
throw new ForeignSourceRepositoryException("can't get a foreign source with a null name!");
}
m_readLock.lock();
try {
return m_foreignSources.getContents(foreignSourceName+".xml");
return getForeignSourcesDirectoryWatcher().getContents(foreignSourceName+".xml");
} catch (FileNotFoundException e) {
final ForeignSource fs = getDefaultForeignSource();
fs.setName(foreignSourceName);
return fs;
} finally {
} finally {
m_readLock.unlock();
}
}

/**
* <p>getRequisitions</p>
*
* @return a {@link java.util.Set} object.
* @throws org.opennms.netmgt.provision.persist.ForeignSourceRepositoryException if any.
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getRequisitions()
*/
@Override
public Set<Requisition> getRequisitions() throws ForeignSourceRepositoryException {
m_readLock.lock();
try {
Set<Requisition> requisitions = new LinkedHashSet<Requisition>();
for(String baseName : m_requisitions.getBaseNamesWithExtension(".xml")) {
try {
Requisition contents = m_requisitions.getContents(baseName+".xml");
requisitions.add(contents);
} catch (FileNotFoundException e) {
LOG.info("Unable to load requisition {}: It must have been deleted by another thread", baseName, e);
}
}
return requisitions;
Set<Requisition> requisitions = new LinkedHashSet<Requisition>();
for(String baseName : getRequisitionsDirectoryWatcher().getBaseNamesWithExtension(".xml")) {
try {
Requisition contents = getRequisitionsDirectoryWatcher().getContents(baseName+".xml");
requisitions.add(contents);
} catch (FileNotFoundException e) {
LOG.info("Unable to load requisition {}: It must have been deleted by another thread", baseName, e);
}
}
return requisitions;
} finally {
m_readLock.unlock();
}
}

/** {@inheritDoc} */

/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getRequisition(java.lang.String)
*/
@Override
public Requisition getRequisition(final String foreignSourceName) throws ForeignSourceRepositoryException {
if (foreignSourceName == null) {
throw new ForeignSourceRepositoryException("can't get a requisition with a null foreign source name!");
}
m_readLock.lock();
try {
return m_requisitions.getContents(foreignSourceName+".xml");
return getRequisitionsDirectoryWatcher().getContents(foreignSourceName+".xml");
} catch (FileNotFoundException e) {
LOG.warn("There is no requisition XML file for {} on {}", foreignSourceName, m_requisitionPath);
LOG.info("There is no requisition XML file for {} on {}", foreignSourceName, m_requisitionPath);
return null;
} finally {
} finally {
m_readLock.unlock();
}
}

/** {@inheritDoc} */
/* (non-Javadoc)
* @see org.opennms.netmgt.provision.persist.FilesystemForeignSourceRepository#getRequisitionURL(java.lang.String)
*/
@Override
public URL getRequisitionURL(final String foreignSource) throws ForeignSourceRepositoryException {
m_readLock.lock();
Expand All @@ -209,6 +200,45 @@ public URL getRequisitionURL(final String foreignSource) throws ForeignSourceRep
}
}

/**
* Gets the foreign sources directory watcher.
*
* @return the foreign sources directory watcher
* @throws ForeignSourceRepositoryException the foreign source repository exception
*/
public DirectoryWatcher<ForeignSource> getForeignSourcesDirectoryWatcher() throws ForeignSourceRepositoryException {
if (m_foreignSources == null) {
try {
m_foreignSources = new DirectoryWatcher<ForeignSource>(new File(m_foreignSourcePath), fsLoader());
} catch (InterruptedException e) {
throw new ForeignSourceRepositoryException("Can't initialize Foreign Sources Directory Watcher for " + m_foreignSourcePath, e);
}
}
return m_foreignSources;
}

/**
* Gets the requisitions directory watcher.
*
* @return the requisitions directory watcher
* @throws ForeignSourceRepositoryException the foreign source repository exception
*/
public DirectoryWatcher<Requisition> getRequisitionsDirectoryWatcher() throws ForeignSourceRepositoryException {
if (m_requisitions == null) {
try {
m_requisitions = new DirectoryWatcher<Requisition>(new File(m_requisitionPath), reqLoader());
} catch (InterruptedException e) {
throw new ForeignSourceRepositoryException("Can't initialize Foreign Sources Directory Watcher for " + m_foreignSourcePath, e);
}
}
return m_requisitions;
}

/**
* Foreign Sources loader.
*
* @return the file reload callback
*/
private static FileReloadCallback<ForeignSource> fsLoader() {
return new FileReloadCallback<ForeignSource>() {
@Override
Expand All @@ -222,6 +252,11 @@ public ForeignSource reload(ForeignSource object, Resource resource) throws IOEx
};
};

/**
* Requisitions loader.
*
* @return the file reload callback
*/
private static FileReloadCallback<Requisition> reqLoader() {
return new FileReloadCallback<Requisition>() {
@Override
Expand All @@ -234,5 +269,5 @@ public Requisition reload(Requisition object, Resource resource) throws IOExcept
}
};
}

}

0 comments on commit d06568b

Please sign in to comment.