Wednesday, August 19, 2015

How to stop disconnecting Wi-Fi when locking a MAC.

Hi All,

I've been experiencing an issue where the Wi-Fi keeps disconnecting when I lock my screen on my MacBook. I found the following solution to keep it up always.

Find the network interface for Wi-Fi.

Use the "ifconfig" command to find the network interface. It would most probably be "en0" or "en1".

Disable disconnecting.

Run the following commands. In the 2nd command, make sure you use the correct network interface found in the earlier step. Running the following commands may ask for the password. If so, please provide.

cd /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources
sudo ./airport en1 prefs DisconnectOnLogout=NO

And thats all. Wi-Fi should be connected even if the screen is locked.

References...

[1] - http://apple.stackexchange.com/questions/71884/wi-fi-disconnects-when-i-lock-the-mac

Wednesday, August 12, 2015

Publishing WSO2 product logs to a Topic in Apache ActiveMQ.

Hi All,

This post will describe how the logs of a WSO2 product can be published to a topic in Apache ActiveMQ. WSO2 products use apache's log4j module for logging purposes. We will be using the "org.apache.log4j.net.JMSAppender" class to publish logs to a topic. ActiveMQ already has a guide on how this can be done [1].

log4j.properties

Add the following configuration to <WSO2_PRODUCT>/repository/conf/log4j.properties file.

log4j.appender.JMS_APPENDER=org.apache.log4j.net.JMSAppender
log4j.appender.JMS_APPENDER.InitialContextFactoryName=org.apache.activemq.jndi.ActiveMQInitialContextFactory
log4j.appender.JMS_APPENDER.Append=true
log4j.appender.JMS_APPENDER.ProviderURL=tcp://127.0.0.1:61616
log4j.appender.JMS_APPENDER.TopicBindingName=dynamicTopics/logTopic
log4j.appender.JMS_APPENDER.TopicConnectionFactoryBindingName=ConnectionFactory

We also have to add the "JMS_APPENDER" logger to the root logger in log4j.properties file. Modify the following line...

log4j.rootLogger=ERROR, CARBON_CONSOLE, CARBON_LOGFILE, CARBON_MEMORY, CARBON_SYS_LOG, ERROR_LOGFILE

To the following...

log4j.rootLogger=ERROR, CARBON_CONSOLE, CARBON_LOGFILE, CARBON_MEMORY, CARBON_SYS_LOG, ERROR_LOGFILE, JMS_APPENDER

Dependency JAR files.

Add the following jar files from <ACTIVEMQ_HOME>/lib to <WSO2_PRODUCT>/repository/components/lib folder.

  • activemq-broker-5.*.jar
  • activemq-client-5.*.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • hawtbuf-1.*.jar

Reading received log messages.

The logs are published as JMS ObjectMessages from the JMSAppender class. So therefore we wont be able to see the content of it directly. We would need to create a customer client by using the activemq dependency which can be downloaded or used as a pom dependency from here. The jar file is also available at the <ACTIVEMQ_HOME>/ folder. Using this dependency jar and the code below, we can extract the contents of the log messages.

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.log4j.spi.LoggingEvent;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;

public class Log4jJMSAppenderExample implements MessageListener {

