All 19 entries tagged Java

View all 243 entries tagged Java on Warwick Blogs | View entries tagged Java at Technorati | There are no images tagged Java on this blog

January 24, 2006

Useful real life coverage of jdk5 features

Writing about web page http://java.sys-con.com/read/171487.htm

Some useful (but you have to read a bit before you get there ;)) info about the (not so) new features in jdk1.5

January 19, 2006

Living on the edge :)

So in the project I am working on we have a bit of code which checks the userAgent and decides whether it is "good" or "bad". It uses this information to determine the appropriate rendition, but that is irrelevant.

We have (as all good developers should ;)) and interface:


  public interface UserAgentChecker {
    boolean isGoodUserAgent(final String userAgent);
  }

with a single implementation. Given that the number of unique user agents is quite small, but each user agent will be used a large number of times, (i.e. unique user agents would be Netscape, IE etc.) this is an ideal candidate for caching (high reads, low writes).

Also, given that the "goodness" or "badness" doesn't change, there is no need to worry about stale data. Even better.

Our existing implementation uses regular expressions which, considering their power are extremely efficient, but they are still (relatively) expensive.

To implement the cache I could use AOP, or I could use a simple decorator. Given that we are overriding all the methods (there is only one!) and the caching (at the moment) isn't reusable by lots of different things, I decided to go with the good old decorator pattern, i.e.:


  public final class CachedUserAgentChecker implements UserAgentChecker {
    public CachedUserAgentChecker(final UserAgentChecker decorated) {
      this.decoratedUserAgentChecker = decorated;
    }
 
    ….
  }

Because the size of the cache will be very small (tens of userAgent strings) I decided to go with a simple Map whose key is the userAgent and the value is Boolean.

Now, the million dollar question :)

Should that map be synchronised :) According to the Javadoc HashMap isn't synchronised so in theory if two threads execute a containsKey() or get() or put() it could throw a ConcurrentModificationException(), but the javadoc really isn't clear on whether it would.

I could do my own synchronisation, or I could use Collections.synchronisedMap(..) but this will all have a minor impact on performance.

So, should I or shouldn't I? I wrote a little test harness which started 100 threads which all did puts/gets/containsKey() and didn't get any concurrency issues.

So, have any of you ever run into concurrency issues using a standard HashMap?

At the moment the production code isn't synchronised ;) Hope I don't get sacked :)


November 30, 2005

Some more hibernate fun :)

So I was debugging a problem with some code and I discovered this little gem:

Assume you have the following hierarchy:

– ClassInterface (use as proxy in mapping files)

– AbstractClass implements ClassInterface (used as the persistent class)

– ClassImpl extends AbstractClass (mapped as subclass of AbstractClass)

Load an instance of ClassImpl ala "ClassImpl ci = (ClassImpl)session.load(AbstractClass.class, someId)"

Now if you are going to compare two instances (using hashCode/equals) then guess what; if you implement that logic in ClassImpl it won't work. You must implement that logic in the abstract class that you have specified in the mapping file.

This is all to do with the joy that is cglib; even though the instance you are loading will eventually be a ClassImpl (based on the discriminator) CGLIB claims not to know that until it is fully initialised :(

OK, what happens if you try and load an instance of ClassImpl? Same thing. The equals method AbstractClass is used.

I have also run into situations with hibernate where code works if you step through, but not if you run it without debugging. I can only think that stepping through is maybe causing toString to initialise the object properly, hence subclass logic is utilised, but I cannot reproduce it.

Lame. Understandable, but lame.

To be fair; hibernate is great. It rocks, but it is not transparent* :)


November 29, 2005

Refigerator code. Nice.

Writing about web page http://www.butunclebob.com/ArticleS.BobKoss.RefrigeratorCode

Learned a new phrase today :)

Yeah; another buzz-word that I can use to confuse co-workers and pretend I am more intelligent than they are!


October 24, 2005

jdepend + eclipse; yeah :)

Writing about web page http://andrei.gmxhome.de/jdepend4eclipse/

So I have used jdepend before with good effect using maven, however I have never bothered integrating it with ant. I should :)

Anyways, just found this excellent plugin for eclipse. Very useful. If only I could remember what all the values mean :)


October 03, 2005

DAOs, love em or hate em, you just gotta have one

So without going into the whole "ORM shouldn't affect your design" coupled with the benefits of transparent persistence (or at least, semi-transparent) :), how do you deal with DAOs (Data Access Objects)?

Typically a DAO looks like:


  interface MyDAO {
    void save(User user);
    void save(BlogEntry entry);
    etc.
    User load(final User details);
    etc.
  }

This gets really tedious very quickly. It is also a bit naff from an OO point of view, try describing it's functionality without using the word "and" a lot :)

Another approach is to use a domain object specific DAO, i.e. a UserDAO and a BlogEntryDAO. This also really gets tedious very quickly.

A third approach, is to introduce an interface which all persistant objects implement, then your DAO has a single save(Persistant persistantObject) etc.

No idea which is the better approach to be honest, all of them suck.

To be honest, the concept of a DAO is quite sucky to start with :)

Thoughts/comments….


September 20, 2005

Another interesting blog

Writing about web page http://dynamicsemantics.blog-city.com/top5principles.htm


September 14, 2005

package structure

Writing about web page http://forum.springframework.org/viewtopic.php?p=34496#34496

Excellent new article on the spring forum about package structure. This is an area that I think developers really don't pay enough attention to.

I will be interested to see how this plays out….

link


August 11, 2005

Useful hibernate blog ;)

Writing about web page http://twasink.net/blog/archives/2005/04/upgrading_to_hi_1.html

So whilst struggling with the wonder that is CGLIB and hibernate proxies and lazy loading, I stumbled across link which is a very useful link.

In summary, hibernate 3 makes everything lazy including classes. This means that unless you explictly define a proxy element you will get a CGLIB class. Unfortunately CGLIB won't work if your class or any methods are final. Since lazy loading of classes isn't really that useful, turning off lazy loading would seem a sensible option. Unfortunately this means that all single point associates (one-to-one, many-to-one) are eagerly loaded, regardless of how they themselves are defined :)

As a matter of course, I always define an interface which my domain objects implement, and thus use proxies.


August 10, 2005

spring rocks ;) Filters + DI solved!

Writing about web page http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/web/filter/DelegatingFilterProxy.html

So I have a couple of filters which need access to spring beans. Previously I was doing something like:


protected final void initFilterBean() throws ServletException {
super.initFilterBean();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
pageResolver = (PageResolver) wac.getBean("pageResolver");
onInitFilterBean(wac);
}

which was a bit fragile. Anyways, spring allows you to define filters in your applicationContext, complete with DI ;)

in web.xml:


<filter>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <filter-name>loadPageFilter</filter-name>
</filter>

Spring will then look for a bean called loadPageFilter in the applicationContext and delegate to that. And of course, you can specify the actual bean name (instead of using the filter-name).

Cool.

P.S. Someone asked me how to do this and I forgot I had blogged about it, I redid this here: link


February 2021

Mo Tu We Th Fr Sa Su
Jan |  Today  |
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28

Search this blog

Tags

Galleries

Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • http://woosight.net/account/login?username=demo by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Loading…
RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI