Friday, April 2, 2010

Convert String to a InputStream

Following code snippet show how to $subject.

import java.io.UnsupportedEncodingException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class StringSample {
public static void main(String[] args) {
String textInput = "Convert String to InputStream";
try {
InputStream is = new ByteArrayInputStream(textInput.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

Programmetically add Log4J Appenders

Following code snippet shows how to programmetically add log4j Appenders to your java code.

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.SimpleLayout;

public class SampleLoggingAppender {
static Logger logger = Logger.getLogger(SampleLoggingAppender.class);

public static void main(String args[]) {
SimpleLayout layout = new SimpleLayout();
FileAppender fileAppender = null;
try {
fileAppender = new FileAppender(layout, "log.txt", false);
} catch (Exception ignored) {
}

ConsoleAppender consoleAppender = new ConsoleAppender(new PatternLayout());

logger.addAppender(fileAppender);
logger.addAppender(consoleAppender);
logger.setLevel(Level.DEBUG);

logger.debug("DEBUG");
logger.info("INFO");
logger.warn("WARN");
logger.error("ERROR");
logger.fatal("FATAL");
}
}

Sunday, March 28, 2010

Transport Statistics Component for WSO2 ESB

Transport Statistics component shows statistics related to Transports and it will be available with the WSO2 Carbon Iridium (version 3.0.0) release. It displays the Transports which are implementing
the interface org.apache.axis2.transport.base.ManagementSupport. These transport statistics include bytes received, bytes sent, faults receiving, faults sending,last reset time, max size received, max size sent, messages received, messages sent, metrics window, min size received, min size sent, queue size, timeouts receiving, timeouts sending.

Viewing Transport Statistics
  • In the navigator, under Monitor, click Transport Statistics. The Transport Statistics page appears.
Figure 1: Transport Statistics
  • The entry page lists down all the exposed Tranports which have implemented the ManagementSupport interface (Figure 1). The graph gives an aggregated view of the bytes received/ bytes sent vs time for all transports.
  • Click on a link to view the relevant statistics which relates to a transport (Figure 2). The graph gives a drilled down view of the bytes received/ bytes sent vs time for a specific transport.
  • Each link will take you to a new page that displays the transport statistics exposed by JMX. These data are the data which is shown in the jconsole and we are giving an aggregated view of the transport stats.
Figure 2: Transport Statistics for HTTP transport
  • These transport statistics include bytes received, bytes sent, faults receiving, faults sending, last reset time, max size received, max size sent, messages received, messages sent, metrics window, min size received, min size sent, queue size, timeouts receiving, timeouts sending.
This is the first component that I developed for WSO2 ESB. During the development stages of this component I had the chance of learning Ajax and applying them to this component, which is showing a screenshot of the statistics at a given point of time.

https://svn.wso2.org/repos/wso2/branches/carbon/3.0.0/components/transport-statistics

Saturday, March 27, 2010

Restore a Registry Resource stored in a file to WSO2 Registry

Following code snippet shows, how to programmetically restore a Registry Resourece written in to a file, back to WSO2 Registry using RemoteRegistry. This is a follow-up to an earlier blogpost.

import org.wso2.carbon.registry.app.RemoteRegistry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

import java.net.URL;
import java.net.MalformedURLException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class RemoteRegistrySampleTest {
public static void main(String[] args) {
System.setProperty("javax.net.ssl.trustStore", "CARBON_HOME/resources/security/client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");

RemoteRegistry esb2xRemoteRegistry = null;
try {
esb2xRemoteRegistry = new RemoteRegistry(new URL("https://localhost:9443/registry"),
"admin", "admin");
esb2xRemoteRegistry.restore("/esb-resources", new FileReader("/home/heshan/Dev/wso2_heshan/temp/registry.xml"));
} catch (RegistryException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Backup WSO2 Registry Resource to a file

Following code snippet shows, how to programmetically backup WSO2 Registry Resource to a file using RemoteRegistry.

import org.wso2.carbon.registry.app.RemoteRegistry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;

import java.net.URL;
import java.net.MalformedURLException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class RemoteRegistrySampleTest {
public static void main(String[] args) {
System.setProperty("javax.net.ssl.trustStore", "CARBON_HOME/resources/security/client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");

RemoteRegistry esb2xRemoteRegistry = null;
try {
esb2xRemoteRegistry = new RemoteRegistry(new URL("https://localhost:9443/registry"),
"admin", "admin");
esb2xRemoteRegistry.dump("/esb-resources", new FileWriter("/home/heshan/Dev/wso2_heshan/temp/registry.xml"));
} catch (RegistryException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Tuesday, March 2, 2010

Remove Line Breaks From a Java String

Following code snippet shows how to remove line termination characters from a String.


import java.util.regex.*;

public class ClassA {
public static void main(String args []) {
String inputText = "sdfsdfsf\nsdfsdfsd";
String inputText2 = "var trpListenerGr\naphDivId = #trpLi\nstenerGraph2\n";
RemoveLineTerminationCharacterss(inputText);
System.out.println("-------------------------------");
RemoveLineTerminationCharacterss(inputText2);

}

public static String RemoveLineTerminationCharacterss(String inputText){
Pattern p;
Matcher m;
System.out.println(" Input Text : " + inputText);
p = Pattern.compile("\n");
m = p.matcher(inputText);
String str = m.replaceAll("");
System.out.println(" OUtput Text: " + str);
return str;
}
}

Sunday, February 21, 2010

WSO2 SOA Workshop NYC

The full-day interactive workshop will show you how to build your own solutions with industry acclaimed best practices and patterns. It will also discuss real SOA deployment issues such as security, interoperability and moving to the Cloud.

Paul Fremantle, Founder and CTO, WSO2 and Asanka Abeysinghe, Lead Solutions Architect will be presenting at the workshop.

For more details and to register visit:
http://wso2.com/events/2010-feb-nyc-soa-workshop/