Skip to main content

Writing your first OSGI Service in Liferay 7

  • Create a folder : D:\Workspace\Liferay7
    Open your command prompt as Administrator

    C:\windows\system32>cd D:\Workspace\Liferay7

    C:\windows\system32>d:

    D:\Workspace\Liferay7>
  • Execute the below command
    D:\Workspace\Liferay7>mvn archetype:generate -DarchetypeGroupId=com.liferay -DarchetypeArtifactId=com.liferay.project.templates.service -DgroupId=com.mindtree -DartifactId=com.mindtree.sample.service -DserviceClass=SampleService -DclassName=SampleServiceImpl -DinteractiveMode=false -Dauthor=AtifAliSiddiqui 

    Note:
    - Before executing the above command, make sure you have installed Maven on your machine.

    Output ::
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> maven-archetype-plugin:3.0.0:generate (default-cli) > generate-sources @ standalone-pom >>>
    [INFO]
    [INFO] <<< maven-archetype-plugin:3.0.0:generate (default-cli) < generate-sources @ standalone-pom <<<
    [INFO]
    [INFO] --- maven-archetype-plugin:3.0.0:generate (default-cli) @ standalone-pom ---
    [INFO] Generating project in Batch mode
    [INFO] Archetype [com.liferay:com.liferay.project.templates.service:1.0.2] found in catalog remote
    Downloading: https://repo.maven.apache.org/maven2/com/liferay/com.liferay.project.templates.service/1.0.2/com.liferay.project.templates.service-1.0.2.pom
    Downloaded: https://repo.maven.apache.org/maven2/com/liferay/com.liferay.project.templates.service/1.0.2/com.liferay.project.templates.service-1.0.2.pom (2 KB at 1.6 KB/sec)
    Downloading: https://repo.maven.apache.org/maven2/com/liferay/com.liferay.project.templates.service/1.0.2/com.liferay.project.templates.service-1.0.2.jar
    Downloaded: https://repo.maven.apache.org/maven2/com/liferay/com.liferay.project.templates.service/1.0.2/com.liferay.project.templates.service-1.0.2.jar (5 KB at 5.1 KB/sec)
    [INFO] ----------------------------------------------------------------------------
    [INFO] Using following parameters for creating project from Archetype: com.liferay.project.templates.service:1.0.2
    [INFO] ----------------------------------------------------------------------------
    [INFO] Parameter: groupId, Value: com.mindtree
    [INFO] Parameter: artifactId, Value: com.mindtree.sample.service
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: package, Value: com.mindtree
    [INFO] Parameter: packageInPathFormat, Value: com/mindtree
    [INFO] Parameter: version, Value: 1.0-SNAPSHOT
    [INFO] Parameter: package, Value: com.mindtree
    [INFO] Parameter: groupId, Value: com.mindtree
    [INFO] Parameter: projectType, Value: standalone
    [INFO] Parameter: className, Value: SampleServiceImpl
    [INFO] Parameter: author, Value: AtifAliSiddiqui
    [INFO] Parameter: serviceClass, Value: SampleService
    [INFO] Parameter: artifactId, Value: com.mindtree.sample.service
    [INFO] Project created from Archetype in dir: D:\Workspace\Liferay7\com.mindtree.sample.service
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 23.892 s
    [INFO] Finished at: 2017-03-15T17:05:24+05:30
    [INFO] Final Memory: 17M/209M
    [INFO] ------------------------------------------------------------------------
  • Please verify the generated directory structure looks as classified below

    - com.mindtree.sample.service
    ----- src
    --------- main
    ------------- java
    ----------------- com
    --------------------- mindtree
    ------------------------- SampleServiceImpl.java
    ------------- resources
    ----------------- .gitkeep
    ----- .gitignore
    ----- bnd.bnd
    ----- build.gradle
    ----- pom.xml
  • Import this maven project into eclipse.

    - Open up eclipse.
    - Mention the Workspace as "D:\Workspace\Liferay7".
    - On the Left hand side, Package Explorer. Right click and select Import.
    - Expand "Maven", choose "Existing Maven Projects".
    - Root Directory - "D:\Workspace\Liferay7".
    - The maven project we just created should appear and be selected under projects. Click Finish.
  • ​Do the following

    - Create following folder structure "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree\service\impl"

    - Create "SampleService.java" class under "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree\service", add the following code in the class file:

    package com.mindtree.service;
    public interface SampleService {
    public int add(int number1, int number2);
    }

    - Update the "SampleServiceImpl.java" class under "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree", add the following code in the class file:

    package com.mindtree.service.impl;
    import org.osgi.service.component.annotations.Component;
    import com.mindtree.service.SampleService;
    /**
    * @author AtifAliSiddiqui
    */
    @Component(
    immediate = true,
    property = {
    // TODO enter required service properties
    },
    service = SampleService.class
    )
    public class SampleServiceImpl implements SampleService {
    @Override
    public int add(int number1, int number2) {
    // TODO Auto-generated method stub
    return 0;
    }
    // TODO enter required service methods
    }

    - Move the "SampleServiceImpl.java" class under "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree\service\impl"
  • Bundle Activator: 
    We need the OSGI framework to understand the service we created, for this we need to register the service in the framework.
    We can use bundle activator to register our service.

    - Create following folder structure "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree\activator"
    - Create "ServiceActivator.java" class under "D:\Workspace\Liferay7\com.mindtree.sample.service\src\main\java\com\mindtree\activator", add the following code in the class file:

    package com.mindtree.activator;
    import com.mindtree.service.SampleService;
    import com.mindtree.service.impl.SampleServiceImpl;
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceRegistration;
    public class ServiceActivator implements BundleActivator {
    private ServiceRegistration registration;
    @Override
    public void start(BundleContext context) throws Exception {
    registration = context.registerService(SampleService.class.getName(), new SampleServiceImpl(), null);
    System.out.println("###########Service Registered Successfully##############");
    }
    @Override
    public void stop(BundleContext context) throws Exception {
    registration.unregister();
    System.out.println("###########Service Unregistered##############");

    }
    }

    - Update the "bnd.bnd" under "D:\Workspace\Liferay7\com.mindtree.sample.service\", modify the content:

    Bundle-Name: com.mindtree.sample.service
    Bundle-SymbolicName: com.mindtree
    Bundle-Version: 1.0-SNAPSHOT
    Bundle-Activator: com.mindtree.activator.ServiceActivator
    Export-Package: com.mindtree.service
  • Lets build our Project / Portlet

    - In Eclipse select "Run" -> "Run Configurations"
    - Right click "Maven Build" -> New
    - Name :: Build Service
    - Base directory :: ${workspace_loc:/com.mindtree.sample.service}
    - Goal :: clean package
    - Click Apply
    - Click Run
    - Check in Console if the Build is Successful
  • ​Lets Deploy our Portlet to Liferay

    - Start up your Liferay7 instance, if not already running.
    - Copy the "com.mindtree.sample.portlet-1.0-SNAPSHOT.jar" from "D:\Workspace\Liferay7\com.mindtree.sample.portlet\target".
    - Paste "com.mindtree.sample.portlet-1.0-SNAPSHOT.jar" to "D:\Liferay\Community Edition 7.0\liferay-ce-portal-7.0-ga3\deploy".
    - See Liferay console / logs on the deployment of your portlet.

    to browse Liferay’s default SOAP web services:

    http://localhost:8080/api/axis

    To browse Liferay’s default JSON web services, visit this URL:

    http://localhost:8080/api/jsonws/

    Important:
    Check the namespace given in service.xml. In this case its "Sample".
    Search for contextName = sample.

    http://localhost:8080/api/jsonws?contextName=sample

