BundleManager 0.2 번들 소스 코드
이전 버전과 달라진 건 클래스 하나.
BundleDirectoryManager.java
import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
public class BundleDirectoryManager implements BundleActivator {
private static final long INTERBAL = 1000;
private static String BUNDLE_DIRECTORY = "E:\\bundles";
private volatile BundleContext context;
private final Thread thread = new BundleManager(BUNDLE_DIRECTORY);
public void start(BundleContext context) throws Exception {
this.context = context;
thread.start();
}
public void stop(BundleContext context) throws Exception {
thread.interrupt();
}
protected Bundle findBundleByLocation(String location) {
Bundle[] bundles = context.getBundles();
for (int i = 0; i < bundles.length; i++) {
if (bundles[i].getLocation().equals(location)) {
return bundles[i];
}
}
return null;
}
private class BundleManager extends Thread {
private File bundleLocation;
private List<String> bundleLocations = new CopyOnWriteArrayList<String>();
public BundleManager(String location) {
bundleLocation = new File(location);
}
public void run() {
if (!bundleLocation.isDirectory())
throw new RuntimeException(bundleLocation.getPath()
+ " is not directory.");
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(INTERBAL);
uninstallDeletedBundles();
installNewBundles(bundleLocation.listFiles(new JarFileFilter()));
}
} catch (InterruptedException e) {
System.out.println("I'm going out");
} catch (BundleException e) {
throw new RuntimeException(e);
}
}
private void uninstallDeletedBundles() throws BundleException {
for(String bundleLocation : bundleLocations){
File file = new File(bundleLocation.substring(5));
if(file == null || !file.exists()){
findBundleByLocation(bundleLocation).uninstall();
bundleLocations.remove(bundleLocation);
}
}
}
private void installNewBundles(File[] bundleFiles) throws BundleException {
for (File file : bundleFiles) {
String bundleLocation = "file:" + file.getAbsolutePath();
if(findBundleByLocation(bundleLocation) == null){
context.installBundle(bundleLocation);
bundleLocations.add(bundleLocation);
}
}
}
}
}
지정한 디렉터리에서 번들 파일을 삭제하면, 해당 번들은 OSGi 플랫폼에서 uninstall 하는 기능 추가. 짝짝짝 멋져부러~
이 멋진 모습을 동영상으로 찍어서 보여드리고 싶은데.. 일단 뭐 다 만든 다음에 보여드리도록 하죠.