我在使用OSGi时遇到了问题。
我有什么:
如前所述,捆绑调用非常简单。 Bundle A调用bundle B.唯一复杂的是,调用是一个doPrivileged调用。
以下场景/示例:
在没有doPrivileged的捆绑A中设置“bla”属性 - >失败(ok)
public void foo() {
System.setProperty("bla", "blubb"); // throws java.security.AccessControlException: access denied ("java.util.PropertyPermission" "bla" "write") -> ok
}
使用doPrivileged - >在包A中设置“bla”属性工作(好)
public void foo() {
AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
@Override
public Boolean run()
throws Exception
{
System.setProperty("bla", "blubb"); // sets the bla property without throwing an exception -> ok
return null;
}
});
}
现在尝试使用bundle A的doPrivileged调用设置bundle B中的“bla”属性 - &gt;失败(为什么?)
捆绑A:
public void foo() {
AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
@Override
public Boolean run()
throws Exception
{
// calls Bundle B
bundleBService.bar();
return null;
}
});
}
捆绑B:
public void bar() {
System.setProperty("bla", "blubb"); // throws java.security.AccessControlException: access denied ("java.util.PropertyPermission" "bla" "write")
}
那么为什么它不能使用bundle A的doPrivileged调用在bundle B中设置属性呢?我希望,doPrivileged调用也可以在这里工作。为什么不呢?这是使用蓝图的错吗?如果不将doPrivileged块添加到bundle B的方法中,是否可以解决这个问题?
更新:这是StackTrace:
java.security.AccessControlException: access denied ("java.util.PropertyPermission" "bla" "write")
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:372)
at java.security.AccessController.checkPermission(AccessController.java:559)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
at java.lang.System.setProperty(System.java:782)
at barpkg.BundleB.bar(BundleB.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.aries.proxy.impl.ProxyHandler$1.invoke(ProxyHandler.java:54)
at org.apache.aries.proxy.impl.ProxyHandler.invoke(ProxyHandler.java:119)
at com.sun.proxy.$Proxy111.bar(Unknown Source)
at foopkg.BundleA$1.run(BundleA.java:73)
at foopkg.BundleA$1.run(BundleA.java:65)
at java.security.AccessController.doPrivileged(Native Method)
at foopkg.BundleA.foo(BundleA.java:65)
at testpkg.PrivilegedTest.testDoPrivileged(PrivilegedTest.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:67)
at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:37)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at org.junit.runner.JUnitCore.run(JUnitCore.java:138)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.invokeViaJUnit(JUnitProbeInvoker.java:111)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.findAndInvoke(JUnitProbeInvoker.java:84)
at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.call(JUnitProbeInvoker.java:72)
at org.ops4j.pax.exam.glassfish.GlassFishTestContainer.call(GlassFishTestContainer.java:271)
at org.ops4j.pax.exam.spi.reactors.SingletonStagedReactor.invoke(SingletonStagedReactor.java:113)
at org.ops4j.pax.exam.spi.reactors.PerSuiteStagedReactor.invoke(PerSuiteStagedReactor.java:47)
at org.ops4j.pax.exam.junit.PaxExam$2.evaluate(PaxExam.java:294)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.ops4j.pax.exam.junit.PaxExam.run(PaxExam.java:111)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:242)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:137)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
我猜,这句话可能是麻烦制造者:
at org.apache.aries.proxy.impl.ProxyHandler$1.invoke(ProxyHandler.java:54)
at org.apache.aries.proxy.impl.ProxyHandler.invoke(ProxyHandler.java:119)
at com.sun.proxy.$Proxy111.bar(Unknown Source)
我会尝试在不使用蓝图的情况下进行此调用,看看会发生什么。 但如果蓝图是问题,我可以以某种方式处理它而不替换它吗?
谢谢
答案 0 :(得分:1)
好吧现在也可以使用aries蓝图框架。
我手动签署了aries blueprint,proxy和utils jars(因此它们也是核心包),看看这是否可以解决问题。工作得很好。
我现在将创建一个maven模块,在那里我将放置我需要的所有蓝图包。所以我可以签署这个捆绑包,不需要单独签署每个jar,也只需要一个捆绑包我需要部署。