[Maven – Build Error] Unsupported WTP Version



Hi,

recently I was working on a project and I found a build error while I tried to execute mvn -eclipse:eclipse command to build eclipse project settings for my maven project.

the solution for the above error message is specify maven-eclipse-plugin setting in your project’s pom file.

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-eclipse-plugin</artifactId>
      <version>2.5</version>
      <configuration>
         <wtpversion>${supported-wtp-version}</wtpversion>
      </configuration>
</plugin>

its all done 🙂 now you will be able to create eclipse project from your maven project.

 

How to use vimdiff as SVN DIFF tool



Hi,

in this post I am going to explain how to configure your svn-client to use vimdiff as default SVN diff tool.

when I work on my Linux machine, and I try to view changes done on a file before committing to SVN, I use SVN DIFF command to compare my changes.

it works cool for files having few changes, but whenever I make N number of changes to a large file, and I try to compare it using SVN DIFF its vary painful and time consuming task, but same will be easy if SVN show these changes graphically like VIMDIFF do. I always used vimdiff for comparing two files on my Linux machine.

that time I got the idea that, can we use VIMDIFF as SVN DIFF tool?

Answer is YES….

and here’s the solution:

create a file called svndiff.sh any where on your machine i.e. /home/girishgaurav/svndiff.sh

 

 

give it the execute permission using

chmod 777 /home/girishgaurav/svndiff.sh

now you done half way 🙂

now open svn-client’s config file i.e. /etc/subversion/config

 

now you all done 🙂

now your SVN DIFF command will use vimdiff as default diff-viewer.

 

Loading library/model classes from your views in CodeIgniter



Hi,

When I was new to CodeIgniter, I need some data manipulation using a library function in my view classes, I simply loaded the library and called the library function, as I always do….

$this->load->library(‘some_library’);

$this->some_library->some_library_function(some_parameters);

but when run this code, I surprised by the error:

Before this I had used library functions many times in the code, and it always works fine, and after checking the code thoroughly I didn’t find any mistake in the code.

So, what happened with that code?

then I realize that I always use the library functions in the Controllers, this is the first time I’m using it in the views.

then I found the problem! You can do almost everything in the view, even when you shouldn’t, but loading library, models etc. has to be done in the controller. After searching and reading many articles on the net I found a solution to use library functions anywhere in the application with the help of get_instance() method.

get_instance() method returns the CodeIgniter super object. this is used to access CodeIgniter native resources from within your custom classes. Normally, to call the codeigniter’s member function we use $this object i.e. $this->load->library(‘some_library_name’); or $this->load->helper(‘some_helper_name’);

but if you want to use your these functions from views, or any other custom class you have to use &get_instance() method to get the codeigniter’s object.

$ci_obj = & get_instance();
$ci_obj->load->library('some_library');
$ci->some_library->some_library_function();

or you can use the given helper function to load library from anywhere in the code:

function load_library($library_name) {

$ci = & get_instance();
$ci->load->library($library_name);
return $ci->$library_name;

}

it will return the library object and you can call any library function using that object in anywhere in the views or any other custom class.

 

Download/Attach source-code/java-docs with maven dependencies



I am using Maven in my projects from last couple of years, and the automatically downloading the Jars from repository feature of maven is really helpful for developers. But If you are using Eclipse and want to read/analyse Source Code or Java Doc of API then you need the jar file that contains the Source code and java doc of the API, but unfortunately maven does not download and attach the source code and java doc of the APIs automatically.

Maven provides some different ways to download and attach that source code and Java Doc:

  • Using maven eclipse plugin
  • Using maven dependency plugin

Note: The sources and javadocs of the libraries must exist in the repository so that the plugin can download it and attach it.

1. Maven eclipse plugin:

Maven dependencies that are deployed with the source and javadocs can be downloaded and attached to the Eclipse library by using maven-eclipse-plugin. It can be done by:

  • passing command-line argument to the maven-eclipse-plugin, or
  • by declaring in the pom.xml

1.1 passing command-line argument to maven-eclipse-plugin:

This example shows that how to do this by passing command line argument to the maven-eclipse-plugin:

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true

1.2 declaring in the pom.xml

This sample pom shows that how to declare downloadSources and downloadJavadocs configuration in pom.xml

<project>

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-eclipse-plugin</artifactId>

<configuration>

<downloadSources>true</downloadSources>

<downloadJavadocs>true</downloadJavadocs>

</configuration>

</plugin>

</plgins>

</build>

</project>

2. Maven dependency plugin:

maven-dependency-plugin provides a goal named sources that resolves the project source dependencies from the repository.

usage:

mvn dependency:sources

This is useful when you want the source attachments downloaded to your local repository and you don’t want to use the eclipse plugin to do this since the eclipse plugin creates/overwrites the eclipse files.

Configuring multiple Tomcat application servers with Apache HTTP server/ Load Balancing with Apache Web Server



In this article, I am going to explain how we can configure multiple Tomcat instances with single Apache Web Server.

There are many solutions posted on the web, but it is the simplest and easiest way to do this.

Prerequisite:

  • Apache HTTP Server 2.x
  • Tomcat 6.x
  • mod_jk.so module for Apache Web server
  • Any text editor, i.e. Notepad, gedit, etc.

if you do not have Apache HTTP server goto http://httpd.apache.org/ and download the appropriate version and install on your machine.

if you do not have Apache Tomcat Server goto http://tomcat.apache.org/ and download the appropriate version and install on your machine.

if you do not have mod_jk.so module, goto http://tomcat.apache.org/download-connectors.cgi and download the appropriate connector for Apache Web Server.

Step 1: Configure Tomcat

If you having one machine per Tomcat, you don’t need to do this. but if you don’t have, you have to configure each tomcat instance for different port to run successfully.

