Thursday, February 27, 2014

Fixing java.lang.NoClassDefFoundError: org/codehaus/classworlds/Launcher

I came across this error after I installed a new version of maven.
$ mvn -version
java.lang.NoClassDefFoundError: org/codehaus/classworlds/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.classworlds.Launcher
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: org.codehaus.classworlds.Launcher.  Program will exit.
Exception in thread "main"
The issue was with having two MAVEN_HOME locations (two versions) in the "path" variable. Once I removed a one, the issue resolved.
$ mvn -version
Apache Maven 3.1.1

Monday, February 24, 2014

Fixing mysql replication errors in a master/slave setup

If you have a mysql master/slave replication setup and have run into replication errors, you can follow the below instructions to fix up the replication break and sync up the data.
1) Stop mysql on the slave.
service mysql stop
2) Login to the master.

3) Lock the master by running the following command
mysql> flush tables with read lock;
NOTE: No data can be written at this time to the master, so be quick in the following steps if your site is in production. It is important that you do not close mysql or this running ssh terminal. Login to a separate ssh terminal for the next part.

4) Now, in a separate ssh screen, type the following command from the master mysql server.
rsync -varPe ssh /var/lib/mysql root@IP_ADDRESS:/var/lib/ —delete-after
5) After this is done, now you must get the binlog and position from the master before you unlock the tables. Run the following command on master
mysql> show master status\G;
Then you’ll get some information on master status. You need the file and the position because that’s what you’re going to use on the slave in a moment. See step 10 on how this information is used, but please do not skip to step 10.

6) Unlock the master. It does not need to be locked now that you have the copy of the data and the log position.
mysql> unlock tables;
7) Login to the slave now via ssh. First remove the two files : /var/lib/mysql/master.info and /var/lib/mysql/relay-log.info

8) Start mysql on the slave.
service mysqld start
9) Immediately login to mysql and stop slave
mysql> slave stop; 
10) Now, you have to run the following query filling in the information from the show master status above (Step 5.)
mysql> CHANGE MASTER TO MASTER_HOST=MASTER_IP,
mysql> MASTER_USER=‘replicate’, MASTER_PASSWORD=‘replicate’,
mysql> MASTER_LOG_FILE=‘mysql-bin.000001’, MASTER_LOG_POS=1234512351;
11) Now start the slave.
mysql > slave start
12) Check slave status.
mysql> show slave status\G;

Thursday, February 20, 2014

XML to XML tranformation using XSLT

In this example I am transforming an XML using XSLT by modifying certain child elements. In coming XML message:
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="/xsl/error.xsl"?>
<outtask_error>
    <server>
        <name>RSDC-C12</name>
    </server>
    <application>
        <name>Jobs</name>
        <version>2012_06_15</version>
        <prod_status>qa</prod_status>
        <datacenter_location>rtwa3</datacenter_location>
        <request_id>n6dft440-8049-4dff-982a-ee05abc8433</request_id>
    </application>
    <error time="2014-02-06T20:01:05" timezone="Pacific Standard Time">
        <error_id>aab10-825-4a52d-a261-7a845262f</error_id>
        <error_default_source>ICSYC</error_default_source>
        <request_id>b2263a40-8049-4dff-942a-ee05ef3f8433</request_id>
        <error_source>ICSync</error_source>
        <error_description>Connection Entity:abc no DB name is not configured</error_description>
        <stack_trace>at Company.Core.Sync.GetConnectionInfo...
        </stack_trace>
    </error>
    <server_variables>
        <param name="SERVER_NAME">RSDC-C12</param>
        <param name="SCRIPT_NAME">C:\Sync.exe</param>
        <param name="QUERY_STRING">Sync.exe Day1</param>
    </server_variables>
    <Error_Context>
        <param name="databasename">Company_MT</param>
        <param name="error_defaultsource">SYS</param>
        <param name="error_defaultapplicationname">Jobs</param>
        <param name="requestid">b8943a40-1349-4dff-232a-ee05ef523452342f</param>
        <param name="dbservername">fabc-sql02,1420</param>
    </Error_Context>
</outtask_error>
XSLT for the transformation:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="outtask_error/server_variables">
        <server_variables>
            <xsl:for-each select="param">
                <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
                <xsl:value-of select="."/>
                <xsl:text disable-output-escaping="yes"><</xsl:text>/<xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
            </xsl:for-each>
        </server_variables>
    </xsl:template>

    <xsl:template match="outtask_error/Error_Context">
        <Error_Context>
            <xsl:for-each select="param">
                <xsl:text disable-output-escaping="yes"><</xsl:text><xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
                <xsl:value-of select="."/>
                <xsl:text disable-output-escaping="yes"><</xsl:text>/<xsl:value-of select="./@name"/><xsl:text disable-output-escaping="yes">></xsl:text>
            </xsl:for-each>
        </Error_Context>
    </xsl:template>

</xsl:stylesheet>
Out going XML:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/xsl/error.xsl"?><outtask_error>
    <server>
        <name>RSDC-C12</name>
    </server>
    <application>
        <name>Jobs</name>
        <version>2012_06_15</version>
        <prod_status>qa</prod_status>
        <datacenter_location>rtwa3</datacenter_location>
        <request_id>n6dft440-8049-4dff-982a-ee05abc8433</request_id>
    </application>
    <error time="2014-02-06T20:01:05" timezone="Pacific Standard Time">
        <error_id>aab10-825-4a52d-a261-7a845262f</error_id>
        <error_default_source>ICSYC</error_default_source>
        <request_id>b2263a40-8049-4dff-942a-ee05ef3f8433</request_id>
        <error_source>ICSync</error_source>
        <error_description>Connection Entity:abc no DB name is not configured</error_description>
        <stack_trace>at Company.Core.Sync.GetConnectionInfo...
        </stack_trace>
    </error>
    <server_variables><SERVER_NAME>RSDC-C12</SERVER_NAME><SCRIPT_NAME>C:\Sync.exe</SCRIPT_NAME><QUERY_STRING>Sync.exe Day1</QUERY_STRING></server_variables>
    <Error_Context><databasename>Company_MT</databasename><error_defaultsource>SYS</error_defaultsource><error_defaultapplicationname>Jobs</error_defaultapplicationname><requestid>b8943a40-1349-4dff-232a-ee05ef523452342f</requestid><dbservername>fabc-sql02,1420</dbservername></Error_Context>
</outtask_error>