Comments

Popular posts from this blog

Apple's iPhone 4.0 software to deliver multitasking support

Apple this summer will go a long way towards silencing critics and catering to one of the most prevalent demands of its iPhone user base, when it introduces a multitasking solution through the handset's 4.0 software update that will finally allow several third party apps to run concurrently and in the background. For more Info.

Everything about Java 8

The following post is a comprehensive summary of the developer-facing changes coming in Java 8. This next iteration of the JDK is currently scheduled for general availability in  September 2013 . Read More

Keyboard First Impressions: Kinesis Advantage Mechanical Ergonomic Keyboard

An ergonomic keyboard with mechanical switches that’s looking to attract users interesting in a high quality, highly ergonomic offering and don’t mind the rather steep learning curve or the price. The  TECK  isn’t the only such keyboard, of course, and I decided to see what other mechanical switch ergonomic keyboards I could get for comparison. Next up on the list is the granddaddy of high-end ergonomic keyboards, the  Kinesis Contour Advantage . I wanted to provide my first impressions of the Kinesis, along with some thoughts on the initial switch and the learning curve. This time, I also made the effort to put together a video of my first few minutes of typing. It actually wasn’t as bad as with the TECK, but that’s likely due to the fact that I already lost many of my typing conventions when I made that switch earlier this year. I’ll start with the video, where I take a typing test on four different keyboards and provide some thoughts on the experience, and then ...