Text

Grails Autocomplete richUI Javascript

I had a need to populate the name with one value and when selected push the id into the text field. Since this is probably a common, I’m providing a sample. The only lines that matter are the top and bottom. The name in line one needs to match the element in the last line. This will display the name and populate the input filed with the id.

 <richui:autoComplete name="person" controller="ActiveDirectory" 
 action="searchAJAX" title="Search for Employee" shadow="true"
 minQueryLength="3" queryDelay="1" foreceSelection="true"
 maxResultsDisplayed="15" 
 onItemSelect="document.getElementById('person').value=id;" />

Not worth much more ;)

Text

Groovy AD LDAP Connection with SSL

I needed to connect from groovy to an Active Directory server and make some LDAP queries using SSL. To get SSL working I needed to get there certificate into a trusted keystore. Openssl to the rescue.

openssl s_client -connect adserver.example.com:636

I then copied from the ——-BEGIN CERTIFICATE——- line to the ——-END CERTIFICATE——- line, including the BEGIN and END lines into a file adserver.crt. Next I needed to import it into a keystore.

keytool -import -trustcacerts -alias adserver.example.com -file adserver.crt -keystore store.jks

Now I can use the keystore in my groovy code.

#!/usr/bin/env groovy

import java.util.Hashtable
import javax.naming.*
import javax.naming.event.*
import javax.naming.directory.*

final String KEYSTORE_PW = "changeit";  
final String KEYSTORE_NAME = "store.jks"
final String LDAPS_URL = "ldaps://adserver.example.com:636/"
final String SEARCH_BASE = "OU=Users,DC=example,DC=com"
final String DOMAIN = "@example.com"

System.setProperty("java.net.ssl.keyStore",KEYSTORE_NAME)
System.setProperty("java.net.ssl.keyStorePassword",KEYSTORE_PW)

// here, we're just reading in some parameters...
def stdin = new BufferedReader(
        new InputStreamReader(System.in)
    )
print "username: " // your username to access LDAP
String username = stdin.readLine()
print "password: " // the password for that account
String passwd = stdin.readLine()

// next we set up an environment for LDAP
Hashtable env = new Hashtable()
env.put(
    Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory"
    )
env.put(
    Context.SECURITY_AUTHENTICATION, 
    "simple"
    )
env.put(
    Context.SECURITY_PROTOCOL,
    "ssl"
    ) 
env.put(
    Context.PROVIDER_URL, 
    LDAPS_URL
    )

// Here, we'll use the simple naming for binding.
env.put(
    Context.SECURITY_PRINCIPAL,
    username + DOMAIN
    ) 
env.put(
    Context.SECURITY_CREDENTIALS,
    passwd
    ) 

// Now we'll try and connect...
try {
    def ctx = new InitialDirContext(env)
    def attr = ctx.getAttributes("")
    def srchInfo = new SearchControls()
    // you'll want to use your own search base
    def searchBase = SEARCH_BASE
    // this is the magic search string sauce:
    def searchFilter = "(&(objectClass=person))"
    // we strong type the next var because it is 
    // used in a Java API that needs String[]
    String[] objAttribs=[
        "sAMAccountName",
        "givenName",
        "cn",
        "sn",
        "mail",
        "mailNickname",
        "userPrincipalName",
        "displayName",
        "memberOf"
    ]
    srchInfo.setSearchScope(SearchControls.SUBTREE_SCOPE)
    srchInfo.setReturningAttributes(objAttribs)

    // Now we get the results and loop through them...
    NamingEnumeration dirObjects = ctx.search(searchBase,searchFilter,srchInfo)
    def nodirObjects = 0
    while (dirObjects != null && dirObjects.hasMoreElements()) {
        def dirObject = dirObjects.next()
        println("'" + dirObject.getName() + "':")
        def attrs = dirObject.getAttributes()
        for(name in objAttribs) {
            println "\t * " + attrs.get(name)
        }
        nodirObjects++ 
    }
    ctx.close()
    println("Number of entries identified: " + nodirObjects)
} catch (Exception e) {
    println "Exception: " + e
}
Text

Pandoc HTML Template Quick Start

Pandoc is a markup converter that reads several formats and outputs into more. It is quick and easy to teach people the syntax since it’s a super set of markdown. For more information on padoc you should visit http://johnmacfarlane.net/pandoc/README.html. This project provides a modified template that uses the scaling grid system from http://adapt.960.gs/. There are minimal build scripts and minimal additional css to help make html documentation look nice. The project can be found on github:

