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: