Skip to content

Commit

Permalink
Monitor for repository changes using new JDK7 WatchService API
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jan 9, 2014
1 parent 8578984 commit 45eb9b9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static ForgeArchive getDeployment1()
return archive;
}

@Test
@Test(timeout = 5000)
public void testPreShutdownIsCalled() throws Exception
{
Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
package org.jboss.forge.furnace.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -55,21 +59,30 @@ public class FurnaceImpl implements Furnace

private final List<AddonRepository> repositories = new ArrayList<>();
private final Map<AddonRepository, Integer> lastRepoVersionSeen = new HashMap<>();
private final Map<AddonRepository, Date> lastRepoUpdateSeen = new HashMap<>();

private final LockManager lock = new LockManagerImpl();

private String[] args;

private int registryCount = 0;

private WatchService watcher;

public FurnaceImpl()
{
if (!AddonRepositoryImpl.hasRuntimeAPIVersion())
logger.warning("Could not detect Furnace runtime version - " +
"loading all addons, but failures may occur if versions are not compatible.");
if (Boolean.getBoolean("furnace.debug"))
enableLogging();
try
{
watcher = FileSystems.getDefault().newWatchService();
}
catch (IOException e)
{
logger.log(Level.WARNING, "File monitoring could not be started.", e);
}
}

@Override
Expand Down Expand Up @@ -147,15 +160,22 @@ public Furnace start(ClassLoader loader)
for (AddonRepository repository : repositories)
{
int repoVersion = repository.getVersion();
Date lastRepoUpdate = repository.getLastModified();
if (repoVersion > lastRepoVersionSeen.get(repository)
|| lastRepoUpdate.after(lastRepoUpdateSeen.get(repository)))
if (repoVersion > lastRepoVersionSeen.get(repository))
{
logger.log(Level.INFO, "Detected changes in repository [" + repository + "].");
lastRepoVersionSeen.put(repository, repoVersion);
lastRepoUpdateSeen.put(repository, lastRepoUpdate);
dirty = true;
}

WatchKey key = watcher.poll();
while (key != null)
{
if (!key.pollEvents().isEmpty())
{
dirty = true;
}
key = watcher.poll();
}
}

if (dirty)
Expand Down Expand Up @@ -292,7 +312,7 @@ public AddonRegistry getAddonRegistry(AddonRepository... repositories)
{
if (repositories == null || repositories.length == 0)
{
result = new AddonRegistryImpl(lock, getLifecycleManager(), new ArrayList<>(getRepositories()), "ROOT");
result = new AddonRegistryImpl(lock, getLifecycleManager(), getRepositories(), "ROOT");
getLifecycleManager().addView(result);
}
else
Expand Down Expand Up @@ -358,6 +378,22 @@ public AddonRepository addRepository(AddonRepositoryMode mode, File directory)
if (mode.isImmutable())
repository = new ImmutableAddonRepository(repository);

try
{
if (watcher != null)
{
if (directory.exists())
directory.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
else
logger.log(Level.WARNING, "Cannot monitor repository [" + directory
+ "] for changes because it does not exist.");
}
}
catch (IOException e)
{
logger.log(Level.WARNING, "Could not monitor repository [" + directory.toString() + "] for file changes.", e);
}

return addRepository(repository);
}

Expand All @@ -376,7 +412,6 @@ public AddonRepository addRepository(AddonRepository repository)

this.repositories.add(repository);
lastRepoVersionSeen.put(repository, 0);
lastRepoUpdateSeen.put(repository, new Date());

return repository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Set<Addon> getAddons(final AddonFilter filter)
@Override
public Set<Addon> call() throws Exception
{
HashSet<Addon> result = new HashSet<Addon>();
HashSet<Addon> result = new HashSet<>();

for (Addon addon : manager.getAddons(AddonRegistryImpl.this))
{
Expand All @@ -109,19 +109,19 @@ public Set<Addon> call() throws Exception
@Override
public Set<AddonRepository> getRepositories()
{
return Collections.unmodifiableSet(new LinkedHashSet<AddonRepository>(repositories));
return Collections.unmodifiableSet(new LinkedHashSet<>(repositories));
}

@Override
public <T> Imported<T> getServices(final Class<T> type)
{
return new ImportedImpl<T>(this, lock, type);
return new ImportedImpl<>(this, lock, type);
}

@Override
public <T> Imported<T> getServices(final String typeName)
{
return new ImportedImpl<T>(this, lock, typeName);
return new ImportedImpl<>(this, lock, typeName);
}

@Override
Expand All @@ -132,7 +132,7 @@ public Set<Class<?>> getExportedTypes()
@Override
public Set<Class<?>> call() throws Exception
{
Set<Class<?>> result = new HashSet<Class<?>>();
Set<Class<?>> result = new HashSet<>();
for (Addon addon : getAddons())
{
if (AddonStatus.STARTED.equals(addon.getStatus()))
Expand All @@ -154,7 +154,7 @@ public <T> Set<Class<T>> getExportedTypes(final Class<T> type)
@Override
public Set<Class<T>> call() throws Exception
{
Set<Class<T>> result = new HashSet<Class<T>>();
Set<Class<T>> result = new HashSet<>();
for (Addon addon : getAddons())
{
if (AddonStatus.STARTED.equals(addon.getStatus()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URL;
import java.util.List;
import java.util.ServiceLoader;
import java.util.concurrent.TimeoutException;

import org.jboss.forge.furnace.Furnace;
import org.jboss.forge.furnace.addons.AddonId;
Expand Down Expand Up @@ -108,8 +109,8 @@ public void testFurnaceLoadsInstalledAddonFromSameInstance() throws IOException
Assert.assertEquals(1, furnace.getAddonRegistry().getAddons().size());
}

@Test(timeout = 5000)
public void testFurnaceLoadsInstalledAddonFromSeparateInstance() throws IOException
@Test(timeout = 20000)
public void testFurnaceLoadsInstalledAddonFromSeparateInstance() throws IOException, TimeoutException
{
Assert.assertEquals(1, furnace.getRepositories().size());
Assert.assertEquals(0, furnace.getAddonRegistry().getAddons().size());
Expand Down

0 comments on commit 45eb9b9

Please sign in to comment.