    public Log4jJMSAppenderExample() throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        final Connection conn = factory.createConnection();
        final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        conn.start();
        final MessageConsumer consumer = session.createConsumer(session.createTopic("logTopic"));
        consumer.setMessageListener(this);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    consumer.close();
                    session.close();
                    conn.close();
                } catch (JMSException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public static void main(String[] args) throws Exception {
        new Log4jJMSAppenderExample();
    }

    public void onMessage(javax.jms.Message message) {
        try {
            // receive log event in your consumer
            LoggingEvent event = (LoggingEvent) ((ActiveMQObjectMessage) message).getObject();
            System.out.println("Received log [" + event.getLevel() + "]: " + event.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Notes

If there is a need to use the jndi.properties file, we will have to place it at the <WSO2_PRODUCT>/ folder instead of the <WSO2_PRODUCT>/repository/conf/. The jndi.properties file is not necessary as we have defined the topic in log4j.properties as "dynamicTopics/logTopic".

I have tested the above with WSO2 ESB 4.8.1 and Apache ActiveMQ 5.11.1.

References...

[1] - http://activemq.apache.org/how-do-i-use-log4j-jms-appender-with-activemq.html
[2] - https://docs.wso2.com/display/ESB481/Configure+with+ActiveMQ

Sunday, August 2, 2015

Unzip all zip files in a folder in terminal.

Hi,

With the following command you can unzip all the zip files in the current folder.

unzip \*.zip

Removing __MACOSX folder from a zip file/archive.

Hi All,

Recently I created a zip file in MAC OS X. But when I copied it from my MAC OS X to my Ubuntu machine and opened it, theres a folder named "__MACOSX" inside. I have no idea how it got there.

But you can delete that folder by running the following command in MAC OS X after compressing.

zip -d filename.zip __MACOSX/\*

Saturday, July 18, 2015

RabbitMQ .NET client to connect to WSO2 MB with SSL.

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging both secured and unsecured.

This post will explain on how the RabbitMQ .NET client can be used to publish or subscribe to WSO2 Message Broker 3.0.0 securely.

Creating the Certificate

Go to "<MB_HOME>/repository/resources/security" folder and run the following command. This will create a "cert" file which is the certification file for SSL communication. "wso2carbon.jks" is the trust store file. So we are exporting the certificate from the trust store. The "keytool" command comes with the JDK distribution.

keytool -export -keystore wso2carbon.jks -storepass wso2carbon -file carbon.cert -alias localhost -keypass wso2carbon

The Code

You can use the same implementation as mentioned in [1][2]. Only difference is we have to set the following properties to the "ConnectionFactory" object.

........
........
// The connection factory to connect with the broker.
ConnectionFactory factory = new ConnectionFactory();

// AMQP URL to connect to the broker.
IProtocol protocol = Protocols.AMQP_0_9_1;
factory.VirtualHost = "/carbon";
factory.UserName = "admin";
factory.Password = "admin";
factory.HostName = "localhost";
// Port for SSL
factory.Port = 8672;
factory.Protocol = protocol;

// SSL configuration
factory.Ssl.Enabled = true;
factory.Ssl.CertPassphrase = "wso2carbon";
factory.Ssl.CertPath = @"C:\Users\wso2\Documents\wso2mb-3.0.0-ALPHA\repository\resources\security\carbon.cert";
factory.Ssl.ServerName = "localhost";
factory.Ssl.AcceptablePolicyErrors = System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors;
factory.Ssl.Version = System.Security.Authentication.SslProtocols.Tls;

using (IConnection conn = factory.CreateConnection())
{
    ............
    ............

Set the correct path for "factory.Ssl.CertPath" to the "cert" file we generated in the earlier step.

Thats all guys!. Click here to download the sample .NET project(VS2010).

References...

[1] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Queue
[2] - https://docs.wso2.com/display/MB300/Publishing+and+Receiving+Messages+from+a+Topic
[3] - https://www.rabbitmq.com/ssl.html
[4] - http://pathberiya.blogspot.com/2010_08_01_archive.html

Wednesday, July 15, 2015

RabbitMQ client to subscribe to a queue in WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that provides reliable messaging.

This post will explain on how the RabbitMQ java client can be used to subscribe to a queue WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.3</version>
</dependency>

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Creating consumer
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, false, consumer);

// Shutdown hook handler
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        try {
            // Closing the connection
            channel.close();
            connection.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
});

// Accepting messages
while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    String message = new String(delivery.getBody());

    System.out.println("Message Received : " + message);
}

Click here to download the sample maven project.

References...

RabbitMQ client to publish JMS messages to WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that supports AMQP and MQTT.

This post will explain on how the RabbitMQ java client can be used to publish JMS messages to WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.3</version>
</dependency>

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Message content to publish
String message = "This is a test message";

// Creating properties for the message.
AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
properties.messageId("ID:" + String.valueOf(UUID.randomUUID()));
properties.contentType("text/plain");

// Publishing message
channel.basicPublish("amq.direct", queueName, properties.build(), message.getBytes());

System.out.println("Message published : " + message);

// Closing the connection
channel.close();
connection.close();

Output on management console.

Click here to download the sample maven project.

References...

Friday, July 10, 2015

Running WSO2 ESB Sample 200: Using WS-Security with policy attachments for proxy services

Hi All,

An enterprise service bus (ESB) is a software architecture construct that enables communication among various applications. WSO2 Enterprise Service Bus is such a middleware.

This post will explain on how to execute the WSO2 ESB 4.8.1 sample number 200. This sample demonstrates how you can use WS-Security signing and encryption with proxy services through WS-Policy. When running this sample you may face the following exception coming through the client.

     [java] org.apache.axis2.AxisFault: Error in encryption
     [java]     at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:76)
     [java]     at org.apache.axis2.engine.Phase.invokeHandler(Phase.java:340)
     [java]     at org.apache.axis2.engine.Phase.invoke(Phase.java:313)
     [java]     at org.apache.axis2.engine.AxisEngine.invoke(AxisEngine.java:261)
     [java]     at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:426)
     [java]     at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:430)
     [java]     at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
     [java]     at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
     [java]     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:554)
     [java]     at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:530)
     [java]     at samples.userguide.StockQuoteClient.executeClient(Unknown Source)
     [java]     at samples.userguide.StockQuoteClient.main(Unknown Source)
     [java] Caused by: org.apache.rampart.RampartException: Error in encryption
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:612)
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.build(AsymmetricBindingBuilder.java:97)
     [java]     at org.apache.rampart.MessageBuilder.build(MessageBuilder.java:147)
     [java]     at org.apache.rampart.handler.RampartSender.invoke(RampartSender.java:65)
     [java]     ... 11 more
     [java] Caused by: org.apache.ws.security.WSSecurityException: An unsupported signature or encryption algorithm was used (unsupported key transport encryption algorithm: No such algorithm: http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p); nested exception is: 
     [java]     java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
     [java]     at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:785)
     [java]     at org.apache.ws.security.message.WSSecEncryptedKey.prepareInternal(WSSecEncryptedKey.java:205)
     [java]     at org.apache.ws.security.message.WSSecEncrypt.prepare(WSSecEncrypt.java:259)
     [java]     at org.apache.rampart.builder.AsymmetricBindingBuilder.doSignBeforeEncrypt(AsymmetricBindingBuilder.java:578)
     [java]     ... 14 more
     [java] Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/OAEPPadding
     [java]     at javax.crypto.Cipher.getInstance(DashoA13*..)
     [java]     at org.apache.ws.security.util.WSSecurityUtil.getCipherInstance(WSSecurityUtil.java:777)
     [java]     ... 17 more

