Thursday, October 20, 2011

EasyMock: Capture passed in values

I was writing a unit test this morning where the void method I was testing created an instance of a objected and passed it into a subsequent method. I needed to capture that object so that I could verify that it was set up properly. EasyMock provides a way to do this.

http://blog.jayway.com/2009/03/25/easymock-capturing-arguments-from-multiple-calls/

example:

Capture captured = new Captured();
expect(serviceInstance.method(EasyMock.capture(captured)).andReturn(/* whatever */);

MyObject obj = captured.getValue();

//run unit tests on obj

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

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

Debugging Pax-Runner Applications

Include as flag passed into paxrunner.jar
--vmOptions="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

In IDE start a remote debugging session and use port 5005.

See Also:

Sunday, March 27, 2011

Sample Java usage for MongoDB

Maven
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.5</version>
</dependency>

Connect to Mongo
import com.mongodb.DB;
import com.mongodb.Mongo;

//...

Mongo mongo = new Mongo(host, 27017);
DB db = mongo.getDB("name of db"); //If db does not exist, then it will be created.

Insert JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

//...

DBCollection coll = db.getCollection("testCollection"); //If collection does not exist, then it will be created.
BasicDBObject doc = new BasicDBObject();
doc.put("hello", "world");

BasicDBObject inner = new BasicDBObject();
inner.put("key","value");
doc.put("inner", inner);

coll.insert(doc);

Fetch JSON Data
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection
import com.mongodb.DBCursor;

//...

DBCollection coll = db.getCollection("testCollection");
BasicDBObject query = new BasicDBObject();
query.put("hello", "world");
DBCursor cursor = coll.find(query);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

GridFS
import com.mongodb.gridfs.GridFS;

//...

GridFS fs = new GridFS(db, "collection"); //If bucket does not exist, it will be created.

GridFS Save File
import java.io.File;
import com.mongodb.gridfs.GridFSInputFile;

//...

GridFSInputFile file = fs.createFile(new File("/path/to/file.ext"));
file.save();

GridFS List Files
import com.mongodb.DBCursor;

//...

DBCursor cursor = fs.getFileList();
while(cursor.hasNext()) {
System.out.println(cursor.next());
}

GridFS Read File
GridFSDBFile file = fs.findOne("file.ext");
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
String line = null;
while((line = reader.readLine()) != null){
System.out.println(line);
}

Map/Reduce
Example Dataset:
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fab"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233d6d638a2ca105b1fac"), "name" : "Jane", "number" : 2 }
{ "_id" : ObjectId("4d8233dbd638e40a183d27f8"), "name" : "John", "number" : 2 }
{ "_id" : ObjectId("4d8233dcd638e40a183d27f9"), "name" : "Jane", "number" : 2 }

Code Example:
import com.mongodb.MapReduceCommand;
import com.mongodb.MapReduceOutput;

//...

String map = "function(){" +
"emit(this.name, {count: 1, sum: this.number});" +
"};";

String reduce = "function( key , values ){" +

"var n = { count: 0, sum: 0}; " +

"for ( var i = 0; i < values.length; i ++ ) {" +

"n.sum += values[i].sum;" +

"n.count += values[i].count;" +

"};" +

"return n;" +

"};";

MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
for ( DBObject obj : out.results() ) {
System.out.println( obj );
}

Expected Output:
{ "_id" : "Jane" , "value" : { "count" : 2.0 , "sum" : 4.0}}
{ "_id" : "John" , "value" : { "count" : 2.0 , "sum" : 4.0}}

Execute Shell Commands in Ruby

There are 3 ways in Ruby to execute Shell commands, each does it a little differently.

exec 'command'
This will execute the command and exit the Ruby script.

system 'command'
This will execute the command but you have no way to access the results. You will simply get a true or false indicating if the command was able to be executed. Call $? to get more information on the exit conditions

`command`
This will execute the command and give you access to any returned strings.

Create Class Dynamically in Ruby

Sample
def create_class_dynamically(mybinding)
clazz = 'class MyClass; end'
Kernel.eval(clazz, mybinding)
end

create_class_dynamically(binding)

Connect to SOAP Server in Ruby

Simple Example
require 'soap/wsdlDriver'

def self.remote_wsdl(url)
wsdl = SOAP::WSDLDriverFactory.new(url)
begin
return wsdl.create_rpc_driver
rescue
return wsdl.create_driver
end
end

