2015-02-12 58 views
0

我試圖創建AEM一個非常基本的吊帶服務:AEM:OSGI吊帶服務激活方法沒有被執行

package com.mypackage; 

/** 
* A simple service interface 
*/ 
public interface TestService { 

    /** 
    * @return the name of the underlying JCR repository implementation 
    */ 
    public String getPropertyName(); 

} 

實現類:

package com.mymypackage.impl; 

import javax.jcr.Repository; 

import org.apache.felix.scr.annotations.Component; 
import org.apache.felix.scr.annotations.Reference; 
import org.apache.felix.scr.annotations.Service; 
import org.apache.sling.jcr.api.SlingRepository; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.mypackage.TestService; 


@Component(label = "Title", description = "Description.", immediate = true, metatype = true, policy = ConfigurationPolicy.REQUIRE) 
@Service(value = {TestService.class}) 
@Properties({ 
    @Property(name = "propertyPath", label = "Property Label", description = "Property Desc.") 
}) 
public class TestServiceImpl implements TestService { 

    private static final Logger log = LoggerFactory.getLogger(TestServiceImpl.class); 

    String propertyPath = null; 

    @Activate 
    public void activate(ComponentContext ctx) { 
     Dictionary properties = ctx.getProperties(); 
     String propertyPath =(String)properties.get("propertyPath"); 
     log.info("====================getPropertyName activate========================"+propertyPath); 
     this.propertyPath = propertyPath; 
    } 


    @Override 
    public String getPropertyName() { 
     // TODO Auto-generated method stub 
     log.info("====================getPropertyName========================"+propertyPath); 
     return propertyPath; 
    } 
} 

,我創建了一個節點類型sling:配置文件夾內的OsgiConfig。此節點的名稱是com.mypackage.impl.TestServiceImpl.xml併成爲它的內容:

<?xml version="1.0" encoding="UTF-8"?> 
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" 
    jcr:primaryType="sling:OsgiConfig" 
    propertyPath="http://www.myuhc.com"/> 

,這是我怎麼想使用它的Java類中:

public static String getTestService() { 
     TestService testService = new TestServiceImpl(); 
     String prop = testService.getPropertyName(); 
     return prop; 
} 

使用customtaglib從JSP中調用此方法(方法通過.ldld文件映射)

當我使用此方法時,不會調用HelloServiceImpl類中的activate方法,因此未設置該屬性。但是,當我使用這樣的組件JSP內的服務:

<%@page import="org.osgi.service.cm.ConfigurationAdmin"%> 
<%@page import="org.osgi.service.cm.Configuration"%> 

<% 
Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("Name of the config"); 
String myProp = (String) conf.getProperties().get("property key"); 
%> 

一切工作正常。在嘗試從Java類調用服務時,我必須要做的事情是非常錯誤的。我怎樣才能使用這種方法。我不想在JSP內部使用scriptlet。此外,任何最佳做法將不勝感激。

在此先感謝

+0

我不發現運行代碼的任何問題。但是請確保相應的軟件包在您的bnd文件中導出/導入,並檢查該服務在Felix控制檯中是否可用。 – rakhi4110 2015-02-12 05:18:30

+0

嘿感謝您的回覆。我剛剛編輯了我的問題。你可以看看那個。再次感謝。 – user972418 2015-02-12 16:31:49

+0

在Java中使用它時是否收到任何錯誤消息?確保你正在導出你的包(Export-Package清單標題),以便其他包可以看到它們(按照rakhi4110的建議)。 – Woodifer 2015-02-12 23:46:48

回答

0

的問題是,有時,當您嘗試重新安裝包,一些老編譯類新不會被刪除,並用新的替換。嘗試刪除/var/classes和/或/var/clientlibs節點並重新安裝您的項目。

+0

嘿感謝您的回覆。我剛剛編輯了我的問題。你可以看看那個。再次感謝。 – user972418 2015-02-12 16:32:20

+1

如果您想在Java類中使用服務,您需要獲取對它的引用。它可以使用@Reference註釋完成。它應該是你的Java類中的一個字段:@Reference(policy = ReferencePolicy.DYNAMIC,cardinality = ReferenceCardinality.MANDATORY_UNARY)private TestService testService;看看:http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html#reference – kmb 2015-02-13 10:27:53

1

OSGi Services在管理服務實例的創建和刪除的生命週期內工作。

作爲這些實例的容器管理的一部分,當容器創建實例時,它會使用適當的值調用激活方法。以便該屬性可以分配。

你在做你的第一個代碼段是什麼:

TestService testService = new TestServiceImpl(); 
String prop = testService.getPropertyName(); 

沒有使用您的組件的容器版本。您使用直接實施並繞過容器管理。

使用容器管理的實例。你需要從容器中請求它。

這恰恰是你的第二個片斷顯示

sling.getService(org.osgi.service.cm.ConfigurationAdmin.class) 

請求從容器中的最佳匹配的服務。

從容器中訪問服務。你或者需要自己成爲一個服務。你可以通過@Reference註解來完成。

你提到的是在taglib中,這使得事情變得更復雜一些。你需要獲得一個SlingScriptHelper的引用,它可以像這樣從pageContext獲得;

ServletRequest request = pageContext.getRequest(); 
    final SlingBindings bindings = (SlingBindings) request 
      .getAttribute(SlingBindings.class.getName()); 
    final SlingScriptHelper scriptHelper = bindings.getSling(); 
    TestService service= scriptHelper 
      .getService(com.mymypackage.impl.TestService.class); 
0

這也可能是有用的,如果你無法使用@Reference註解注入服務

// get the TestServiceImpl.java service component 
TestServiceImpl testService = getSlingScriptHelper().getService(TestServiceImpl.class); 

//com.adobe.cq.sightly.WCMUsePojo.getSlingScriptHelper()