Unix Command Line Productivity Tips

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

It happened with me while i was trying to extract a HTTPRequest parameter from an ActionInvocation object. I was writing an interceptor in Struts2 and was trying to check a request parameter. This is how you do it when you are in an interceptor:

public String intercept(ActionInvocation invocation) throws Exception {
    Map<String,Object> params  = invocation.getInvocationContext().getParameters();
    String param_var= params.get("parameter_name");
    /*
        Other stuff
    */
}

And it gave me following error:

 java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

According to the Java Doc it should return an Object and when i tried to debug it. I found that it is actually returning an array object. In some cases it may be correct because a request can contain an array against a key. HttpRequest gives two separate function for this but in this case it was only returning an array. Done following modifications to run the code :

public String intercept(ActionInvocation invocation) throws Exception {
    Map<String,Object> params  = invocation.getInvocationContext().getParameters();
    String[] paramArr_var= (String[])params.get("parameter_name");
    /*
        Other stuff
    */
}

Working with Maven Project in Eclipse

Maven helps the software project life cycle in many ways. One of the many attributes if it is that it integrates well with IDEs such as eclipse, Net beans and IntelliJ. I am trying to list certain things here which you can do while working with Maven managed project in Eclipse.

1. You definitely need to build your projects correctly in Eclipse and Maven can help you resolving the dependencies correctly so that you project can reference to its repository for building the project. You need to run following two commands:

2. Use m2eclipse plugin for managing for project. Visit m2eclipse.sonatype.org for more details related to installation, features etc.

Maven 2 – An overview

Adding values to Apache Logging – httpd

Apache HTTP server logs are source of very valuable information. Many trends, usage analysis can be extracted from these logs. Some times you may not get the information you require in the logs as it is not logged by the server like time taken by the request or port of the server serving the request. httpd made it quite simple to change its format of logging.

You can just go to the httpd configuration file and search for LogFormat and you will be able to see some c printf like code there. Generally it is like :

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 

It is the part of configuration of mod_log_config which is responsible for httpd logging. You can change the Log format to get the desired fields printed in log files. use following table to add more fields in your log file or check http://httpd.apache.org/docs/1.3/mod/mod_log_config.html:

%...a:          Remote IP-address
%...A:          Local IP-address
%...B:          Bytes sent, excluding HTTP headers.
%...b:          Bytes sent, excluding HTTP headers. In CLF format
        i.e. a '-' rather than a 0 when no bytes are sent.
%...c:          Connection status when response was completed.
                'X' = connection aborted before the response completed.
                '+' = connection may be kept alive after the response is sent.
                '-' = connection will be closed after the response is sent.
%...{FOOBAR}e:  The contents of the environment variable FOOBAR
%...f:          Filename
%...h:          Remote host
%...H       The request protocol
%...{Foobar}i:  The contents of Foobar: header line(s) in the request
                sent to the server.
%...l:          Remote logname (from identd, if supplied)
%...m       The request method
%...{Foobar}n:  The contents of note "Foobar" from another module.
%...{Foobar}o:  The contents of Foobar: header line(s) in the reply.
%...p:          The canonical Port of the server serving the request
%...P:          The process ID of the child that serviced the request.
%...q       The query string (prepended with a ? if a query string exists,
        otherwise an empty string)
%...r:          First line of request
%...s:          Status.  For requests that got internally redirected, this is
                the status of the *original* request --- %...>s for the last.
%...t:          Time, in common log format time format (standard english format)
%...{format}t:  The time, in the form given by format, which should
                be in strftime(3) format. (potentially localized)
%...T:          The time taken to serve the request, in seconds.
%...u:          Remote user (from auth; may be bogus if return status (%s) is 401)
%...U:          The URL path requested, not including any query string.
%...v:          The canonical ServerName of the server serving the request.
%...V:          The server name according to the UseCanonicalName setting.