2015-11-06 60 views
1

我試過了這個組件工廠的示例link。 接口:karaf在組件工廠示例中不顯示任何錯誤

package com.java.examplefactoryservice; 

public interface ExampleFactoryService { 
    public void start(); 

    public void stop(); 
} 

工廠提供:

package com.java.examplecomponentfactoryserviceprovider; 

import java.util.Map; 

import org.apache.felix.scr.annotations.Activate; 
import org.apache.felix.scr.annotations.Component; 

import com.java.examplefactoryservice.ExampleFactoryService; 

@Component(name = "ExampleComponentFactoryServiceProvider", factory = "example.factory.provider") 
public class ExampleComponentFactoryServiceProvider implements ExampleFactoryService { 

    @Activate 
    public void activate(Map<String, Object> properties) { 
     System.out.println("Actiavted!!!"); 
    } 

    @Override 
    public void start() { 
     System.out.println("Started !!!!"); 
    } 

    @Override 
    public void stop() { 
     System.out.println("Stopped!!!"); 
    } 

} 

廠長:

package com.java.examplecomponentfatorymanager; 

import java.util.Map; 

import org.apache.felix.scr.annotations.Activate; 
import org.apache.felix.scr.annotations.Component; 
import org.apache.felix.scr.annotations.Reference; 
import org.osgi.service.component.ComponentFactory; 
import org.osgi.service.component.ComponentInstance; 

import com.java.examplefactoryservice.ExampleFactoryService; 

@Component(name = "ExampleComponentFactoryManager", enabled = true, immediate = true) 
public class ExampleComponentFactoryManager { 

    @Reference(target = "(component.factory=example.factory.provider)", bind = "setFactory", unbind = "unsetFactory") 
    private ComponentFactory factory; 

    private ComponentInstance instance; 

    private ExampleFactoryService service; 

    @Activate 
    public void activate(Map<String, Object> properties) { 
     System.out.println("activate in manager !!!!"); 
     instance = factory.newInstance(null); 
     service = (ExampleFactoryService) instance.getInstance(); 
     System.out.println("service = " + service.toString()); 
    } 

    public void setFactory(final ComponentFactory factory) { 
     this.factory = factory; 
     System.out.println("setfactory called"); 
    } 

    public void unsetFactory(ComponentFactory factory) { 
     this.factory = null; 
     System.out.println("Unset factory called"); 
    } 

} 

一切順利的編譯和安裝在karaf 2.3.10。工廠已成功註冊。但沒有任何顯示。

karaf輸出:

[ 71] [Active  ] [   ] [ 80] osgi.cmpn (4.3.1.201210102024) 
[ 79] [Active  ] [   ] [ 80] testI (0.0.1.SNAPSHOT) 
[ 80] [Active  ] [   ] [ 80] testImpl (0.0.3.SNAPSHOT) 
[ 110] [Active  ] [   ] [ 80] Apache Felix Declarative Services (1.6.0) 
[ 140] [Active  ] [   ] [ 80] ExampleFactoryService (0.0.1.SNAPSHOT) 
[ 152] [Active  ] [   ] [ 80] ExampleComponentFactoryServiceProvider (0.0.1.SNAPSHOT) 
[ 158] [Active  ] [   ] [ 80] ExampleComponentFatoryManager (0.0.1.SNAPSHOT) 

SCR:列表輸出

[9 ] [FACTORY   ] ExampleComponentFactoryServiceProvider 

SCR:細節

scr:deactivate scr:details 
[email protected]> scr:details ExampleComponentFactoryServiceProvider 
Component Details 
    Name    : ExampleComponentFactoryServiceProvider 
    State    : FACTORY 
    Properties   : 
    service.vendor=The Apache Software Foundation 
    component.factory=example.factory.provider 
    component.name=ExampleComponentFactoryServiceProvider 
References 

的pom.xml

<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>ExampleComponentFatoryManager</groupId> 
    <artifactId>ExampleComponentFatoryManager</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
       <extensions>true</extensions> 
       <version>2.3.5</version> 
       <configuration> 
        <instructions> 
         <Import-Package> 
          *, 
          javax.servlet*;version="[2.5,4)" 
         </Import-Package> 
        </instructions> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-scr-plugin</artifactId> 
       <version>1.14.0</version> 
       <executions> 
        <execution> 
         <id>generate-scr-scrdescriptor</id> 
         <goals> 
          <goal>scr</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.apache.felix.scr.annotations</artifactId> 
      <version>1.9.6</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.osgi.compendium</artifactId> 
      <version>1.4.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.osgi</groupId> 
      <artifactId>org.osgi.core</artifactId> 
      <version>4.3.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>ExampleFactoryService</groupId> 
      <artifactId>ExampleFactoryService</artifactId> 
      <version>0.0.1-SNAPSHOT</version> 
     </dependency> 
    </dependencies> 
</project> 

最初我嘗試在karaf命令行中安裝捆綁包而不是名稱,它顯示捆綁包的絕對路徑。經過一段時間,我試圖通過放置捆綁包來展示捆綁包的確切名稱。 我不明白karaf爲什麼在執行適當的命令行安裝時未顯示軟件包名稱的行爲。它完全不顯示任何錯誤[dependency/compilation/wiredexecption]。任何人都可以告訴我包中有什麼問題嗎?

+0

是從'SCR全輸出:list'? 'ExampleComponentFactoryManager'缺少 –

+0

是的。我只安裝了一個軟件包。 ExampleComponentFactoryManager不顯示在scr:list中。 – Shriram

+0

@Neil,至少我期待SOP將被打印。但沒有打印。另外,如果我通過cmd行安裝它,捆綁包名稱不會顯示,而是顯示絕對路徑。我不明白卡拉夫的行爲。這是卡拉夫的性能問題還是我做錯了什麼? – Shriram

回答

0

你可以去導入aQute.bnd.annotation而不是 爲karaf導入org.apache.felix.scr.annotations。

參考下面的代碼

@Component(name="ExampleComponentFactoryManager",immediate=true,enabled=true) 
public class ExampleComponentFactoryManager 
{ 

    private static final Logger LOG = LoggerFactory.getLogger(ExampleComponentFactoryManager.class); 

    private ComponentFactory factory; 

    // @Reference(target = "(component.factory=example.factory.provider)",unbind = "unsetFactory") 
    //private ComponentFactory factory; 

    private ComponentInstance instance; 

    private ExampleFactoryService service; 

    @Activate 
    public void activate(Map<String, Object> properties) { 
     LOG.info("activation factorymanager"); 
     instance=factory.newInstance(null); 
     service =(ExampleFactoryService)instance.getInstance(); 
     LOG.info("service instance from factory "+service.toString()); 

    } 
    @Reference(target = "(component.factory=example.factory.provider)",unbind = "unsetFactory") 
    public void setFactory(final ComponentFactory factory) { 
     LOG.info("set Factory"); 
     this.factory = factory; 
     System.out.println("setfactory called"); 
    } 

    public void unsetFactory(ComponentFactory factory) { 
     this.factory = null; 
     System.out.println("Unset factory called"); 
    } 
}