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

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

Hands-on with Mozilla’s Web-based “Firefox OS” for smartphones

Launching a new mobile OS is a difficult project since the market leaders, Android and iOS, have such  a big lead. Even Microsoft, with its near-infinite financial resources and vast ecosystem of complementary products, has struggled to gain traction. And new entrants face a chicken-and-egg problem: developers don't want to write apps for a platform without many users, while users don't want to buy a phone without many apps. Mozilla, the non-profit foundation behind Firefox, believes it can tackle this dilemma. In 2011, it announced a new project  called Boot2Gecko to build an operating system around its browser. Last year the project was  re-branded Firefox OS, and Mozilla began preparations for a major push into the mobile phone market. In February, Mozilla  unveiled an impressive initial list  of hardware and network partners. If all goes according to plan, Firefox OS phones will be available in a number of countries, mostly in the developing world, la...

Three reasons Microsoft wants to kill the Windows Desktop

Microsoft's Windows Blue update to Windows 8  makes it increasingly clear that Microsoft wants to kill the Desktop.  That may seem self-defeating, but there's method in Microsoft's madness. Here are three reasons I think it wants to eventually kill the Desktop. Help Windows Phone and Windows tablets gain market share Unify the operating system Lock enterprises into future versions of Windows Read More