If so, you can following these steps below to overcome the issue.

Step 1

Copy the "bcprov-jdk15.jar" from <ESB_HOME>/repository/axis2/client/lib/ folder and paste it in <ESB_HOME>/repository/components/plugins folder.

Step 2

Download the Java Cryptography files for the specific java version you have installed from below.

Step 3

Take a backup of the <JRE_HOME>/lib/security folder and keep it somewhere safe as a safety precaution.

Step 4

Copy the files from the downloaded archive and paste them to <JRE_HOME>/lib/security folder.

By doing above steps I was able to run the sample without a hassle.

References...

Friday, June 26, 2015

Compiling and Running using Mono

Introduction...

Hi All,

Following are a basic set of "mono" commands that allows to run .NET code. These commands ran on MAC OS X.

Compiling code to an EXE

Here we are going to compile a C# code using "mcs" command that comes with Mono. Lets say we have the following code with the name of the file being "hello.cs".

using System;
 
public class HelloWorld
{
    static public void Main ()
    {
        Console.WriteLine("Hello Mono World");
    }
}

To compile the code to an EXE, run the following command.

mcs hello.cs

Running the above command would create an a file named "hello.exe". To execute the "hello.exe", run the following command.

mono hello.exe

The above should print the output as "Hello Mono World".

Now lets say there is an external DLL dependency file which is needed to compile the program successfully. To do so, run the following command.

mcs hello.cs -r:My.Reference.dll

Goodluck!!!

References...

Thursday, June 25, 2015

Configuring ActiveMQ to MySQL Database

Introduction...

Hi All,

By default, ActiveMQ broker uses KahaDB as the persistence message store. In the following post we will be configuring ActiveMQ to use a MySQL database.

Steps 1

Modify the "activemq.xml" file which is located at "<ACTIVEMQ_HOME>/conf/" folder. Here we have to add a bean which contains the connection details for the MySQL database and also have to indicate that we are gonna use MySQL as the message store.

Add the MySQL database connection information as below.

<beans .....>
    ......
    <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/activemq"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="poolPreparedStatements" value="true"/>
    </bean>
    ......
</beans>

Remove the KahaDB message store and add the MySQL message store.

<beans .....>
    <broker .....>
        ......
        <persistenceAdapter>
            <jdbcPersistenceAdapter dataSource="#mysql-ds"/>
            <!-- <kahaDB directory="${activemq.data}/kahadb"/> -->
        </persistenceAdapter>
        ......
    </broker>
</beans>

Steps 2

Add the MySQL driver to "<ACTIVEMQ_HOME>/lib/optional/" folder

And thats all folks.

References...

[1] - http://activemq.apache.org/how-to-configure-a-new-database.html
[2] - http://activemq.2283324.n4.nabble.com/ActiveMQ-4-0-won-t-start-td2343357.html

Sunday, May 24, 2015

Creating a New User in Cassandra Database

Introduction...

Hi All,

Recently I was working on user authentication of DSE(DataStax Enterprise[1]) Cassandra and I noticed that any username and password allowed me to access the cassandra cluster. So I looked into the docs and saw that there are several authenticators[2].

By default the used authenticator is the "org.apache.cassandra.auth.AllowAllAuthorizer" authenticator which allowed access with any username and password. So to create a user and grant permissions, you can follow these steps...

Steps...

Shutdown the Cassandra cluster(can use kill -9) if it is already running and the modify the cassandra.yaml file. If you are using DSE cassandra, it will reside in "<DSE_HOME>/resources/cassandra/conf/cassandra.yaml". Else if you are using Apache Cassandra, it will reside in "<APACHE_CASSANDRA_HOME>/conf/cassandra.yaml".

Modify the file in a way that "org.apache.cassandra.auth.AllowAllAuthorizer" authenticator is commented and add "org.apache.cassandra.auth.PasswordAuthenticator" authenticator[3].
#authenticator: AllowAllAuthenticator
authenticator: PasswordAuthenticator

By default, Cassandra has a user with username as "cassandra" and password as "cassandra" which can be used to create a new user using "cqlsh" tool[4]. Login using the "cqlsh" tool by executing the following command.

