Sunday, March 15, 2015

Troubleshooting WSO2 server database operations with log4jdbc-log4j2

If you are using WSO2 Carbon based servers and are facing issues related to the database, there are few steps you should take in order to rectify those issue. Since Carbon 4.2.0 based products use Tomcat JDBC Connection Pool, first thing you could do is to try tuning the datasource parameters in the master-datasources.xml (or *-datasources.xml) file located in the ${CARBON_HOME}/repository/conf/datasources/ directory. Some of the parameters you might want to double check is;

  1. Set the "validationQuery" parameter 
  2. Set "testOnBurrow" to "true"
  3. Set a "validationInterval" and try tuning it to fit your environment
For a detailed explanation about those properties and also addition parameters that can be used to tune the JDBC pool, please visit the Tomcat site listed above.

Even though these parameters might help fix some of the JDBC issues you'd encounter, there might be instances where you'd want additional information to understand what's going on between the WSO2 server and the underlying database. 

We can use log4jdbc-log4j2 which is an improvement of the log4jdbc to do an in depth analysis JDBC operations between the WSO2 server and the database. In this post I'll be explaining how to configure log4jdbc-log4j2 with WSO2 servers.  

To setup a WSO2 server to log4jdbc-log4j2, follow the steps below (In the post I am assuming that the server has already been configured to point to the external database and setup with the necessary JDBC driver etc)
  1. Download log4jdbc-log4j2 jar and copy it to the ${CARBON_HOME}/repository/components/lib directory. 
  2. Prepend "jdbc:log4" to the JDBC url, <url> parameter in the datasource configuration, so the url would look like;
  3. jdbc:log4jdbc:mysql://localhost:3306/governance
    
    
  4. Change the "driverClassName" to "net.sf.log4jdbc.sql.jdbcapi.DriverSpy" as follows;
  5. net.sf.log4jdbc.sql.jdbcapi.DriverSpy
    
    
  6. To direct the log4jdbc-log4j2 output to a separate log file, add the following entries to the log4j.properties file located in the conf/ directory
  7. log4j.logger.jdbc.connection=DEBUG, MySQL
    log4j.logger.jdbc.audit=DEBUG, MySQL
    log4j.additivity.jdbc.connection=false
    log4j.additivity.jdbc.audit=false
      
    log4j.appender.MySQL=org.apache.log4j.RollingFileAppender
    log4j.appender.MySQL.File=${carbon.home}/repository/logs/mysql-profile.log
    log4j.appender.MySQL.layout=org.apache.log4j.PatternLayout
    
  8. Finally, you need to start the server with the system property;
  9. log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator

Note: You can set the system property in the wso2server.sh file located in the bin/ directory for ease of use.

Now that you have the log4jdbc-log4j library and the required configurations in place, you can start using the server. The JDBC debug logs will be printed in the mysql-profile.log file located in the logs/ directory. There are six different loggers you can use to troubleshoot different types of problems, check section 4.2.2 of this page for more information on the different logging options.

Good luck !!!

Friday, April 5, 2013

How to do a WebEx screen share on 64-bit Ubuntu 12.04

If your favorite operating system is Ubuntu/Linux, then you might agree with me when I say that its a bit of a pain to have to login to Windows just to join a WebEx session. Well if its just attending WebEx as a listener/viewer its one thing. Imagine if you do all your development in Ubuntu and have all your server/environment etc setup in Ubuntu, and you have to demonstrate your work via a WebEx by sharing your screen. Needless to say having to setup an identical work environment on Windows merely for a presentation is not worth it! As if its going to work just like that on Windows anyway ;) No offense meant, but as software developers we know all too well that its not so simple to switch between platforms.

That doesn't mean you can stay away from doing the demonstrations either. So the next solution is to somehow get the blessed thing to work on Ubuntu. If you are using a 32-bit distribution on Ubuntu, then it might be pretty simple, all you need to do is simply install the 32-bit Java plugin to your Firefox web browser and join the WebEx. However if you are using a 64-bit Ubuntu distribution just installing the Java plugin to your existing (64-bit) Firefox might not work. I know it didn't work for me, initially.

