vrijdag 20 februari 2009

more java reading

check this link, is a list of java resources. Funny how they mention themselves... recursivity all over :D.

http://www.ibm.com/developerworks/java/library/j-javaresources.html


The list of Web sites (with URLs):

* java.sun.com
This is the place from which the JDK originates and is often the first site that Java developers go to download a copy of the JDK and documentation. But beyond that, it is also a good source for information on the entire Java platform, including a very rich section on the JVM itself (Hotspot), which describes the internals of the JVM to a surprising depth.

* java.net
The Java.NET site is a smorgasbord of open-source projects, documentation, blogs, wikis, news, and other community resources. It's a motherlode of information on Java technology, possibly too much. For all that though, cruising through the list of projects can lead to some real gems such as the FEST-Swing project, which aims to create a "fluent interface" for building Swing applications, making them vastly simpler to create unit tests around.

* InfoQ
Created by Floyd Marinescu, founding editor-in-chief of The ServerSide, InfoQ differs from many of the other resources listed here in that it is explicitly technology-neutral, covering not only Java code but also .NET and Ruby and offering great coverage of agile approaches and insights. Though the commenting community on InfoQ isn't as large or as diverse as that of TheServerSide, the discussion is usually more tame and less hostile.

* TheServerSide
Often called "the the Slashdot of Java," TheServerSide was the first place that enterprise Java topics were openly questioned, hotly debated, and aggressively pursued. Some of the earliest thoughts around lightweight containers (as typified by Spring) occurred here and many of the individuals now considered to be "thought leaders" in the Java community emerged here. Although more recently the debates have sometimes taken on the character of shrill shouting matches, TSS will always be the original home of much of what turned into the Java ecosystem we now live within; as such, it will always be a source of information, historical if nothing else.

* DZone
DZone began life as a resource for Java developers, but more recently has begun to branch out into the other technology sectors, including .NET and Ruby, as well as several other topical "zones" of coverage. As such, while it's possible for the Java developer to focus entirely on just the Java zone, some cross-pollination does occur and it's the wise developer who will take advantage of that.

* IBM® developerWorks
If you're reading this article, then you've already found dW. In the interest of full disclosure, I must note that I have contributed articles other than this one to this site, but I can say with integrity that dW stands as a great resource for articles and material on Java coding and the JVM. In particular, be sure to check out Java theory and practice, a long-running series by Brian Goetz, which includes a three-part description of the JVM garbage collector that introduces the core concepts of GC in a surprisingly approachable way.

* JavaWorld
After a long hiatus, JavaWorld has returned to its youthful glory with a revamped look, high-quality technical content, and a handy topic taxonomy for quickly finding the types of articles you seek. Don't miss the extensive archives, which date back to its inception and include articles by some of the Java community's most notable contributors.

* Artima
Created by Bill Venners, Artima has evolved into a huge collection of articles, blogs, and interviews, not all of it entirely focused on Java development, and has some of the biggest names in the Java space writing there. (Check out Bill Joy's disbelief over the complexity of the Java language after Java 5 was released and the example he uses to justify his worries — Enum<> — as a classic example.) Well worth

fast & simple jsf mockups with hibernate

I'm working on a prototype for several administration pages and needed a quick mockup for testing. So I quickly set up a HSQLDB in memory and used hibernate annotations to map my model to it. Here are some code snippets to check how to configure hibernate to work in the jsf context:

you'll need a filter to instantiate hibernate:
package fi.tavutaito.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
*
* SessionFilter initializes EntityManagerFactory and serves each request
* with an EntityManager.
*
* @author markus
*
*/
public class EntityManagerFilter implements Filter {
/** Logger */
protected Logger log = Logger.getLogger(EntityManagerFilter.class.getName());

private EntityManagerFactory entityManagerFactory;

/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig config) throws ServletException {
Map configOverrides = new HashMap();
// this enables to see hibernate generated SQL in the server log
configOverrides.put("hibernate.show_sql","true");
entityManagerFactory = Persistence.createEntityManagerFactory("database", configOverrides);
}

/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {

EntityManager em = entityManagerFactory.createEntityManager();
request.setAttribute("entitymanager", em);
EntityTransaction tx = null;
try {
// begin the transaction
tx = em.getTransaction();
tx.begin();

// continue processing to JSF
chain.doFilter(request, response);

// after returning from JSF commit the transaction
tx.commit();

} catch (RuntimeException e) {
// if something fails, log the error and rollback
log.log(Level.SEVERE, "",e);
tx.rollback();
} finally {
// always close the entitymanager
em.close();
}
}

/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
entityManagerFactory = null;
}

} 

and this filter needs to be configured in the web.xml like this:
<!-- Entity Manager Filter declaration -->
<filter>
<filter-name>EntityManagerFilter</filter-name>
<filter-class>fi.tavutaito.filter.EntityManagerFilter</filter-class>
</filter>

<!-- Filter is setup on all requests than end with jsf suffix -->
<filter-mapping>
<filter-name>EntityManagerFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>

Now don't forget to configure you're persistence.xml. I'm used to putting it into the META-INF folder but it wasn't found so I got exceptions like:
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named database
Not sure yet why but I think it has something to do with the way I run it using jetyy. Will get back to this post when I have a solution.

If all goes well you can access the EntityManager through the FacesContext like this:
(EntityManager)
((HttpServletRequest)FacesContext
.getCurrentInstance()
.getExternalContext()
.getRequest()
)
.getAttribute("entitymanager");
Also check the resources of this post for some utilities.

I'm managing dependencies using maven and running all this code using the maven jetty plugin ;-).

external links:
* a tutorial on jsf + hibernate + facelets
* an example of HSQLDB hibernate config properties