./cqlsh localhost -u cassandra -p cassandra

After the cqlsh console is opened, to get the current list of users run the following command.

list users;

To create a new user, run the following command. Here the username is "myUserName" and the password is "myPassword".

CREATE USER myUserName WITH PASSWORD 'myPassword' SUPERUSER;

The above user will get created with superuser privileges. The newly created user should be there when running the "list users;" command again.

Goodluck!!!

References...

[1] - http://www.datastax.com/products/products-index
[2] - http://docs.datastax.com/en/cassandra/1.2/cassandra/security/secure_config_native_authorize_t.html
[3] - http://www.datastax.com/dev/blog/a-quick-tour-of-internal-authentication-and-authorization-security-in-datastax-enterprise-and-apache-cassandra
[4] - http://docs.datastax.com/en/cassandra/1.2/cassandra/security/security_config_native_authenticate_t.html

Wednesday, May 20, 2015

View Hazelcast Management Center for WSO2 Message Broker 3.0.0.

WSO2 products allows to use multiple instances to form a cluster and divide up the work to enhance the performance and reliability of their intended processes[1]. WSO2 Message Broker 3.0.0 uses Hazelcast[2] to communicate within the instances of a cluster and is heavily depended on on it when it comes message delivering scenarios and etc. The following post is on how to access the hazelcast management center[3] of the Message Broker(s). The hazelcast management center helps to monitor the overall state of the cluster.

Lets see how we can make it happen. The following prerequisites are needed :
  • A WSO2 Message Broker cluster(clustering enabled).
  • Tomcat to host hazelcast management center.

First we have to modify the axis2.xml file of all the instances. Access the "axis2.xml" file in the following path.
<WSO2_PRODUCT>/repository/conf/axis2/axis2.xml
At the bottom of the file in the "clustering" section, the following commented line can be seen.
<!--<parameter name="mgtCenterURL">http://localhost:8081/mancenter/</parameter>-->
Uncomment the line and modify the port to 8080. This is the default port in tomcat which we will get back later.
<parameter name="mgtCenterURL">http://localhost:8080/mancenter/</parameter>
Startup the Message Broker instance(s) afterwards.

Now lets host the hazelcast management center.

To do this we need to get the "mancenter-3.2.6.war" file from the hazelcast official site. The version of the war file should be the exact hazelcast version that is being used in WSO2 Message Broker. I.E current version is 3.2.6

Download Hazelcast - The war file is included.

After downloading, place the "mancenter-3.2.6.war" file in the "<TOMCAT_HOME>/webapps" folder and rename the war file to "mancenter.war". Start up the tomcat server using "./startup.sh" in the "bin" folder of tomcat.

Then go to the following URL :
http://localhost:8080/mancenter/

Login to the hazelcast management center using username and password as "admin" and "admin". Once logged in, you should see the hazelcast management center.

Following are several images of the hazelcast management center.


This mentioned solution should also work with other WSO2 products which uses hazelcast for clustering. But I have not tested them out. I do not guarantee this solution will work or held responsible.

[1] - https://docs.wso2.com/display/CLUSTER420/Overview
[2] - http://hazelcast.org/
[3] - http://docs.hazelcast.org/docs/latest/manual/html/managementcenter.html

Thanks,
Hemika

Unable to mount ExFat external HDD (' stderr: ERROR:._.com.apple.timemachine.donotpresent' has invalid checksum)

Hi All,

I have an external hard drive(Western Digital My Passport Ultra 1TB) which I keep all my movies, work stuff and etc. Initially I formatted the drive in ExFat format in Mac OS X platform so that I can use it in Windows and Mac. Also Ubuntu seems to have a fix to read ExFat file systems[1].

But recently when I connected this external hard drive to my Ubuntu machine, it gave me this error(something similar) saying that it cannot mount the disk: 
Error mounting /dev/sdb1 at /media/rbuse/My Passport: Command-linemount -t "exfat" -o "uhelper=udisks2,nodev,nosuid,uid=1000,gid=1000,io charset=utf8,namecase=0,e rrors=remount-ro,umask=0077" "/dev/sdb1" "/media/rbuse/My Passport"' exited with non-zero exit status 1: stdout:FUSE exfat 1.0.1

stderr: ERROR:._.com.apple.timemachine.donotpresent' has invalid checksum (0x6634 != 0x4044). '
I was stunned because all my work was in there, especially my VDIs. 

So I started my research and one solution was to plug it back in to my Mac OS X machine and use the "Disk Utility" tool to repair it(Use "Repair Disk" button) [2][3]. When I tried this, the following error came up.
Error: Disk Utility can’t repair this disk. Back up as many of your files as possible, reformat the disk, and restore your backed-up files.
I was like "Huh?!?!?" . I continued my research and found that if I attempt several times to "Repair Disk", it will work at sometime[4]. I tried like 20+ times, it didn't work.

