samedi 20 septembre 2008

Meditation at work.

In Holland Electrician of Nuon, Bankers of ING, Civils servant can practice meditation at work now.

After the gymnastic period, the spriritual wave is breaking on dutch company to the point that even some syndicalist want to add a special clause on the collective convention to legally add meditation on work time.

This example ask a question on the relationship a worker has to his company. Should the entreprise go along with the personnal developpment.

This question make sens as one must spend 80% of its time on the working place, and on the other hand the serenity of the employee may benefit to the company, the employee may take distance to its work and be in a better state to propose improvement or create better binding inside the team.

Well to be honnest, I'm not sure to approve this idea. I think if personnal development is an important thing, not anybody may find it through meditation.

I keep on thinking that spare time is the best thing that can be offered to anyone for its personal development, then one may choose to use it as it wants.

jeudi 18 septembre 2008

The method hashCode() and the method equals(Object obj)

The Object class implement two methods : hashCode() and equals(Object obj).

Most of the time you don't need to override them, except when you intend to use those objects with the collection API.

First let's refresh our memory about equals and hashcode and what they are for.

Most of the time developpers know what equals is for. It defines when two objects should be regarded as equal despite the fact they don't share the same memory location.

But when it comes to explain what is hashCode for, strangely there is much fewer able to explain.

Let's have a look to the javadoc :

hashCode returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by HashTable.

Indeed the main purpose for hashCode is for the collection API, as in the collection API many datastructure rely on the hashTable pattern : HashSet, LinkedHashSet, HashMap, HashTable for example.

How a HashTable work ?

A HashTable is an array of Vector (also called Bucket) with an initial capacity c, when you add an object to the hashTable, first you evaluate the bucketPosition = (hashCode modulo c) and you add the object to the bucket living in the bucketPosition index.

Then inside the bucket you use the equals method to check if the object don't already exist.

let's see what really happen when you add an object in a hashTable :

//this could be the body of a add method
bucketPosition = object.hashCode() % c;
Bucket bucket = hashTable[buketPosition];
//check the object is not already in the bucket
boolean prensentInTheBucket = false;
Iterator it = bucket.iterator();
while (it.hasNext()){
Object objToCompare = it.next();
if (objToCompare.equals(object)){
prensentInTheBucket = true;
break;
}
}
//we did not found it, good !!!
if(!prensentInTheBucket){
bucket.add(object);
}



Checking if the hashTable contains the object also rely on hashCode


//this could be the body of a contains method
boolean containObject = false
bucketPosition = object.hashCode() % c;
Bucket bucket = hashTable[buketPosition];
Iterator it = bucket.iterator();
while (it.hasNext()){
Object objToCompare = it.next();
if (objToCompare.equals(object)){
containObject = true;
break;
}
}


What's the very big advantage about storing the object this way ?

It's the efficienty of course, when you add or search the object in the collection you're not going to search the whole collection to find if the object exist. You limit your search to a single bucket. Thus you just have to process a limited number of comparison.

But what's needed to make the add/search action consistent.

1) First if two objects are equal they should return the same hashCode, otherwise you're going two put two objects regarded as equals in two different bucket (sad).

By the way, there is no reciprocity two object not equals may return the same hashCode

2) You're hashCode should be stable, I mean here that if a slight change on the object generate a different hashcode you may never be able to retreive your object. HashCode should be stable.

3) Of course three logical extra rules apply as well Symmetry, Transitivity, and Reflexivity but they are out of the scope of our study.

mercredi 17 septembre 2008

Upload a file with JBoss Seam

Uploading a file with JBoss Seam is a really simple thing thaks to the component

<s:uploadFile />

Here is a use in a view :


<f:facet name="header">Upload a file</f:facet>
<br/>

<h:form id="fooForm" enctype="multipart/form-data">

<s:fileUpload id="file"
data="#{uploadHandler.file}"
accept="image/jpg"/>

<h:inputText
converter="#{bankAccountConverter}"
value="#{myBean.bankAccount}" />

<h:commandButton
id="upload"
value="Upload !!"
action="#{uploadHandler.handleUpload}"/>

</h:form>


