Friday, November 1, 2013

Identifying OSGI components in server startup .......

For this post I will publish a code snippet for identifying unsatisfied osgi bundles. 

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.telnet.TelnetClient;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.core.MultipleServersManager;

import java.io.*;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;

import static org.testng.Assert.assertTrue;

/*
  This class can be used to identify required osgi component service components (eg: unsatisfied) in server startup
 */
public class OSGIServerBundleStatusTest {

    private static final Log log = LogFactory.getLog(OSGIServerBundleStatusTest.class);

    private static int telnetPort = 2000;
    private TelnetClient telnet = new TelnetClient();
    private ArrayList<String> arrList = new ArrayList<String>();
    private ArrayList<String> unsatisfiedList = new ArrayList<String>();
    private HashMap<String, String> serverPropertyMap = new HashMap<String, String>();
    private MultipleServersManager manager = new MultipleServersManager();

    @BeforeClass(alwaysRun = true)
    public void init() throws Exception {

        serverPropertyMap.put("-DportOffset", "1"); // to start the server from a different port offset
        serverPropertyMap.put("-DosgiConsole", Integer.toString(telnetPort)); // start with osgicomponentservice
        CarbonTestServerManager server = new CarbonTestServerManager(System.getProperty("carbon.zip"),
                serverPropertyMap);
        manager.startServers(server);
    }

    @AfterClass(alwaysRun = true)
    public void stopServers() throws Exception {
        disconnect();  // telnet disconnection
        // manager.stopAllServers();
    }

    @Test(groups = "wso2.as", description = "Identifying and storing unsatisfied OSGI components")
    public void testOSGIUnsatisfiedComponents() throws Exception {

        telnet.connect(InetAddress.getLocalHost().getHostAddress(), telnetPort);
        telnet.setSoTimeout(10000);

        ArrayList<String> arr = retrieveUnsatisfiedComponentsList("ls");

        for (int x = 0; x < arr.size(); x++) {
            unsatisfiedList.add(arrList.get(x).split("\t")[3]);
            log.info(unsatisfiedList.get(x));
        }


        assertTrue(unsatisfiedList.size() > 0);  // TODO - Will vary according to the requirement
    }

    private ArrayList<String> retrieveUnsatisfiedComponentsList(String command) throws IOException {
        writeInputCommand(command);
        try {
            readResponse();
        } catch (SocketTimeoutException e) {
            log.error("Socket timeout Exception "  e);
        }
        return arrList;
    }

    private void writeInputCommand(String value) {

        PrintStream out = new PrintStream(telnet.getOutputStream());
        out.println(value);
        out.flush();
        log.info(value);
    }

    private void readResponse() throws IOException {

        InputStream in = telnet.getInputStream();
        BufferedReader inBuff = new BufferedReader(new InputStreamReader(in));
        String inputLine = inBuff.readLine();

        while (!inputLine.equals("")) {
            if (inputLine.contains("Unsatisfied")) {  // filtering Unsatisfied components
                arrList.add(inputLine);
            }
            inputLine = inBuff.readLine();
            log.info(inputLine);
        }
    }

    private void disconnect() throws IOException {
        telnet.disconnect();
        log.info("Telnet connection is disconnected");
    }
}


import java.io.IOException;
import java.util.HashMap;

public class CarbonTestServerManager extends TestServerManager {

    public CarbonTestServerManager() {
    }

    public CarbonTestServerManager(String carbonZip, HashMap<String, String> startupParameterMap) {
        super(carbonZip, startupParameterMap);
    }

    public CarbonTestServerManager(int portOffset) {
        super(portOffset);
    }

    public String startServer() throws IOException {
        String carbonHome = super.startServer();
        System.setProperty("carbon.home", carbonHome);
        return carbonHome;
    }

    public void stopServer() throws Exception {
        super.stopServer();
    }

    protected void copyArtifacts(String carbonHome) throws IOException {
    }
}

No comments:

Post a Comment