tar - Archive contains obsolescent base-64 headers

Problem:
> tar -xvf myfile.tar.gz
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Archive contains obsolescent base-64 headers
tar: Error exit delayed from previous errors

Resolution:
> gzip -d myfile.tar.gz
> tar -xf myfile.tar

Open Port in Redhat

Instructions

  1. nano /etc/sysconfig/iptables
  2. Insert the following line with the desired port number (in this case, replace 22 with desired port)
    • -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  3. Save and close the file
  4. /etc/init.d/iptables restart

See Also

Debug Virgo from Eclipse

Eclipse Setup

  1. In Eclipse's Debug Perspective, click on the Run -> Debug Configurations menu
  2. Find "Remote Java Application" and then click New and fill out the information

Virgo Setup

  1. Start the server using the "-debug" & "-suspend" attributes

Magic

  1. Start Virgo, then start the configuration in Eclipse
  2. It may ask you to associate the code before step threw can happen, simply choose the code in your workspace that is appropriate.

See Also

Saturday, March 26, 2011

Tool for converting xml for html

Tool that make converting xml for rendering in html easy.

http://centricle.com/tools/html-entities/

How to build a basic Pax-Runner application with Maven that utilizes spring-dm

My goal is to create a Maven based solution that can automatically assemble a pax-runner driven application that is ready to be deployed.

To make this work with a larger application, there should be an outer pom that includes this and any other bundles as modules. This should be the last module processed, the assembly process will pick up on the other modules built at the same time.


Directory Structure:
  • pom.xml
  • src
    • main
      • etc
        • assembly
          • paxrunner-assembly.xml
        • scripts
          • run.bat - for windows
          • run.sh - for *nix

To start I created a Maven pom file that has pax-runner as a dependency and references a custom assembly file.

pom.xml
<dependencies>
<dependency>
<groupId>org.ops4j.pax.runner</groupId>
<artifactId>pax-runner</artifactId>
<version>${pax-runner.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<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>src/main/etc/assembly/paxrunner-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

The assembly file will tell Maven how to package the final output. The xml is fairly easy to read, see the maven-assembly-plugin documentation for specifics. The one thing worth calling out is the exclusion of the spring and related libraries, this is because pax-runner already has a built in profile that will download and wire them up properly.

paxrunner-assembly.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

<id>distribution</id>

<formats>
<format>dir</format>
<format>zip</format>
</formats>

<includeBaseDirectory>false</includeBaseDirectory>

<files>
<file>
<source>src/main/etc/scripts/run.sh</source>
<fileMode>0777</fileMode>
</file>
<file>
<source>src/main/etc/scripts/run.bat</source>
<fileMode>0777</fileMode>
</file>
</files>

<moduleSets>
<moduleSet>
<binaries>
<unpack>false</unpack>
<includeDependencies>true</includeDependencies>
<outputDirectory>lib/module-bundles</outputDirectory>
<dependencySets>
<!-- 3rd party libraries -->
<dependencySet>
<outputDirectory>lib/required-bundles</outputDirectory>
<excludes>
<exclude>org.springframework:org.springframework.core</exclude>
<exclude>org.springframework:org.springframework.aop</exclude>
<exclude>org.springframework:org.springframework.asm</exclude>
<exclude>org.springframework:org.springframework.beans</exclude>
<exclude>org.springframework:org.springframework.context</exclude>
<exclude>org.springframework:org.springframework.context.support</exclude>
<exclude>org.springframework:org.springframework.expression</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>org.aopalliance:*</exclude>
<exclude>org.apache.commons:*.logging</exclude>
</excludes>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>

<dependencySets>
<!-- Adds pax-runner to root of distribution -->
<dependencySet>
<scope>provided</scope>
<includes>
<include>org.ops4j.pax.runner:pax-runner</include>
</includes>
<outputFileNameMapping>${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
</dependencySet>
</dependencySets>

</assembly>

Finally for the scripts, these do nothing more than start up pax-runner and pass in any directories of bundles or configuration files.


run.sh
java -jar pax-runner.jar --profiles=spring.dm --workingDirectory=. scan-dir:lib/required-bundles scan-dir:lib/module-bundles

run.bat
@echo off
call java -jar pax-runner.jar --profiles=spring.dm --workingDirectory=. scan-dir:lib/required-bundles scan-dir:lib/module-bundles %1 %2 %3