This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[updater] Uses the bundle location to track changes to its source
- Loading branch information
Showing
10 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="bin" path="src"/> | ||
<classpathentry kind="src" output="bin_test" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="con" path="aQute.bnd.classpath.container"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,3 @@ | ||
/generated/ | ||
/bin/ | ||
/bin_test/ |
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,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>osgi.enroute.updater.provider</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>bndtools.core.bndbuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>bndtools.core.bndnature</nature> | ||
</natures> | ||
</projectDescription> |
10 changes: 10 additions & 0 deletions
10
osgi.enroute.updater.provider/.settings/org.eclipse.core.resources.prefs
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,10 @@ | ||
eclipse.preferences.version=1 | ||
encoding/.classpath=UTF-8 | ||
encoding/.gitignore=UTF-8 | ||
encoding/.project=UTF-8 | ||
encoding//.settings/org.eclipse.jdt.core.prefs=UTF-8 | ||
encoding//src/osgi/enroute/updater/provider/UpdaterImpl.java=UTF-8 | ||
encoding//test/.gitignore=UTF-8 | ||
encoding//test/osgi/enroute/updater/provider/UpdaterImplTest.java=UTF-8 | ||
encoding/bnd.bnd=UTF-8 | ||
encoding/readme.md=UTF-8 |
12 changes: 12 additions & 0 deletions
12
osgi.enroute.updater.provider/.settings/org.eclipse.jdt.core.prefs
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,12 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
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,25 @@ | ||
# | ||
# OSGI ENROUTE UPDATER PROVIDER BUNDLE | ||
# | ||
|
||
|
||
Bundle-Description: \ | ||
Updates bundles based on their location if they use a file: URL. | ||
|
||
Private-Package: \ | ||
osgi.enroute.updater.provider | ||
|
||
-buildpath: \ | ||
osgi.enroute.base.api;version=1.0 | ||
|
||
-testpath: \ | ||
osgi.enroute.junit.wrapper;version=4.12 | ||
|
||
-includeresource: {readme.md} | ||
|
||
|
||
-runrequires: \ | ||
osgi.identity;filter:='(osgi.identity=osgi.enroute.updater.provider)' | ||
|
||
-runbundles: \ | ||
${error;Resolve first} |
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,15 @@ | ||
# OSGI ENROUTE UPDATER PROVIDER | ||
|
||
${Bundle-Description} | ||
|
||
## Example | ||
|
||
## Configuration | ||
|
||
Pid: osgi.enroute.updater | ||
|
||
Field Type Description | ||
|
||
## References | ||
|
136 changes: 136 additions & 0 deletions
136
osgi.enroute.updater.provider/src/osgi/enroute/updater/provider/UpdaterImpl.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,136 @@ | ||
package osgi.enroute.updater.provider; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.nio.file.Paths; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.BundleActivator; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.BundleEvent; | ||
import org.osgi.framework.wiring.FrameworkWiring; | ||
import org.osgi.util.tracker.BundleTracker; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A very simple bundle that tracks bundles that have a location | ||
* that maps to a file. If so, it checks the lastModified times | ||
* of the bundle and if necessary refreshes the bundles. | ||
*/ | ||
public class UpdaterImpl extends Thread implements BundleActivator { | ||
private static final String REFERENCE = "reference:"; | ||
private static final int SYSTEM_BUNDLE = 0; | ||
private static Logger logger = LoggerFactory | ||
.getLogger(UpdaterImpl.class); | ||
|
||
static class BInfo { | ||
File file; | ||
Bundle bundle; | ||
long lastPolled; | ||
} | ||
|
||
private BundleTracker<BInfo> tracker; | ||
private FrameworkWiring framework; | ||
private AtomicBoolean inRefresh = new AtomicBoolean(false); | ||
|
||
public UpdaterImpl() { | ||
super("OSGi Bundle Updater"); | ||
} | ||
|
||
@Override | ||
public void start(BundleContext context) throws Exception { | ||
framework = context.getBundle(0).adapt(FrameworkWiring.class); | ||
tracker = new BundleTracker<BInfo>(context, -1, null) { | ||
@Override | ||
public BInfo addingBundle(Bundle bundle, BundleEvent event) { | ||
try { | ||
|
||
if (bundle.getBundleId() == SYSTEM_BUNDLE) | ||
return null; | ||
|
||
String location = bundle.getLocation(); | ||
if (location.startsWith(REFERENCE)) | ||
location = location.substring(REFERENCE.length()); | ||
|
||
if (!location.startsWith("file:")) | ||
return null; | ||
|
||
File f = Paths.get(new URI(location)).toFile(); | ||
if (!f.exists()) | ||
return null; | ||
|
||
BInfo b = new BInfo(); | ||
b.bundle = bundle; | ||
return b; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
}; | ||
tracker.open(); | ||
start(); | ||
} | ||
|
||
@Override | ||
public void stop(BundleContext context) throws Exception { | ||
interrupt(); | ||
} | ||
|
||
public void run() { | ||
logger.info("Starting"); | ||
try { | ||
while (!isInterrupted()) | ||
try { | ||
logger.info("Sleep"); | ||
sleep(1000); | ||
if (inRefresh.get()) | ||
continue; | ||
|
||
Set<Bundle> bundles = new HashSet<Bundle>(); | ||
|
||
long recently = System.currentTimeMillis() - 1000; | ||
for (BInfo b : tracker.getTracked().values()) { | ||
long lastModified = b.file.lastModified(); | ||
|
||
if (lastModified > recently) | ||
continue; | ||
|
||
if (lastModified > b.bundle.getLastModified()) { | ||
bundles.add(b.bundle); | ||
b.bundle.stop(); | ||
} | ||
} | ||
|
||
if (bundles.isEmpty()) | ||
continue; | ||
|
||
logger.info("Update " + bundles); | ||
for (Bundle b : bundles) try { | ||
b.update(); | ||
b.start(); | ||
} catch(Exception e) { | ||
logger.error("Unexpected error updating bundle "+b, e); | ||
} | ||
|
||
logger.info("Refresh start"); | ||
inRefresh.set(true); | ||
framework.refreshBundles(bundles, event -> { | ||
inRefresh.set(false); | ||
logger.info("Refresh end"); | ||
}); | ||
|
||
} catch (InterruptedException e) { | ||
return; | ||
} catch (Exception e) { | ||
logger.error("Unexpected error", e); | ||
} | ||
} finally { | ||
logger.info("Exiting"); | ||
} | ||
} | ||
} |
Empty file.
18 changes: 18 additions & 0 deletions
18
osgi.enroute.updater.provider/test/osgi/enroute/updater/provider/UpdaterImplTest.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,18 @@ | ||
package osgi.enroute.updater.provider; | ||
|
||
import junit.framework.TestCase; | ||
|
||
/* | ||
* | ||
* | ||
* | ||
*/ | ||
|
||
public class UpdaterImplTest extends TestCase { | ||
|
||
/* | ||
* | ||
* | ||
* | ||
*/ | ||
} |