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

Google leaps language barrier with translator phone

GOOGLE is developing software for the first phone capable of translating foreign languages almost instantly — like the Babel Fish in The Hitchhiker’s Guide to the Galaxy. By building on existing technologies in voice recognition and automatic translation, Google hopes to have a basic system ready within a couple of years. If it works, it could eventually transform communication among speakers of the world’s 6,000-plus languages. The company has already created an automatic system for translating text on computers, which is being honed by scanning millions of multi-lingual websites and documents. So far it covers 52 languages, adding Haitian Creole last week. Google also has a voice recognition system that enables phone users to conduct web searches by speaking commands into their phones rather than typing them in. Now it is working on combining the two technologies to produce software capable of understanding a caller’s voice and translating it into a synthetic equivalent in a foreign

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

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