Reformatted to match Felix coding style.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@688761 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
index 7b39b90..77b0ff0 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
@@ -32,18 +32,20 @@
import org.osgi.service.log.*;
import org.osgi.service.packageadmin.*;
-public class DirectoryWatcher extends Thread {
- final static String ALIAS_KEY = "_alias_factory_pid";
- public final static String POLL = "felix.fileinstall.poll";
- public final static String DIR = "felix.fileinstall.dir";
- public final static String DEBUG = "felix.fileinstall.debug";
- File watchedDirectory;
- long poll = 2000;
- long debug;
- BundleContext context;
- boolean reported;
+public class DirectoryWatcher extends Thread
+{
+ final static String ALIAS_KEY = "_alias_factory_pid";
+ public final static String POLL = "felix.fileinstall.poll";
+ public final static String DIR = "felix.fileinstall.dir";
+ public final static String DEBUG = "felix.fileinstall.debug";
+ File watchedDirectory;
+ long poll = 2000;
+ long debug;
+ BundleContext context;
+ boolean reported;
- public DirectoryWatcher(Dictionary properties, BundleContext context) {
+ public DirectoryWatcher(Dictionary properties, BundleContext context)
+ {
super(properties.toString());
this.context = context;
poll = getLong(POLL, poll);
@@ -51,8 +53,9 @@
String dir = (String) properties.get(DIR);
if (dir == null)
+ {
dir = "./load";
-
+ }
this.watchedDirectory = new File(dir);
this.watchedDirectory.mkdirs();
}
@@ -62,24 +65,31 @@
* between installed and newly found/lost bundles and configurations.
*
*/
- public void run() {
+ public void run()
+ {
log(POLL + " (ms) " + poll, null);
log(DIR + " " + watchedDirectory.getAbsolutePath(), null);
log(DEBUG + " " + debug, null);
Map currentManagedBundles = new HashMap(); // location -> Long(time)
Map currentManagedConfigs = new HashMap(); // location -> Long(time)
- while (!interrupted()) {
- try {
- Set/* <String> */installed = new HashSet();
- Set/* <String> */configs = new HashSet();
+ while (!interrupted())
+ {
+ try
+ {
+ Set/* <String> */ installed = new HashSet();
+ Set/* <String> */ configs = new HashSet();
traverse(installed, configs, watchedDirectory);
doInstalled(currentManagedBundles, installed);
doConfigs(currentManagedConfigs, configs);
Thread.sleep(poll);
- } catch (InterruptedException e) {
+ }
+ catch (InterruptedException e)
+ {
return;
- } catch (Throwable e) {
+ }
+ catch (Throwable e)
+ {
log("In main loop, we have serious trouble", e);
}
}
@@ -94,31 +104,40 @@
* @param discovered
* Newly found configurations
*/
- void doConfigs(Map current, Set discovered) {
- try {
+ void doConfigs(Map current, Set discovered)
+ {
+ try
+ {
// Set all old keys as inactive, we remove them
// when we find them to be active, will be left
// with the inactive ones.
Set inactive = new HashSet(current.keySet());
- for (Iterator e = discovered.iterator(); e.hasNext();) {
+ for (Iterator e = discovered.iterator(); e.hasNext();)
+ {
String path = (String) e.next();
File f = new File(path);
- if (!current.containsKey(path)) {
+ if (!current.containsKey(path))
+ {
// newly found entry, set the config immedialey
Long l = new Long(f.lastModified());
- if (setConfig(f)) {
+ if (setConfig(f))
+ {
// Remember it for the next round
current.put(path, l);
}
- } else {
+ }
+ else
+ {
// Found an existing one.
// Check if it has been updated
long lastModified = f.lastModified();
long oldTime = ((Long) current.get(path)).longValue();
- if (oldTime < lastModified) {
- if (setConfig(f)) {
+ if (oldTime < lastModified)
+ {
+ if (setConfig(f))
+ {
// Remember it for the next round.
current.put(path, new Long(lastModified));
}
@@ -127,14 +146,18 @@
// Mark this one as active
inactive.remove(path);
}
- for (Iterator e = inactive.iterator(); e.hasNext();) {
+ for (Iterator e = inactive.iterator(); e.hasNext();)
+ {
String path = (String) e.next();
File f = new File(path);
- if (deleteConfig(f)) {
+ if (deleteConfig(f))
+ {
current.remove(path);
}
}
- } catch (Exception ee) {
+ }
+ catch (Exception ee)
+ {
log("Processing config: ", ee);
}
}
@@ -147,14 +170,16 @@
* @return
* @throws Exception
*/
- boolean setConfig(File f) throws Exception {
- ConfigurationAdmin cm = (ConfigurationAdmin) FileInstall.cmTracker
- .getService();
- if (cm == null) {
- if (debug != 0 && !reported) {
+ boolean setConfig(File f) throws Exception
+ {
+ ConfigurationAdmin cm = (ConfigurationAdmin) FileInstall.cmTracker.getService();
+ if (cm == null)
+ {
+ if (debug != 0 && !reported)
+ {
log(
- "Can't find a Configuration Manager, configurations do not work",
- null);
+ "Can't find a Configuration Manager, configurations do not work",
+ null);
reported = true;
}
return false;
@@ -167,11 +192,13 @@
String pid[] = parsePid(f.getName());
Hashtable ht = new Hashtable();
ht.putAll(p);
- if (pid[1] != null) {
+ if (pid[1] != null)
+ {
ht.put(ALIAS_KEY, pid[1]);
}
Configuration config = getConfiguration(pid[0], pid[1]);
- if (config.getBundleLocation() != null) {
+ if (config.getBundleLocation() != null)
+ {
config.setBundleLocation(null);
}
config.update(ht);
@@ -186,39 +213,55 @@
* @return
* @throws Exception
*/
- boolean deleteConfig(File f) throws Exception {
+ boolean deleteConfig(File f) throws Exception
+ {
String pid[] = parsePid(f.getName());
Configuration config = getConfiguration(pid[0], pid[1]);
config.delete();
return true;
}
- String[] parsePid(String path) {
+ String[] parsePid(String path)
+ {
String pid = path.substring(0, path.length() - 4);
int n = pid.indexOf('-');
- if (n > 0) {
+ if (n > 0)
+ {
String factoryPid = pid.substring(n + 1);
pid = pid.substring(0, n);
- return new String[] { pid, factoryPid };
- } else {
- return new String[] { pid, null };
+ return new String[]
+ {
+ pid, factoryPid
+ };
+ }
+ else
+ {
+ return new String[]
+ {
+ pid, null
+ };
}
}
Configuration getConfiguration(String pid, String factoryPid)
- throws Exception {
- ConfigurationAdmin cm = (ConfigurationAdmin) FileInstall.cmTracker
- .getService();
- if (factoryPid != null) {
- String filter = "(|(" + ALIAS_KEY + "=" + factoryPid
- + ")(.alias_factory_pid=" + factoryPid + "))";
+ throws Exception
+ {
+ ConfigurationAdmin cm = (ConfigurationAdmin) FileInstall.cmTracker.getService();
+ if (factoryPid != null)
+ {
+ String filter = "(|(" + ALIAS_KEY + "=" + factoryPid + ")(.alias_factory_pid=" + factoryPid + "))";
Configuration configs[] = cm.listConfigurations(filter);
- if (configs == null || configs.length == 0) {
+ if (configs == null || configs.length == 0)
+ {
return cm.createFactoryConfiguration(pid, null);
- } else {
+ }
+ else
+ {
return configs[0];
}
- } else {
+ }
+ else
+ {
return cm.getConfiguration(pid, null);
}
}
@@ -232,13 +275,16 @@
* @param discovered
* A set of paths that represent the just found bundles
*/
- void doInstalled(Map current, Set discovered) {
+ void doInstalled(Map current, Set discovered)
+ {
boolean refresh = false;
Bundle bundles[] = context.getBundles();
- for (int i = 0; i < bundles.length; i++) {
+ for (int i = 0; i < bundles.length; i++)
+ {
Bundle bundle = bundles[i];
String location = bundle.getLocation();
- if (discovered.contains(location)) {
+ if (discovered.contains(location))
+ {
// We have a bundle that is already installed
// so we know it
discovered.remove(location);
@@ -252,9 +298,10 @@
Long oldSizeObj = (Long) current.get(location);
long oldSize = oldSizeObj == null ? 0 : oldSizeObj.longValue();
- if (file.lastModified() > bundle.getLastModified() + 4000
- && oldSize != newSize) {
- try {
+ if (file.lastModified() > bundle.getLastModified() + 4000 && oldSize != newSize)
+ {
+ try
+ {
// We treat this as an update, it is modified,,
// different size, and it is present in the dir
// as well as in the list of bundles.
@@ -264,7 +311,9 @@
refresh = true;
in.close();
log("Updated " + location, null);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log("Failed to update bundle ", e);
}
}
@@ -272,24 +321,34 @@
// Fragments can not be started. All other
// bundles are always started because OSGi treats this
// as a noop when the bundle is already started
- if (!isFragment(bundle)) {
- try {
+ if (!isFragment(bundle))
+ {
+ try
+ {
bundle.start();
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log("Fail to start bundle " + location, e);
}
}
- } else {
+ }
+ else
+ {
// Hmm. We found a bundlethat looks like it came from our
// watched directory but we did not find it this round.
// Just remove it.
if (bundle.getLocation().startsWith(
- watchedDirectory.getAbsolutePath())) {
- try {
+ watchedDirectory.getAbsolutePath()))
+ {
+ try
+ {
bundle.uninstall();
refresh = true;
log("Uninstalled " + location, null);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log("failed to uninstall bundle: ", e);
}
}
@@ -297,8 +356,10 @@
}
List starters = new ArrayList();
- for (Iterator it = discovered.iterator(); it.hasNext();) {
- try {
+ for (Iterator it = discovered.iterator(); it.hasNext();)
+ {
+ try
+ {
String path = (String) it.next();
File file = new File(path);
InputStream in = new FileInputStream(file);
@@ -311,19 +372,27 @@
starters.add(bundle);
log("Installed " + file.getAbsolutePath(), null);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log("failed to install/start bundle: ", e);
}
}
- if (refresh || starters.size() != 0) {
+ if (refresh || starters.size() != 0)
+ {
refresh();
- for (Iterator b = starters.iterator(); b.hasNext();) {
+ for (Iterator b = starters.iterator(); b.hasNext();)
+ {
Bundle bundle = (Bundle) b.next();
- if (!isFragment(bundle)) {
- try {
+ if (!isFragment(bundle))
+ {
+ try
+ {
bundle.start();
- } catch (BundleException e) {
+ }
+ catch (BundleException e)
+ {
log("Error while starting a newly installed bundle", e);
}
}
@@ -340,18 +409,27 @@
* @param e
* The throwable to log
*/
- void log(String message, Throwable e) {
+ void log(String message, Throwable e)
+ {
LogService log = getLogService();
if (log == null)
+ {
System.out.println(message + (e == null ? "" : ": " + e));
- else {
- if (e == null) {
+ }
+ else
+ {
+ if (e == null)
+ {
log.log(LogService.LOG_ERROR, message, e);
- if (debug > 0 && e != null) {
+ if (debug > 0 && e != null)
+ {
e.printStackTrace();
}
- } else
+ }
+ else
+ {
log.log(LogService.LOG_INFO, message);
+ }
}
}
@@ -360,10 +438,11 @@
*
* @return
*/
- LogService getLogService() {
- ServiceReference ref = context.getServiceReference(LogService.class
- .getName());
- if (ref != null) {
+ LogService getLogService()
+ {
+ ServiceReference ref = context.getServiceReference(LogService.class.getName());
+ if (ref != null)
+ {
LogService log = (LogService) context.getService(ref);
return log;
}
@@ -381,13 +460,18 @@
* @param jardir
* The directory to traverse
*/
- void traverse(Set jars, Set configs, File jardir) {
+ void traverse(Set jars, Set configs, File jardir)
+ {
String list[] = jardir.list();
- for (int i = 0; i < list.length; i++) {
+ for (int i = 0; i < list.length; i++)
+ {
File file = new File(jardir, list[i]);
- if (list[i].endsWith(".jar")) {
+ if (list[i].endsWith(".jar"))
+ {
jars.add(file.getAbsolutePath());
- } else if (list[i].endsWith(".cfg")) {
+ }
+ else if (list[i].endsWith(".cfg"))
+ {
configs.add(file.getAbsolutePath());
}
}
@@ -399,18 +483,24 @@
* @param bundle
* @return
*/
- boolean isFragment(Bundle bundle) {
+ boolean isFragment(Bundle bundle)
+ {
PackageAdmin padmin;
- if (FileInstall.padmin == null) {
+ if (FileInstall.padmin == null)
+ {
return false;
}
- try {
+ try
+ {
padmin = (PackageAdmin) FileInstall.padmin.waitForService(10000);
- if (padmin != null) {
+ if (padmin != null)
+ {
return padmin.getBundleType(bundle) == PackageAdmin.BUNDLE_TYPE_FRAGMENT;
}
- } catch (InterruptedException e) {
+ }
+ catch (InterruptedException e)
+ {
// stupid exception
}
return false;
@@ -419,12 +509,16 @@
/**
* Convenience to refresh the packages
*/
- void refresh() {
+ void refresh()
+ {
PackageAdmin padmin;
- try {
+ try
+ {
padmin = (PackageAdmin) FileInstall.padmin.waitForService(10000);
padmin.refreshPackages(null);
- } catch (InterruptedException e) {
+ }
+ catch (InterruptedException e)
+ {
Thread.currentThread().interrupt();
}
}
@@ -436,23 +530,32 @@
* @param dflt
* @return
*/
- long getLong(String property, long dflt) {
+ long getLong(String property, long dflt)
+ {
String value = context.getProperty(property);
- if (value != null) {
- try {
+ if (value != null)
+ {
+ try
+ {
return Long.parseLong(value);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
log(property + " set, but not a long: " + value, null);
}
}
return dflt;
}
- public void close() {
+ public void close()
+ {
interrupt();
- try {
+ try
+ {
join(10000);
- } catch (InterruptedException ie) {
+ }
+ catch (InterruptedException ie)
+ {
// Ignore
}
}
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
index 457436c..0378383 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
@@ -31,23 +31,24 @@
* fragment).
*
*/
-public class FileInstall implements BundleActivator, ManagedServiceFactory {
+public class FileInstall implements BundleActivator, ManagedServiceFactory
+{
static ServiceTracker padmin;
static ServiceTracker cmTracker;
- BundleContext context;
- Map watchers = new HashMap();
+ BundleContext context;
+ Map watchers = new HashMap();
- public void start(BundleContext context) throws Exception {
+ public void start(BundleContext context) throws Exception
+ {
this.context = context;
Hashtable props = new Hashtable();
props.put(Constants.SERVICE_PID, getName());
context.registerService(ManagedServiceFactory.class.getName(), this,
- props);
+ props);
padmin = new ServiceTracker(context, PackageAdmin.class.getName(), null);
padmin.open();
- cmTracker = new ServiceTracker(context, ConfigurationAdmin.class
- .getName(), null);
+ cmTracker = new ServiceTracker(context, ConfigurationAdmin.class.getName(), null);
cmTracker.open();
// Created the initial configuration
@@ -60,23 +61,32 @@
}
// Adapted for FELIX-524
- private void set(Hashtable ht, String key) {
+ private void set(Hashtable ht, String key)
+ {
Object o = context.getProperty(key);
- if (o == null) {
+ if (o == null)
+ {
o = System.getenv(key.toUpperCase().replaceAll(".", "_"));
if (o == null)
+ {
return;
+ }
}
ht.put(key, o);
}
- public void stop(BundleContext context) throws Exception {
- for (Iterator w = watchers.values().iterator(); w.hasNext();) {
- try {
+ public void stop(BundleContext context) throws Exception
+ {
+ for (Iterator w = watchers.values().iterator(); w.hasNext();)
+ {
+ try
+ {
DirectoryWatcher dir = (DirectoryWatcher) w.next();
w.remove();
dir.close();
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
// Ignore
}
}
@@ -84,19 +94,23 @@
padmin.close();
}
- public void deleted(String pid) {
+ public void deleted(String pid)
+ {
DirectoryWatcher watcher = (DirectoryWatcher) watchers.remove(pid);
- if (watcher != null) {
+ if (watcher != null)
+ {
watcher.close();
}
}
- public String getName() {
+ public String getName()
+ {
return "org.apache.felix.fileinstall";
}
public void updated(String pid, Dictionary properties)
- throws ConfigurationException {
+ throws ConfigurationException
+ {
deleted(pid);
DirectoryWatcher watcher = new DirectoryWatcher(properties, context);
watchers.put(pid, watcher);