Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, February 26, 2009

Working with tiles

Today I found this very educative posts at Sergey Malenkov's Blog, and I would like to put a reference to them in my blog:

Design Patterns Explained With Java and Uml2

Design Patterns Explained With Java and Uml2 2008

Saturday, December 13, 2008

Heavyweight + Lightweight

Today seems a good day for many Java developer.
Via Rich Unger's Blog I found the problem with havyweight and lightweight components is solved in Java6u12 and also Java6u12 early access is availably too.
I must test it in the Balloon project, nice to merge WWJ with NetBeans platform.

Tuesday, December 02, 2008

Working with rollbacks in JPA

First examples you could see about JPA shows you how easy is to work with it. Don't misunderstand me, I like JPA, we (the developers) need a standard in the ORM world.
What I mean is when you really start working in a project using JPA discover you must understand fine the standard and the ORM engine implementing it (in my case TopLink).

Previously, in another post I put a couple of links on how to detach entities, here I would like to point to another post, from David Van Couvering's blog, talking about transaction rollbacks.

Wednesday, November 26, 2008

Databases: thinking different

Today I read another great article from Martin Fowler's web page DatabaseThaw.
In these days it seems there is no place to databases philosophy other than relational and the RDBMS titans.
I would like to note one database in "the dark side" commented in the above article, neo4j, a graph oriented database.

Tuesday, November 25, 2008

Detach an entity from JPA persistence context

1 - Previously
Ok, you have designed a great domain model and then translete it to a database or, also much probably, have designed a great database and then generated the corresponding entity classes.

