Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Before you begin, be sure to setup your project as explained in the previous topic. If you have done that, you can now proceed to the second step in developing the Student Management feature, which is to develop the required components:

...

  • Update the org.wso2.carbon.student.mgt → pom.xml file:

    Expand
    titleClick here to see the updated pom.xml
    Code Block
    <?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">
        <parent>
            <artifactId>student-manager-components</artifactId>
            <groupId>org.wso2.carbon</groupId>
            <version>4.2.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
     
        <artifactId>org.wso2.carbon.student.mgt</artifactId>
     
        <packaging>bundle</packaging>
        <name>WSO2 Carbon - Student Manager Server Component</name>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
                            <Bundle-Name>${pom.artifactId}</Bundle-Name>
                            <Export-Package>org.wso2.carbon.student.mgt.*</Export-Package>
                        </instructions>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    Note

    When you export packages for the OSGI bundle, remember that if you are using any API classes or any other dependencies in a separate package, you have to export them as well. In this example, we will have our data classes in the org.wso2.carbon.student.mgt.data package. Exporting the parent mgt package will export everything that is required..

  • Update the student-manager-components → pom.xml file:

    Expand
    titleClick here to see the updated pom.xml
    Code Block
    <?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">
        <parent>
            <artifactId>student-manager</artifactId>
            <groupId>org.wso2.carbon</groupId>
            <version>4.2.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
     
        <artifactId>student-manager-components</artifactId>
     
        <packaging>pom</packaging>
        <name>WSO2 Carbon - Student Manager Components</name>
     
        <modules>
            <module>org.wso2.carbon.student.mgt</module>
            <!-- There are 2 more projects to be added here later in this tutorial stub, ui -->
        </modules>
    </project>

Adding Java classes to the server component

Now, create two java classes named Student (in src/main/java/org/wso2/carbon/student/mgt/data/) and StudentManager (in src/main/java/org/wso2/carbon/student/mgt/) in the org.wso2.carbon.student.mgt project. 

The Student class will simply hold a few student details (ID, first name, last name) with getters and setters as follows:

Expand
titleClick here to see the sample Student class
Code Block
package org.wso2.carbon.student.mgt.data;
 
public class Student {
    private int ID;
    private String firstName;
    private String lastName;
 
    public Student(int ID, String firstName, String lastName) {
        this.ID = ID;
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    public int getID() {
        return ID;
    }
    public void setID(int ID) {
        this.ID = ID;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

The StudentManager class will create an array of students in the constructor and have a public method to return the array as follows:

Expand
titleClick here to see a sample StudentManager class
Code Block
package org.wso2.carbon.student.mgt;
 
import org.wso2.carbon.student.mgt.data.Student;
 
public class StudentManager {
    private Student[] students;
 
    public StudentManager() {
        students = new Student[2];
        students[0] = new Student(1234,"Amal", "Gunatilake");
        students[1] = new Student(4321, "John", "Carter");
    }
 
    public Student[] getStudents() {
        return students;
    }
}

Creating the services.xml file

Finally, the services.xml file should be configured as shown below to expose the StudentManager service. We have exposed StudentManager as our ServiceClass

Expand
titleClick here to see the sample services.xml file
Code Block
<serviceGroup>
    <service name="StudentManager" scope="transportsession">
        <transports>
            <transport>https</transport>
        </transports>
        <parameter name="ServiceClass">org.wso2.carbon.student.mgt.StudentManager</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>

...

Info

We can manually install those 3 OSGI bundles (JAR files) by adding them to the <PRODUCT_HOME>/repository/components/dropins folder. Then, start the Carbon server. The Students link will now appears on the left side navigation panel. The sample code used in this tutorial can be found at http://svn.wso2.org/repos/wso2/carbon/kernel/branches/4.2.0/samples/student-manager/.