Sunday, July 31, 2011

Java 7 Warning with the behaviour of WatcherService ?



I look at new functionnality for WatcherService.So I created a directory and I watch for events like files created or deleted or modify.I follow the example from http://blogs.oracle.com/thejavatutorials/entry/watching_a_directory_for_changes
I was surprised for an unexpected  behaviour with WatcherService :


I make the following :
mkdir ~/tmp
touch ~/tmp/toto


Then I watched for events in ~/tmp/toto and it works fine.

Then I do the following :

mv ~/tmp ~/tmp.old
touch ~/tmp.old/titi


I look at events and there is events ! However I move the directory so the program should be throw an exception because of reset methods() but no !
So I post a message (It's the first time I post a message on the Oracle's web site !!!! ) and I wait for an answer ...
I will see if I post a good note ....


Here is the example source code form Oracle'sblog :

for (;;) {

    //wait for key to be signaled
    WatchKey key;
    try {
        key = watcher.take();
    } catch (InterruptedException x) {
        return;
    }

    for (WatchEvent event: key.pollEvents()) {
        WatchEvent.Kind kind = event.kind();

        //This key is registered only for ENTRY_CREATE events,
        //but an OVERFLOW event can occur regardless if events are
        //lost or discarded.
        if (kind == OVERFLOW) {
            continue;
        }

        //The filename is the context of the event.
        WatchEvent ev = (WatchEvent)event;
        Path filename = ev.context();

        //Verify that the new file is a text file.
        try {
            //Resolve the filename against the directory.
            //If the filename is "test" and the directory is "foo",
            //the resolved name is "test/foo".
            Path child = dir.resolve(filename);
            if (!Files.probeContentType(child).equals("text/plain")) {
                System.err.format("New file '%s' is not a plain text file.%n", filename);
                continue;
            }
        } catch (IOException x) {
            System.err.println(x);
            continue;
        }

        //Email the file to the specified email alias.
        System.out.format("Emailing file %s%n", filename);
        //Details left to reader....
    }

    //Reset the key -- this step is critical if you want to receive
    //further watch events. If the key is no longer valid, the directory
    //is inaccessible so exit the loop.
    boolean valid = key.reset();
    if (!valid) {
        break;
    }
}




Sunday, July 24, 2011

cvc-complex-type.2.4.a: Invalid content was found starting with element 'display-name'

Problem :

cvc-complex-type.2.4.a: Invalid content was found starting with element 'display-name'. One of '{"http://java.sun.com/xml/ns/javaee":servlet-class, "http://java.sun.com/xml/ns/javaee":jsp-file}' is 
 expected.

In Eclipse, I have the following target (display-name) in red :

web.xml :
  <servlet>
<servlet-name>monitor</servlet-name>
<display-name>monitor</display-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/monitor-servlet.xml</param-value>
    </init-param>
<load-on-startup>1</load-on-startup>
  </servlet>

Solution:

You have to put the tag display-name first.

  <servlet>
<display-name>monitor</display-name>  
<servlet-name>monitor</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/monitor-servlet.xml</param-value>
    </init-param>
<load-on-startup>1</load-on-startup>
  </servlet>

Note : In my google project http://code.google.com/p/lin-mon-webapp/, I have committed a web.xml with no validation error (I just make right click to be sure not having validation error)

Monday, July 18, 2011

java.lang.ClassNotFoundException: javax.faces.FacesException

Problem :

In JBoss 6.0.0 when I deploy my war, I have the following error :

java.lang.ClassNotFoundException: javax.faces.FacesException 

Solution :

I've add following Maven dependencies :

<dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0.0-Beta2</version> </dependency>

Friday, July 15, 2011

Create a database with a user

Just a simple reminder ...

mysql>mysql -u root
mysql>create database db;
mysql>GRANT ALL ON db.* TO 'integration'@'localhost' IDENTIFIED BY 'integration';
mysql>GRANT ALL ON db.* TO 'integration'@'%' IDENTIFIED BY 'integration';
mysql>flush privileges;
mysql>quit