So lets see how you can get WebEx applet to work on 64-bit Ubuntu (12.04).

1. First you need to install ia32-libs on your machine, follow this post if you have issues installing it.
2. Next download 32-bit Firefox and 32-bit Java (JDK) to your machine. (I am assuming you can Google and find the downloads :))
3. Extract Firefox and the JDK to a directory, lets say ~/webex/firefox/ and ~/webex/jdk_<version>/ respectively.
4. The you need to create a symbolic link in ~/webex/firefox/plugins/ directory to ~/webex/jdk_<version>/jre/lib/i386/libnpjp2.so. You can do that as follows;

$cd ~/webex/firefox/
$mkdir plugins
$cd plugins/
$ln -s ~/webex/jdk_/jre/lib/i386/libnpjp2.so

This will basically install the 32-bit Java plugin for your new 32-bit Firefox.

5. Now you are all good to go, simply run Firefox as follows;

$cd ~/webex/firefox/
$./firefox

Make sure you do a "./firefox" and not just "firefox" which might open the default 64-bit browser.

6. To make sure that 32-bit Java is installed correctly go to Java test page and check if the "Java Architecture" is "32-bit". If so, congratulations you are good to go!

Good Luck .! :)

Sunday, March 31, 2013

Installing ia32-libs on Ubuntu 12.04


If you ever wanted to run a 32-bit program in a 64-bit Ubuntu 12.04 and encountered package dependency issues while trying to install the ia32-libs via apt-get, don't worry, you are not alone. I myself tried this once when I wanted to install JD-GUI (Java de-compiler) but gave up because I ran out of options that time. This time however I had no choice but to get it to work. Good news is that I manage to to install the necessary libs thanks to post found at [1]. For my own future reference and also for those who might need this I am writing it down here, hope some find it useful, lemme know if its you :)

Downgrade the installed package is follows;

$sudo vi /etc/apt/preferences

and insert the following code;

Package: *       
Pin: release a=precise*
Pin-Priority: 2012

and "write & quite" by doing ":wq!"

Next you can downgrade the packages as follows;

$sudo apt-get dist-upgrade

This operation is going to take a while, so let if complete. Once it is done you can install ia32-libs as follows;

$sudo apt-get install ia32-libs

Don't forget to remove the preference file you created, remove it as follows;

$sudo rm /etc/apt/preferences

If not new updated will not be installed. That's it you can now run you 32-bit applications without a issue. Hope this post will help you.!

[1] - http://askubuntu.com/questions/136394/cannot-install-ia32-libs

Thursday, July 28, 2011

Scaling transparent images in Java.

Ever had the need to scale images using Java? If so you would have realized that its a fairly easy task given the amount of support provided by Java Imaging API's as well as the numerous resources available online. Yup I just said it, numerous resources online, so the obvious question why write another blog post? Well you are about to find out.

I've been working on scaling uploaded images using Java for a couple of days now. Everything works fine for an opaque image, but not too well for transparent images. As it turned out most of the articles on scaling images discussed how to scale opaque images but not transparent ones. So I thought I might as well blog about it.

The issue with scaling transparent images are that the resulting images is distorted, and the transparent areas are drawn in black! Check out the two images below.

                                        Original image                           Scaled Image

This obviously wasn't acceptable, as a quick fix we investigated on coloring the transparent images to White color but the that was an ugly fix too.

Most of the blogs and forums that discussed this issue of black background issue suggested setting the ColorModel to BufferedImage.TYPE_INT_ARGB but unfortunately that didn't quite work for me.

The issue is how we paint the image once transformed, make sure to write the image in PNG format so that transparency is preserved. JPEG doesn't support transparency and GIF formats behave differently depending on the background etc. On the other hand using PNG works fine for transparent and opaque images.
          