To configure Tomcat’s port, goto conf directory of any tomcat instance you have and open the server.xml file in the text editor.

search these lines in the server.xml and make the value of port property unique for each instance of tomcat.

<Server port=”8005″ shutdown=”SHUTDOWN”>

<Connector port=”8080″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443″ />

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />

Step 2: Creating configuration file of Tomcat instances for Apache Web Server

now open text editor and create a file called workers.properties and enter the information of all tomcat instances you have. and save it  under the conf directory of Apache.

A sample workers.properties is given:

# lists the workers by name

worker.list=tomcat1, tomcat2, loadbalancer

Tomcat1 configuration

worker.tomcat1.port=8009 //AJP1.3 Connector’s port

worker.tomcat1.host=localhost //IP of machine where tomcat1 is running

worker.tomcat1.type=ajp13 //AJP Connector type.

worker.tomcat1.lbfactor=100 //Load Balancing factor

Tomcat2 configuration

worker.tomcat2.port=8119 //AJP1.3 Connector’s port

worker.tomcat2.host=localhost //IP of machine where tomcat1 is running

worker.tomcat2.type=ajp13 //AJP Connector type.

worker.tomcat2.lbfactor=100 //Load Balancing factor

#Load Balance worker configuration

worker.loadbalancer.type=lb

worker.loadbalancer.balanced_workers=tomcat1,tomcat2

Step 3: Configure JK module

Copy the downloaded JK module file (mod_jk.so) to the modules directory of Apache Web Server. now goto conf directory of Apache Server, and open the httpd.conf file in the text editor.

and add these lines to the end of httpd.conf

LoadModule jk_module modules/mod_jk.so

JkWorkersFile conf/workers.properties

JkLogFile logs/mod_jk.log

JkLogLevel info

now you have done all the configuration, now you need to configure the JK Module to redirect the requests to specific tomcats. It can be done by the following:

To redirect specific request to specific Tomcat add this line to httpd.conf:

JkMount /abc/* tomcat1

or

JkMount /*.do tomcat2

Or, if you want to configure requests to be served by any of the tomcat then add this:

JkMount /*.jsp loadbalancer

if any of the tomcat is stopped then all the request will be served by other tomcat and if both the tomcat are running then the requests are redirected to tomcat servers according to the lbfactor specified in the workers.properties.

Now start all the Tomcat Servers and Apache Server, and its done.

 

Accessing Private Members of a Java class using Reflection



Somebody asked me that, “Is there any way of accessing private members of a Java Class?

Then I got the idea to write this article, because Java Reflection API provides a feature for accessing private members of a Java Class.

java.lang.reflect package provides the AccessibleObject class that is parent class of Constructor, Method, and Field class. Using the AccessibleObject class, we can change the access control flags of a Reflection Object (Reflection is a feature in the Java programming language. It allows an executing Java program to examine or “introspect” upon itself, and manipulate internal properties of the program. For example, it’s possible for a Java class to obtain the names of all its members and display them.), and the setAccessible(boolean) method is used to change the access control flag of an Object.

package test;

/**

  • @author girish.gaurav

*

*/

class DemoClass {

private int privateInt = 10;

private String privateString = “temp String”;

private long privateLong = 1234567890L;

private void resetFields(int i, String s, long l) {

privateInt = i;

privateString = s;

privateLong = l;

}

public void display() {

System.out.println(“Int = ” + privateInt);

System.out.println(“String = ” + privateString);

System.out.println(“Long = ” + privateLong);

}

}

class Test {

/**

* @param args

*/

public static void main(String[] args) {

DemoClass obj = new DemoClass();

obj.display();

Class clazz = obj.getClass();

try {

Field intField = clazz.getDeclaredField(“privateInt”);

Field strField = clazz.getDeclaredField(“privateString”);

Field longField = clazz.getDeclaredField(“privateLong”);

intField.setAccessible(true);

strField.setAccessible(true);

longField.setAccessible(true);

System.out.println(“value of privateInt field is : ” + intField.get(obj));

System.out.println(“value of privateString field is : ” + strField.get(obj));

System.out.println(“value of privateLong field is : ” + longField.get(obj));

intField.set(obj, 100);

strField.set(obj, “this is new string”);

longField.set(obj, 55555L);

obj.display();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

try {

Method method = clazz.getDeclaredMethod(“resetFields”, int.class, String.class, long.class);

method.setAccessible(true);

method.invoke(obj, 25, “new String from resetField method.”, 987654321L);

obj.display();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

}

References:

Java Reflection API

 

Introduction to Nonblocking Sockets



Here, I am going to explain what non-blocking sockets are, how it works, and where it can be useful, everything in details.

Preface:

Non-blocking sockets are introduced in Java 2 Standard Edition 1.4. It allows network communication between applications without blocking processes using the sockets.

A nonblocking socket allows input/output operation on a channel without blocking the processes using it. I’m talking about asynchronous high-performance read/write operations that, as you will see, turn upside-down the techniques for designing and developing socked-based applications.

Java developers who already working or worked on sockets might ask that, “Why we use a new technology to work on Sockets while we already have an old one that working fine? Or whats wrong with the traditional (Java 1.3.x) socket programming? and What are the advantage with new non-blocking socket API?”

Suppose we are going to implement a server application that accepts huge number of client connections and as well as, we want the server that can be able to process multiple requests simultaneously. If we use older socket programming to achieve this requirement we have two ways:

1. Implement a multithread server that manually handles a thread for each connection.
2. By using an external third-party module.

Both the given solution can work fine, but if we adopt first solution then the whole thread-management solution will be developed by the programmer and all the concurrency related issues will also be considered by the programmer. and the second solution may cost money. And by using Non-blocking sockets, you can implement a nonblocking server without directly managing threads or resorting to external modules.

Load more