-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt at having the app poll the plugins directory to dynamic…
…ally reload plugins. Class unloading occurs when threadpools are shutdown and the correct hooks are triggered, now just a matter of making sure the correct triggers happen at the correct time.
- Loading branch information
Graham Allan
committed
Nov 1, 2013
1 parent
7eb6cac
commit 42e16ea
Showing
18 changed files
with
284 additions
and
243 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/org/netmelody/cieye/core/observation/ForeignAgencies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.netmelody.cieye.core.observation; | ||
|
||
import org.netmelody.cieye.core.domain.CiServerType; | ||
|
||
public interface ForeignAgencies { | ||
ObservationAgency agencyFor(CiServerType type); | ||
boolean hasChanged(); | ||
} |
26 changes: 0 additions & 26 deletions
26
src/main/java/org/netmelody/cieye/core/observation/ForeignAgents.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/netmelody/cieye/server/configuration/PluginDirectory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.netmelody.cieye.server.configuration; | ||
|
||
import java.io.File; | ||
import java.io.FileFilter; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.filefilter.SuffixFileFilter; | ||
|
||
public final class PluginDirectory { | ||
|
||
private final File directory; | ||
private Date lastReadDate = new Date(0L); | ||
|
||
public PluginDirectory(File directory) { | ||
this.directory = directory; | ||
} | ||
|
||
public Iterable<File> jars() { | ||
lastReadDate = new Date(); | ||
File[] jarFiles = directory.listFiles((FileFilter)new SuffixFileFilter(".jar")); | ||
return jarFiles == null ? Collections.<File>emptySet() : Arrays.asList(jarFiles); | ||
} | ||
|
||
public boolean updateAvailable() { | ||
return FileUtils.isFileNewer(directory, lastReadDate); | ||
} | ||
|
||
} |
56 changes: 0 additions & 56 deletions
56
src/main/java/org/netmelody/cieye/server/configuration/RecordedForeignAgents.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
.../java/org/netmelody/cieye/server/configuration/ServiceLoadingRecordedForeignAgencies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package org.netmelody.cieye.server.configuration; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.Date; | ||
import java.util.Iterator; | ||
import java.util.ServiceLoader; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.netmelody.cieye.core.domain.CiServerType; | ||
import org.netmelody.cieye.core.logging.LogKeeper; | ||
import org.netmelody.cieye.core.logging.Logbook; | ||
import org.netmelody.cieye.core.observation.ForeignAgencies; | ||
import org.netmelody.cieye.core.observation.ObservationAgency; | ||
|
||
import com.google.common.collect.FluentIterable; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Sets; | ||
|
||
public final class ServiceLoadingRecordedForeignAgencies implements ForeignAgencies, Refreshable { | ||
private final static Logbook LOGBOOK = LogKeeper.logbookFor(ServiceLoadingRecordedForeignAgencies.class); | ||
|
||
private final PluginDirectory pluginDirectory; | ||
|
||
private final AtomicReference<Date> lastUpdate = new AtomicReference<Date>(new Date(0L)); | ||
private final AtomicReference<Date> lastAsked = new AtomicReference<Date>(new Date(0L)); | ||
|
||
private final AtomicReference<ServiceLoader<ObservationAgency>> services; | ||
|
||
public ServiceLoadingRecordedForeignAgencies(PluginDirectory pluginDirectory) { | ||
this.pluginDirectory = pluginDirectory; | ||
this.services = new AtomicReference<ServiceLoader<ObservationAgency>>(newServiceLoader()); | ||
} | ||
|
||
@Override | ||
public ObservationAgency agencyFor(CiServerType type) { | ||
final String typeName = type.name(); | ||
Iterator<ObservationAgency> agencies = available(); | ||
|
||
while (agencies.hasNext()) { | ||
ObservationAgency agency = agencies.next(); | ||
if (agency.canProvideSpyFor(type)) { | ||
return agency; | ||
} | ||
} | ||
|
||
throw new IllegalStateException("No CI Observation Module for " + typeName); | ||
} | ||
|
||
private ServiceLoader<ObservationAgency> newServiceLoader() { | ||
return ServiceLoader.load(ObservationAgency.class, pluginsClassLoader()); | ||
} | ||
|
||
private Iterator<ObservationAgency> available() { | ||
return services.get().iterator(); | ||
} | ||
|
||
private ClassLoader pluginsClassLoader() { | ||
Iterable<File> jarFiles = pluginDirectory.jars(); | ||
return new URLClassLoader(FluentIterable.from(urlsOf(jarFiles)).toArray(URL.class)); | ||
} | ||
|
||
private Set<URL> urlsOf(Iterable<File> jarFiles) { | ||
Set<URL> urls = Sets.newHashSet(); | ||
Set<Throwable> problems = Sets.newHashSet(); | ||
for (File file : jarFiles) { | ||
try { | ||
urls.add(file.toURI().toURL()); | ||
} catch (MalformedURLException e) { | ||
problems.add(e); | ||
} | ||
} | ||
|
||
if (!problems.isEmpty()) { | ||
logProblems(problems); | ||
} | ||
|
||
return ImmutableSet.copyOf(urls); | ||
} | ||
|
||
private static void logProblems(Set<Throwable> problems) { | ||
System.out.printf("Found [%d] problems obtaining plugin jars. See logs for more details.%n", problems.size()); | ||
for (Throwable throwable : problems) { | ||
LOGBOOK.error("Error loading plugin.", throwable); | ||
} | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
if (pluginDirectory.updateAvailable()) { | ||
services.set(newServiceLoader()); | ||
lastUpdate.set(new Date()); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public boolean hasChanged() { | ||
Date lastAskedDate = lastAsked.getAndSet(new Date()); | ||
return lastUpdate.get().after(lastAskedDate); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/main/java/org/netmelody/cieye/server/observation/ObservationAgencyConfiguration.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.