Monday, January 26, 2009

J2EE/Java Grips 1: Websphere is garbage, OMFG 1

I was debugging a websphere/RAD issue, thinking that maybe I had messed up a configuration way down in the cellar of RAD's infinite supply of configurations. Maybe the flux-capacitor node mx3e cell.xml missed the rptptppt_Vn_ setting. No my friends, RAD seemed to have bombed on a missing servlet tag in the web.xml. NPE, NullPointer on not having the most basic setting in a J2EE app. Don't get me wrong, a developer should have the setting, but something more helpful like 'You are missing your servlet mapping' would have been helpful. here is the configuration and the error.


<servlet-mapping>
<servlet-name>OnlineServlet</servlet-name>
<url-pattern>/OnlineServlet</url-pattern>
</servlet-mapping>

------

[1/26/09 23:30:14:166 EST] 00000047 SystemOut O Caused by: java.lang.NullPointerException
at com.ibm.ws.wswebcontainer.webapp.WebAppConfigurationHelper.constructServletMappings(WebAppConfigurationHelper.java:405)
at com.ibm.ws.wswebcontainer.webapp.WebAppConfigurationHelper.createConfiguration(WebAppConfigurationHelper.java:147)
at com.ibm.ws.webcontainer.metadata.WebMetaDataFactory.createMetaData(WebMetaDataFactory.java:169)
at com.ibm.ws.runtime.component.MetaDataMgrImpl.createMetaDataFromFactories(MetaDataMgrImpl.java:135)
at com.ibm.ws.runtime.component.MetaDataMgrImpl.createMetaData(MetaDataMgrImpl.java:243)
at com.ibm.ws.runtime.component.DeployedModuleImpl.start(DeployedModuleImpl.java:561)
at com.ibm.ws.runtime.component.DeployedApplicationImpl.start(DeployedApplicationImpl.java:814)
at com.ibm.ws.runtime.component.ApplicationMgrImpl.startApplication(ApplicationMgrImpl.java:948)
at com.ibm.ws.runtime.component.ApplicationMgrImpl$1.run(ApplicationMgrImpl.java:1478)
at com.ibm.ws.security.auth.ContextManagerImpl.runAs(ContextManagerImpl.java:3811)
at com.ibm.ws.security.auth.ContextManagerImpl.runAsSystem(ContextManagerImpl.java:3893)
at com.ibm.ws.security.core.SecurityContext.runAsSystem(SecurityContext.java:245)

Thursday, January 15, 2009

Simple Agents/Concurrent Programming in Clojure


"All concurrency issues boil down to coordinating access to mutable state. The less mutable state, the easier it is to ensure thread safety."

-- Java Concurrency in Practice (from Bill Clementson's blog)

Like Erlang, Clojure is becoming a very powerful concurrent oriented language. It is designed to be a simple language making it easier to coordinate access to resources.

"Agents provide independent, asynchronous change of individual locations. Agents are bound to a single storage location for their lifetime, and only allow mutation of that location (to a new state) to occur as a result of an action"

-- http://clojure.org/agents
Here is a simple example of two agent functions 'agent' and 'send':

;;
;; Simple example of agents (concurrent coding) in Clojure
;; The send function waits/hangs and then returns the result.
;;
;; Note: only the calls to 'Thread.sleep' in the main program
;; block the main thread.
;; @see http://clojure.org/agents
;;
;; Function definition: (agent <state>). Example, (agent "Init")
;;
;; Function definition: (send <agent> <func> & <args>).
;; Example, (send abc (fn [_] "Done"))

;; (Author: Berlin Brown (on 1/5/2009))
;;

;; (A) Define the agent as 'abc'.
(def abc
(agent "Initial Value of Agent = This string"))

;; (B) The function to send a message to the agent.
(defn go []
(send abc
(fn [_]
;; Wait here for 5 seconds)
(. java.lang.Thread sleep 5000)
(println "Print from concurrent send agent")
"1 2 3 4 (Final Value, I am done)")))