So I googled more and found a command that should fix the broken external hard drive [5][6][7].
sudo fsck_exfat -d <device>
To find the correct value for <device>(eg : disk1s2) use the following command[8] to list down the device IDs.
diskutil list
At the end of the fsck_exfat command, I got the following error[9]. 
fsck_exfat: Could not update main boot region: Bad file descriptor
Sigh!.

As the last attempt to fix, I used the "chkdsk" command of Windows OS[10]. And IT WORKED!!. I ran the following command. It took sometime to complete the task. So be patient.
chkdsk /f <Volume_ID>
Here the Volume_ID refers "C:" or "D:" of the corrupted hard disk. eg : 
chkdsk /f D:

Hope this article helped!,

Thanks,
Hemika

[1] - http://askubuntu.com/questions/451364/how-to-enable-exfat-in-ubuntu-14-04
[2] - https://bbs.archlinux.org/viewtopic.php?pid=1481063#p1481063
[3] - http://osxdaily.com/2014/01/27/verify-disk-command-line-mac-os-x/
[4] - http://beshoy.girgis.us/2013/11/solved-error-disk-utility-cant-repair-disk-backup-many-files-possible/
[5] - https://discussions.apple.com/message/24958301#24958301
[6] - http://craigsmith.id.au/2014/07/06/repairing-a-corrupted-mac-osx-exfat-partition/
[7] - https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/fsck_exfat.8.html
[8] - https://discussions.apple.com/thread/4154638?start=30&tstart=0
[9] - https://discussions.apple.com/thread/4154638?start=45&tstart=0
[10] - https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/chkdsk.mspx?mfr=true

Thursday, April 9, 2015

Changing Version Control in IntelliJ

IntelliJ supports different types of version controlling methods. I recently had an issue of changing the version control for a project in IntelliJ. IntelliJ was showing an SVN version control, but it was not picking up the GIT version control even if all the git files were there. The solution was to edit the ".idea/vcs.xml" file.

The content was as follows.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
    <component name="VcsDirectoryMappings">
        <mapping directory="$PROJECT_DIR$" vcs="svn" />
    </component>
</project>
I modified it to the following.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
    <component name="VcsDirectoryMappings">
        <mapping directory="$PROJECT_DIR$" vcs="Git" />
    </component>
</project>
Then I reopened the project in IntelliJ and was able to see the GIT sub menu in the VCS menu.

The content in the vcs.xml file might be different depending on the project.

Recently one of my colleagues was having the same issue. In his vcs.xml file, There were several mappings all directed to "svn". We changed to "Git" and reopened the project. An error notification started appearing at the top right corner mentioning a version control error and there was a link as "configuration". Once clicking the link it showed the "Version Control" of the project. We removed the broken mappings and added a new mapping with GIT. We checked the vcs.xml file afterwards, it was as following.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
    <component name="VcsDirectoryMappings">
        <mapping directory="" vcs="Git" />
    </component>
</project>
The Git menu also came up in the VCS menu in IntelliJ.

Please note that I haven't tested with other version control methods in IntelliJ. This is just sharing hick-ups I had during coding. :)

Sunday, April 5, 2015

Open Sublime in Terminal for Mac OSX

Even though we install Sublime 2 or 3 using a DMG file in Mac OSX, we cannot open Sublime using the terminal. To fix this, run the following...

For Sublime Text 2

cd /usr/local/bin
ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" subl

For Sublime Text 3

cd /usr/local/bin
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" subl

To open Sublime in terminal, use the following command.

subl 
To see usages of subl command

subl --help

References

  • https://www.sublimetext.com/docs/3/osx_command_line.html
  • http://stackoverflow.com/questions/11889484/command-subl-from-terminal-dont-work
  • https://bensmann.no/open-folder-in-sublime/

Wednesday, March 25, 2015

Creating Runnable JAR using Maven.

At times we need to create a runnable jar using maven. So that we can run it as following.

java -jar MyJar.jar

This can be achieved using 2 different ways as far as I have learnt.

  1. Using "maven-shade-plugin" Plugin of Maven
  2. Using "maven-jar-plugin" Plugin of Maven

Using "maven-shade-plugin" Plugin of Maven

This might be the easiest way of creating a jar file of your java program. Simply add the following plugin to pom.xml.

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.hemika.samples.MyMainClass</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Here we have to add the reference path of the class which contains the main method to the "mainClass" element in the xml.

Sometimes we might encounter the following exception.

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

The reason for this is as follows...

" The reason java.lang.SecurityException is raised is because some dependency jar files are signed jar files. A jar file is signed by using jarsigner, which creates 2 additional files and places them in META-INF: a signature file, with a .SF extension, and a signature block file, with a .DSA, .RSA, or .EC extension. Since the uber-jar file is created, the signatures and integrity of signed JAR files are no longer valid. When the uber-jar file is executed, java.lang.SecurityException is thrown. " - maven shade plugin: Invalid signature file digest for Manifest main attributes

The solution is it add the following filter.

<configuration>
 <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
 </filters>
</configuration>

