Archive

Archive for the ‘java’ Category

Hunspell4Eclipse 0.8.2 (beta) out

No Gravatar

Changes:

  • threshold feature
  • update site back to normal
  • limited Java support – reports misspelled words (underline), but no proposals
  • latest JNA

Link to project page: http://code.google.com/p/hunspell4eclipse/

Commons Logging templates for Eclipse

No Gravatar

After my last article some folks at the company I work for asked for a Apache Commons Logging version of templates, so here it is.

Just as with the last article, import this templates: clogger-templates

The mappings are (in case you would like to use with SLF4J):

cloggercreate a logger instance with imports
cloginfocreate log entry of info level
clogerrorcreate log entry of error level
clogwarncreate log entry of warn level
clogtracecreate log entry of trace level
clogdebugcreate log entry of debug level
clogfatalcreate log entry of fatal level

Have fun.

Jackrabbit with Spring

No Gravatar

I was searching for a way to use Jackrabbit with Spring. There are articles about this topic, but each of them suggests using spring-modules. Now, my problem with spring-modules jcr implementation is, that they depend on non-released or obsolete 3rd party JARs, like apravez.* and jeceira. Just take a look at the pom file, and you’ll see.

On the other hand I do not feel a need for jcrTemplate, jcrCallback or any other ORM flavor helpers. I just need a nice, easy integration of Jackrabbit, where I can easily inject JCR Session into any class with spring.

The JNDI way would do, but I do not want to use JNDI, I want to configure Jackrabbit from my spring beans.

Now with those premises, I came out with a quite easy setup, that follows below.

Spring configuration:



  
    
    
  

  
    
      
    
  

  
  

The configuration of Jackrabbit is defined in repository.xml file, – I just took the default repository.xml, the one that is created when running TransientReposiotry for the first time. Of course for better integration and configuration define your own.

There is one more configuration point and that could be seen in the XML from above. Spring bean jcrConfiguration defines where the repository.xml configuration is and the path to repository resources, – the second argument. For test purpose I just used /tmp/repository but for production you might set it to something like: repository

Do not forget to set up spring RequestContextListener or you’ll be missing session (and request) scope in spring configurations. In web.xml just define a listener:



  org.springframework.web.context.request.RequestContextListener

From code we’ll just going to use the jcrSession. Here is a small example for usage:

@Service
public class TopicService implements BeanFactoryAware {

  private BeanFactory beanFactory;

  public Session getJcrSession() {
    Session jcrSession =
	  (Session) beanFactory.getBean("jcrSession");
    return jcrSession;
  }

  @Override
  public void setBeanFactory(BeanFactory beanFactory)
      throws BeansException
  {
    this.beanFactory = beanFactory;
  }

  public void myFucntion() {
    Session session = getJcrSession();

    // here you do what you would do with JCR...
  }
}

As my service is singleton I inject Spring Bean Factory with the help of org.springframework.beans.factory.BeanFactoryAware mixin, so each time I call getJcrSession() I get a JCR session bind to HTTP session.

Well, that’s it. A simple configuration, easy usage.

Ubuntu guy in a world of Apples… Part IV: Java Developer

No Gravatar

After a lot of work and some traveling I’m back, and I’m about to write about how do I manage to use Mac for Java development. (Note: This is the last part of the Apple Project series.)

First of all, let me describe what I use and how do I/we use it.

Eclipse is our choice of IDE. We use Subversion as SCM, Maven as a build system/dependency management/reporting/documentation/etc.. Beside these we use, (just to mention few that could be in relation with OS):

  • AspectJ
  • Spring
  • Hibernate for JPA
  • TestNG

Primarily we are building web applications, backed with RDBMS – so I need a some local database for my Mac, and our choice of RDBMS is PostgreSQL, but we use Oracle and DB2 as well.

Read more…

Bye, bye P2! Hello UM again!

No Gravatar

Had enough. P2 gave me just too much headache.

Today I removed P2 from my two production Eclipse installation. What a relief…

Update Manager (UM) works fine, as it worked in 3.3. Installed AJDT, Q4E, SpringIDE, TestNG, AnyEdit, … without any issue.

At my last few tries, I managed to install AJDT successfully, but after it I locked myself out. At next install P2 were missing AspectJ feature, and were willing to do nothing.

I read somewhere that P2 will not let you install uninstallable or wrong software… well… no comment.

I still beleave P2 is “not an evil”, and it will turn out to be great. But now I think it came in to suddenly and with lot of missing features/bugs. – I know this is the best way to test it… :-) , and I usually happily volunteer in these kind of testings, but not arbitrarily.