Now here is the use on the component side :


@Name("uploadHandler")
@Scope(ScopeType.EVENT)
public class UploadHandler {

@In(value="#{facesContext}")
FacesContext facesContext;

@Logger private Log log;


private byte[] file;

public void handleUpload() throws IOException{

String path = ((ServletContext) facesContext.getExternalContext().getContext()).getRealPath("/my/files/dir");
File f = new File(path,"myFile.txt");
log.info(f);
FileOutputStream fo = new FileOutputStream(f);
fo.write(file);
fo.flush();
fo.close();

}

public byte[] getFile() {
return file;
}

public void setFile(byte[] file) {
this.file = file;
}

}


Et voila !!

dimanche 14 septembre 2008

Using a CMS to build a ECommerce solution

I recently had to build an e-commerce application from a blank page, of course we could capitalize on our developper experience and the use of many frameworks (hibernate, spring mvc, spring web flow, velocity ) but we had to start from nearly nothing in term of content management.

But if you watch it carrefully an ecommerce solution is 70% on content management (creating content, syndicate content, document, image, comment, publication, approval, public and private comment, url rewriting, indexing and searching content) and the remaning 30% is on ecommerce question (order workflow management, payment gateaway, gifts, packaging, linkage to account and stock software).

Thus one would consistently ask this question : Why didn't you build your solution on top of an open source CMS ?

My answer is : when your team know Spring, hibernate and Struts/Spring MVC, do you know an open source CMS based on Spring, Struts/Spring MVC hibernate and extensible enough to support sophisticated ecommerce functionnality ?

I've been searching for this 5 legs sheep quite a while, and if this kind of thing exist in php I really could not find that in the spring portfolio.

I guess that the guys who choose to work on the JBoss platform, especially the JBoss seam framework, did a better choice, as they both have in their portfolio a framework to go through specific customer need, and aleady advanced product like JBoss portal, nuxeo 5, on top of a strong CMS. But I only guess, I haven't really try in a professional context actually.

Openism

I call openism a way to fight a closed system by opposing an open system doing the same job.

A simple example wikipedia :

Wikipedia is an openism action against the closed encyclopedic system. Wikipedia is open as anybody may improve it while a classical encyclopedic system allow only a limited and allowed number of personns to improve it.

As a conscequence wikipedia is more up-to-date and richer than any closed encyclopedic system. Those old closed systems are just going to disappear now.

This concept lead me to ask new questions :

  • Which other openism action could we list ?
  • What's needed for an openism action ?
  • What will be the next big closed system openism will attack ?
  • Understanding Outjection in Seam

    Seam come with a new concept it's the @Outjection.

    Outjection is pushing a variable in one of the contexts of Seam after you invoke a seam component method.


    @Name("abean")
    public class ABean {

    @Out(scope=ScopeType.SESSION)
    public String outString = "st_0";

    public void call1(){
    outString = "st_1";
    }


    public void call2(){
    outString = "st_2";
    }

    }


    When you invoke one of the method (call1 or call2), the context variable "outString" is created in the session context if it doesn't exist yet or is updated if it already exists. But if no invocation is made on "abean" outString exist in no context at all.

    Let's see it from this view



    <f:facet name="header">Showing outjection</f:facet>
    <br/>

    <s:link action="#{abean.call1}"> Call 1 </s:link> <br/>

    <s:link action="#{abean.call2}"> Call 2 </s:link> <br/>

    <h:outputText value="#{outString}" />


    The first time you load this view, you see the two link Call 1 and Call 2 but the outputext is empty.

    Once you click Call 1 or Call 2 then you get an output st_1 or st_2 respectivly. Actually you'll never see st_0.

    The advantage of this design, is that the buisness method doesn't have to notify the context that the variable they deal with has changed.

    First post

    Hi this is the first post of my blog.

    I create this blog to share my experience and my curiousity.

    I'm a developper, mainly in Java and sometime in PHP.

    I'm curious on all very huge cahotic system like the Stock Exchange, the climate, the evolution of an internet community, the development of a language and so on.

    Otherwise I like spending time with my family, my friends, biking, surfing and running.