2 - Now
You have some GUI to get user input data. Easy. You create a new domain object (customer, product, bill or whatever else) and persist it through your favourite JPA engine.
Now you have some GUI that lists the available objects in your domain (again customer, product, bill or whatever else. I good idea could be obtain that objects in the way of domain object, I suppose for that reason you create a model, then you can change properties in some of that objects and store (persist) again those changes, so called merge changes.

3 - The question

Here comes the question. How can you detach an object from the persistence context? If you could get a dettached object you could modify its properties, then if the user agrees persist its new state or leave unchaged otherwise.

4 - The solution (or better say 'some of the solutions')
Detach an entity from JPA/EJB3 persistence context

Friday, September 26, 2008

Swing Application Framework & Beans Binding

This is a short post related to two Java project that can be very useful.

Swing Application Framework (https://appframework.dev.java.net) is a small set of Java classes that simplify building desktop applications. I recommned it because it can be viewed as a set of good practices programming with Java. Also take a look at this article.

On the other hand, lately working with NetBeans I make use fo BeansBinding. It could be a bit confusing at the begining but to work with tables and bind values to DB tables, Lists or other objects, is very poweful

Thursday, August 07, 2008

Using JAXB to generate KML Java classes

Some days ago I write a post about using JAXB with the KML's XSD file.
Due to a comment I would like to write more explicitly how I generated the Java files.

First of all, what you need is the XSD files which defines the KML syntax (http://schemas.opengis.net/kml).
Also, you need the xjc utility, included in the JDK6 (or download the JAXB project files).

Uncompress the XSD zip file and go into the uncompressed folder. Execute:
xjc -xmlschema -verbose -extension ogckml22.xsd
and get an error similar to this:

[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 1058 of file:kml_files/ogckml22.xsd

[ERROR] (Related to above error) This is the other declaration.
line 255 of file:kml_files/ogckml22.xsd

[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 350 of file:kml_files/ogckml22.xsd

[ERROR] (Related to above error) This is the other declaration.
line 261 of file:kml_files/ogckml22.xsd


The problem is there are two scale elements defined, one like 'scale' and the other with upper case 'Scale'. And the same for the snippet element.
By default, JAXB uses case insensitive which will produce duplicated class names.

To resolve this there are some solutions. One is to customize the JAXB specifying the class name to be generated for some elements. The later is a workaround (that I used) that consist to change the name for one of the duplicated elements.
In line 255 change:




by




and in line 1391 chage:



by




The same for the snippet element.

Now you can re-execute the xjc command and a set of Java classes will be generated. The only difference is the change in the scale element which produces a different class name.

Friday, June 27, 2008

Working with time

It can seem trivial but sometimes working with time can become a real headache.
Here I present a short code snippet showing one common but confusing situation:
if you create two different Calendar objects and print it getting its Date and using 'println()' method they can seem the same.

Please read code comments which clarifies every situation.


import java.util.Calendar;
import java.util.TimeZone;

public class utc {

/**
* @param args
*/
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance(TimeZone
.getTimeZone("Europe/Madrid"));

Calendar c2 = (Calendar) c1.clone();
c2.setTimeZone(TimeZone.getTimeZone("UTC"));

// Print Calendars. By default the toString() Calendar method is called
// and gets a String version of the objects.
// As you can see the String version shows the time zone of the Calendar.
System.out.println("Cal1: " + c1);
System.out.println("Cal2: " + c2);

// Print the Date objects of each Calendar.
// Here the toString method of the Date objects are invoked and this method
// by default translates the Date to the default system timezone. Thus both Dates
// seems equal.
System.out.println("Cal1: " + c1.getTime());
System.out.println("Cal2: " + c2.getTime());

// Finally we get the value of HOUR, MINUTE and SECOND of each calendar and
// prints it on the screen.
int h1 = c1.get(Calendar.HOUR_OF_DAY);
int m1 = c1.get(Calendar.MINUTE);
int s1 = c1.get(Calendar.SECOND);

int h2 = c2.get(Calendar.HOUR_OF_DAY);
int m2 = c2.get(Calendar.MINUTE);
int s2 = c2.get(Calendar.SECOND);

System.out.println("Cal1: " + h1 + ":" + m1 + ":" + s1);
System.out.println("Cal2: " + h2 + ":" + m2 + ":" + s2);
}
}

Wednesday, May 14, 2008

Using JPA in a web application

I usually work creating my entity data model classes using JPA and creating session beans (or stateless/web services beans) in an EJB module to acces this data. Later, I create a web/desktop application to access entities through the previous module.

But sometimes you need/want to do a more quickly/ugly job working with entities directly from your web application. For those (like me today ;)) I put here this link to a JEE5 blueprint named: Design Choices in a Web-only Application Using Java Persistence.
Among other things, It talks about using container or application managed entity managers.

Saturday, April 26, 2008

Obfuscating a NetBeans Java application project

Some time ago I found a couple of posts talking about how obfuscating a NetBeans RCP module (here and here).

Getting some parts of the ant targets presented in the previous post, this one present a simple target that allows to obfuscate a normal java library.

For this, you need to have installed the obfuscator ProGuard.

Take into account I am talking about obfuscating a Java library. This implies the obfuscation is lighter than if you obfuscate a closed application, that is, all public methods and interfaces must maintain its name (if not you can call your library methods anymore).

Open your build.xml Java application file and paste this target:






classpath="${proguard.jar.path}" />



renamesourcefileattribute="SourceFile" ignorewarnings="true">




























name ="class$"
parameters="java.lang.String" />
name ="class$"
parameters="java.lang.String,boolean" />











type="**[]"
name="values"
parameters="" />
type="**"
name="valueOf"
parameters="java.lang.String" />






type ="long"
name ="serialVersionUID" />
name ="**"/>
name ="**"/>
name ="**"/>
type ="void"
name ="writeObject"
parameters="java.io.ObjectOutputStream" />
type ="void"
name ="readObject"
parameters="java.io.ObjectOutputStream" />
name ="writeReplace"
parameters="" />
name ="readResolve"
parameters="" />








Special attention to these couple of lines:



Tuesday, March 18, 2008

Differences between source level 1.5 and 1.6 in NetBeans

When you create a new project you can specify the source level and the JDK to use with it.
This is an important fact because depending on the source level the GUI builder matisse will use new classes available in JDK6 or not.

Before JDK6 or using 1.5 source level, swing-layout project is used to provide some useful swing layouts.
As you can read in the project home page: As with other swing-labs projects this is exploratory work, parts of this that are successful will be pulled into future JDKs. The baseline work has already been pulled into 1.6.

This means that for source level 1.6, NetBeans builds the GUIs using the same (or similar) classes located in the JDK6 instead of swing-layout library.

Remember it, if you want to create GUIs that must run in older version of JDK and not only in JDK6.

Tuesday, January 22, 2008

Some notes on GlassFish v2, JDK6 and JDBC4 (PostgreSQL)

These are my today experiences (really my today headaches) porting a couple of applications from GlassFish v1 running with JDK5 to GlassFish v2 running with JDK6.

The applications was developed with NetBeans 5 and consist of a couple of EJB modules and a couple of web applications using the VisualWebPack in the NetBeans 5 (JSF).

First, I downloaded and installed JDK6 and GlassFish v2.

Before deploying the application you must take into account that my JDBC driver in GF1 (running JDK5) was a JDBC3 PostgreSQL driver. This driver is compiled for JDK5, thus I need to download and install (in the GF2 server) the JDBC4 version (not completed yet).

Next, I have created some database resources using the JDBC4 drivers. This works but generates a lot of warnings when application runs. To avoid them you need to put this option in your resources JDBC30DataSource=true.

Finally I have developed the applications. I this step I found strange behaviors and some server crashing problems. After trying 1000 thing I found that the problem was in a hyperlink in a JSF page. For an unknown reason, changing the hyperlink by a button (mantaining the same action) resolve the problem.

If you are working, like me, with PostgreSQL I would like to point this link about sequences:
http://wiki.glassfish.java.net/Wiki.jsp?page=FaqPostgresSequences

Thursday, September 27, 2007

Preparing Liferay EXT environment

  • Install JDK and ANT.
  • Set JAVA_HOME and ANT_HOME to your Ant and Java directories. Add JAVA_HOME\bin and ANT_HOME\bin to System Path.
  • Create a Liferay directory:
$HOME/liferay-devel (refered as {LIFERAY}).
  • Create a directory called:
{LIFERAY}/portal
and unzip de Liferay sources into it.

  • Create a file called:
release.{user name}.properties, where {user name} is the user name used to develop.
  • and put the next lines with the appropiate values:
lp.ext.dir={LIFERAY}/ext
  • If you expect to be using eclipse add also a property called lp.eclipse.project.name with the desired name of the project:
lp.eclipse.project.name=liferay-ext

  • Create a file called:
app.server.{user name}.properties
  • and put the next lines with the appropiate values:
app.server.type=tomcat
app.server.tomcat.dir={LIFERAY}/tomcat
sourceforge.mirror=http://belnet.dl.sourceforge.net/sourceforge/lportal

  • Create a file called:
build.{user name}.properties
  • and put the next lines with the appropiate values:
javac.compiler=modern, for use Sun's compiler.
javac.fork=true

  • Go into {LIFERAY}/portal directory an execute:
> ant clean
> ant start
> ant build-ext

This must create a directory {LIFERAY}/ext (or what you called in lp.ext.dir) and create your EXT environment.
  • Copy files release.{user name}.properties, app.server.{user name}.properties and build.{user name}.properties to the {LIFERAY}/ext directory.
  • Go to the {LIFERAY}/ext/servers and execute:
> ant install-tomcat
It must download and install a tomcat liferay bundle into {LIFERAY}/tomcat.

  • The idea is to have:
{LIFERAY}
{LIFERAY}/ext -> Your EXT environment to develop.
{LIFERAY}/portal -> The portal sources used to create the EXT.
{LIFERAY}/tomcat -> The server (lifery bundle) used to develop.

  • Now execute:
> chmod a+x {LIFERAY}/tomcat/bin/*.sh
  • and try to start liferay bundle:
> {LIFERAY}/tomcat/bin/catalina.sh start

  • Connect to http://localhost:8080 (user test@liferay.com, password test).

Thursday, July 05, 2007

Closing JPA EntityManger in web applications

I want to note this post from my neighborhood JavaNotepad.

When working with resources it's always important to ensure they are closed when not longer needed.
Working with JPA there are two kind of resources we can take care of: EntityManager and transactions.


Read...

Wednesday, June 27, 2007

Enabling/Disabling Swing Containers

(From Alexander Potochkin's Blog)

The fact that disabling a Swing container doesn't mean disabling its child component has always been surprising for Swing beginners...

Read...

Be worry with PixelGrabber

Recently my friend Fran (that is working on an amazing project) talk me about the perils of PixelGrabber. I use it in my Balloon project and after looking up for a while I found this post:
PixelGrabber is too slow and has memory problems!

You can read some example using BufferedImage instead PixelGrabber and, at the end, an explanation post about the old 1.0 Java image API.

Wednesday, June 20, 2007

Managing volatility

(From developerWorlks)

The Java™ language contains two intrinsic synchronization mechanisms: synchronized blocks (and methods) and volatile variables. Both are provided for the purpose of rendering code thread-safe. Volatile variables are the weaker (but sometimes simpler or less expensive) of the two -- but also easier to use incorrectly. In this installment of Java theory and practice, Brian Goetz explores some patterns for using volatile variables correctly and offers some warnings about the limits of its applicability.

Read...

Tuesday, June 05, 2007

Extending the DataProvider API with VisualWebPack

Until the coming version of NetBeans+VisualWebPack arrive and we can bind a WebService or EJB to a table or other component, here is a little idea to know how to do it in a correct way.

First of all, here are a couple of blogs that can help us in our search:
http://blogs.sun.com/winston
http://jkook.blogspot.com

If the VWP components are enough for your application, I think VWP is a grat tool that helps you top spend much more less time to create an application. The separation of request, session and application beans, the set of components, drag&drop and binding component to data sources: like a DB table.

However, what happens when you want to bind a component to the result of a web service or EJB. For the moment, you can extend the DataProvider API and play requesting data on request or session bean.

ObjectListData Provider work around - Sample Project
Creator Tip: Work around for Object List Data Provider design time problem

Sunday, April 22, 2007

Controlling Threads by Example

Recently I saw this article where you can understand the importance of threads in the applications GUI.

ONJava.com -- Controlling Threads by Example