Friday, December 2, 2011

Adding user data to a timer thread

I had the quite usual requirement to add user info to a timer.
Some code was retrieving user date from the request scope in order to log the data with each change in the database.
Unfortunately, the timer has no request scope associated with it. And of course no user object attached to the request scope.

With Spring you can user the RunAs mechanism to add rights to a user. This is exactly what I did.

1. First I use the thread scope found www.springbyexample.org/examples/custom-thread-scope-module.html which I configure as
  
      
          
              
                  
              
          
      
  


Now the authentication itself gets configured:
    
    
        
        
    


The third element of configuration is the proxy configuration after the timer.
    
        
        
        
            
                threadScopeAfterAdvice
                threadAuthenticationInterceptor
            
        
    


the threadScopeAfterAdvice only cleans the scope after the thread execution is done.

the threadAuthenticationInterceptor is a org.aopalliance.intercept.MethodInterceptor

public Object invoke(MethodInvocation invocation) throws Throwable {
        UserDetails userDetail = userDetailService.loadUserByUsername(userId);
        RunAsUserToken runAsUserToken = new RunAsUserToken("mykey", userDetail, USER_CREDENTIALS,
                userDetail.getAuthorities(), null);

        Authentication authentication = authenticationManager.authenticate(runAsUserToken);
        if (authentication.isAuthenticated()) {
            SecurityContext sc = new SecurityContextImpl();
            sc.setAuthentication(authentication);
            SecurityContextHolder.setContext(sc);
        }

        try {
            return invocation.proceed();
        } finally {
            SecurityContextHolder.clearContext();

        }
    }

Thursday, October 20, 2011

Step filtering in eclipse

With the Mockito framework it may be annoying to step through the internal Mockito code instead of your code.

With eclipse there is a way to prevent this. Step filtering.

Tuesday, July 19, 2011

Setting language in gimp

I regularly have the problem that my Windows machine is set for German! Some times I'm not even allowed to set it to English, but when I can, it doesn't work as it should. It is only half in English.

Changing the language of The Gimp. Well the problem is that gtk+ has the wrong language.
So control panel -> system -> advanced system setting.
Add a new environment varialbe named "lang" with a value of "c"
Restart The Gimp
et voila


thanks to Syko

Friday, July 1, 2011

JSF2 state saving and event phase shifting

An annoying aspect of valueChangeListener in JSF2 is that it is not handled in the phase I expected.







The method selectAllChangeListener is in the validation phase and not in the invoke application phase as you may need it.
The good news is you can re-throw the event.

public void selectAllChangeListener(ValueChangeEvent e) {
if (!e.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) {
e.setPhaseId(PhaseId.INVOKE_APPLICATION);
e.queue();
} else {
boolean selectAll = (Boolean) e.getNewValue();
boolean lastSelectAllStateSet = (Boolean) getStateHelper().get(LAST_SELECTED_ALL_SET);
if (lastSelectAllStateSet != selectAll) {
getTableModel().setSelectAll(selectAll);
getStateHelper().put(LAST_SELECTED_ALL_SET, selectAll);
}
}
}

It is important to queue the event on the same component. Which is what e.queue() does. It is equivalent to

e.getComponent().queueEvent(e);


Another important point is the call to getStateHelper.
You cannot keep states in a JSF component. You will loose the reference to the component after a refresh of your page (F5 on Firefox). You must keep them with the getStateHelper method.

Friday, June 24, 2011

Setting attributes in JSF2's commandLinks

One of the recurrent problem with JSF is how can I pass argument from my xhtml facelet page to my backing bean. I've seen various possibilities, but the simplest is probably this one.

<h:commandlink value="click me">
<f:setpropertyactionlistener target="#{myBean.mySetterMethod}" value="myNewValue" />
</h:commandlink>

The example is of course minimalist.

Monday, June 20, 2011

Spring with external configuration

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer is well known for configuring external properties.
What may be less known is how to define default values.

1. Setting the values on the PropertyPlaceholderConfigurer
 


file://${system.properties.pointing.to.config.dir}/config.xml





42





2. Setting the value on the bean
 


file://${system.properties.pointing.to.config.dir}/config.xml







Monday, May 30, 2011

Spring PersistenceUnitPostProcessor

I needed 2 persistence.xml files, one for testing and one for production.
The only difference was the transaction-type which I had to set to JTA, as we were using Atomikos as a transaction manager for production. But for unit testing, resource-local was a better choice (less configuration).

The solution I found was to use a "PersistenceUnitPostProcessor" which changes the value after loading.




















The interesting part is the persistenceUnitPostProcessors property which registers a post processor.
Notice also the persistenceXmlLocation property. As we deploy on Websphere and make use of JPA 2.0, we can't name our file persistence.xml. It would conflict with Webphere !

The "TransactionTypeSelectorPersistenceUnitPostProcessor" java class is trivial, it just sets the transaction type.


public class TransactionTypeSelectorPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {

private PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;

@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
pui.setTransactionType(transactionType);
}

public void setTransactionType(PersistenceUnitTransactionType transactionType) {
this.transactionType = transactionType;
}

public PersistenceUnitTransactionType getTransactionType() {
return transactionType;
}

}

Wednesday, May 4, 2011

Setting tomcats'language

I unfortunately have to work with computers with default language set to German.
Until now I was always able to change the language, but Windows does really support this. Which means I have part of my computer in English and many application remain in German.
It is very annoying for stack trace for instance.

Thus you can set the language of apache tomcat via a system property.
In eclipse you can set

-Duser.language=en

as VM argument to the launch configuration of tomcat.

Tuesday, April 19, 2011

VerifyError in Websphere

When deploying my application in Websphere 7.0 I got the following error. It is somehow related to class loading.
I could resolved the comflict by removeing the jta.jar file from the dependencies of my war file.
Caused by: java.lang.VerifyError: com/ibm/websphere/uow/UOWSynchronizationRegistry.registerInterposedSynchronization(Ljavax/tranhttp://www.blogger.com/img/blank.gifsaction/Synchronization;)V
 at org.springframework.transaction.jta.WebSphereUowTransactionManager$UOWActionAdapter.run(WebSphereUowTransactionManager.java:356)
 at com.ibm.ws.uow.UOWManagerImpl.runUnderNewUOW(UOWManagerImpl.java:1067)
 ... 38 more


Thanks to Erik-Berndt Scheper


The basic problem is that you should not provide classes that are already provided by Websphere.
The basically remove all "javax" package classes from you war.