title

fire chief's random developer tidbits

Wednesday, June 1, 2011

Jersey JSON error after upgrading

We are stuck in the past with a lot of our libraries which causes headaches.  One example was that we were still using Jersey 1.0.3, which was causing problems when upgrading to Glassfish 3.1 from 2.1.  Upgrading all the project poms to Jersey 1.5 (which is bundled in Glassfish 3.1 at the moment, where 1.8 is almost done...) wasn't too bad, and I centralized the version number in the process.  The app deploys fine, but then we ran into this error:


javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class blah, and Java type class blah, and MIME media type application/json was not found

All it took to solve this problem was to add jersey-json to the dependencies:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.5</version>
</dependency>

Credit to http://stackoverflow.com/questions/6027097/how-to-produce-json-output-with-jersey-1-6-using-jaxb

Thursday, February 24, 2011

Running javascript inside a sandbox in a jvm

I'm putting a couple of pieces together here that weren't in one place.  When introducing Rhino javascript engine into our code, we wanted to lock down its access.  Java 6 hides the internal Rhino context well enough that I couldn't figure out how to get at it.  So to restrict access to a given set of classes when running untrusted Javascript inside the JVM you need to start your own Rhino context up, then hook in the filter.

The useRhinoDirectly() method gives a Rhino context in which we can set up a class filter:
http://stackoverflow.com/questions/4639892/whats-the-difference-between-java-6s-built-in-version-of-rhino-and-the-rhino-pa

Building the class filter is easier standing on someone else's shoulders.  This example blocks access to static methods:
http://riven8192.blogspot.com/2010/07/java-rhino-fine-grained-classshutter.html
(Not sure which Text class this was using, but String.split() seemed to do the job.)

Wednesday, February 9, 2011

Most awesome error message ever

Trying to list the contents of a keystore I got the most awesome error message ever:

$ keytool -list -keystore keystore.jks
keytool error: gnu.javax.crypto.keyring.MalformedKeyringException: incorrect magic

uh, what?

$ which keytool
/usr/bin/keytool

Also notice it has "gnu" in the class name.  The path /usr/bin wins over the later path /opt/jdk1.6.0_21/bin which is the keytool I actually wanted since I'm using Sun and not GNU.



Friday, February 4, 2011

Wicket deployment vs. development mode with Maven build

pom.xml:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                ...
                <webResources>
                    <webResource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                        <targetPath>WEB-INF</targetPath>
                        <filtering>true</filtering>
                    </webResource>
                </webResources>
            </configuration>
        </plugin>

   ...
  <profiles>
      <profile>
          <id>dev</id>
          <properties>
              <wicketconfig>development</wicketconfig>
          </properties>
          <activation>
              <activeByDefault>true</activeByDefault>
          </activation>
      </profile>
      <profile>
          <id>release</id>
          <properties>
              <wicketconfig>deployment</wicketconfig> <!-- variable in web.xml -->
          </properties>
      </profile>
  </profiles>


web.xml:

    <filter>
        <filter-name>WicketApplication</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>com.rootAppClassNameGoesHere</param-value>
        </init-param>
        <init-param>
            <param-name>configuration</param-name>
            <param-value>${wicketconfig}</param-value>
        </init-param>
    </filter>

Friday, December 17, 2010

Extremely useful vim settings

make the mouse work like it should by default:
:set mouse=a

make vim use vim improvements instead of defaulting to older vi behavior
:set nocompatible

show hidden characters like dos carriage returns (^M)
:set list

Debugging remotely and waiting for debugger attachment

Starting with Java 5, you can run a java program this way and have it suspended until you attach a debugger remotely:

-agentlib:jdwp=suspend=y,server=y,transport=dt_socket,address=HOST:PORT

More info here:

Thursday, December 16, 2010

Crazy Vim Stuff

Given a huge text file you want to extract some things from, here's how to delete all the lines that DON'T start with a regex in Vim:

:%s/^\(\(search text\)\@!.\)*$//g

then remove all the blank lines:

:g/^$/ d
(there's a space before the d)

then sort it with:
:sort

Help came from these pages:
http://vim.wikia.com/wiki/Search_and_replace
http://www.computing.net/answers/unix/delete-blank-line-thro-vi-editor/4552.html