;; (C) Main entry point of the program
(defn main []
(println "Running")
;; (1) Send a message to 'abc' agent by calling 'go'
(go)
;; (2) Wait for 1 second
(. java.lang.Thread sleep 1000)
;; (3) What is the value, we shouldn't be done yet?
(println @abc)
;; (4) Wait some more, 6 more seconds
(. java.lang.Thread sleep 6000)
;; (5) Hopefully after 7 seconds, the agent is done,
;; Check the value.
(println @abc)

;; Wait a little bit more and then print done.
(. java.lang.Thread sleep 12000)
(. System exit 1)
(println "\nDone"))

(main)
;; End of Script

Output of Running Program
Running
Initial Value of Agent
Print from send agent
1 2 3 4 (Final Value, I am done)

Resources:

Clojure COP/OOP Article

http://clojure.org/agents

-----
Edited: Here is an example using a Java Thread approach (some code not included):

(defn file-monitor-loop [file]
(let [delay-t (prop-int resources-core-sys "Octane_Sys_filemonitor_delay")
enable-file-mon (prop-bool resources-win-opts "file_monitor_enabled")
pth (. file getAbsolutePath)]
(when enable-file-mon
(.start (new Thread (fn []
(while (not (. shell isDisposed))
(Thread/sleep delay-t)
(when (file-modified? file)
;; Reload the file as it grows and refresh
;; the file.
(open-file pth true)))))))))

-----

Friday, January 2, 2009

Dexter and Death Note

Overview of Dexter and Death Note



I am taking a detour from my normal posts and going to discuss two pop culture icons. Dexter is a modern TV series on showtime that details the life of a serial killer that also happens to be a forensic scientist. Don't be alarmed, yes there is gore but it is part black comedy so it is hard to take the constant stream of murders very seriously. Death Note is an Manga and Anime that has a similar theme, Light Yagami is a young kid that just happens to come into contact with a 'Death Notebook'. If the owner writes down a name in the notebook, that person dies. Essentially, Dexter and Light are two 'good' characters that are following through with their own style of vigilante justice. Dexter is a smart scientist that was trained by his detective foster parent to murder other murderers. Light is an intelligent teenager that is using his magical notebook to rid the world of evil-doers.

Both series will make you question corporal punishment, the law and the death penalty. Should some third-party have the authority to kill another? Is there any justification for murder? Light is a little bit more liberal as an executioner. He has killed FBI agents and innocent women. His goal is to lay down justice but also not get caught. Dexter on the other hand, really questions his so called 'code'. In the series, he says he doesn't kill children and kills other murderers. He too doesn't want to get caught but has questioned killing the innocent in order to save his own skin. In both cases, the justification for wanting to continue playing this game at all costs is, does one mistaken murder of an innocent trump the overall mission. That mission is to rid the world/community of evil-doers. In both series, you encounter scenarios where that question gets asked.



Similarities between Death Note and Dexter

  • Light and Dexter are both super intelligent and quality males. I guess this is to offset their evil character. If you are a serial killer/vigilante, I guess it makes sense for your 'good' character to be extremely good.
  • Light and Dexter sort of have girl-friends to make them seem normal. And as expected, the girl-friends are clueless and don't know what is going on when Light or Dexter aren't around.
  • Both Light and Dexter have a strong devotion to family. Light really cares for his father and Dexter is out to protect his sister.


Differences between Death Note and Dexter

  • Death Note has a high degree of fantasy, Anime fiction. The whole concept of a Death Note stretches reality just a bit. As far as we know, Light has killed hundreds or thousands of so called villains and has not been caught. Dexter has only killed in the 40s, 50s.
  • Light will kill innocent people to achieve his goal. Dexter has really tried not to break his 'code' that he learned from his father.
  • Light is a teenager, Dexter is an adult. Light is amazingly intelligent but the writers still give us a sense of young idealism.