我正在使用此处的Java money(JSR354)参考实现:http://javamoney.github.io/ri.html
但是,我在OSGI环境中使用它。这给了我以下例外:
[qtp305372452-33] ERROR org.javamoney.moneta.spi.MonetaryConfig - Error loading javamoney.properties, ignoring bundleresource://7.fwk302155142:3/javamoney.properties
java.lang.IllegalStateException: AmbiguousConfiguration detected for 'load.ECBHistoricRateProvider.resource'.
at org.javamoney.moneta.spi.MonetaryConfig.updateConfig(MonetaryConfig.java:90)
at org.javamoney.moneta.spi.MonetaryConfig.<init>(MonetaryConfig.java:53)
at org.javamoney.moneta.spi.MonetaryConfig.<clinit>(MonetaryConfig.java:39)
at org.javamoney.moneta.DefaultMonetaryContextFactory.getContext(DefaultMonetaryContextFactory.java:38)
at org.javamoney.moneta.Money.<clinit>(Money.java:79)
at org.javamoney.moneta.internal.MoneyAmountBuilder.create(MoneyAmountBuilder.java:42)
at org.javamoney.moneta.internal.MoneyAmountBuilder.create(MoneyAmountBuilder.java:33)
at org.javamoney.moneta.spi.AbstractAmountBuilder.create(AbstractAmountBuilder.java:61)
at com.eijsink.ef.module.pricing.Price.createAmount(Price.java:214)
at com.eijsink.ef.module.pricing.Price.<init>(Price.java:73)
at com.eijsink.ef.module.pricing.view.PriceField.storeValue(PriceField.java:210)
at com.eijsink.ef.module.pricing.view.PriceField.lambda$6(PriceField.java:169)
at com.eijsink.ef.module.pricing.view.PriceField$$Lambda$163/23040973.valueChange(Unknown Source)
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 com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1137)
at com.vaadin.ui.AbstractField.setValue(AbstractField.java:548)
at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:709)
at com.vaadin.ui.ComboBox.changeVariables(ComboBox.java:674)
at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:486)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:305)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:184)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:92)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:350)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:76)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(HttpServerManager.java:386)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:229)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:533)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1088)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:370)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:744)
使用Java money类时不会抛出异常,而是仅记录它。一切正常,但这个异常会在我的日志中一直弹出,这对我和我的同事来说非常烦人。
我已将问题追溯到库中的这个类:
package org.javamoney.moneta.spi;
//imports
public final class MonetaryConfig {
private static final Logger LOG = Logger
.getLogger(MonetaryConfig.class.getName());
private static final MonetaryConfig INSTANCE = new MonetaryConfig();
private Map<String, String> config = new HashMap<>();
private Map<String, Integer> priorities = new HashMap<>();
private MonetaryConfig() {
try {
Enumeration<URL> urls = getClass().getClassLoader().getResources(
"javamoney.properties");
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
try {
Properties props = new Properties();
props.load(url.openStream());
updateConfig(props);
} catch (Exception e) {
LOG.log(Level.SEVERE,
"Error loading javamoney.properties, ignoring "
+ url, e);
}
}
} catch (IOException e) {
LOG.log(Level.SEVERE, "Error loading javamoney.properties.", e);
}
}
private void updateConfig(Properties props) {
for (Map.Entry<Object, Object> en : props.entrySet()) {
String key = en.getKey().toString();
String value = en.getValue().toString();
int prio = 0;
if (key.startsWith("{")) {
int index = key.indexOf('}');
if (index > 0) {
String prioString = key.substring(1, index);
try {
prio = Integer.parseInt(prioString);
key = key.substring(index + 1);
} catch (NumberFormatException e) {
LOG.warning("Invalid config key in javamoney.properties: " + key);
}
}
}
Integer existingPrio = priorities.get(key);
if (Objects.isNull(existingPrio)) {
priorities.put(key, prio);
config.put(key, value);
} else if (existingPrio < prio) {
priorities.put(key, prio);
config.put(key, value);
} else if (existingPrio == prio) {
throw new IllegalStateException(
"AmbiguousConfiguration detected for '" + key + "'.");
}
// else ignore entry with lower prio!
}
}
public static Map<String, String> getConfig() {
return Collections.unmodifiableMap(INSTANCE.config);
}
}
updateConfig()方法抛出异常。 MonetaryConfig构造函数记录它。出现问题的原因是
行Enumeration<URL> urls = getClass().getClassLoader().getResources(
"javamoney.properties");
为同一个文件返回2个URL,即:“bundleresource://7.fwk302155142/javamoney.properties”和“bundleresource://7.fwk302155142:3 / javamoney.properties”。
我检查了.JAR文件,只有一个属性文件,它肯定被读了两次。
问题的真正根源是资源的加载方式。在OSGI中,这不是加载资源的方式(您应该从您的包中获取资源)。但是,我不知道如何修改此库加载资源的方式(除了修改和重新编译源代码,我不想这样做)。
加载此库时是否可以使用任何OSGI技术使其正常工作?
编辑:我的清单包类路径
Bundle-ClassPath: jars/moneta-1.0-RC3.jar,
jars/money-api-1.0-RC3.jar,
jars/javax.annotation-api-1.2.jar
答案 0 :(得分:4)
来自网址:
bundleresource://7.fwk302155142/javamoney.properties
bundleresource://7.fwk302155142:3/javamoney.properties
由于Bundle-Classpath
,资源似乎在那里两次。 :3
应将索引引用到Bundle-Classpath
。确保Bundle-Classpath
中的任何条目都不包含属性文件。
答案 1 :(得分:1)
这是一个古老的话题,但是令人惊讶的是,我今天遇到了这个问题。对我来说固定的是改变我的依赖性 来自:
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.1</version>
</dependency>
收件人:
<dependency>
<groupId>org.javamoney.moneta</groupId>
<artifactId>moneta-core</artifactId>
<version>1.3</version>
</dependency>
这是针对Java 8项目的。