To build the jar, run the following command...

mvn clean install

...and thats all for making a shaded jar.

Click here to see a sample.

Using "maven-jar-plugin" Plugin of Maven

Using this way requires to use 2 maven plugins. After executing "mvn clean install", the target folder will contain the jar file and also a "lib" folder. The "lib" folder will contain all the dependencies. This way is more advantageous as the depending library files are available from outside of the jar. Later if we need to replace the dependency jar with a new version or so, this will become useful.

Add the following to the pom.xml.

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
 <artifactId>maven-dependency-plugin</artifactId>
 <executions>
   <execution>
     <phase>install</phase>
     <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
       <outputDirectory>${project.build.directory}/lib</outputDirectory>
     </configuration>
   </execution>
 </executions>
      </plugin>
      <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-jar-plugin</artifactId>
 <configuration>
   <archive>
            <manifest>
       <addClasspath>true</addClasspath>
       <useUniqueVersions>false</useUniqueVersions>
       <classpathPrefix>lib/</classpathPrefix>
       <mainClass>com.hemika.samples.Main</mainClass>
            </manifest>
          </archive>
 </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

The class that has the main method needs to be added at the "mainClass" node in the xml.

To build the jar, run the following command...

mvn clean install

...and thats all for creating a runnable jar.

Click here to see a sample.

References...

Monday, March 23, 2015

Use a Config File for Easy SSH Connections.

Problem ?

When working with Amazon EC2 instances its pretty annoying to connect to the instances using SSH. All the time we have to give the path to the pem file, the username of the EC2 instances, the public DNS for the instance. This is pretty much annoying.

$ ssh -i Key.pem ubuntu@ec2-11-11-11-11.xx-xxxx-xx.compute.amazonaws.com

But there is an easier way that you can store all those variables in a config file an use it to connect the instances from any folder.

Solution

Go to the root directory using "CD".

$ cd

Go the to ".ssh" folder. This folder might already be there.

$ cd .ssh/

Create a file named "config" using an editor. I'll be using VIM.

$ vim config

The config file should contain all our variables for the EC2 instances or where ever you are connecting to. Lets say we have the following EC2 instances.

ID Username Public DNS PEM File Path
instance1 ubuntu ec2-11-11-11-11.xx-xxxx-xx.compute.amazonaws.com ~/.ssh/pems/Key.pem
instance2 ubuntu ec2-22-22-22-22.xx-xxxx-xx.compute.amazonaws.com ~/.ssh/pems/Key.pem
instance3 ubuntu ec2-33-33-33-33.xx-xxxx-xx.compute.amazonaws.com ~/.ssh/pems/Key.pem

In the above table, the ID is a unique value for each instance. You can use any value for the ID. Username is the login username for the instance. Public DNS is the host name for the instance. The PEM File Path is the file path for the identity file.

Using those data we can create the config file content as follows.

Host instance1
    User ubuntu
    Hostname ec2-11-11-11-11.xx-xxxx-xx.compute.amazonaws.com
    IdentityFile ~/.ssh/pems/Key.pem

Host instance2
    User ubuntu
    Hostname ec2-22-22-22-22.xx-xxxx-xx.compute.amazonaws.com
    IdentityFile ~/.ssh/pems/Key.pem

Host instance3
    User ubuntu
    Hostname ec2-33-33-33-33.xx-xxxx-xx.compute.amazonaws.com
    IdentityFile ~/.ssh/pems/Key.pem

After saving the "config" file, you can connect to any of those instances from any folder. If you want to connect to "instance2", it will be as follows.

$ ssh instance2

Happy SSH-ing!

Saturday, March 21, 2015

How to run a WSO2 MB 3.0.0 Cluster Locally.

Introduction

WSO2 Message Broker 3.0.0 is a distributed message broker that supports AMQP and MQTT. This post explains the necessary steps to run several instances of WSO2 MB(Message Broker) on a single machine(locally) with the minimum number of configurations. WSO2 MB comes with an inbuilt H2 database which acts as the message store. When configuring for a cluster, the existing H2 database cannot be used. This is because the H2 database cannot work as a centralized database. We can use MSSQL, MySQL or Oracle as the message store. Since we only need to do a quick setup, we will use MySQL. Make sure you have already installed MySQL in your machine

Configuring MySQL for the Message broker

Creating the MySQL database

Create a database in MySQL with the name "wso2_mb". One way of creating the database is through the MySQL command line.

mysql -uroot -proot; #To login to MySQL console
CREATE DATABASE wso2_mb; #Creating the database

Configuring WSO2 Message Broker 3.0.0

Lets take a scenario where we need to setup a 3 node cluster. Following are the files that needs to be configured.

1. axis2.xml
2. broker.xml
3. carbon.xml
4. master-datasources.xml

axis2.xml

The axis2.xml file resides in "<MB_HOME>/repository/conf/axis2" folder. The axis2.xml file needs to be configured in all 3 MB instances.

