Caused by: java.lang.RuntimeException: No such var: json/pprint

Clojure, Clojurescript No Comments »

If you get (for example when using a later version of cljsbuild):

Caused by: java.lang.RuntimeException: No such var: json/pprint

You may need

https://github.com/clojure/data.json/

Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Hippo CMS and PostgreSQL – The Setup

Hippo CMS, PostgreSQL No Comments »

The original instructions on the Hippo site are somewhat outdated. Moreover, the linked blog post is currently down but archive.org still has a copy:

http://web.archive.org/web/20101026204317/http://gotext.org/people/bluesman/en/2009/10/english-configuring-hippo-7-for-postgresql-and-tomcat/

Most of the instructions are still relevant but it’s probably unnecessary to install HST 2, so the first step can be skipped.

The instructions starting at “Go to your-project-dir/cms/src/main/webapp/WEB-INF…” are still correct, however in the repository.xml the section

  <Security appName="Jackrabbit">
    <AccessManager class="org.hippoecm.repository.security.HippoAccessManager"/>
    <LoginModule class="org.hippoecm.repository.security.HippoLoginModule"/>
  </Security>

now has to be 

  <Security appName="Jackrabbit">
    <SecurityManager class="org.hippoecm.repository.security.SecurityManager"/>
    <AccessManager class="org.hippoecm.repository.security.HippoAccessManager"/>
    <LoginModule class="org.hippoecm.repository.security.HippoLoginModule"/>
  </Security>

because of an upgrade. Note the new SecurityManager tag. You otherwise get

java.lang.ClassCastException: org.apache.jackrabbit.core.security.simple.SimpleSecurityManager cannot be cast to org.hippoecm.repository.security.SecurityManager

As for step three, it’s now unnecesary to copy around any jar files.

Simply add a dependency in <cms_root>/cms/pom.xml like so:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>

Then run

mvn clean install

and enjoy a scalable and flexible backend for your Hippo CMS.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Date Formats in Clojurescript

Clojure, Clojurescript No Comments »

Formatting dates in JavaScript can be tricky, even getting the name of the month.

It’s possible to use the clunky $.datepicker.formatDate (); from jquery.ui, but in Clojurescript we have the Google Clojure library at our disposal.

Its goog.i18n.DateTimeFormat class has a few useful functions that can be applied like so:

Example usage:

> (format-date-generic :LONG_DATE (js/Date.))

> “July 14, 2012”

> (format-date-generic “dd MMMM yyyy” (js/Date.))

> “14 July 2012”

> (format-date-generic “MMMM” (js/Date.))

> “July”

Ah, there’s the name of the month!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Thumbs from Downloaded Images in Clojure

Clojure No Comments »

The code creates a thumb from either a local file or a remote URI. It combines ideas from a blog post dealing with image downoads in clojure and an older post dealing with thumbnail creation.

(import javax.imageio.ImageIO)
(import java.awt.image.BufferedImage)
(use '[clojure.java.io :o nly [as-file input-stream output-stream] :as io])
 
(defn make-thumbnail-generic [input new-filename width]
(let [img (javax.imageio.ImageIO/read input)
imgtype (java.awt.image.BufferedImage/TYPE_INT_ARGB)
width (min (.getWidth img) width)
height (* (/ width (.getWidth img)) (.getHeight img))
simg (java.awt.image.BufferedImage. width height imgtype)
g (.createGraphics simg)]
(.drawImage g img 0 0 width height nil)
(.dispose g)
(javax.imageio.ImageIO/write simg "png" (as-file new-filename))))
 
(defn make-thumbnail-from-file [filename new-filename width]
(make-thumbnail-generic filename new-filename width))
 
(defn make-thumbnail-from-stream [uri file width]
(with-open [in (io/input-stream uri)
out (io/output-stream file)]
(make-thumbnail-generic in out width)))

Example usage:

(make-thumbnail-from-stream some-image-url (str “~/my_thumbs/” (some-image-id) “.png”) 100)

Enjoy!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Naïveté to Brilliance

Clojure No Comments »

Unlike Scala, even beginning learners of Clojure will experience the fun of functional programming almost immediately. 

Converting the Rails function “simple_format” from Ruby

def simple_format(text, html_options={}, options={})
        text = text ? text.to_str : ”
        text = text.dup if text.frozen?
        start_tag = tag(‘p’, html_options, true)
        text.gsub!(/\r\n?/, “\n”)                    # \r\n and \r -> \n
        text.gsub!(/\n\n+/, “</p>\n\n#{start_tag}”)  # 2+ newline  -> paragraph
        text.gsub!(/([^\n]\n)(?=[^\n])/, ‘\1<br />’) # 1 newline   -> br
        text.insert 0, start_tag
        text.concat(“</p>”)
        text = sanitize(text) unless options[:sanitize] == false
        text
      end

to Clojure (leaving out options)

(defn simple-format [text]
  (let [patterns 
      {#"\r\n?" "\n"  #"\n\n+" "</p>\n\n<p>" #"([^\n]\n)(?=[^\n])” (str #”$1″  “<br />”)}
      ]
    (str “<p>” (reduce #(clojure.string/replace %1  (key %2) (val %2)) text patterns) “</p>”)))

illustrates the fundamental naïveté of imperative programming with its silly reassignments, incidental state and mutability. It’s laughing at this and marvelling at the sheer brilliance of Clojure in equal measure that make Clojure so much fun.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]