WSO2 Carbon Server Component - Hello World!

Carbon is the core framework of all the WSO2 products. This tutorial helps you to develop a simple web service which can be deployed in WSO2 Carbon.
Prerequisite:

Step 1:
Go the File → New → Project…, select Maven Project in the dialog and click Next.


Step 2:
Check 'Create a simple project (skip archetype selection)' and click Next.


Step 3:
Provide the Group Id, Artifact Id and Version, and click Finish.
Group Id: com.javahelps
Artifact Id: com.javahelps.samplecarbon.service
Version: 1.0.0


Step 4:
Edit the pom.xml as shown below and update the project (Right click on  the project → Maven → Update Project... or Alt + F5).
<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.javahelps</groupId>
    <artifactId>com.javahelps.samplecarbon.service</artifactId>
    <version>1.0.0</version>
    <packaging>bundle</packaging>

    <repositories>
        <!-- Below configuration is used to download the relevant jars and
            plugins from the remote maven repositories -->
        <repository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </repository>

        <repository>
            <id>wso2.snapshots</id>
            <name>WSO2 Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>wso2.releases</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>

        <pluginRepository>
            <id>wso2.snapshots</id>
            <name>WSO2 Snapshot Repository</name>
            <url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
        <pluginRepository>
            <id>wso2-nexus</id>
            <name>WSO2 internal Repository</name>
            <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
            </releases>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${artifactId}</Bundle-SymbolicName>
                        <Bundle-Name>${artifactId}</Bundle-Name>
                        <Export-Package>com.javahelps.samplecarbon.service</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Here we have defined the necessary repositories and Apache Felix plugin to build the project as an OSGI bundle. Do not miss the <packaging>bundle</packaging> property.

Step 5:
Create a new package com.javahelps.samplecarbon.service in src/main/java.

Step 6:
Create a new class SampleService.java with the following content in the package created in Step 5.
package com.javahelps.samplecarbon.service;

public class SampleService {
    private static final String[] LANGUAGES = { "Java", "C++", "Python", "Go" };

    public String[] getLanguages() {
        return LANGUAGES;
    }
}

Step 7:
Create META-INF folder in src/main/resources folder.

Step 8:
Create a new file 'services.xml' with the given content inside the META-INF folder.
<serviceGroup>
    <!-- Name of the service with the Axis2 session -->
    <service name="SampleService" scope="transportsession">
        <transports>
            <transport>https</transport>
        </transports>
        <!-- Class which provides the service -->
        <parameter name="ServiceClass">com.javahelps.samplecarbon.service.SampleService
        </parameter>
    </service>

    <parameter name="adminService" locked="true">true</parameter>
    <parameter name="hiddenService" locked="true">true</parameter>
    <parameter name="AuthorizationAction" locked="true">/permission/protected/manage</parameter>
</serviceGroup>
Here:
<transport>https</transport> - makes the service secure.
<parameter name="adminService"> - makes the service an admin service.
<parameter name="hiddenService"> - makes the service hidden to public.
<parameter name="AuthorizationAction"> - makes the service authorized.

Project Structure after all the changes must look like this:

Step 9:
Now it is the time to build the JAR file of this project. You can build the JAR file using Terminal (which is my preference) or Eclipse.

Using Terminal:
Open the Terminal in the project folder where you have 'pom.xml' and execute the following command.
mvn package

Using Eclipse:
Right-click on the project and select Run as → Maven build.

Set the Name and Goals in the appeared dialog as given below, click Apply and Run.
Name: SampleService
Goals: package


Step 11:
Copy the com.javahelps.samplecarbon.service-1.0.0.jar from target directory and paste it in  <CARBON_HOME>/repository/components/dropins folder.

Step 12:
In Step 14, we will get access to the WSDL of our web service, which can be used to validate the successful service deployment and later used to create the service client. By default, WSDL of secured service is not accessible from the browser. To make it accessible, open the <CARBON_HOME>/repository/conf/carbon.xml in your favorite text editor and change the <HideAdminServiceWSDLs> parameter from true to false.

Step 13:
Run the WSO2 Carbon using the following command from <CARBON_HOME>/bin folder.
Unix users:
./wso2server.sh
Windows usres:
./wso2server.bat

Step 14:
Visit to https://localhost:9443/services/SampleService?wsdl and save the WSDL as SampleService.wsdl file. This will be used to create a stub for this service in the next article. Now you have successfully deployed your first web service in WSO2 Carbon. Now you can revert the changes done in Step 12.

In the next article, I will show you how to create a stub for our SampleService using Apache Maven.

Find the project @ Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *