2015-08-30 26 views
-2

我想使用其中一個示例to launch a FrameworkFactory。無論是我的編譯器拒絕編譯...我需要加載一個OSGi FrameworkFactory的maven依賴項

Map<String,String> props=new HashMap<String,String>(); 
props.put("org.osgi.framework.storage","target/osgi-store"); 
props.put("org.osgi.framework.storage.clean","onFirstInit"); 
FrameworkFactory factory=ServiceLoader.load(FrameworkFactory.class); 
// Type mismatch: cannot convert from ServiceLoader<FrameworkFactory> to FrameworkFactory 

或虛擬機加載失敗工廠...

ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class); 
Iterator<FrameworkFactory> iter = loader.iterator(); 
FrameworkFactory ff = iter.next(); // there is no "next" 

這是我的聚甲醛

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

<modelVersion>4.0.0</modelVersion> 
<groupId>prices.otemba.org</groupId> 
<artifactId>PricesFromOtemba</artifactId> 

<version>0.0.1</version> 
<packaging>jar</packaging> 

<properties> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 


<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.3</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
       <configuration> 
        <fork>true</fork> 
        <executable>C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe</executable> 
       </configuration> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<dependencies> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-core-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.13</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.5.4</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
     <version>1.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-core</artifactId> 
     <version>4.0.1.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>4.0.1.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.1.6.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>4.1.6.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>4.0.1.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-java</artifactId> 
     <version>1.2.2</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>info.cukes</groupId> 
     <artifactId>cucumber-junit</artifactId> 
     <version>1.2.2</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>org.apache.felix.gogo.runtime</artifactId> 
     <version>0.16.2</version> 
    </dependency> 

    <dependency> 
     <groupId>org.osgi</groupId> 
     <artifactId>org.osgi.core</artifactId> 
     <version>4.3.0</version> 
    </dependency> 

    <dependency> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>org.osgi.core</artifactId> 
     <version>1.4.0</version> 
    </dependency> 

</dependencies> 

如果您有一個關於我的普通話的建議,請隨時提供建議。

+0

解決問題與... \t \t \t \t \t org.eclipse.core \t \t \t 運行 \t \t \t 3.10。0-v20140318-2214 \t \t otembajelle

+1

如果你解決了它,或者回答你自己的問題,所以這個問題將被關閉。或刪除它。 – phiver

+0

[OSGi得到FrameworkFactory的可能重複無法實例化錯誤](https://stackoverflow.com/questions/42760795/osgi-got-frameworkfactory-could-not-be-instantiated-error) – Vogel612

回答

0

所以我的問題是關於java.util.ServiceLoader

documentation指出:「它搜索應用程序的類路徑上的服務提供商」。我構建了這些例子,並對這一說法持懷疑態度。這些示例需要在.jar中與META-INF中的子目錄進行彙編,具有完整的服務名稱。

在這種情況下,這將是實現org.osgi.framework.launch.FrameworkFactory的類的全限定名。

所以我檢查了我的M2存儲庫,如果捆綁包org.osgi.core有這樣的條目。答案是不。

結論:我使用的例子是不適合於那些沒有爲java.util.ServiceLoader

準備
1

束所以下面的條目:

<dependency> 
    <groupId>org.osgi</groupId> 
    <artifactId>org.osgi.core</artifactId> 
    <version>4.3.0</version> 
</dependency> 

添加了OSGi 4.3 API到您的項目(請注意,仿製藥需要4.3.1或更高版本)。這爲您提供了FrameworkFactory接口,但服務加載程序找不到實現。爲了得到一個實現,你需要在運行時在你的類路徑上有一個OSGi R4.3(或更高版本)框架。

例如添加了Apache菲利克斯框架R6實現(這是向後兼容R4.3 API兼容)你可以這樣:

<dependency> 
    <groupId>org.apache.felix</groupId> 
    <artifactId>org.apache.felix.framework</artifactId> 
    <version>5.0.1</version> 
    <!-- You aren't compiling against Felix, just using it at runtime --> 
    <scope>runtime</scope> 
</dependency> 

我也建議刪除以下:

<dependency> 
    <groupId>org.apache.felix</groupId> 
    <artifactId>org.osgi.core</artifactId> 
    <version>1.4.0</version> 
</dependency> 

你應該刪除它的原因是它是OSGi API jar的一個老版本。它不包含FrameworkFactory接口(它早於它),它將與編譯路徑上的OSGi 4.3 API衝突。

相關問題