Monday 28 June 2010

Release port 8080 after tomcat crash on windows 7

Check which process is using port 80

On the command prompt window, type the following command.

netstat -o -n -a | findstr 0.0:8080

C:\dev>netstat -o -n -a | findstr 0.0:8080
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       6352


Open Task Manager to check the process ID
  1. Right click on the taskbar to open the the task manager.
  2. Go to the Processes tab.
  3. Click the View menu
  4. And make sure you select the PID (Process Identifier)
Find the process and kill it

Tuesday 27 April 2010

Oracle: convert a varchar2 to a CLOB without dropping the table

Current defn of column in table NOTIFICATION

DETAIL VARCHAR2(4000),

Want it to be a CLOB.

create a temporary table 'NOTIFICATION2' with the required column defintions. This is then used as a template to convert the table column.

exec dbms_redefinition.start_redef_table('TSUSER','NOTIFICATION','NOTIFICATION2','ID, RECIPIENT, SUBJECT, TO_CLOB(DETAIL) DETAIL, FOOTER, ISSUE_DATE, SENT_DATE, SENT_STATUS, RETRY_COUNT, ROLE',dbms_redefinition.cons_use_pk);
exec dbms_redefinition.finish_redef_table('TSUSER','NOTIFICATION','NOTIFICATION2');

Thursday 15 April 2010

Scala asymmetrical function definitions

I was impressed by the clarity of the "Better Haskell solution" to this Odd Word problem. (Disclaimer: I don't know haskell, but I can just about read a simple example like the one in this problem.)

haskell:
import Data.List

 enumerate = zip $ cycle [0,1]

 oddwords x = foldl' add "" (enumerate ws) ++ "."
     where add "" (_,t) = t
           add s (0, t) = s ++ " " ++ t
           add s (1, t) = s ++ " " ++ reverse t
           ws = words $ takeWhile (/= '.') x


So I added a Scala version that is a translation of the Haskell which I'm repeating here which copies the essence of the Haskell algorithm as well as the names used:
object OddWordProblem1 {
  
    // define cycle as per Haskell as Scala does not define it 
    def cycle[T](seq: Seq[T]) = Stream.continually(seq).flatten

    def enumerate[T](words: Seq[T]) = cycle(List(0,1)) zip words   
    
    def oddwords(input : String) = {
        
        def add(stringSoFar : String, wordAndOddIndicator : (Int,String)) = 
            (stringSoFar, wordAndOddIndicator) match {
                case ("", (_, t)) => t
                case (s, (0, t)) => s + " " + t
                case (s, (1, t)) => s + " " + t.reverse
            }
        
        def ws(words : Seq[String]) = words takeWhile ("." !=)
        
        // split the string into words,  first making sure that "kansas." always becomes "kansas ."
        val words =  input.replaceFirst("""\.""", " .").split(" +").toList
        
        enumerate(ws(words)).foldLeft("")(add) + "."
    }

    
    def main(args : Array[String]) = {
        println(oddwords("whats the matter with kansas."))
    }
}



The core logic is a bit longer than the Haskell version because
(a) Its doing IO(!) and handling input corner cases and is self contained and runnable.
(b) It defines cycle which is no big deal
(c) It defines method parameters and their types
(d) It has more brackets than haskell as haskell does not use brackets for function application. Not much we can do about that.

So, we have to define method parameters and their types, or do we? Now it could be argued that defining the parameters and types is good for readability, but it would be nice to have the choice. But of course with inline anonymous functions, the parameters and types are not required. So instead of defining the add method separately, we can write,

def oddwords(input : String) = {

  def ws(words : Seq[String]) = words takeWhile ("." !=)

  val words =  input.replaceFirst("""\.""", " .").split(" +").toList

  enumerate(ws(words)).foldLeft(""){
    (_, _) match {
      case ("", (_, t)) => t
      case (s, (0, t)) => s + " " + t
      case (s, (1, t)) => s + " " + t.reverse
    }
  } + "."
} 

or to name the parameters without declaring the types,

// ...
  enumerate(ws(words)).foldLeft(""){
    (stringSoFar, wordAndOddIndicator) => 
      (stringSoFar, wordAndOddIndicator) match {
        case ("", (_, t)) => t
        case (s, (0, t)) => s + " " + t
        case (s, (1, t)) => s + " " + t.reverse
      }
  } + "."


So that's the asymmetry: Scala named function definitions require parameter names and types, and the anonymous functions don't, because the parameter types can usually be inferred by usage. The Haskell "where" syntax is rather nice and a similar feature in Scala would be great...

An imaginary Scala syntax for haskell style context based method definition:

// ... 
enumerate(ws(words)).foldLeft("")(add)
where {
  def add = 
    (_, _) match {
       case ("", (_, t)) => t
       case (s, (0, t)) => s + " " + t
       case (s, (1, t)) => s + " " + t.reverse
    }
  }  
  // other defs
}

It would just be an aliased anonymous functions which could (a) be used more than once in the expression and (b) simplify the overall expression compared to usage with anonymous functions.

Just a thought!

Friday 26 March 2010

The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

I got this error after redeploying a web app to tomcat6 in myeclipse 8.

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 22 in the generated java file
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:317)

The reason was that
javax.servlet.jsp.jar
javax.servlet.jar

had been copied to

...\webapps\myapp\WEB-INF\lib

And the versions conflicted with the tomcat versions

Deleting
javax.servlet.jsp.jar
javax.servlet.jar
from
...\webapps\myapp\WEB-INF\lib

fixed the problem

Tuesday 23 February 2010

Dynamically create spring bean

I used this in a dbunit test to create a dummy service layer bean dynamically.

The rest of the beans were configured in xml files. I created this bean dynamically in the test so that I didn't have to pollute the xml config.

// Dynamically create a DummyServiceToMakeCodeTransactional bean and register with spring
     DefaultListableBeanFactory autowireCapableBeanFactory = (DefaultListableBeanFactory) getApplicationContext().getAutowireCapableBeanFactory();
     AbstractBeanDefinition beanDefinition =
          BeanDefinitionBuilder.rootBeanDefinition(
               DummyServiceToMakeCodeTransactional.class.getName()).getBeanDefinition();
     autowireCapableBeanFactory.registerBeanDefinition("DummyService", beanDefinition);
     DummyServiceToMakeCodeTransactional bean = (DummyServiceToMakeCodeTransactional) 
     getApplicationContext().getBean("DummyService");