private static DataHandler scaleImage(DataHandler dataHandler, int height, int width) throws IOException {

        Image image = ImageIO.read(new BufferedInputStream(dataHandler.getInputStream()));
        // Check if the image has transparent pixels
        boolean hasAlpha = ((BufferedImage)image).getColorModel().hasAlpha();

        // Maintain Aspect ratio
        int thumbHeight = height;
        int thumbWidth = width;
        double thumbRatio = (double)width / (double)height;
        double imageRatio = (double)image.getWidth(null) / (double)image.getHeight(null);
        if (thumbRatio < imageRatio) {
            thumbHeight = (int)(thumbWidth / imageRatio);
        } else {
            thumbWidth = (int)(thumbHeight * imageRatio);
        }

        BufferedImage thumb;
        // Check if transparent pixels are available
        // and set the color mode accordingly 
        if (hasAlpha) {
            thumb = new BufferedImage(thumbWidth, thumbHeight,
                    BufferedImage.TYPE_INT_ARGB);
        } else {
            thumb = new BufferedImage(thumbWidth, thumbHeight, 
                    BufferedImage.TYPE_INT_RGB);
        }
        Graphics2D graphics2D = thumb.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

        // Save the image as PNG so that transparent pixels are rendered
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageIO.write(thumb, "PNG", output);

        DataSource dataSource= new ByteArrayDataSource(output.toByteArray(), 
                               "application/octet-stream");
        return new DataHandler(dataSource);
    }

As you can see the image format is set to PNG when and the final image is returned as a DataHandler.

If you want to iterate each and every pixel of an image and then identify transparent pixels and change the color of that pixel you can do it as follows;

int destWidth = 151;
  int destHeight = 179;
  BufferedImage dest = new BufferedImage(destWidth, destHeight,
    BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < dest.getHeight(); i++) {
   for (int j = 0; j < dest.getWidth(); j++) {
    int pixel = dest.getRGB(j, i);
    byte alpha=(byte)pixel;
    alpha%=0xff;
    if (pixel == 0) {
     // Set the color of the pixel to White
     dest.setRGB(j, i, Color.WHITE.getRGB());  
    }
   }
  }

More useful links on scaling images are listed below.


[1] - http://developeriq.in/articles/2010/oct/07/playing-with-images-using-java/
[2] - http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html


Tuesday, July 19, 2011

Setting up WSO2 Stratos on a local machine.

Setting up WSO2 Stratos has become as simple as executing a few commands, this post discusses how Startos can be setup on a local machine.

You can checkout carbon from the trunk or branch and build the Stratos services and set it up locally.
Carbon trunk is found at - https://svn.wso2.org/repos/wso2/trunk/carbon/
Carbon 3.2.0 branch at - https://svn.wso2.org/repos/wso2/branches/carbon/3.2.0

Next you need to configure some variables in ~/.bashrc file as follows;
export CARBON_DIR="/home/user/trunk/carbon" => carbon checkout directory 
export STRATOS_DIR=/home/user/stratos/deploy => directory to setup stratos 
export STRATOS_VERSION=1.5.1 => Stratos version
export SSO_ENABLED=true => enable / disable single sign-on
export CREATE_DB=true
export STRATOS_MAIL_TRANSPORT='****.***.***25 false false cloud-test@gmail.com'
export NOTIFICATION_EMAIL=user@gmail.com


If you have all the Stratos services in a single directory can use use the PACKS_DIR option instead of CARBON_DIR. For Example, if all your packs are located at /home/user/services; then set the PACKS_DIR option as follows;
export PACKS_DIR=/home/user/services

In order to execute the setup script you need to install List More Utils, you can do this as follows;
$sudo apt-get install liblist-moreutils-perl
You can find the Stratos setup scripts at ${CARBON_DIR}/build directory. You can execute the setup script - stratos-setup.pl as follws;
$./stratos-setup.pl