1. First we have to enable clustering mode for the node.
<clustering class="org.wso2.carbon.core.clustering.hazelcast.HazelcastClusteringAgent" enable="true">
2. Change the "membershipScheme" parameter to "wka".
<parameter name="membershipScheme">wka</parameter>
3. Add the IP address of the machine to "localMemberHost" parameter. All 3 nodes will have the same IP address as it is running locally.
<parameter name="localMemberHost">192.168.0.100</parameter>
4. Next step is assigning the value for "localMemberPort" parameter. As all the nodes are running locally, we need to provide them with different ports. Lets assume that following ports are assigned.
mb1 - 4000
mb2 - 4001
mb3 - 4002
So in mb1, set "localMemberPort" parameter value as 4000. This value is set by default. But for mb2 and mb3 the value should be 4001 and 4002 respectively. For example, mb2 will have the following value.
<parameter name="localMemberPort">4001</parameter>
5. The next step is to add the members. When adding the members, we have to add the IP addresses and assigned ports of the nodes except for the current node. For mb1, the following members should be added.
<members>
 <member>
  <hostName>192.168.0.100</hostName>
  <port>4001</port>
 </member>
 <member>
  <hostName>192.168.0.100</hostName>
  <port>4002</port>
 </member>
</members>
For mb2, it will be as follows.
<members>
 <member>
  <hostName>192.168.0.100</hostName>
  <port>4000</port>
 </member>
 <member>
  <hostName>192.168.0.100</hostName>
  <port>4002</port>
 </member>
</members>

broker.xml

The broker.xml file resides in "<MB_HOME>/repository/conf/" folder. The broker.xml file needs to configured in all 3 MB instances.

1. Modify "thriftServerHost" value to the IP address.
<thriftServerHost>192.168.0.100</thriftServerHost>
2. Change the "bindAddress" value to the IP address.
<bindAddress>192.168.0.100</bindAddress>

carbon.xml

The carbon.xml file resides in "<MB_HOME>/repository/conf/" folder. The carbon.xml file needs to be configured in all 3 MB instances. This is to avoid the nodes from using the same ports.

1. Set the "offset" value to a unique value in each node. Assume that following are the assigned offset values.
mb1 - 0
mb2 - 1
mb3 - 2
For example, mb2 will have the following value.
<Offset>1</Offset>

master-datasources.xml

The master-datasources.xml file resides in "<MB_HOME>/repository/conf/datasources" folder. The master-datasources.xml file needs to be configured in all 3 MB instances. In this file we will be configuring the WSO2 registry database configuration and the WSO2 MB message store database configuration.

1. Fix WSO2 MB registry's url path to H2 database. In the master-datasources.xml, we can see the following section.
<datasource>
    <name>WSO2_CARBON_DB</name>
    <description>The datasource used for registry and user manager</description>
    <jndiConfig>
        <name>jdbc/WSO2CarbonDB</name>
    </jndiConfig>
    <definition type="RDBMS">
        <configuration>
            <url>jdbc:h2:repository/database/WSO2CARBON_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</url>
            <username>wso2carbon</username>
            <password>wso2carbon</password>
            <driverClassName>org.h2.Driver</driverClassName>
            <maxActive>50</maxActive>
            <maxWait>60000</maxWait>
            <testOnBorrow>true</testOnBorrow>
            <validationQuery>SELECT 1</validationQuery>
            <validationInterval>30000</validationInterval>
        </configuration>
    </definition>
</datasource>
For the "url" value we can see the default value as "repository/database/WSO2MB_DB". We have to give the absolute file path for the H2 database. Since each node has their own H2 database, this needs to be done is all MB nodes with their specific paths. Performing this change would imply that each node has their own registry and that it is not shared. Lets say that my MB nodes resides in the following paths.
mb1 - /home/hemikak/Documents/wso2/mb1/
mb2 - /home/hemikak/Documents/wso2/mb2/
mb3 - /home/hemikak/Documents/wso2/mb3/
So for mb1, it should be as follows...
<datasource>
    <name>WSO2_CARBON_DB</name>
    <description>The datasource used for registry and user manager</description>
    <jndiConfig>
        <name>jdbc/WSO2CarbonDB</name>
    </jndiConfig>
    <definition type="RDBMS">
        <configuration>
            <url>jdbc:h2:/home/hemikak/Documents/wso2/mb1/repository/database/WSO2CARBON_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</url>
            <username>wso2carbon</username>
            <password>wso2carbon</password>
            <driverClassName>org.h2.Driver</driverClassName>
            <maxActive>50</maxActive>
            <maxWait>60000</maxWait>
            <testOnBorrow>true</testOnBorrow>
            <validationQuery>SELECT 1</validationQuery>
            <validationInterval>30000</validationInterval>
        </configuration>
    </definition>
