Skip to content

Instantly share code, notes, and snippets.

@martinlau
Created August 18, 2012 11:32
Show Gist options
  • Save martinlau/3386235 to your computer and use it in GitHub Desktop.
Save martinlau/3386235 to your computer and use it in GitHub Desktop.
A simple clojure portlet
(ns au.com.permeance.clojure.ClojurePortlet
(:gen-class :implements [javax.portlet.Portlet]
:init construct
:main false
:state state)
(:import (javax.portlet ActionRequest PortletMode)))
(defn- include-path [this path request response]
(let [state @(.state this)
config (:config state)
context (.getPortletContext config)
request-dispatcher (.getRequestDispatcher context path)]
(.include request-dispatcher request response)))
(defn -construct []
[[] (ref {:count 0})])
(defn -init [this config]
(dosync
(let [state (.state this)]
(alter state #(assoc %1 :config config)))))
(defmulti -processAction
(fn [this request response]
(keyword (.getParameter request ActionRequest/ACTION_NAME))))
(defmulti -render
(fn [this request response]
(.getPortletMode request)))
(defn -destroy [this]
(dosync
(let [state (.state this)]
(alter state #(apply dissoc %1 (keys %1))))))
(defmethod -processAction :increment [this request response]
(dosync
(let [state (.state this)]
(alter state #(assoc %1 :count (inc (:count %1)))))))
(defmethod -processAction :reset [this request response]
(dosync
(let [state (.state this)]
(alter state #(assoc %1 :count 0)))))
(defmethod -render PortletMode/HELP [this request response]
(include-path this "/help.jsp" request response))
(defmethod -render PortletMode/VIEW [this request response]
(let [state @(.state this)
count (:count state)]
(.setAttribute request "count" count))
(include-path this "/view.jsp" request response))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment