简体   繁体   中英

Get osgi service reference from pure osgi bundle in Liferay portlet

I have successfully register the org.amdatu.mongo service in apche felix like shown below, 在此处输入图片说明

Bundle is shown below which export the service which is in active/running state.

在此处输入图片说明

Now I want to use this service in my portlet and I have return below code,

 package com.example.portlet;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import java.io.IOException;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.amdatu.mongo.MongoDBService;
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.osgi.service.component.annotations.Component;
@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=loveworld Portlet",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.resource-bundle=content.Language",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class LoveworldmvcportletPortlet extends MVCPortlet {
     @ServiceDependency
     private volatile MongoDBService m_mongoDbService;



    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
            throws IOException, PortletException {
        System.out.println( m_mongoDbService);//**getting NULL**
        super.doView(renderRequest, renderResponse);
    }
}

I have tried with annotation @ServiceDependency no luck getting null.My mongoDb server is running fine!!

I have Menifest.mf file as below,

Manifest-Version: 1.0

Bnd-LastModified: 1469380000381

Bundle-ManifestVersion: 2

Bundle-Name: loveworld

Bundle-SymbolicName: loveworld

Bundle-Version: 1.0.0

Created-By: 1.8.0_51 (Oracle Corporation)

Import-Package: com.liferay.portal.kernel.portlet.bridges.mvc;version=

 "[1.0,2)",javax.portlet;version="[2.0,3)",javax.servlet,javax.servlet

 .http,org.amdatu.mongo;version="1.0.0"

Javac-Debug: on

Javac-Deprecation: off

Javac-Encoding: UTF-8

Private-Package: com.example.portlet,content

Provide-Capability: osgi.service;objectClass:List<String>="javax.portl

 et.Portlet",liferay.resource.bundle;bundle.symbolic.name=loveworld;re

 source.bundle.base.name="content.Language"

Require-Capability: osgi.extender;filter:="(&(osgi.extender=jsp.taglib

 )(uri=http://java.sun.com/portlet_2_0))",osgi.extender;filter:="(&(os

 gi.extender=jsp.taglib)(uri=http://liferay.com/tld/aui))",osgi.extend

 er;filter:="(&(osgi.extender=jsp.taglib)(uri=http://liferay.com/tld/p

 ortlet))",osgi.extender;filter:="(&(osgi.extender=jsp.taglib)(uri=htt

 p://liferay.com/tld/theme))",osgi.extender;filter:="(&(osgi.extender=

 jsp.taglib)(uri=http://liferay.com/tld/ui))",osgi.ee;filter:="(&(osgi

 .ee=JavaSE)(version=1.8))"

Service-Component: OSGI-INF/com.example.portlet.LoveworldmvcportletPor

 tlet.xml

Tool: Bnd-3.2.0.201605172007

bnd.bnd file is as below,

Bundle-SymbolicName: loveworld
Bundle-Version: 1.0.0
Import-Package: com.liferay.portal.kernel.portlet.bridges.mvc;version="[1.0,2)",javax.portlet;version="[2.0,3)",javax.servlet,javax.servlet.http,org.amdatu.mongo;version="1.0.0"

Portlet which consume the service is as shown below,

在此处输入图片说明

You are using the following two annotations:

import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.osgi.service.component.annotations.Component;

One of them is the standard OSGi annotation for Declarative Services components, the other one from the Felix Dependency Manager project. Be sure that you use annotations only from org.osgi.service.component.annotations package.

Also, you should add a bind method to your component for the service reference.

First problem: the service provided by your bundle is of type ManagedServiceFactory . You are trying to inject a service of type MongoDBService . This obviously doesn't match.

Second problem: you should not be trying to directly consume ManagedServiceFactory yourself... it is part of the Config Admin specification, and should only be consumed by Config Admin itself.

Third problem: as Balazs points out, you are using annotations from a mixture of different frameworks: Declarative Services (DS) and Dependency Manager (DM). These cannot be mixed within a single component... pick one!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM