Wednesday, April 27, 2011

jquery.dataTables

Great plugin for jQuery that makes creating a dynamically sortable, searchable, and page-able table via JavaScript a snap.

First create a properly formated html table with a unique id: eg. <table id="mytable"><thead></thead><tbody></tbody></table>

Then with one line of JavaScript, the table transforms into the dynamic table described above.
<script>
$('#myTable').dataTable();
</script>

PROS: Super easy with lots of baked in functionality
CONS: Requires all the table data to be written out to html page, possibly eating up unnecessary bandwidth.

See Also:

Friday, April 22, 2011

How to create a custom TypeConverter in Stripes

Stripes uses TypeConverters to take convert JSP responses (typically a serialized form), and convert it back into Java types. Most conversions that you will need are already provided by default (see like below), but I found with Enums sometimes special cases are needed. There are ways to register customer converts globally, but in this case I just needed it for a specific case.

Example Custom TypeConverter
public class MyEnumeratedTypeConverter extends EnumeratedTypeConverter{

@Override
public MyEnum convert(String input, Class targetType, Collection errors) {
  MyEnum type = //Your conversion logic here.
  if(type == null){
    errors.add(new ScopedLocalizableError("converter.enum", "notAnEnumeratedValue"));
  }
  return type;
  }
}

Once the class is defined, it needs to be wired up in the ActionBean. In the below snipped, note the converter param in the @Validate annotation.

Example Wire-Up
@Validate(required = false, on={"fetch"}, converter=MyEnumeratedTypeConverter.class)
public void setMyEnums(Collection myenums){
  this.myenums = myenums;
}

See Also:

Thursday, April 7, 2011

Ignoring Certs for Testing

Had an issue while using XFire (legacy version of CXF), where I needed to access a website using ssl but that had a self-signed cert. The Protocol constructor is depreciated, but for testing purposes works fine.

Example
//protocol=https, etc
protected static void ignoreCertsFor(String protocol, int port){
  Protocol.registerProtocol(protocol, new Protocol(protocol, new EasySSLProtocolSocketFactory(), port));
}

EasySSLProtocolSocketFactory basically uses a "null" implementation of a X509TrustManager.

"null" X509TrustManager Example
new X509TrustManager() {

  public void checkClientTrusted(X509Certificate[] certs, String authType) {
  }

  public void checkServerTrusted(X509Certificate[] certs, String authType) {
  }

  public X509Certificate[] getAcceptedIssuers() {
    return null;
  }
};

See Also:
http://stackoverflow.com/questions/2301548/calling-axis2-web-service-from-xfire-client-the-endpoint-reference-epr-for-the

Friday, April 1, 2011

How to create a Self-Executing Jar with Maven

Example
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>namespace.ClassWthMainMethod</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

Maven Assembly Plugin Example

Example
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <id>create-target</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
             <descriptors>
               <descriptor>path/to/assembly.xml</descriptor>
             </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>

See Also:

Monday, March 28, 2011

OSGi bootdelegation

I found a situation where an osgi bundle made a reference to an expected system library that wasn't there, but since the bundle did not include the dependency as an osgi reference, the framework (Felix in this case) threw a 'Class Not Found' exception.

Turns out, our friends at the OSGi Alliance have a solution for this kind of problem.

org.osgi.framework.bootdelegation=package

Essentially, packages specified will bypass the osgi environment and go directly to the VM.

Seems like this feature should be used sparingly and only when necessary.

MongoDB Shell Command Reference

Good reference for MongoDB shell commands:
http://www.mongodb.org/display/DOCS/dbshell+Reference