Tuesday, June 19, 2012

JAX-WS, Jersey, GF 3.1.2, don't forget an @ApplicationPath annotated class in your webapp for your resource classes to be deployed


NetBeans saved my day. I wanted to test Jersey (with Eclipse Indigo) and GF 3.1.2 simply didn't see them when deployed my webapp, the browser didn't see any response from sever for my "/alfa" URL.

I had a resource class:

package com.test.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("alfa")
public class MyResource {

 public MyResource() {
 }

 @Produces("text/plain")
 @GET
 public String sayHello() {
  return "hello again";
 }

}

So tried the example in the java6 tutorial (helloworldapplication) with Netbeans 7.1, right-cliked and chosen "Test RESTFfull Webservices " (as the tutorial says) and in NetBeans it worked perfectly this way (it launches a super web interface for testing all the class's resource methods...impressing) and also when deployed and testing it from a browser.

So, what was the difference between my webapp ant this one?

And i saw that netbeans generated a class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.netbeans.rest.application.config;
/**
 * This class is generated by the Netbeans IDE,
 * and registers all REST root resources created in the project.
 * Please, DO NOT EDIT this class !
 */
@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends javax.ws.rs.core.Application {
}


...and i copied it in my source files in Eclipse and there you go, it worked after a redeploy....

So When using JAX-RS, include this @ApplicationPath annotated class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test.jersey;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 * This class is generated by the Netbeans IDE, and registers all REST root
 * resources created in the project. Please, DO NOT EDIT this class !
 */
@ApplicationPath("")
public class ApplicationConfig extends Application {
}

I replaced "resources"  by "", to be able to use URLs like /alfa and not /resources/alfa, to hit my class.

Saturday, June 16, 2012

Glassfish 3.1.2 plugin for Eclipse 3.7 Indigo is not compatible (won't install) in Eclipse 3.5 Galileo

Eclipse 3.5 Galileo does not allow installation (through update sites) of the GF 3.1.2 plugin for Eclipse 3.7 Indigo (plugin that is called "Oracle GF Server Tools" in fact) from the update site "http://download.java.net/glassfish/eclipse/indigo" (Glassfish 3.1.2 plugin for Eclipse 3.7 Indigo ).

So in Eclipse 3.5 you are stucked with the Eclipse 3.5 Galileo plugin for Glasfish 3.1 (not for 3.1.2) that strangely can control also a Glassfish 3.1.2 runtime environment. (it was available here: https://ajax.dev.java.net/eclipse , as indicated here: http://marketplace.eclipse.org/content/glassfish-java-ee-application-server-plugin-eclipse)

Short Story:
Initally i had a GF 3.1.1 installed and then added in Eclipse 3.5 a new server environment for it, using the default Eclipse 3.5 plugin for Glassfish 3.1 (Java EE 6) that Eclipse proposes in the Nwe Server wizzard.

Afterword, GS's Update tool updated my GF 3.1 to 3.1.2 so this is how i ended up having a Eclipse 3.5 and a plugin for GF3.1, but actually controlling GF 3.2

Wednesday, November 30, 2011

Very important CSS solution: vertical align of an image in IE7 or IE8

This is a copy/paste of a very important resource to CSS users to solve the problem of vertical aligning of an image in IE7 or IE8. The original url is here. The key parts are highlighted in blue. This may be the solution to your problem.

 

You may also be interested in how to make divs (block elements) behave as inline blocks (that is: "flow" next to eachother "like words in a paragraph" - and next to other inline elements like words, images, spans - and wrap down on a new line automatically when there is no more space in the container, and all this by keeping them as "blocks" with respect to their size and inner content which can be anything), so also take a short look at this nice example called "IE7-/Win: inline-block and hasLayout".

-------------------------------------------------------------------

Centering (horizontally and vertically) an image in a box

The problem

We want to center an image inside a container, bigger than the image and with assigned dimensions. The image is not a background one, it’s a true <img> element.
Any image has well defined dimensions, so the easiest and most reliable way to solve the problem is:
  • make the image display:block
  • assign to the image the needed left and top margins to get it centered in the container. Each of this margin value is obviously half the difference between a dimension of the container and that of the image.
For example if you have images of two different sizes, like in a photo gallery with “landscape” and “portrait” images, to be centered in fixed size containers, then assigning to each <img> one of two classes (with different margins) will solve the problem.
But there are cases when one does not want to use the previous method. For example when many images, all with different dimensions are involved and it is not practical to assign different margins to each of them, or when the dimensions of the container and those of the image are not expressed in the same units (px, em, %, …) hence the difference cannot be computed. It’s useful to have a different solution, independent on the image’s size.

A solution

The horizontal centering is not difficult. If the image is left with its default inline display, then text-align: center is the obvious solution, working well in all browsers.
For the vertical centering the best solution, working in modern browsers, is to assign display: table-cell; vertical-align: middle to the container. This works in Gecko based browsers, Opera 6+, Safari, IE8. It leaves IE7 and lower (both Windows and Mac) out.
For IE7- the idea is to create a sort of line-box having as height the height of the container, and the using again vertical-align: middle. The line-height property cannot be used to achieve this, since it doesn’t work correctly in IE7-/Win in presence of images. Also the use of a big font-size (without specifying line-height) is problematic, because the height of the generated box is slightly bigger than the font-size. And IE5/Mac (differently from IE/Win) is able to resize (according to user choice) line-height and font-size expressed in pixels, so it would have problems with this approach, unless the height of the container is expressed in em. Note: this same argument precludes the general use of such vertical centering method, based on line-height, in Gecko based and Safari browsers.
Fortunately IE7- has (partial) support for display: inline-block. If an empty inline-block element (for example a span) is added inside the container and it is assigned height: 100%; vertical-align: middle then it allows to precisely get what we want: a line box with the desired height. In other words, the inline-block element respects an assigned height (equal to the container’s one) and props the line open, so that vertical-align: middle (both on the extra element and the image) gives the desired vertical centering.
Some details:
  • The extra, empty inline-block element may have zero width in IE7-/Win, but it must have at least width: 1px in IE/Mac, otherwise it has no effect (this sometimes offsets the horizontal centering by 1px. It could be counteracted with a -1px margin, but the problem is barely visible.)
  • In IE5/Mac the vertical centering is some pixels off. The use of a small font-size or line-height on the container seems beneficial, not clear why (IE5/Mac is a bit incoherent here, especially when playing with its font size settings.)
A combined solution, using display: table-cell and the extra span with display: inline-block, works in Gecko based browsers, Opera 6+. Safari, IE5+/Win, IE5/Mac.
A variation of this technique can be used to vertically center a block element (even with unknown height) inside another one (with known height) which is a more interesting problem.

The code

Putting all together, and naming “wraptocenter” the class of the container, that’s the relevant CSS. Code for IE/Mac is wrapped in a suitable filter. Code for IE7-/Win is put in conditional comments.
<style type="text/css">
.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: ...;
    height: ...;
}
.wraptocenter * {
    vertical-align: middle;
}
/*\*//*/
.wraptocenter {
    display: block;
}
.wraptocenter span {
    display: inline-block;
    height: 100%;
    width: 1px;
}
/**/
</style>
<!--[if lt IE 8]><style>
.wraptocenter span {
    display: inline-block;
    height: 100%;
}
</style><![endif]-->
And that’s the relevant HTML
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>

Some examples

To demonstrate the solution, it is applied to two different sized images, and to two different sizes for the containers. The first containers have dimensions expressed in pixels, the second ones in ems. The containers are given a grid background to better check the centering.

(images removed for not displaying corectly on copy/paste to this blog post, see original url)

Some other examples where the images contain a (red) grid, which should align with the light grey grid of the container background.

(images removed for not displaying corectly on copy/paste to this blog post, see original url)

Other, slightly more complex, examples: images in floated containers, an image gallery with captions.

An article on this problem by Steve Clay.
Many thanks to Ingo Chao, for his help and comments.
CSS tests home

Wednesday, April 6, 2011

Type 'com.theapplicationbox.gwt.jpa.entities.Person' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized

I had this exception today when using Gilead 1.3.2.1839 + GWT 2.2 (and other technologies, but not important: Hibernate 3.3.2.GA + Hibernate EntityManager 3.4.0.GA(JPA 1.0)+Spring 3.0.5:)


Type 'com.theapplicationbox.gwt.jpa.entities.Person' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.
 Everything concerning Gilead was set correctly in my GWT project in Eclipse, that is:
  •  the application.gwt.xml (place in com.theapplicationbox) contained the Gilead configuration
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='application'>
    <!-- Inherit the core Web Toolkit stuff.                        -->
    <inherits name='com.google.gwt.user.User' />


    <!-- Inherit the default GWT style sheet.  You can change       -->
    <!-- the theme of your GWT application by uncommenting          -->
    <!-- any one of the following lines.                            -->
    <inherits name='com.google.gwt.user.theme.standard.Standard' />
    <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
    <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->


    <!-- Other module inherits                                        -->
    <!-- Gilead inherits, as in the docs: http://noon.gilead.free.fr/gilead/index.php?page=stateless -->
    <!--  adapted from "net.sf.gilead.Adapter4Gwt15" to "net.sf.gilead.Gilead4Gwt" -->
    <inherits name='net.sf.gilead.Gilead4Gwt' />
    <!-- Uncomment if your Domain class use JPA mapping annotations -->
    <inherits name='net.sf.gilead.emul.java5.ejb3.Ejb3' />


    <!-- Specify the app entry point class.                         -->
    <!--<entry-point class='com.theapplicationbox.gwt.client.ExampleEntyPoint' />-->
    <!-- Specify the paths for translatable code                    -->
    <source path='gwt/client' />
    <source path='gwt/shared' />
    <source path='middletier/common/interfaces' />
    <source path='jpa/entities' />


    <entry-point class="com.theapplicationbox.gwt.client.entrypoints.application.ApplicationEntryPoint"></entry-point>
</module>
  •  jpa/entities folder which contains Person.java is declared as source folder
  <source path='jpa/entities' />
  • the entity class had a default constructor, implemented Serializable and inherited LightEntity of Gilead:
    import net.sf.gilead.pojo.gwt.LightEntity;
    public class Person extends LightEntity implements Serializable {
    public Person(){}
    ...
    }

So there was no reason i got this.
But...

My service interface generating this problem were:
public interface IPersonService<T> extends RemoteService {


    List<T> getAll() throws IllegalArgumentException;


    public List<T> saveModifications(List<T> persons);
}
and
public interface IPersonServiceAsync<T> {
    void getAll(AsyncCallback<List<T>> callback) throws IllegalArgumentException;


    void saveModifications(List<T> persons, AsyncCallback<List<T>> callback);
}
The problem is that i shouldn't have used generics HERE in GWT. So:

  • either foget about <T> in the interface and method definitions and just use List<Person> in the methods definitions
  • keep the genericity but in a new couple of interfaces called IGenericService and IGenericServiceAync and then inherit from them:
public interface IPersonService extends IGenericService<Person> {
}
and
public interface IPersonServiceAsync extends IGenericServiceAsync<Person> {
}
   And in your code make sure you use the IPersonServiceAsync, not the generic version.


The answer by Bruno Marchesson to a similar pb (see bellow) says:
Looks like the RPC service signature declares that it returns an array of Object, which is forbidden by GWT serialization policy (too generic).
You should return an array of Pais instead.

The same happened in my case behind the scenes, even though i didn't have List<Object> as return type, but it finally got down to that and here is why.

The genericity is useful at compile time but the java class file bytes will not contain any type parameters information like T.
This is called type erasure. Instead, List<T> is replaced by List<Object> and here we are in exactly the same case.

The answer to the Exception
The answer is explained in this post by Bruno Marchesson in response to this original post.
The person in the original post signaled this well-known exception:

Caused by: com.google.gwt.user.client.rpc.SerializationException: Type '[Ljava.lang.Object;' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.

How i found these answers
I looked on the Gilead forum with these keywords "was not included in the set of types which can be serialized by this SerializationPolicy" and tried randomly until these 2 posts: the original and the response.

Also this post says a very interesting advice:

 I've found you have to be as
specific as possible when delaring your RPC methods and serializable
class fields. 

This post i founded googling with:
"Type * was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized"

Tuesday, February 8, 2011

OnMouseDown in Firefox blocking OnMouseMove. Fix: add "return false;" in OnMouseDown

In your "onmousedown" event handler of a DOM element in Firefox (FF), make sure you add a "return false;" in case you have problems. That could solve your problem.

This was the solution in my case. I had a problem because not returning false from that handler caused the element "to be dragged by FF": that is, after i mousedown-ed on it and then started to move without releasing the mouse, FF initiaited that "draggable behaviour", like it does for the images, where the the element follows your mouse pointer and is a bit transparent.

So, while still with the mouse down:
  • FF initiaited that "draggable behaviour"
  • this was stopping document.onmousemove handler to act (that i had assigned in the onmousedown handler).
Releasing the mouse pointer had another strange effect:
  • onmouseup was also NOT triggered and,
  • just after that, document.onmousemove was "free" and acting.
So i ended up with the mouse up and document.onmousemove acting - which is not what i wanted.

I was implementing a kind of scroller(see code snippet bellow) where you mousedown the cursor of the scroll, move you mouse around to make the cursor move and then release the mouse and cancel the cursor move.
This is a snippet of the code that works; note the "return false;"


/*snippet of code, for all code email me*/

/*when mousedown-ing the cursor of the scroll, prepare the "real-time"
update of the cursor and content position(thanks to document.onmousemove).
I used jquery to avoid the differences between browsers
*/

$(cursor).mousedown(function(event){
    registerReferences(event);
    $(document).mousemove(scroll);
    return false;
});


/*this does the actual move of the cursor and the content we scroll with this scroller
*/

function scroll(event){
    var newValue = cursorTop + event.screenY - y;
    if (newValue < min) 
        newValue = min;
    else 
        if (newValue > max) 
            newValue = max;
    cursor.style.top = appendUnit(newValue);
    setContentMarginTop(newValue);
}


/*relasing the mouse cancels modification of the position of the cursor and content
*/

$(document).mouseup(function(event){
    $(document).unbind('mousemove');
});
...

Before adding "return false;", on the first mouse down and mouse up the scrolling worked. The above problem started the second time i tried to manipulate the scroll. So this must be a consequence of the very first onmousedown call not having returned false. The third and the n-th attempt behaved like the 2nd: not working.

I found the "return false;" mention here googling with "firefox drag disable". I tried it in my case and that did it.

Sunday, December 19, 2010

Welcome to the blog of theapplicationbox.com website.

Hi, I'm Vlad BALAN, from theapplicationbox.com The future posts will be my way to log various experiences i had related to JEE, HTML/CSS, Javascript/Ajax and other Java and webdesign subjects: workarounds, solutions, answers, etc. that i found on web explained by others and that should be known by many of us, developers.

Use "hibernate.hbm2ddl.auto=update" to update your schema (add remove columns, etc.) without losing data in tables.

Use <property name="hibernate.hbm2ddl.auto" value="update" /> to update your schema (add remove columns, etc...