0

I have successfully register the org.amdatu.mongo service in apche felix like shown below, enter image description here

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

enter image description here

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,

enter image description here

Abhishek Suthar
  • 674
  • 1
  • 8
  • 27

2 Answers2

1

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.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
0

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!

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • I'll change the annonation to @reference???But here it shows that we can use directly take a look.http://www.amdatu.org/components/mongodb.html – Abhishek Suthar Jul 25 '16 at 03:04
  • @AbhishekSuthar In the example the Component annotation comes from org.apache.felix.dm.annotation.api package. The first issue was that you mixed the two technologies (Declarative Services and Dependency Manager). – Balazs Zsoldos Jul 25 '16 at 17:15
  • Yes you should use @Reference but even if you do this you still need to address the other two problems. – Neil Bartlett Jul 25 '16 at 18:24
  • Ok but how to tackle second problem...how to get service from config admin? – Abhishek Suthar Jul 26 '16 at 03:36
  • You don't get services from config admin. You get the service of the type you're actually looking for, which is probably `MongoDBService`. Is there such a service? – Neil Bartlett Jul 26 '16 at 07:09
  • I think so because on this link they have provided the code how to use that service http://www.amdatu.org/components/mongodb.html and they are using like the way I have written. – Abhishek Suthar Jul 26 '16 at 18:01
  • There is no need to guess about this. Can you see a `MongoDBService` service listed in the console, or can't you? – Neil Bartlett Jul 26 '16 at 18:45