Archive for the Category »java «

Oct
21
Handbook issued to passengers on Campania

Image via Wikipedia

We needed different log files for a common web project, this gateway project simply loads web services dynamically and generates the wsdd files at runtime, but the problem was huge size of the common log file used by all services. So we needed different log files for different services.

Here is the step by step solution to separate&customize the log files:

  • The first step is adding a filter to web.xml file so we can set the service names:
    <filter>
    <filter-name>WSLoggerFilter</filter-name>
    <filter-class>com.logger.WSLoggerFilter</filter-class>
    <description></description>
    </filter>
    
    <filter-mapping>
    <filter-name>WSLoggerFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  • Here is the simple filter – WSLoggerFilter.java:
    public final class WSLoggerFilter implements Filter {
    private static final String SERVICE_NAME = "serviceName";
    
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    String[] url = ((HttpServletRequest)request).getRequestURI().split("/");
    MDC.put(SERVICE_NAME, url != null ? url[url.length - 1] : "");
    // Forward the request to the next resource in the chain
    chain.doFilter(request, response);
    MDC.remove(SERVICE_NAME);
    }  .....
    
  • The next step is adding a custom adapter that uses the MDC “serviceName” parameter to separate the log files, we just alter the name of the default logging file by adding the serviceName parameter we set at the filter. We add the custom adapter to the log4j.properties file:
    # custom file appender
    log4j.appender.F=com.logger.WSLoggerAppender
    log4j.appender.F.mdcName=serviceName
  • Next step is implementing the custom appender. I used DailyRollingFileAppender to change the name of the logging file, simply by adding service name. Simply do not forget to override close method to close all the appenders we used. The custom adapter file WSLoggerAppender.java:
    public class WSLoggerAppender extends DailyRollingFileAppender {
    private String mdcName;
    
    private Map<string, dailyrollingfileappender=""> map = new HashMap<string, dailyrollingfileappender="">();
    
    @Override
    public synchronized void doAppend(LoggingEvent event) {
    Object key = MDC.get(getMdcName());
    if (key == null) {
    super.doAppend(event);
    return;
    }
    String serviceName = key.toString();
    if (serviceName != null &amp;&amp; serviceName.length() > 0 &amp;&amp; !map.containsKey(serviceName)) {
    try {
    DailyRollingFileAppender dailyRollingFileAppender = new DailyRollingFileAppender(layout, fileName + "." + serviceName, getDatePattern());
    map.put(serviceName, dailyRollingFileAppender);
    } catch (IOException e) {
    }
    }
    DailyRollingFileAppender ap = (DailyRollingFileAppender) map.get(serviceName);
    if (ap != null)
    ap.doAppend(event);
    else
    super.append(event);
    }
    
    @Override
    public void close() {
    for (Iterator iter = map.values().iterator(); iter.hasNext();) {
    DailyRollingFileAppender appender = (DailyRollingFileAppender) iter.next();
    appender.close();
    }
    super.close();
    }
    
    public void setMdcName(String mdcName) {
    this.mdcName = mdcName;
    }
    
    public String getMdcName() {
    return mdcName;
    }
    }

That was all!

Reblog this post [with Zemanta]
Dec
09

We started to work on some projects, and after getting the source code, we could not use the web projects in eclipse because they were not defined as Eclipse Dynamic Web projects. Here is a small trick to convert these general projects to Dynamic Web projects.

more…

Category: java  Tags:  Leave a Comment
Mar
18

I migrated Eclipse 3.2 to 3.3.1, at first I used the same workspace and I had some problems with myln plugins and the server did not start correctly. Then i tried with a new workspace and everything was smooth except at server startup:

Timeout waiting for Tomcat v5.5 Server at localhost to start. Server did not start after 45s.

You need to set server timeout delay = Unlimited from Window>Preferences>Server…

Reblog this post [with Zemanta]
Category: java  Tags:  Leave a Comment
Jan
20

I needed to merge the branch changes into trunk of the project, after many changes at both the branch and trunk…

First we have to commit all changes, in case of some failure at the merge operation we may revert all changes.

Select Team > Merge from menu, select the path of the branch and the revision. Revision number is selected from the Show Log, and is number when we created the branch.

svn_merge

Some useful links for SVN merge:

Eclipse SDK – Subversion Eclipse Plugin Manual

SVN Book

Category: java  Tags: ,  One Comment
Dec
18

I tried Maven plugins for code analysis and finding duplicate/similar code.

CPD-PMD’s Copy/Paste Detector can be used for finding duplicate code, which is included in PMD, a Java code analysis tool.
run mvn pmd:cpd ,
The report is displayed at cpd.html file, and the results are very efficient.

Simian – Similarity Analyser is also an alternative for finding duplicates.

Dec
18

We started code review using Crucible, so i searched for some code analysis maven plugins. PMD is a Java code analysis tool and used to find potential problems like unused code, duplicate code, unused variables… PMD is useful and easy:

pom.xml:


<reporting>
<outputdirectory>target/reporting/pmd</outputdirectory>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-pmd-plugin</artifactid>
</plugin>
</plugins>
</reporting>

more…

Dec
10

I need formatting java code snippets at my blogs so i use an eclipse plugin, simply you can copy html code to editor.

java2html Eclipse Plugin

I think the most practical way to format html and xml code snippets is using:
http://www.manoli.net/csharpformat

Thanks to Truong Hong Thi for sharing :)

Edit: I also found a brilliant wordpress plugin for formatting source code.
SyntaxHighlighter

Easy to use, just add source language=’java’ tag :)


public class FileComponent implements Serializable{
private String fileName;
private String contentType;
private int size;
private String localPath;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

}
Category: java  Tags: ,  Leave a Comment
Nov
27

I upgraded maven 2.0.4 to 2.0.7 and the i couldn’t compile the existing projects because of filtering problem – filtering files couldn’t be read, the source of problem was maven-assembly-plugin. I think such kind of incongruities generally may occur. The simplest solution of such problems is simply renaming the maven folder ….m2repositoryorgapache and installing the project again for full download of all maven and maven plugins :)

The details of the problem and the solution is below:

http://jira.codehaus.org/browse/MASSEMBLY-178

Category: java  Tags: ,  Leave a Comment
Nov
27

Using Tomcat 5.5.17 after starting application, jsp pages couldn’t be rendered and the following error is taken with an empty page. To fix the problem simply check the commons-el.jar versions and also remove geronimo-spec-jsp.jar from WEB-INFlib directory if this jar is not already excluded:

java.lang.VerifyError: (class: org/apache/jasper/runtime/PageContextImpl, method: getExpressionEvaluator signature: ()Ljavax/servlet/jsp/el/ExpressionEvaluator;) Wrong return type in function
at org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:99)
at org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:61)
at org.apache.jsp.pages.P60.Common.StandardPageParentTemplate_jsp._jspService(StandardPageParentTemplate_jsp.java:87)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Category: java  Tags: ,  Leave a Comment