Friday, June 10, 2011

ResourceBundles and special characters

Seems that some special characters when used in a ResourceBundle proper file will throw off the parser for substitution. These characters need to be escaped properly, the escape notation is as follows:

The special chars \', { and }:

escape ' with another ' '' (double-single quote)
escape \ with another \
\\
(double backslash)
enclose } with '
'}'
enclose { with '
'{'

See also:

Monday, May 9, 2011

Autowiring objects not instantiated by the Spring Framework

In order to enabled autowiring capabilities, add the following properties to the beans element.
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context"


And make sure this line is included within the beans element
<context:annotation-config />

Next, in your code, get an instance of AutowireCapableBeanFactory.

Hint: Subclasses of AbstractApplicationContext have a getAutowireCapableBeanFactory, just one way to get such an instance.

Finally call the autowireBean(MyObject) method and like magic, so long as the annotations are correct, Spring will inject the correct instances.

Thursday, May 5, 2011

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