Friday, 23 September 2011

finding the correct spring jar for a known class

With Spring 3 you now have to get all the individual jars that you need for an application.

One solution is obviously to download the lot.

But what if you just want to install what is needed.
How do you easily find which jar a class is in ?

Well I have just found this Spring Bundle Search tool.

You can enter just the class name or the class with its full package name.

Wednesday, 31 August 2011

windows7 Administrator mode from the command line

Type 'cmd' in the Run window as normal but instead of simply hitting the return key, use Ctrl-Shift-Return

Monday, 4 July 2011

Auditing using AOP and annotations in Spring

Lord, how I dislike the old XML configuration for AOP (among other things).

I have not given myself the opportunity to try out Spring's annotation based AOP in spring, until now and boy is it easy (with one minor gotcha).

Here are the building blocks:

1) Make your own Annotation. This is a simple way to mark all methods you want to audit.

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ParmsAudit
{
  ActionAuditType  auditType();
}


2) ActionAuditType is an enum that will be used to identify the thing being audited.

public enum ActionAuditType {
USER_LOGGED_IN("User Logged In"),
USER_LOGGED_OUT("User Logged Out");

private String mDescription;
  private ActionAuditType(String aDescription) {
 mDescription = aDescription;
}


public String getDescription() {
 return mDescription;
}
public void setDescription(String aDescription) {
 mDescription = aDescription;
}
}

3) Create an Aspect. This is the code that is run when the method marked by the annotation is called.

Here is the minor gotcha - well for me atleast.
I had to list the full package path to the Annotation in the @Before statement.


@Aspect
public class AuditAspect {

@Before(value="execution(@uk.co.billcomer.audit.ParmsAudit * *(..)) &&   @annotation(parmsAudit)", argNames="parmsAudit")
public void logTheAuditActivity(JoinPoint aPoint, ParmsAudit parmsAudit) {
 String userName = getUserName();
 mlogger.info("Parms Auditing User Name: " + userName);
 mlogger.info("auditType:" + parmsAudit.auditType().getDescription());

 String arguments = getArgs(aPoint.getArgs());

 if (arguments.length() > 0) {
 mlogger.info("args-" + arguments);
 }
}

private String getArgs(Object[] args) {
 String arguments = "";
 int argCount = 0;

 for (Object object : args) {
   if (argCount > 0) {
     arguments += ", ";
   }
   arguments += "arg[" + ++argCount + "]=" + "[" + object + "]";
 }
 return arguments;
}

private String getUserName() {
 try {
   return SecurityContextHolder.getContext().getAuthentication().getName();
 } catch (NullPointerException npe) {
   return "Unknown User";
 }
}
}

4) Mark the method to be audited.


@Service("parms.DummyAuditThing")
public class DummyAuditThing implements DummyAudit {

@Override
@ParmsAudit(auditType = ActionAuditType.USER_LOGGED_IN)
public void aspectAuditMethodTwoParams(String param1, Long param2) {
 mlogger.info("In method to be audited param1[" + param1 + "], p2[" + param2 + "]");
}
}


5) Context configuration
This is needed to speed up the compilation process. Essentially it tells the compiler what classes will have Aspects
   
   <aop:aspectj-autoproxy proxy-target-class="true"/>   
   
   <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
   <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
   
   <bean id="bill.auditAspect" class="uk.co.billcomer.audit.AuditAspect" />

Wednesday, 1 June 2011

android phone calendar not synching

The other day the calendar on my android phone stopped synching correctly to my google calendars.

It was partly way there. It new about the calendars but would not display any of the data.

I found this which did not describe my solution 100% but put me on the right track.

Go to Settings->Applications->Manage Applications->Calendar and select 'Clear data'.
This will give you a pop up box saying all the applications data willbe deleted permanently. Do not worry & select the the 'OK'.

Now go back to your calendar app and you will find all your calendars restored.

Thursday, 26 May 2011

unix WC command out by 1 - wtf

I am develop on a windows PC (for my sins) but to keep me feeling secure I use cygwin (a unix shell for windows. Today this has done me in.

The application I am working has to create files and in the final line of the file it has to include the number of the lines in the file. All simple enough and it did exactly what it was supposed to do.

However when I came to check the results manually I used the unix 'wc' command and this gave me a row count 1 less than what I was expecting and what the footer said was in the file. Strange.

I edited the file with 'vim' and low & behold wc was incorrect - what is going on ?

I then edited the file with hexEdit and noticed there was no CR & LF on the last line. Adding these made wc give the correct count.



Thursday, 10 March 2011

Logging bind variables with hibernate

You could use something like p6spy as suggested by a friend of mine Andy, but I wanted something that was trivial to configure and potentially available to the live application and easy to set up.

After a bit of digging I found Sathya's blog with the solution I needed.

I already had the log4j jar.
What I was missing was the slf4j-log4j12 jar

and the following line in my log4j.properties

log4j.logger.org.hibernate.type=TRACE



Thursday, 24 February 2011

FileUpload with multipart/form-data in Spring 2.5.x and Annotations

Firstly a qualifier, This is for Spring 2.5.x with annotations.
I have not had the pleasure of upgrading to Spring 3.X yet.

There is some excellent documentation on File Upload with Spring.

However for an annotation driven Controller it misses out one important step, namely the @InitBinder annotation.

@InitBinder
public void initBinder(ServletRequestDataBinder aBinder) throws ServletException {
// to actually be able to convert Multipart instance to a String
// we have to register a custom editor
aBinder.registerCustomEditor(String.class, new StringMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}


Thursday, 17 February 2011

Chrome and Grouping/Organising tabs

Do you find that you end up with loads of tabs open for several different subjects, and not wanting to close them......just yet.

Well today I found this chrome plugin for grouping tabs. It is something I have wanted for a while.

It is not quite as funky as Forefox's soon to be released Release 4 Tab Organising but it does the job nicely.