File upload not working with spring roo (java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream)

June 6, 2011 Leave a comment

I have published spring roo project (spring mvc) on tomcat 7. Everything worked fine, except file uploading.

After submitting page with selected file, I’m getting java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream (full stack trace is below).

Google showed me that I was missing commons-io.something.jar library. I’ve copied org.apache.commons.io_2.0.1.v201101200200.jar to the WEB-INF/lib directory, restarted tomcat server, and it’s working fine.

Full stack trace:

SEVERE: Servlet.service() for servlet [RANDB] in context with path [/RANDB] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream] with root cause
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
        at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
        at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
        at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:155)
        at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:138)
        at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:887)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:750)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
        at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
        at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
        at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
        at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
        at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:636)

How to backup svn repository using svnsync and VisualSVN Server on Windows

January 2, 2011 2 comments

This guide is for windows (Tested on Windows 7)
Nice guide for linux / unix is available at http://www.thoughtspark.org/node/10

Steps:

  1. Instal VisualSVN Server
    this will also install svn and svnsync command line tools
  2. Create empty repository using VisualSVN Server managment console
    1. righ click on the Repositories
    2. Click Create new repository
    3. Enter repository name (do not check create Create default structure)
  3. Create svn user
    1. Righ click on the users
    2. Click Create users …
    3. Enter username and password
  4. Set permissions
    1. Righ click on repository
    2. Click properties
    3. Set read / write permissions for user
  5. Create empty hook
    1. Righ click on repository
    2. Click properties
    3. Open tab Hooks
    4. Click pre-revision property change hook
    5. Click edit
    6. Insert couple of blank lines
    7. pre-revision property change should be in bold text
    8. Click OK
  6. Initialize repository for svnsync
    1. Open command line (start>run>cmd)
      cd to the VisualSVNServer directory, for example:
    2. In this directory there should be svn.exe, svnsync.exe and similar binaries
    3. run following command:
      svnsync init https://MACHINENAME:8443/svn/project/ http://svn.example.online.repo.com/svn/project

      if needed, you will be asked for username / password

  7. Synchronize
    1. run following command:
      svnsync sync https://MACHINENAME:8443/svn/project/
    2. wait for synchronization to complete🙂

Linq to sql, ctx.submitchanges progress

January 2, 2011 1 comment

Linq to sql provides no default mechanism for tracking progress of the ctx.submitchanges() method.

This can be troublesome if you are using batch import with linq to sql (which is not the best practice by itself).

I asked question on the stackoverflow
http://stackoverflow.com/questions/4563828/linq-to-sql-submitchangess-progress

Response to the similar question was to divide inserts into multiple ctx.submitchanges() methods. This would work fine, but I needed transactions (all inserts or none)

I can get total number of inserts by using ctx.GetChangeSet().Inserts.Count;

Using ActionTextWriter class I found on
http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers

I was able to track progress of the inserts using following snippet of code

ctx.Log = new ActionTextWriter(s => {
  if (s.StartsWith("INSERT INTO"))
    ReportProgress();
});

Linq to sql will write to the log file every sql statement. if the statements begins with INSERT INTO, that’s new insert.

Evertything I need for the progress bar is here🙂

Also, performance wise, I’m reporting progress for every 10 000 inserted records. Refreshing screen too often can be slow sometimes.

Generating SVN statistics using statSVN

October 17, 2010 Leave a comment

Today I generated svn statistics for a project I am working on for a last year and half. It’s very insightful Smile

Here is a short guide on how to it using windows machine

  1. You should install java from http://www.java.com/en/download/manual.jsp
  2. Download statsvn from http://www.statsvn.org/downloads.html. I used version 0.7
  3. Download CollabNet Subversion Command-Line Client from http://www.collab.net/downloads/subversion/ and install it
  4. Do svn checkout
    C:\project>svn checkout http://svn.your.svn.repository.url.com/trunk/
  5. Generate svn log
    C:\project>svn log ––xml -v > svn.log
  6. Generate statistics
    C:\project>java -jar statsvn.jar Project\svn.log Project
  7. Wait Smile
    (my project has about 1 000 000 lines of code… I waited for a few hours)
  8. Open generated index.html and enjoy Winking smile

LinqToSql Sum over empty collection causes exception

October 15, 2010 Leave a comment

Linq to Sql Sum method over empty collection returns null. Linq to object Sum method over empty collection returns 0.

This Linq To Sql behavior can cause exception :

"The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

This will happen if you try to assign sum to decimal. Decimal is not nullable. If sum is called over empty collection (e.g. no items are return from where clause) sum will try to return null. decimal is not nullable and you have exception Sad smile

One solution is to cast sum so it returns nullable decimal (decimal ?), Second solution is to use linq to object, because, linq to object sum over empty collection returns 0.

Why is all this happening?

In SQL, Sum() aggregate operator returns null for an empty set. So this is as designed.

Since users often rely on LINQ to SQL producing the same results as an equivalent SQL query, we decided not to convert the null to zero as in case of LINQ to Objects. There are a number of cases where we considered forcing .NET/C#/VB semantics for consistency with LINQ to Objects vs retaining SQL semantics for consistency with SQL. In most cases, we favored the latter for consistency and more enforceable and performant implementation,

Source: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/479f2823-c942-4990-adca-21e03f969a12

 

 

You can read more about this issue on

http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

and

http://stackoverflow.com/questions/696431/linq-query-with-nullable-sum-problem/

Why foreign keys are important

October 14, 2010 Leave a comment

On one project, about two years ago, we have decided that we didn’t need relations in relational database.

So we deleted them. We just used tables for storage, almost like flat files.

At first, programming was easy, but then, we started implementing logic for relational integrity, and that was no easy task. We had to implement every “relation” manually in code…

After a while, we started using relations again. Relations are there for a reason. Sometimes it’s a pain in the neck to deal with them. But, it’s even worse without them.

In relational databases, just use relations. Many smart people have figured that out. Smarter than me, that’s for sure.

P.S. document databases also have some kind of “nesting” relations built-in.

Categories: Databases, Software design

Deinterlace video

October 12, 2010 Leave a comment

I was digitalizing couple of MiniDV tapes, and I’ve notices some horizontal lines… just like this:

distortion2

Those “lines” are there because of interlacing… technique used for smart compression… or whatever Smile

VirtualDub is a nice freeware video editing tool that has deinterlace filter Smilewhich will nicely fix this “lines” problem.

Next time you are digitalizing miniDV Tapes, don’t forget to turn on deinterlace filter