Feeds:
Posts
Comments

Archive for the ‘Java’ Category

It seems to me that the JavaMail Mock library has dodgy handling of BCC fields. Sending a message with BCC fields makes it possible to read the fields again when receiving the email (!) This is against the definition of BCC, isn’t it?

    Session session = Session.getDefaultInstance(mMailProperties, null);
    MimeMessage message = new MimeMessage(session);
    String body = "body from " + getClass().getName()
      + " @" + Calendar.getInstance().getTime().toString();
    message.setContent(body, "plain/text");
    message.setRecipients(RecipientType.TO, mTo);
    message.setRecipients(RecipientType.BCC, address2 + ", " + address3);
    Transport.send(message);
  ...
  store.connect(...);
  Folder folder = store.getFolder(...);
  folder.open(Folder.READ_WRITE);
  javax.mail.Message[] allMessages = folder.getMessages();
  for (javax.mail.Message message : allMessages) {
    ...
    Address[] addresses = theMessage.getRecipients(RecipientType.BCC);

The variable ‘addresses’ now hold two entries. Note that Mailbox.get(mTo) works as you’d expect.

Read Full Post »

Solr is a great piece of software but its logging is bloody annoying. Defaultwise on my Tomcat, it will write all its log messages to the system out log, which is a pain in the but to say the least.

To make it log to its own log file, this is what I did to tomcat/conf/logging.properties

# added 6localhost.org.apache.juli.FileHandler to the list of handlers

6localhost.org.apache.juli.FileHandler.level = FINE
6localhost.org.apache.juli.FileHandler.directory = /var/log/escenic
6localhost.org.apache.juli.FileHandler.prefix = solr.

org.apache.solr.level=INFO
org.apache.solr.handlers=6localhost.org.apache.juli.FileHandler

Now, I get all solr messages in /var/log/escenic/solr-2009-07-22.log. What a relief! Of course, you may want to drop the logging level to WARNING

Read Full Post »

DBUnit and Hibernate

You wouldn’t think the difference between these two code snippets were anything to blog about, right?

String selectString = "select * from mytable";
List result = getHibernateTemplate().find(selectString);

and this one, and no, you woun’t get any points for pointing out that this is a better, non-deprectaed way of executing a query in Hibernate ­čśë

String selectString = "select * from mytable";
Query query = getSession().createQuery(queryString);
List result = query.list();

And now to the funny part: the latter implementation makes DBUnit hang forever!

Doing a thread dump of the Maven Surefire process reveals that this part of code is hanging:

"main" prio=10 tid=0x09945c00 nid=0x5279 in Object.wait() [0xb7d74000..0xb7d751f8]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(Native Method)
	- waiting on  (a org.apache.commons.pool.impl.GenericObjectPool)
	at java.lang.Object.wait(Object.java:485)
	at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:748)
	- locked  (a org.apache.commons.pool.impl.GenericObjectPool)
	at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
	at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)

Now, who can tell me why this is?

Read Full Post »

Complex queries in Hibernate

I’m sure it’s doable, but it’s definitely not easy to do.

Hibernate has added a complex, sorry “rich”, API on top of JDBC to construct queries and the like (in addition to OR mapping and I’m sure a host of other tings). Now, most tutorials out there are mere Micky Mouse examples and not that useful for what I need right now: joining three (maybe four tables) and returning an entity (and yes, I do know that joining many tables is expensive).

I’ve joined two tables before using one DetachedCriteria & Criteria and I assume I then can do a three table join by using two DetachedCriterias? Maybe, have to try this later. Matter of fact is that the API to construct queries is so complex, create so long code lines and so many of them, that I ventured into doing this using Hibernate’s own SQL dialect.

Of course, I believe it’s doable but I couldn’t find any good examples out there on doing it, I got this Array (or something) of java.lang.Objects rather than my nicely mapped Hibernate entity when I executed my Query object.