Then you can do on localhost or  on your IP:

mysql -u integration -p 
mysql -u integration -h 127.0.0.1 -p

Thursday, July 7, 2011

java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener

Problem :

In JBoss 6.0.0 when I deploy my war, I have the following error :

java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener 

Solution :

I've add following Maven dependencies :

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.0-Beta2</version>
</dependency>
<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.0-Beta2</version>
</dependency>

Tuesday, July 5, 2011

ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory

Problem :

When I deploy my war on JBoss 6.0.0, I have the following error :

java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory


Here is an extract of my Maven pom.xml file :

   <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.4.0.GA</version>
     <exclusions>
       <!-- Exclude Commons Logging in favor of SLF4j -->
       <exclusion>
         <groupId>org.apache.commons</groupId>
         <artifactId>com.springsource.org.apache.commons.logging</artifactId>
       </exclusion>
   </dependency>

Solution :

You have to exclude xml-apis because there is a conflict between xml-apis and librairies into JBoss (xeres-impl).
So you have to do the following :
     <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.4.0.GA</version>
     <exclusions>
       <!-- Exclude Commons Logging in favor of SLF4j -->
       <exclusion>
         <groupId>org.apache.commons</groupId>
         <artifactId>com.springsource.org.apache.commons.logging</artifactId>
       </exclusion>
   <exclusion>
     <groupId>xml-apis</groupId>
     <artifactId>xml-apis</artifactId>
   </exclusion>        
     </exclusions>
   </dependency>

Sunday, July 3, 2011

Eclipse : Can not find the tag library descriptor for "http://java.sun.com/jsf/core"

Problem :

In Eclipse, I have the following error : Can not find the tag library descriptor for "http://java.sun.com/jsf/core"

Solution :

I add this dependencies for having the jat in Eclipse classpath :

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.0.0-Beta2</version>
</dependency>

Saturday, July 2, 2011

Can not find the tag library descriptor for "http://java.sun.com/jsf/html"

Problem :

I have the following error in Eclipse : Can not find the tag library descriptor for "http://java.sun.com/jsf/html"

Solution :

Add the following for having the jar in Eclipse classpath :

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.0.0-Beta2</version>
</dependency>

Friday, July 1, 2011

SAXException: The content of element type "web-app" must match ...


Problem


I deploy a war in JBoss 6.0.0 ( idem JBoss 5.1), I have the following error :

Caused by: org.xml.sax.SAXException: The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)". @ vfs:///opt/jboss/jboss-6.0.0.Final/server/dr_jbossweb-standalone/deploy/getting-spring.war/WEB-INF/web.xml[53,11]

I check my web.xml and there is nothing abnormal :

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:META-INF/properties/log4j.properties</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!--All spring configuration files need to be loaded in context. Although we have single file but
  with the growing configuration stuff we need to break down configuration files too-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <!--Spring's front controller or dispatcher which will be responsible to handle every web request
    and dispatch it to a specific web controller-->
    <servlet-name>integration</servlet-name>
    <display-name>integration</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/integration-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--Let every request be handled by Spring's Dispatcher servlet and it'll decide which web controller to call -->
  <!--based on @RequestMapping-->
  <servlet-mapping>
    <servlet-name>integration</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>
</web-app>

So where is the error ?


Solution

The solution is simple but a little strange :
You have to put :
  <!--All spring configuration files need to be loaded in context. Although we have single file but
  with the growing configuration stuff we need to break down configuration files too-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>

just before :

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

Cela donne le web.xml suivant :

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:META-INF/properties/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/integration-*.xml</param-value>
  </context-param>  
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <!--Spring's front controller or dispatcher which will be responsible to handle every web request
    and dispatch it to a specific web controller-->
    <servlet-name>integration</servlet-name>
    <display-name>integration</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/integration-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!--Let every request be handled by Spring's Dispatcher servlet and it'll decide which web controller to call -->
  <!--based on @RequestMapping-->
  <servlet-mapping>
    <servlet-name>integration</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>  
</web-app>