Thursday, October 20, 2011

Log4j mixing log messages between applications into Weblogic


I have an Web Service JWS application running in Weblogic 10.2 and its logs were being mixing with another web application running in the same server.

I thought that applications were totaly independent in the server but they are not. They share some classloaders. See picture bellow:



This problem occurs because jog4j.jar is shared between applications and it is retrieved from the same classloader.

In order to solve this issue, it is necessary that each application has its own log4j.jar packed in its .war (or .ear when applicable). It is important that all applications have its own log4j packed. If one of them does not, the problem will still persist.

Besides having log4j.jar packed, its also necessary to edit weblogic-application.xml to warn Weblogic to use log4j classes from application classloader.

Here you have an example of weblogic-application.xml:


<?xml version='1.0' encoding='UTF-8'?>
<weblogic-application xmlns="http://www.bea.com/ns/weblogic/90" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<prefer-application-packages>
   <package-name>org.apache.log4j.*</package-name>
</prefer-application-packages>
</weblogic-application>



If you are using ant to build your Weblogic JWS webservice with task jwsc, here you have a snippet of build.xml that you can use to pack log4j into .war and use a custom weblogic-application.xml.

log4j.jar must be placed into .war in WEB-INF/lib and weblogic-application.xml into .ear in META-INF


<target name="jwsc" description="build web service">
  
    <delete dir="${build.dir}"/>
    <mkdir dir="${build.dir}"/>  
  
    <jwsc
        srcdir="${webapp.dir}"
        destdir="${build.dir}" 
    verbose="on" debug="on"
    keepGenerated="no" >


     <jws file="${jwsc.file}"
           compiledWsdl="${jwsc.jws.compiledWsdl}"
           contextPath="${jwsc.jws.contextPath}" 
           type="JAXWS" explode="false" >
</jws>
    </jwsc>
  
    <war destfile="${war.built}" update="true" >
        <fileset dir="${webapp.dir}">
          <include name="**/*.jar" />
  </fileset>
    </war>


    <copy file="${service.src}/resource/weblogic-application.xml" todir="${build.dir}/META-INF/" overwrite="true" />
 
 </target>


It is important to know that ${war.built} must point to .war file and ${webapp.dir} must point to the web service source that owns WEB-INF folder, that owns lib folder, that owns log4j.jar.

Links:
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/classloading.html
https://forums.oracle.com/forums/thread.jspa?threadID=689458
http://www.coderanch.com/t/554020/Web-Services/java/adding-jar-files-building-war
http://ant.apache.org/manual/Tasks/war.html

Wednesday, October 19, 2011

Why Log4j is not logging?


Recently I had a problem with the log mechanism in a small system I had to maintain.

It simply was ignoring log4j.properties configuration and was logging messages with INFO level.

After some time I discovered that it was using common-logging instead pure log4j.

There is no problem using commons-logging, it does work fine. The real problem was that only commons-logging-api.jar and log4j.jar were included in classpath. It was missing commons-logging.jar.

To support log4j, commos-logging needs both jars in classpath: commons-logging and commons-logging-api.

After adding commons-logging.jar in the classpath, log4j.properties started being read and respected.

Read more about commons-logging here: http://commons.apache.org/logging/guide.html#Jars Included in the Standard Distribution

Saturday, October 1, 2011

1Z0-894 Oracle Certified Expert Java Platform, Enterprise Edition 6 JavaServer Pages and Servlet Developer



I've just started my preparation to Java for Web development certification, former SCWCD - Sun Certified Web Component Developer. 


In the newest version of this certification, and already in the Oracle world, its certification
code is 1Z0-894, and the name is Oracle Certified Expert, Java Platform, Enterprise Edition 6 Servlet and JavaServer Pages Developer.


You'll find more details about this test on the Oracle site: http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=41&p_exam_id=1Z0_894


EPractize Labs's website also has interesting information, including a simulation of how long you will need to prepare for the test: http://www.epractizelabs.com/blog2/?p=106


Because this test is relatively new, and I had already read the book Head First Servlets & JSP 1.4 JEE twice, I decided to prepare only with the Study Guide and Mock Exam EPractize Labs:
http://www.epractizelabs.com/certification/sun/oce-jsp-servlet-exam-6.html


This material is $ 40.5 dollars, but the EPractize Labs guarantees money back if you get the test failed.


This Study Guide has some Ctlr-C/Ctrl-V errors, several typos and content is very superficial, but I intend to fill the gap in Internet surveys.


I have also created a project in Eclipse to get going in practice what I learn in the Study Guide.


