Presentation on Spring Framework

Hi all,

Recently I did a session for a set of QA engineers at work on “Introduction to Spring Framework”. Thought of sharing it here.

Cheers! 🙂

~Rajind Ruparathna

Advertisements

Implementing an Embedded ElasticSearch Server

Hi all, recently I added an embedded ElasticSearch server for the upcoming release of  AdroitLogic UltraESB. Embedded ES Server will be useful when a large ES cluster is not required and also for ES integration tests. Let’s have a look at the code.

import org.adroitlogic.logging.api.Logger;
import org.adroitlogic.logging.api.LoggerFactory;
import org.adroitlogic.ultraesb.api.monitoring.StatisticsServer;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

/**
 * @author rajind
 * Implementation of embedded elasticsearch server.
 *
 */

public class ElasticSearchStatisticsServer implements StatisticsServer {

    private Node node;
    private static final Logger logger= LoggerFactory.getLogger(ElasticSearchStatisticsServer.class);

    private static final String ES_PROPERTY_PATH_HOME = "path.home";
    private static final String ES_PROPERTY_PATH_CONF = "path.conf";

    /**
     * Create an elasticsearch embedded node with provided default configuration.
     */
    @SuppressWarnings("UnusedDeclaration")
    public ElasticSearchStatisticsServer() {
    }

    @Override
    public void init(String confDir, String homeDir) {
        try {
            logger.info("Embedded Elasticsearch server is initializing");
            Settings elasticsearchSetting = Settings.settingsBuilder()
                    .put(ES_PROPERTY_PATH_HOME, homeDir)
                    .put(ES_PROPERTY_PATH_CONF, confDir)
                    .build();
            node = nodeBuilder().settings(elasticsearchSetting).build().start();
        } catch (Exception e) {
            logger.warn("Could not init embedded elasticsearch node", e);
        }
    }

    public Client getClient() {
        if (node != null) {
            return node.client();
        } else {
            return null;
        }
    }

    @Override
    public void destroy() {
        try {
            logger.info("Stopping embedded elasticsearch server");
            node.close();
        } catch(Exception e) {
            logger.warn("Could not stop embedded elasticsearch node due to {}", e.getMessage());
        }
    }
}

It’s actually very simple. “path.conf” will contain the elasticsearch configuration yaml file “elasticsearch.yml“. Please remember that “path.home” will always have to be set as elasticsearch looks for that by default.

Cheers! 🙂

~Rajind Ruparathna

The Maximum-Subarray Problem

What is meant by the maximum sub-array problem is that we want to find the non-empty, contiguous sub-array of an array whose values have the largest sum. Following is a divide & conquer solution implemented for it using Java language.

/**
 *
 * @author Rajind
 */
public class MaximumSubarrayProblem {
    public static int[] maxSubArray(int[] A) {
        int newsum = A[0];
        int max = A[0];

        int low = 0;
        int high = 0;

        for(int i=1;i<A.length;i++){ 
            if(newsum+A[i] > A[i]){
                high++;
            }else{
                low = i;
                high = i;
            }

            newsum = Math.max(newsum+A[i],A[i]);
            max= Math.max(max, newsum);
        }

        int[] arr = new int[3];
        arr[0] = max;
        arr[1] = low;
        arr[2] = high;
        return arr;
    }

    public static void main(String args[]){
        int[] arr = {13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
        int []array = maxSubArray(arr);
        System.out.println("max: "+array[0]);
        System.out.println("low: "+array[1]);
        System.out.println("high: "+array[2]);
    }
}

~Rajind Ruparathna

Reverse Words in a String

Reversing words in a string is a popular coding interview question. Here what is meant by reversing words in a string is something like this.

Input string:     “My name is rajind and i am a believer”
Output string:  “believer a am i and rajind is name My”

Doing this in-place without use additional space to store the string is the trick. Following is the java code to do that.

/**
 *
 * @author Rajind
 */
public class ReverseWordsString {

    public static char[] stringReverse(char[] arr, int start, int end){
        while(start < end){
            arr[start] = (char) (arr[start]^arr[end]);
            arr[end] = (char) (arr[start]^arr[end]);
            arr[start] = (char) (arr[start]^arr[end]);
            start++;
            end--;
        }
        return arr;
    }

    public static void main(String Args[]){
        /*char a = 'A';
        char b = 'B';
        a = (char) (a^b);
        b = (char) (a^b);
        a = (char) (a^b);

        System.out.println(a + "   "+ b);
        */

        char[] str = "My name is rajind and i am a believer".toCharArray();
        str = stringReverse(str, 0, str.length -1);

        int start = 0;
        for(int i=0; i < str.length; i++){
            if(str[i] == ' '){
                str = stringReverse(str, start, i-1);
                start = i + 1;
            }
        }

        str = stringReverse(str, start, str.length-1);
        System.out.println(str);
    }
}

~Rajind Ruparathna

WiFi Information Extraction Java

Hello everyone, I was recently involved in a wireless networking project where we had to develop a student response system. In that I had to extract certain WiFi related information such as list of available SSIDs, connected SSID, IP address and Subnet Mask

Java doesn’t allows us to directly get that low level information. Therefore I had run command line and run netsh commands inside java an extract that information. Following is the class I implemented for this. Please note that this only works on windows.

Continue reading “WiFi Information Extraction Java”

Solution to Senate Bus Problem

This is Java Implementation of the Semaphore based solution for Senate Bus Problem

https://github.com/Rajind/SenateBusProblem

The Senate Bus Problem (From The Little Book of Semaphores by Allen B. Downey)

This problem was originally based on the Senate bus at Wellesley College. Riders come to a bus stop and wait for a bus. When the bus arrives, all the waiting riders invoke boardBus, but anyone who arrives while the bus is boarding has to wait for the next bus. The capacity of the bus is 50 people; if there are more than 50 people waiting, some will have to wait for the next bus. When all the waiting riders have boarded, the bus can invoke depart. If the bus arrives when there are no riders, it should depart immediately.

Continue reading “Solution to Senate Bus Problem”