https://github.com/nosrednakram/Pandoc-Quick-Start-With-HTML-Template

Text

Java Tools Installer

Having spent the better part of my life doing system administration I have found that consistency in systems make maintenance easier. To this end since I’ve started using java, groovy, grails, gant, etc. I discovered that the installation steps are the same for all of them just the names have changed. To make installing these tools consistent for myself and others I created a simple shell script to help with the installations. It has good documentation and for RHEL, Centos, Fedora it will use the alternatives command to make it easy to switch between versions. For more check it out on github:

https://github.com/nosrednakram/Java-Tools-Installer

Text

Sun Java 7 on Fedora 15 Linux

You’ll need to download the latest JDK form Oracle’s Java Download Page. Now we will install java with the following commands, make sure you specify your rpm and paths correctly if you have a different version of the jdk:

sudo rpm -ivh jdk-7u1-linux-x64.rpm
sudo alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_01/bin/java 17001 --slave /usr/lib/jvm/jre jre /usr/java/jdk1.7.0_01
sudo alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_01/bin/javac 17001
sudo alternatives --install /usr/bin/javadoc javadoc /usr/java/jdk1.7.0_01/bin/javadoc 17001
sudo alternatives --install /usr/bin/javah javah /usr/java/jdk1.7.0_01/bin/javah 17001
sudo alternatives --install /usr/bin/javap javap /usr/java/jdk1.7.0_01/bin/javap 17001
sudo alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.7.0_01/bin/javaws 17001

The first line installed the actual java software. The subsequent lines use the alternatives tool to allow us to have more than one package that provides a specific service on a machine and easily switch between them. You probably only need the first three alternatives but since it’s cut and past you might at well add them all. To see what java versions are available and switch between them now issue the following command.

sudo alternatives --config java

For more information on rpm or alternatives you should review the man page or search the web. Now you should be able to run the following and command to verify you have the correct version of java.

java -version

The next step creates or appends a the JAVA_HOME variable for a bash shell.

echo export JAVA_HOME=/etc/alternatives/jre >> /etc/profile.d/java.sh

These steps can be repeated for additional versions of java you may want to install and will allow for you to switch between them when desired. I also included the steps on Pastbin.com to make downloading them easier.

Text

Quick Shell Path Munge

I often need to do path munging in start scripts or when changing environments and find the following helpful. Also on Pastebin.com.

pathmunge () {
        if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
           if [ "$2" = "after" ] ; then
              PATH=$PATH:$1
           else
              PATH=$1:$PATH
           fi
       fi
}   
Text

Groovy/Grails No suitable driver found for jdbc:oracle:thin:@

I use grails as a wrapper around processes and do not tie it directly into our enterprise database. We do access our enterprise database using groovy jdbc connects:

import groovy.sql.Sql
....

This works great while developing but when I tried to make a connection after deploying as a war file using Tomcat 7 I received the No suitable driver found for jdbc:oracle:thin:@…. message. The correct ojdbc6.jar file is in the apps lib directory but for whatever reason it doesn’t load. To get around this issue I copied the ojdbc6.jar file into by $CATALENA_HOME/lib directory and then added the jar file to the class path when starting using the jsvc daemon from the commons-daemon project. Below is a example:

cd $CATALENA_HOME
./bin/jsvc -cp ./bin/bootstrap.jar:./bin/tomcat-juli.jar:./lib/ojdbc6.jar \
   -outfile ./logs/catalina.out -errfile ./logs/catalina.err \
   org.apache.catalina.startup.Bootstrap

The key element is the ./lib/ojdbc6.jar file listed at the end of the class path.

Text

Groovy/Grails Oracle PL/SQL type support

If you are using groovie’s Sql class or grails by extension with oracle you need to be aware that PL/SQL types are not supported.  I discovered this trying to retrieve  a boolen using the oracle provided DBMS_SESSION.IS_ROLE_ENABLED function which return an PL/SQL boolean. Below if quote from and oracle document:

Oracle SQLJ and JDBC do not support calling arguments or return values of the PL/SQL BOOLEAN type or RECORD types. Also, when using the Thin driver, they do not support calling arguments or return values of PL/SQL TABLE types (known as indexed-by tables). TABLE types are supported for the OCI driver, however.

You can write a wrapper around supplied functions/procedures to get around this if you need to. I have written a fair amount of PL/SQL that uses all of these data types and a few functions/procedures that return there results as one of the un-supported types. To this end if I need to write a generic PL/SQL wrapper I’ll post it.