在我们的应用程序中,我们使用ini文件来配置OSGi应用程序。现在,我考虑添加一个WatchService来检测更改,并在必要时重新加载修改后的文件。
到目前为止,我发现了多个教程。它们中的大多数是相似的,结果如下所示:http://java.dzone.com/news/how-watch-file-system-changes。我是这样收养的:
36: public Ini(final File file) throws IOException {
if (!file.exists()) {
throw new IOException("Ini-File does not exist.");
} else if (!file.canRead()) {
40: throw new IOException("Ini-File is not readable.");
}
iniFile = file;
FileSystem fileSystem = FileSystems.getFileSystem(file.toURI());
45: Path test = fileSystem.getPath(file.getAbsolutePath());
Path ini = FileSystems.getDefault().getPath(file.getParent(), file.getName());
Logger.debug(ini.toString());
readIni(ini);
50: service = FileSystems.getDefault().newWatchService();
file.getParentFile().toPath().register(service, ENTRY_MODIFY);
Thread watcher = new Thread(() -> {
// noinspection InfiniteLoopStatement
55: while (true) {
WatchKey key = null;
try {
key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
60: Logger.debug("event received");
if (event.kind() != ENTRY_MODIFY) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
65:
File filename = ev.context().toFile();
if (filename.getAbsolutePath().equals(iniFile.getAbsolutePath())) {
Logger.debug(filename.getName() + " has changed");
70: config = new HashMap<>();
try {
readIni(filename.toPath());
} catch(IOException e) {
Logger.warning(e);
75: }
}
}
} catch (Exception e) {
Logger.warning(e);
80: }
if (key != null)
key.reset();
}
});
85:
watcher.start();
}在创建新实例时,我首先检查文件是否存在。在此之后,我希望创建一个WatchService分析此配置文件(最初只创建这个配置文件)。稍后,我将对所有加载的文件更改为1 WatchService )。
有几行(44-46)以不同的方式进行相同的操作。他们都不起作用。
现在,当我创建这样一个Ini文件时,felix告诉我如下:
[ERROR] : java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems$DefaultFileSystemHolder
java.lang.IllegalStateException: java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems$DefaultFileSystemHolder
at org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler.__M_stateChanged(LifecycleCallbackHandler.java:171)
at org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler.stateChanged(LifecycleCallbackHandler.java)
at org.apache.felix.ipojo.InstanceManager.setState(InstanceManager.java:560)
at org.apache.felix.ipojo.InstanceManager.start(InstanceManager.java:440)
at org.apache.felix.ipojo.ComponentFactory.createInstance(ComponentFactory.java:179)
at org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:319)
at org.apache.felix.ipojo.IPojoFactory.createComponentInstance(IPojoFactory.java:240)
at org.apache.felix.ipojo.extender.internal.linker.ManagedType$InstanceSupport$1.call(ManagedType.java:312)
at org.apache.felix.ipojo.extender.internal.linker.ManagedType$InstanceSupport$1.call(ManagedType.java:306)
at org.apache.felix.ipojo.extender.internal.queue.JobInfoCallable.call(JobInfoCallable.java:114)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems$DefaultFileSystemHolder
at java.nio.file.FileSystems.getDefault(FileSystems.java:176)
at java.nio.file.spi.FileSystemProvider.installedProviders(FileSystemProvider.java:156)
at java.nio.file.FileSystems.getFileSystem(FileSystems.java:219)
at df.core.config.internal.Ini.<init>(Ini.java:44)
at df.core.config.internal.ConfigImpl.__M_addConfig(ConfigImpl.java:180)
at df.core.config.internal.ConfigImpl.addConfig(ConfigImpl.java)
at df.core.config.api.Config$$Proxy.addConfig(Unknown Source)
at df.playground.IniTest.__M_validate(IniTest.java:24)
at df.playground.IniTest.validate(IniTest.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.felix.ipojo.util.Callback.call(Callback.java:237)
at org.apache.felix.ipojo.util.Callback.call(Callback.java:193)
at org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallback.call(LifecycleCallback.java:86)
at org.apache.felix.ipojo.handlers.lifecycle.callback.LifecycleCallbackHandler.__M_stateChanged(LifecycleCallbackHandler.java:162)
... 13 more这说明我在第44行中搜索文件的fileSystem对象。我是不是因为我的代码找不到DefaultFileSystemHolder而做错了什么?它是一个java内部类,所以我不需要在我的MANIFEST.MF中使用一个导入语句,对吗?
它在Ubuntu14.04上运行,使用ApacheFelix4.4.1和Oracle Java 8并使用Maven构建。
提前感谢
发布于 2014-08-07 13:34:14
tl;dr
向Felix添加以下配置:
org.osgi.framework.bootdelegation=sun.nio.*解释:
Oracle中的FileSystems实现使用内部类sun.nio.fs.DefaultFileSystemProvider。这是JDK实现的一个好奇心,因为它希望在任何类加载器中都可以看到一些内部类,比如这个类。在OSGi中,所有包都可以看到的惟一类是以java.开头的类,如java.lang.String等。
引导委托配置允许我们扩展始终可见的包集。它正是针对此场景的,即用于JVM内部类。有时,它对于运行已被检测的代码也很有用,例如,对于覆盖率或分析。它不是公开来自JDK的附加API的通用设置。
https://stackoverflow.com/questions/25178582
复制相似问题