My goal is to make the test in the first week of December/2011, I hope it is enough time!


If you have tips or questions about this certification, share commenting on this post.


--
PS: The result: http://dotdebug.blogspot.com/2011/12/pass-1z0-894-oracle-certified-expert.html

Wednesday, March 9, 2011

java.sql.SQLException: ORA-00911: invalid character

Tags: Java, JDBC, Oracle, ORA-00911, invalid character, semicolon

Here you have the formal description of this fault:


ORA-00911:

invalid character
Cause:identifiers may not start with any ASCII character other than letters and numbers. $#_ are also allowed after the first character. Identifiers enclosed by doublequotes may contain any character other than a doublequote. Alternative quotes (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual.
Action:none


... but it doesn't help much if your SQL query looks fine and runs well in Toad or PL SQL Developer.

When I got this exception I thought I could have add some strange non-printable character in my StringBuilder object, but it was easier:

JDBC queries are not allowed to have semicolons.

If you have this issue, check if you have a semicolon ";" at the end of the query.

// this will work fine in Toad or PL SQL Developer but will raise ORA-00911
ResultSet result = st.executeQuery("select * from dual ; ");


// this will work fine in Java, JDBC
ResultSet result = st.executeQuery("select * from dual");



Reference:
http://ora-00911.ora-code.com/
http://www.fromdev.com/2008/09/javasqlsqlexception-ora-00911-invalid.html

Friday, December 17, 2010

How to make downloads from Linux machine without GUI

Although I have been working about four years with Solaris, I am not able to do many things using only the command line.

One of the things that I had never been done via the command line is downloads on the Internet.

The magic command is wget. Just pass the download link as a parameter for the command.

I tested this command on a CentOS and it worked very well:


]# wget http://www.smartfoxserver.com/products/download.php?d=76
--2010-12-12 13:20:26--  http://www.smartfoxserver.com/products/download.php?d=76
Resolving www.smartfoxserver.com... 62.149.227.100
Connecting to www.smartfoxserver.com|62.149.227.100|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: ../download/SFSPRO_linux_1.6.6.tar.gz [following]
--2010-12-12 13:20:27--  http://www.smartfoxserver.com/download/SFSPRO_linux_1.6.6.tar.gz
Connecting to www.smartfoxserver.com|62.149.227.100|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 136461484 (130M) [application/x-gzip]
Saving to: `SFSPRO_linux_1.6.6.tar.gz'


100%[===================================================================================================================>] 136,461,484  221K/s   in 9m 2s


2010-12-12 13:29:29 (246 KB/s) - `SFSPRO_linux_1.6.6.tar.gz' saved [136461484/136461484]



did not need anything advanced, but you can do miracles with wget, for example,download an entire site. See more details in reference [1].

Reference:
[1] http://www.simplehelp.net/2008/12/11/how-to-download-files-from-the-linux-command-line/








How to configure an error page HTTP 404 Page Not Found for Struts 2

I had the simple task of creating a page for the treatment of HTTP 404 Error Page Not Found on a project using Tomcat/Struts2/Spring.

My first idea was to try to solve this issue in struts.xml, but after some research I was directed to try to solve it in web.xml, as it is done in any Java Web project:



  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
  5. version="2.4">  
  6.    <display-name>Aplicacao web simples</display-name>  
  7.    <welcome-file-list>  
  8.      <welcome-file>/bemvindo.jsp</welcome-file>  
  9.    </welcome-file-list>  
  10.     <error-page>  
  11.         <error-code>404</error-code>  
  12.         <location>/erro.jsp</location>  
  13.     </error-page>  
  14. </web-app>



That did not work. When a non existent page was accessed, the following exception was raised and the page was blank:


2010-12-08 14:50:43 Dispatcher [WARN] Could not find action or result
There is no Action mapped for namespace / and action name pan. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:178)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:112)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)



After some more research, I was properly directed to the solution, and it really was in struts.xml. No configuration is necessary in web.xml. [1]

You just have to set a default action. So, when struts receivea request that doesn't match any action, the request will be sent to your custom error page. The struts.xml looks like this:



    <default-action-ref name="pageNotFound" />
    <action name="pageNotFound">
        <result>/pageNotFound.jsp</result>
    </action>



Links:
[1] http://struts.apache.org/2.0.14/docs/action-configuration.html
[2] http://wiki.metawerx.net/wiki/Web.xml.ErrorCode
[3] http://www.coderanch.com/t/428310/Struts/Interceptor-messes-up-action-class
[4] http://www.mkyong.com/struts2/there-is-no-action-mapped-for-namespace-and-action-name-youractionname/