Chitika

Tuesday, December 30, 2014

Failed to read candidate component class

I was having following error,

Failed to read candidate component class: file [/a/b/c/z$1.class]; nested exception is java.lang.SecurityException: Prohibited package name: java.lang: Prohibited package name: java.lang applicationContext-jpa.xml /im4/src/main/resources/META-INF/spring line 5 Spring Beans Problem

When clicked on it, it opens applicationContext-jpa.xml, with line <repositories base-package="a.b.c" />

Looking into the code of z.java, it is found out that it is util class having package a.b.c, which spring scans for beans.

Changing the package to a.b.z resolves the issue.

Thursday, November 20, 2014

hibernate reverse engineering database - composite key, no many-to-many relationship

I was trying to generate hibernate code through hibernate tools but I was getting issue with association table being created as entity and another id entity was created, and many-to-many relationship was not recognized.

Most of the places, it is written that for many-to-many relationship, association table must have just two foreign keys and no other field. But they forget to mention that these two fields but be the composite primary key of that table.

By setting the foreign keys as composite primary key, hibernate successfully recognized it as many-to-many table along with neither entity of association table is created nor id entity was created. It works like a charm.

Monday, October 27, 2014

Windows ffmpeg split mp3 file into pieces

The other day I was listening a long (more than an hour long) audio file on car stereo using memory stick. I had to stop in between playing, after reaching destination. When I again take the driving seat, the recording started from start. I came to know that there is no memory feature in most of car stereos for storing last playback position.

The solution come to my mind is to split the recording into 10 minutes pieces each. I used following commands for it:

for /L %i IN (1, 600, 9999) DO (ffmpeg -ss %i -i recording.mp3 -c copy -t 600 recording-%i.mp3)

Where 600, is the pieces playback time, i.e. 10 minutes * 60 seconds = 600
9999, is the total playback time in seconds, keep it extra past the playback time, say if your playback time is 1 hour 30 minutes, set it to 1 = 60 mins + 30 minutes = 90 mins (add extra 10 minutes, make it 100 minutes), 100 * 60 = 6000, you can use 6000.

sources:
http://superuser.com/questions/525210/splitting-an-audio-file-into-chunks-of-a-specified-length
http://serverfault.com/questions/59014/for-loop-counting-from-1-to-n-in-a-windows-bat-script

Friday, August 1, 2014

Linux interface Bonding

Bonding is a tool/technique in linux to implement network load balancing or fault tolerance.


Install module

apt-get install ifenslave-2.6


Load module with options

modprobe bonding mode=active-backup miimon=100 updelay=200 downdelay=200

active-backup or 1 mode for fault tolerance
balance-rr or 0 (default) mode for load balance


Enlist interfaces for balancing/fallback

ifenslave bond0 wlan0 eth0


Obtain ip on bond0

dhclient bond0
or
ifconfig bond0 192.168.1.5


Remove bond0 interface

rmmod bonding

Thursday, April 3, 2014

assign IPv6 address

sudo ifconfig eth0 inet6 add 2001:0db8:100:1:2:3:4:5/48

Wednesday, January 29, 2014

easy grails + spring module integration

Dependencies:

Enable pom for maven dependency resolving:

In BuildConfig.groovy, before repository, set pom to true, as:

 pom true
    repositories {
// ...
mavenRepo "http://repository.codehaus.org"
//...

make sure maven repository path for spring module is enabled.

create pom.xml at same directory level as grails-app, at root folder, with contents as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ANY_GROUP_ID_DOESNT_MATTER</groupId>
    <artifactId>ANY_PROJECT_ID_DOESNT_MATTER</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>
    </dependencies>
</project>

Make sure module version matches with spring version comes package with grails, you may know by creating war, i.e. grails war, and extracting the war in folder and see file names of spring jars package in war.

Spring Beans:

Create resources.xml, at grails-app/conf/spring/, and define spring beans, as done in any normal spring application (beans.xml):

<beans xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.apache.activemq.command.ActiveMQQueue" id="destinationQueue">
<constructor-arg value="TEST.Q1"></constructor-arg>
</bean>

<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="connectionFactoryQueue">
<property name="brokerURL">
<value>nio://localhost:61616</value>
</property>
</bean>

<bean class="org.springframework.jms.core.JmsTemplate" id="producerTemplate">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="defaultDestination" ref="destinationQueue"></property>
</bean>

<bean class="com.test.activemq.MessageSender" id="activeMQMsgSender">
<property name="jmsTemplate" ref="producerTemplate"></property>
</bean>

<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"
id="jmsContainer">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="destination" ref="destinationQueue"></property>
<property name="messageListener" ref="messageReceiverService"></property>
</bean>

</beans>

Above bean configuration connects to activemq at transport "nio://localhost:61616/", and send/receive message on queue "TEST.Q1"

Message Sender:

Create message sender bean, MessageSender.java, in src/java/com/test/activemq/, with following content:
(Note: its declaration in bean configuration file, resources.xml, above)

package com.test.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class MessageSender {
protected JmsTemplate jmsTemplate;

public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void sendMessages(final String msg) throws JMSException {
System.out.println("PRODUCER [" + msg + "]");
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session
.createTextMessage(msg);
return message;
}
};
jmsTemplate.send(messageCreator);
}
}

Create controller ActiveMQController.groovy, as grails controller with action "send" as follows:
(Note: activeMQMsgSender bean usage, with related to MessageSender.java and resources.xml)

        def activeMQMsgSender
def send = {
def msg = params.msg
activeMQMsgSender.sendMessages(msg)
respond msg, model:[msg:msg]
}

Create corresponding view send.gsp as grails view with following content:
send [${msg}]

Message Receiver:

Create message receive bean, MessageReceiverService.goovy, as grails service as follows:
(please note, how it is specified in beans configuration file, resources.xml, above)

package com.test.activemq

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import grails.transaction.Transactional

@Transactional
class MessageReceiverService implements MessageListener {


public void onMessage(Message message) {
System.out.print("RECEIVER ");
TextMessage msg = (TextMessage) message;
try {
System.out.print("MESSAGE: [" + msg.getText() + "]");
} catch (JMSException e) {
e.printStackTrace();
}

System.out.println();
}
}

Testing

Run grails app for testing, sending message to queue.
On browser, run: localhost:8080/test/activeMQ/send?msg=abcd
which would place abcd text message on activemq, queue TEST.Q1
and, if everything went well, output on console will show sending message to queue and receiving message from queue.

(sources: http://grails.org/doc/latest/guide/spring.htmlhttp://icodingclub.blogspot.com/2011/07/introduction-of-spring-jms-with.html)

Friday, January 17, 2014

network scan for hosts

NMap utility is used to scan network for different criteria, such as all alive hosts on network can be get using following command:

nmap -sn 192.168.2.0/24

where network is 192.168.2.0/24, and -sn switch tells no ports to consider.

Friday, January 3, 2014

convert offline web to pdf

htmldoc --webpage -f example.pdf example_path/toc.html example_path/*.html
source: http://kmkeen.com/mirror/2009-02-05-14-00-00.html 

wget website subsection fetching

Following "wget" command can be used to fetch portion of website:


wget -r -l3 -k -p -I /docs,/static/images http://www.example.com/man/index.html

Where:
-r   : fetch recursively
-l3 : depth of 3, fist/index page is at depth 1
-k  : convert links to local links for offline viewing
-p  : prerequisites downloads for perfect page viewing (css/images)
-I   : comma seperated list of base path, to only fetch items containing uri starting from these base paths