Object thing=[[Ljava.lang.Object;@956b74]

I didn’t see any good way of manipulating this Object thing, so I ventured lower down the food chain to the native SQL support in Hibernate:

SQLQuery query = Session.createSQLQuery(String);

Thanks to this one and setting the return entity, with the alias I used in the SQL String:

query.addEntity("b", Blue.class);

I finally got this thing working.

Now, to all of the Hibernate enthusiasts out there; if there’s a beautiful way of joining three tables returning an entity using the high level Hibernate API, I love to hear about it. But for now, I’m happy with my Hibernate/native SQL solution ­čÖé

Read Full Post »

I’d like to thank everyone involved in walking the last mile of creating a true free distriubution of the Java 6 platform.

It was fantastic that Sun open sourced 95% of Java and it’s great that RedHat and GNU Classpath has made the last steps to give us a truly free open source distribution of the Java 6 platform.

Why does this matter when Java 6 can be downloaded and used free of charge you may ask. Because a lot of us love Java and care about software freedom, that is why ­čÖé

Read Full Post »

Thread dumps saving the day

I’m amazed of the wonders thread dumps can do for you when debugging “why on earth is this taking so long?”. Given that your Java application is running on a Unix or Linux system, you can simply ask the JVM to dump the current threads active by sending it a QUIT signal. This is totally harmless and can be done in a production environment without any performance penalty or process shutdown (yes, the signla name is misleading ­čÖé

# find the PID of your process
$ ps auxww grep java

# send a QUIT signal to the process
$ kill -QUIT <pid>

I always make three thread dumps with 1-2 seconds in between them. If I then find the same thread dump in all three thread dump listings, I know what the application is having problems with.

The thread dumps are written to standard out, so for instance if you’re running your application in the Apache Tomcat servlet ocntainer, the dumps are written to $CATALINA_HOME/logs/catalina.out.

Read Full Post »

Emacs with on the fly Java compilation

Thanks to the blog post at www.credmp.org, I’ve got Java flymake support working in Emacs. It works surprisingly well (and fast) using the Eclipse compiler.

When setting it up, be sure to get the ecj.jar off the Eclipse website, I tried first with the ecj.jar supplied with the Debian package and that didn’t suffice.

Currently, the best solution to see the errors and warnings, is to hover the mouse over the coloured line. Works well when using a track point, but still, it would be nicer if I could get a one line message in the mini buffer.

Be that as it may, I’ve finally got Mickey Mouse help and support for generics in Emacs!

Read Full Post »

Tomat is creepy

I love Tomcat, but it really gives me the creeps sometimes. Using the latest 6.0.14 release, I’ve been linking my log4j configuration from $CATALINA_HOME/lib/trace.properties to whereever it actually resides.

The symlinking of both JAR libraries and trace.properties has always been fine. Now, all of a sudden, my logging config didn’t work anymore. I had been using the same setup the whojle day. All of a sudden, after five hours of work it seized to work. After some trial and error, I removed the symlink and copied the trace.properties into lib/ instead. All of a sudden the logging worked again (!)

And it’s not that symlinking from CATALINA_HOME/lib doesn’t work anymore, the JARs are picked up just fine. It’s just the trace.properties file that is no longer allowed to be symlinked. WHAT IS HAPPENING HERE?!!

Read Full Post »

Tomcat kept throwing annoying NoClassDefFoundErrors at me although the class was in a jar that resided in webapp classpath (a jar in mywebapp/WEB-INF/lib). Also, it didn’t happen all the time, just “often”. Restarting the server sometimes helped, sometimes not. Testing the same webapp on the Resin app server gave no such errors.

To my (so far) great positive surprise, an upgrade from Apache Tomcat 6.0.10 to 6.0.14 seems to have remedied it. Excellent! It’s amazing how such basic things can make you smile ­čÖé

Read Full Post »

I was positively suprised to find a rather good RPM plugin for Maven2. It has a neat way of specify both mapping and package scripts. I haven’t looked at package dependencies yet, but it looks good!

Now, if only the DEB plugin for Maven2 would be usable, we would be in package heaven once again! For now, I’m experimenting with
alien --scripts file.rpm

Read Full Post »

Older Posts »