Once you execute the script as shown above, the Stratos services will be unpacked and deployed at the directory denoted as STRATOS_DIR. Also several configuration files will be modified under the hood to ensure no port conflicts occur while starting multiple services on one machine, and also include custom URL's based on the service, in this case, esb.cloud-test.wso2.com for the ESB as a Service etc.

Once the script runs, you need to add the following host entries to the /etc/hosts file;
127.0.0.1 cloud-test.wso2.com
127.0.0.1 identity.cloud-test.wso2.com
127.0.0.1 governance.cloud-test.wso2.com
127.0.0.1 appserver.cloud-test.wso2.com
127.0.0.1 bam.cloud-test.wso2.com
127.0.0.1 dss.cloud-test.wso2.com
127.0.0.1 bps.cloud-test.wso2.com
127.0.0.1 brs.cloud-test.wso2.com
127.0.0.1 cep.cloud-test.wso2.com
127.0.0.1 esb.cloud-test.wso2.com
127.0.0.1 gadget.cloud-test.wso2.com
127.0.0.1 mb.cloud-test.wso2.com

127.0.0.1 mashup.cloud-test.wso2.com 

Now you are ready to run Straos on your machine. Simply navigate to the  $STRATOS_DIR and run the stratos.sh file as follows;
$cd $STRATOS_DIR
$sh stratos.sh start all => Starts all the Stratos services
$sh stratos.sh stop all => Stop all the running Stratos services

Alternately you can change to specific service directory and start the respective server. To starts Stratos ESB you can simply do;
$cd $STRATOS_DIR/wso2stratos-esb-1.5.1
$sh bin/wso2server.sh

The option Single Sign-On means that once a user logs in to a single service, eg: Stratos Manager using their credentials they will automatically be signed in to any other service eg: ESB or Governance.

Hope you enjoy using WSO2 Stratos !!!

Sunday, November 14, 2010

Hello World, Blogger style.

Hello!

Just in case your don't know much about me just as yet, my name is Shariq. I am working as a Software Engineer at WSO2. I completed my BSc (Hons) in Software Engineering with First class Honors at the Informatics Institute of Technology (IIT) which is affiliated to the University of Westminster, UK.

Many a times during the past few weeks I have been thinking of starting my own blog. To be honest with you, if one of my friends told me to write my own blog say an year or two ago, my immediate response would be like, "Whaaa?!? you got to be kidding me, right ?!? :D", and yea no kidding. So then, why the sudden change in perception?, that's something you'll figure out soon enough. ;)

It all started at WSO2. At WSO2 personal branding is an important factor and all employees are encouraged to develop a unique identity for themselves. Whats the use of an identity unless the world knows about it anyway? One way of letting the world know about what you are up to is putting it in writing, preferably in the form of a blog so that interested individuals can learn learn from it and also express their own views on that particular domain.

Besides we are in the internet age for quite sometime now, its important make our presence felt on the web too, don't you think ;) ?!? OK so your probably thinking that I am writing this blog to create a brand for my self, hmmm I wish it was that simple !!

Personal agendas aside, I am blogging because I feel my posts would help individuals researching on IT and technology related domains.
77% of Internet users read blogs according to Universal McCann
That kinda totally make sense too, cause its always easy to learn through ones experience rather than reading lengthy documentation. I personally find blogs much more useful and accurate rather than lengthy documentation when trying get a piece of code to do something for me, so I just though "May be I can return the favor :)". Besides working in a innovative environment such as WSO2, I am pretty sure I could make quite a few useful posts.

Just for the record, I am no typical geek and I find many other things that are interesting, that doesn't necessarily reside within the boundaries of IT. So yes I would be blogging about my other interests too, which include sports such as soccer, cricket and TT of course ;), movies, TV series, books, games etc. Oh and yea, I am a bit of a funny person too, so I'll do my best to post some interesting and amusing articles from time to time, after all "laughter is the best medicine" or so they say :D.

Well that's about me and what I am up to as far as blogging is concerned. Hope you find my posts useful  and equally amusing to follow. :)

K then till next time ...