• Consume OSGI Service in Liferay Portlet

    Posted on November 22, 2016 by Hamidul Islam in liferay 7.

    consume-osgi-service


    aimWe can easily Consume OSGI Service in Liferay Portlet. Before reading this article please read my previous article. This article is an extension of the previous article. In this article we will create a Sample Portlet and consume the OSGI Service which is explained in the previous article.


    Read my previous article

    Writing your first OSGI Service in Liferay 7


    For creating OSGI based portlet in liferay 7 follow the below link

    Liferay 7-OSGI Module – MVC Portlet by Maven


    For this article the Portlet is created by below command

    mvn archetype:generate
    -DarchetypeGroupId=com.liferay
    -DarchetypeArtifactId=com.liferay.project.templates.mvc.portlet
    -DgroupId=com.proliferay
    -DartifactId=com.proliferay.demo
    -DinteractiveMode=false
    -DclassName=ConsumeOsgiService
    

    POM:

    Please look into dependency section of the pom.xml. In the currebt POM, the previous artifact (i.e., the service creation) is added as dependency so that we can access SampleService.java in the current project.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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>com.proliferay</groupId>
    	<artifactId>com.proliferay.demo</artifactId>
    	<version>1.0-SNAPSHOT</version>
    	<packaging>jar</packaging>
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    	<dependencies>
    		<dependency>
    			<groupId>com.proliferay</groupId>
    			<artifactId>com.proliferay.sample.osgi.service</artifactId>
    			<version>1.0-SNAPSHOT</version>
    		</dependency>
    		<dependency>
    			<groupId>com.liferay.portal</groupId>
    			<artifactId>com.liferay.portal.kernel</artifactId>
    			<version>2.0.0</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>com.liferay.portal</groupId>
    			<artifactId>com.liferay.util.taglib</artifactId>
    			<version>2.0.0</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>javax.portlet</groupId>
    			<artifactId>portlet-api</artifactId>
    			<version>2.0</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.0.1</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>jstl</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.osgi</groupId>
    			<artifactId>org.osgi.compendium</artifactId>
    			<version>5.0.0</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.osgi</groupId>
    			<artifactId>org.osgi.core</artifactId>
    			<version>6.0.0</version>
    			<scope>provided</scope>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>3.1</version>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-jar-plugin</artifactId>
    				<version>2.6</version>
    				<configuration>
    					<archive>
    						<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
    					</archive>
    					<excludes>
    						<exclude>**/META-INF/resources/**/.sass-cache/</exclude>
    					</excludes>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>biz.aQute.bnd</groupId>
    				<artifactId>bnd-maven-plugin</artifactId>
    				<version>3.2.0</version>
    				<executions>
    					<execution>
    						<id>default-bnd-process</id>
    						<goals>
    							<goal>bnd-process</goal>
    						</goals>
    					</execution>
    				</executions>
    				<dependencies>
    					<dependency>
    						<groupId>biz.aQute.bnd</groupId>
    						<artifactId>biz.aQute.bndlib</artifactId>
    						<version>3.2.0</version>
    					</dependency>
    					<dependency>
    						<groupId>com.liferay</groupId>
    						<artifactId>com.liferay.ant.bnd</artifactId>
    						<version>2.0.28</version>
    					</dependency>
    				</dependencies>
    			</plugin>
    			<plugin>
    				<groupId>com.liferay</groupId>
    				<artifactId>com.liferay.css.builder</artifactId>
    				<version>1.0.20</version>
    				<executions>
    					<execution>
    						<id>default-build-css</id>
    						<phase>generate-sources</phase>
    						<goals>
    							<goal>build-css</goal>
    						</goals>
    					</execution>
    				</executions>
    				<configuration>
    					<portalCommonPath>/</portalCommonPath>
    					<docrootDirName>src/main/resources</docrootDirName>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    

    Portlet Class:

    package com.proliferay.portlet;
    
    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
    import com.liferay.portal.kernel.util.ParamUtil;
    import com.proliferay.service.SampleService;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.portlet.Portlet;
    import javax.portlet.PortletException;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import javax.portlet.ResourceRequest;
    import javax.portlet.ResourceResponse;
    
    import org.osgi.service.component.annotations.Component;
    import org.osgi.service.component.annotations.Reference;
    
    @Component(immediate = true, property = { "com.liferay.portlet.display-category=category.sample",
    		"com.liferay.portlet.instanceable=true", "javax.portlet.display-name=com.proliferay.demo 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 ConsumeOsgiServicePortlet extends MVCPortlet {
    
    	@Override
    	public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
    			throws IOException, PortletException {
    
    		super.doView(renderRequest, renderResponse);
    	}
    
    	@Override
    	public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
    			throws IOException, PortletException {
    
    		int number1 = ParamUtil.getInteger(resourceRequest, "firstNumber", 0);
    		int number2 = ParamUtil.getInteger(resourceRequest, "secondNumber", 0);
    		int result = 0;
    		if (_sampleService != null) {
    
    			result = _sampleService.add(number1, number2);
    			System.out.println("############Result###############" + result);
    		}
    
    		resourceResponse.setContentType("text/html");
    		PrintWriter writer = resourceResponse.getWriter();
    		writer.print(result);
    		writer.flush();
    		writer.close();
    		super.serveResource(resourceRequest, resourceResponse);
    	}
    
    	@Reference
    	public void set_sampleService(SampleService _sampleService) {
    		this._sampleService = _sampleService;
    	}
    
    	private SampleService _sampleService;
    }
    

    Note 1: The Service is injected in the portlet class using @Reference annotation. The setter method is mandatory.
    Note 2: At any point of time a service can be removed. So before using it check the null reference
    Note 3: There is another way to discover OSGI Service using BundleContext

    BundleContext bundleContext = FrameworkUtil.getBundle(ConsumeOsgiServicePortlet.class).getBundleContext();
    		
    ServiceReference ref = bundleContext.getServiceReference(SampleService.class.getName());
    		
    SampleService sampleService = (SampleService)bundleContext.getService(ref);
    		
    int result = sampleService.add(100, 200);
    

    For the JSP download the source code and look the view.jsp.
    Sample Output:

    output

    Note:

    This portlet is dependent another service. So without deploying the service the portlet will not work as expected. 

    Download Source Code:

    com-proliferay-demo

    Post Tagged with , ,

3 Responses so far.

  1. Ricky says:

    hi i was trying your sample within Liferay IDE. Somehow Maven build can't find the osgi service dependency?

    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] /C:/Development/Projects/liferay-blade/liferay7/modules/com.proliferay.demo/src/main/java/com/proliferay/portlet/ConsumeOsgiServicePortlet.java:[5,30] package com.proliferay.service does not exist
    [ERROR] /C:/Development/Projects/liferay-blade/liferay7/modules/com.proliferay.demo/src/main/java/com/proliferay/portlet/ConsumeOsgiServicePortlet.java:[56,39] cannot find symbol
    symbol: class SampleService
    location: class com.proliferay.portlet.ConsumeOsgiServicePortlet
    [ERROR] /C:/Development/Projects/liferay-blade/liferay7/modules/com.proliferay.demo/src/main/java/com/proliferay/portlet/ConsumeOsgiServicePortlet.java:[60,17] cannot find symbol
    symbol: class SampleService
    location: class com.proliferay.portlet.ConsumeOsgiServicePortlet
    [INFO] 3 errors
    [INFO] -------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.174 s
    [INFO] Finished at: 2016-12-07T13:09:39-07:00
    [INFO] Final Memory: 16M/219M
    [INFO] ------------------------------------------------------------------------

  2. Hamidul Islam says:

    Hi Ricky,
    This example requires SampleService which is explained in http://proliferay.com/writing-your-first-osgi-service-in-liferay-7/

    Follow this link first and then try. Remember you must install artifact in local repository. Then build the current example(i.e., consume service)

    Regards
    Author
    http://www.proliferay.com

  3. Thirumal Reddy says:

    Hi Hamidul Islam,
    How can we access service-builder module services in
    legacy(6.2) plugin portlets?

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close