</datasource>
2. Next step is to comment the current H2 database connection configuration for the messages store. Comment the "WSO2 MB embedded H2 Store" section.
<!-- WSO2 MB embedded H2 Store     -->
<datasource>
    <name>WSO2_MB_STORE_DB</name>
    <description>The datasource used for message broker database</description>
    <jndiConfig>
        <name>WSO2MBStoreDB</name>
    </jndiConfig>
    <definition type="RDBMS">
        <configuration>
            <url>jdbc:h2:repository/database/WSO2MB_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</url>
            <driverClassName>org.h2.Driver</driverClassName>
            <maxActive>50</maxActive>
            <maxWait>60000</maxWait>
            <testOnBorrow>true</testOnBorrow>
            <validationQuery>SELECT 1</validationQuery>
            <validationInterval>30000</validationInterval>
        </configuration>
    </definition>
</datasource>
3. Next step is to point the WSO2 MB message store to the MySQL database we configured earlier. Uncomment the "MySQL data source" section.
<datasource>
     <name>WSO2_MB_STORE_DB</name>
     <jndiConfig>
         <name>WSO2MBStoreDB</name>
     </jndiConfig>
     <definition type="RDBMS">
         <configuration>
             <driverClassName>com.mysql.jdbc.Driver</driverClassName>
             <url>jdbc:mysql://localhost/wso2_mb</url>
             <username>root</username>
             <password>root</password>
             <maxActive>50</maxActive>
             <maxWait>60000</maxWait>
             <minIdle>5</minIdle>
             <testOnBorrow>true</testOnBorrow>
             <validationQuery>SELECT 1</validationQuery>
            <validationInterval>30000</validationInterval>
         </configuration>
     </definition>
 </datasource>
4. Now we have to copy the MySQL driver to "<MB_HOME>/repository/components/lib" folder. You can get the driver from here.

Starting up WSO2 Message Broker 3.0.0

1. Open up the terminal or command prompt in the "<MB_HOME>/bin" folder and run the following command based on the OS.
./wso2server.sh -Dsetup
or
./wso2server.bat -Dsetup

Troubleshooting

1. "Address is already in use". This is because there are nodes that use the same "Offset" value in carbon.xml.
Exception during startup: org.wso2.andes.transport.TransportException: Could not bind to /192.168.0.100:5672
org.wso2.andes.transport.TransportException: Could not bind to /192.168.0.100:5672
        at org.wso2.andes.transport.network.mina.MinaNetworkTransport.accept(MinaNetworkTransport.java:147)
        at org.wso2.andes.server.Broker.startupImpl(Broker.java:254)
        at org.wso2.andes.server.Broker.startup(Broker.java:108)
        ...
        ...
Caused by: java.net.BindException: Address already in use
        at sun.nio.ch.Net.bind0(Native Method)
 at sun.nio.ch.Net.bind(Net.java:444)
 at sun.nio.ch.Net.bind(Net.java:436)
 at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
2. "Cannot access the registry". This happens when the WSO2 MB registry database is not properly configured in master-datasources.xml file.
[2015-03-21 10:06:29,050] ERROR {org.wso2.carbon.event.core.internal.builder.EventBrokerHandler} -  Can not create the event broker
org.wso2.carbon.event.core.exception.EventBrokerConfigurationException: Cannot get the subscriptions 
        at org.wso2.carbon.event.core.internal.CarbonEventBroker.loadExistingSubscriptions(CarbonEventBroker.java:90)
        at org.wso2.carbon.event.core.internal.CarbonEventBroker.init(CarbonEventBroker.java:67)
        at org.wso2.carbon.event.core.internal.CarbonEventBrokerFactory.getEventBroker(CarbonEventBrokerFactory.java:90)
        ...
        ...
Caused by: org.wso2.carbon.event.core.exception.EventBrokerException: Cannot access the registry 
        at org.wso2.carbon.event.core.internal.subscription.registry.RegistrySubscriptionManager.getAllSubscriptions(RegistrySubscriptionManager.java:291)
        at org.wso2.carbon.event.core.internal.CarbonEventBroker.loadExistingSubscriptions(CarbonEventBroker.java:78)
        ... 103 more
Caused by: org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException: Resource does not exist at path /_system/governance/event/topicIndex
        at org.wso2.carbon.registry.core.jdbc.EmbeddedRegistry.get(EmbeddedRegistry.java:532)
        at org.wso2.carbon.registry.core.caching.CacheBackedRegistry.get(CacheBackedRegistry.java:172)
        at org.wso2.carbon.registry.core.session.UserRegistry.getInternal(UserRegistry.java:613)
        at org.wso2.carbon.registry.core.session.UserRegistry.access$400(UserRegistry.java:60)
        at org.wso2.carbon.registry.core.session.UserRegistry$5.run(UserRegistry.java:596)
        at org.wso2.carbon.registry.core.session.UserRegistry$5.run(UserRegistry.java:593)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.wso2.carbon.registry.core.session.UserRegistry.get(UserRegistry.java:593)
        at org.wso2.carbon.event.core.internal.subscription.registry.RegistrySubscriptionManager.getAllSubscriptions(RegistrySubscriptionManager.java:266)
        ... 104 more

Hope you got the cluster up and running.