Wednesday, September 23, 2009

Why is Lucene MoreLikeThis Final?

Lucene has a class MoreLikeThis which can used to build a search query finding documents similar to a passed example document.

This is very useful; in my RSS reader I allows users to find articles similar to an article they find interesting.

I'd like to do something a bit advanced: given an instance of class A, find similar instances of class B. The two classes are stored in separate Lucene indices; the vast majority of the time they are to be queried separately.

For some reason they have chosen to implement MoreLikeThis as final. I can't see any reason for this. I'd like to be able to extend it to add a new public Query like... method but no dice. The primary methods I need (createQuery()) to call are private so there doesn't seem to be a way around it:

I'll need to make my own version of the whole class. Ick. Looking at it positively, this will encourage me to learn the internals of the class instead of using it as a black box.

Saturday, September 19, 2009

Upgrading to EhCache 1.6.2 with Hibernate to enable JMX Monitoring

I couldn't (easily) find this answer, so here goes: Yes, it is completely OK to use 1.6.2 with Hibernate (3.3.2 in my case).

For whatever reason, the maven package hibernate-ehcache uses an old version of ehcache, version 1.2.3. This version doesn't support JMX (which is why I wanted to upgrade) but supposedly there are significant performance improvements in newer ehcache versions as well.

My app is maven/spring/hibernate, so first in your spring config make sure to use the Singleton version of the ehcache provider, otherwise you won't be able to turn on JMX:

<entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />


Then in maven, exclude the old version and bring in the new one:

        <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>3.3.2.GA</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.6.2</version>
</dependency>


Finally you will need to register JMX mbeans for your cache when your app starts:

        CacheManager manager = CacheManager.getInstance();
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ManagementService.registerMBeans(manager, mBeanServer, false, false, false, true);


Now you can see how many items are in each cache region using JConsole. Enjoy!

Friday, September 18, 2009

Hibernate Search FTW

In praise of Hibernate Search:

-Search is both easy to learn and easy to use
-A great book "Hibernate Search in Action" is available to tame the learning curve
-It is very well designed, exposing just the right amount of the underlying Lucene extendability. I am using Filters, programmatic construction of complex queries, FieldBridges to search custom types, and a custom Scorer for result ranking

Just wanted to put this out there: If you're thinking of using Hibernate Search, jump in. To the Hibernate Search team: thank you!

Friday, July 17, 2009

JBossCache 3 vs Oracle Coherence

I'm looking at both products right now. If anyone has real world comparative experience, please chime in.

My initial impression is that Coherence has a more sophisticated replication scheme that eliminates the need for all data to be replicated to all nodes; this probably helps them to get closer to true linear scalability.

My other impression is that Coherence is insanely expensive. Their cheapest edition is $4600 per processor; whoa!

Hibernate and JBossCache

While attempting to move from EHCache to JBossCache I was having a hard time finding information or documentation. Then I found this: http://galder.zamarreno.com/wp-content/uploads/2008/09/hibernate-jbosscache-guide.pdf

It took me almost two hours to stumble across this, hopefully this saves someone some time.

Monday, May 25, 2009

Maven Plugins for Eclipse

I've tried both q4e and m2eclipse pretty thoroughly at this point and I hereby declare m2eclipse the winner. My application is a multi-module project and q4e got terribly confused, m2eclipse handles this better. It's not perfect but way better than switching out to the shell to build, and a real time saver if you frequently work across multiple modules. Go get it now: http://m2eclipse.sonatype.org/

Tuesday, May 19, 2009

Hibernate Redux

Hey big suprise, another long ignored hibernate bug (ok ok this one is only six months old), this time it is creating duplicate Foreign Keys (increasing the size of my DB, certainly not *helping* performance). At least there is a workaround. http://opensource.atlassian.com/projects/hibernate/browse/HHH-3532

Monday, May 18, 2009

Has Hibernate Been Abandoned?

When I run into a serious defect like this one, and it hasn't been worked in almost a year, I have to wonder if anyone over at JBoss/RedHat works on Hibernate any more. Maybe they just decided to abandon it? Are there other similar big bugs that they seem to be studiously ignoring?
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3332

Sunday, April 19, 2009

Posting Source Code in Blogger

This should be so easy - BBEdit code tags anyone? Here's a nice and easy solution for posting cleanly formatted code samples in blogger: FormatMySourceCode

Saturday, April 18, 2009

Spring Security, HTTP Basic plus Form Authentication

This is probably pretty common - we have a restful API that can be used for third-party systems integration but which is also used to support our AJAX user interface.

To support both cases, I want HTTP-Basic and Form authentication to protect the service URLs - ideally spring security would be configured to transparently support either one.

I finally got this working but not as cleanly as I might have hoped for - if anyone out there has an improved solution I would love to hear it!

1. expose the API on two URLs, I created one service mapping for /api and one for /services both mapped to the same servlet, one will be http-basic and one will use form-login

2. Change the default filter-name that Spring uses:
<filter-mapping>
<filter-name>customSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



3. Configure spring security using the custom filter chain. Use a new instance of HttpSessionContextIntegrationFilter with allow session creation set to false to prevent the wasted overhead of session creation for http basic clients, who will likely not present JSESSIONID cookies. (thanks to magomarcelo here http://raibledesigns.com/rd/entry/upgrading_to_spring_security_2 for that one)


    <http auto-config="false">

<intercept-url pattern="/api/**" access="ROLE_USER,ROLE_GROUPADMIN,ROLE_SYSADMIN" />

<http-basic/>
<form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?msg=fail" default-target-url="/" always-use-default-target="true" />
<logout logout-url="/logout" logout-success-url="/login.jsp" />

</http>


<beans:bean id="basicExceptionTranslationFilter"
class="org.springframework.security.ui.ExceptionTranslationFilter">
<beans:property name="authenticationEntryPoint" ref="_basicAuthenticationEntryPoint"/>
<beans:property name="accessDeniedHandler">
<beans:bean class="org.springframework.security.ui.AccessDeniedHandlerImpl"/>
</beans:property>
</beans:bean>


<beans:bean id="httpSessionContextIntegrationFilterWithASCFalse"
class="org.springframework.security.context.HttpSessionContextIntegrationFilter">
<beans:property name="allowSessionCreation" value="true"/>
</beans:bean>



<beans:bean id="customSecurityFilterChain" class="org.springframework.security.util.FilterChainProxy">
<filter-chain-map path-type="ant">
<filter-chain pattern="/api/**"
filters="httpSessionContextIntegrationFilterWithASCFalse,_basicAuthenticationFilter,
basicExceptionTranslationFilter,
_filterSecurityInterceptor"/>
<filter-chain pattern="/**"
filters="_httpSessionContextIntegrationFilter,_logoutFilter,_formLoginFilter,
_securityContextHolderAwareRequestFilter,
_exceptionTranslationFilter,
_filterSecurityInterceptor"/>

</filter-chain-map>
</beans:bean>