Eclipse WTP Server start hangs… on Windows…

No Gravatar

We have a strange animal: A web application with really lots of jars. (Today total of 273 JARs.)

We use Maven 2, Eclipse with WTP, Tomcat, Q4E, etc.., but from these technologies only Eclipse, WTP and Tomcat are relevant for the problem I’d like to describe. Most of developer machines runs Windows XP, and some Windows 2000, and just few Linux.

As we use Maven 2, the jars came form ${user.home}/.m2/repository/ folder. We just let Q4E set up the path for our projects inside Eclipse. For some reason we stack with Q4E 0.3.0, and we tweaked the WTP Server (Tomcat) launch settings, to have all JARs included. We did not used “served modules without publishing”, nor “J2EE Module Dependencies” for few (here) non-relevant reasons.

After adding few new modules (JARs) to the webapp, for few developers the Tomcat Server just failed to start. No log, and nothing in the debug view, nor in the error view. Eclipse WTP were just waiting for Tomcat to start… forever… We were bitterly searching for solution/bug. With no luck. After adding even more JARs, even more developers were complaining: the same problem.

Read more…

Autoboxing and synchronization…

No Gravatar

We were writing a small, low level code to provide sequence number generator. It was backed by database sequences, and it worked fine, or at least we thought so…

All unit tests passed fine. – We wrote some multi-thread testNG tests. So we were quite sure, everything is fine, until we got a bug report… They said we have duplications. Well the first reaction was “no way”. But the error came out again and again. To isolate the problem, we wanted to be sure that our code is fine, so we wrote a stress test: 10 threads, 20 iterations of getting 1 million numbers and storing it into a unique field of database table.

And the stress test started. We event started it 3 times: 3 separate VMs * 20 iterations simultaneously consuming 10 threads. And it ran… ran… for about 5h… and crash… oups, unique constraint violation… :-(

Well there is no way to reproduce this kind of error in debug mode, at least not until you know where the error might be. So we started an “eye debug” session… and after, about half an hour we got it! The code was:

private volatile Long last = -1l;
public final V getNext() {
  if (!initialized) {
   init();
  }

  synchronized (last) {
    if( last % increment == 0 ) {
      dbSequenceGetNextValue();
    }
    last++;
    return (V)last;
  }
}

Well, it looks fine… or at least at first glance. But this code is nasty.

The problem is with autoboxing. Well not exactly with autoboxing, but how we used it.

If you look at the line #11, that would be actually:Long.valueOf(last.intValue()++)

And that is the problem! We have a new object here! And we were using that variable to synchronize our block! Ouch.

Now the correct version is:

private volatile Long last = -1l;
//synch object
private volatile Object getNextSynch = new Object();
public final V getNext() {
  if (!initialized) {
    init();
  }
  synchronized (getNextSynch) {
    if( last % increment == 0 ) {
      dbSequenceGetNextValue();
    }
    last++;
    return (V)last;
  }
}

Of course we could use AtomicLong too, but we do not need that complexity…

Moral of the story:

  1. Always think twice, when you are coding thread-safe, synchronized, parallel, etc…
  2. Instant sugar is evil!

p.s.: Stress tests still running… :-) Hope we’ll get a result by tomorrow.

Hibernate 3.2.x and Groovy – ASM madness

No Gravatar

I was playing a bit with Hibernate and Groovy. I was about to examine the possibility of using Groovy producing JPA Entities.

As I use Maven 2, the natural way is to create a project and define dependencies. As a good Maven user I was looking for the best artifact in the maven repository. I found Hibernate, and Groovy too. I took the latest from both. For groovy there was a choice between groovy-all and groovy. The groovy POM file was more descriptive, so I took that one.

Now that was a bad idea.

The problem is: Hibernate depends on ASM 1.5.3 while Groovy depends on ASM 2.2. ASM is not backward compatible, so ASM 2.2 won’t work for Hibernate. It will fail with class not found exception, (and vice versa for Groovy). After a day of hacking Maven POM file, and looking for some solution, I found this. Just look at the last paragraph, there is the solution. Just pick groovy-all. So I did that, and everything is fine again. Or at least to thought so…
I work on a framework, so I need to use ASM to enhance or to create some code at runtime. I usually do that by using ASM Eclipse plugin, because that is the fastest and easiest path. But that plugin relies on ASM 2.2, and now I’m back where I was… :-(

Ok, I could use a class loader hierarchy, but that would mean special configuration for Maven, or even worst, special configuration at deployment.

I think the Hibernate team should consider the solution Groovy team made.

Or I’m going to do that. :-)

Nice and easy.