Posts Tagged java

lame java collection erasure problem

Just ran into an example of where Java’s dumb erasure strategy just didn’t cut it.

I wrote a small class to collect up vararg junk into typed collections, something like:

public class Collector {
    public Collection collect( typename... vs ) {...}
    public Collection collect( Collection collection, typename... vs ) {...}
};

This worked fine up until the point where the typename was a typed collection, at which point the second method was invoked instead.

My work around was to rename the latter to “collectInto”

Lame…

Leave a Comment

dear maven, yes! I want to use fckng java 1.5 generics, you ass!

OMG! I am so…. tired of doing this for every new archetype’d project:

<build>
	<plugins>
		<plugin>
			<!-- seriously? -->
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.5</source>
				<target>1.5</target>
			</configuration>
		</plugin>
	</plugins>
</build>

The gnashing of teeth!

Leave a Comment

code too large

This is always funny to me… Generated code to put every dictionary word into a set:

(09:21:50 pistolero.java)→ javac Dicto.java
Dicto.java:25: code too large
    public Set load( Set words ) {
                         ^
Dicto.java:3: too many constants
public class Dicto {
       ^
2 errors
(09:22:16 pistolero.java)→ ls -lh Dicto.java
-rw-r--r-- 1 brian brian 2.2M 2009-10-23 09:20 Dicto.java
(09:22:22 pistolero.java)→ wc -l Dicto.java
80341 Dicto.java

Leave a Comment

how ’bout a nice game of mvn+seam+jsf mit jboss?

I spent some time looking for a good recent archetype to do this that actually worked before giving up and deciding to do it in 3 easy pieces as part of a multiproject.

Since I’m pretty gnu to seam/jsf, I decided to start with seam-numberguess-2.1.0.CR1.ear and put it back together again.

Sadly, this process didn’t result in a completely clean solution… in the end I couldn’t quite get the ear plugin to do what I wanted and ended up overriding the application.xml, jboss-app.xml, jamming some stuff into a lib dir in the ear etc.. Hope to clean it up at some point.

Perhaps you can get softeu to work for you… for me it just blowed up in various interesting ways…

multiproject

groupId=jsf-in3
package=us.versus.them.demos.jsf.in3
version=1.0.0-SNAPSHOT
common="-DgroupId=${groupId} -DpackageName=${package} -Dversion=${version}"
webapp=-DarchetypeArtifactId=maven-archetype-webapp

mvn archetype:create ${common} -DartifactId=in3-project
cd in3-project
cat pom.xml | sed 's,>jar<,>pom<,' > fu
mv fu pom.xml
mvn archetype:create ${common} -DartifactId=in3-jar
mvn archetype:create ${common} ${webapp} -DartifactId=in3-war
mvn archetype:create ${common} ${webapp} -DartifactId=in3-ear

Unfortunately, there is a lot more junk that has to be done by hand… Stuff that is really pretty dumb.

java 5, please

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

jboss repo

    <repositories>
        <repository>
            <id>central</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>jboss</id>
            <name>JBoss Maven Repository</name>
            <layout>default</layout>
            <url>http://repository.jboss.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

war: wtf is it good for?

So I pulled the goodies out of the war and put them in their maven-ish spots:

.
|-- pom.xml
`-- src
    `-- main
        |-- resources
        |   `-- messages.properties
        `-- webapp
            |-- WEB-INF
            |   |-- components.xml
            |   |-- pages.xml
            |   `-- web.xml
            |-- cheat.jspx
            |-- confirm.jspx
            |-- giveup.jspx
            |-- index.html
            |-- index.jsp
            |-- jboss-seam-numberguess.war
            |-- lose.jspx
            |-- niceforms.css
            |-- niceforms.js
            |-- numberGuess.jspx
            `-- win.jspx

there is a also a “images” directory under webapp I left out, cuz it’s kinda boring.

This structures is a little lame, but I don’t wanna change too much right now…

war deps

There are a two deps that I resolved to something like this:

  <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.7.0</version>
  </dependency>
  <dependency>
      <groupId>jboss</groupId>
      <artifactId>jboss-seam-ui</artifactId>
      <version>2.0.0.BETA1</version>
  </dependency>

jar it

mkdir -p src/main/resources
touch src/main/resources/seam.properties

seam.properties has to end up in the root of the jar or seam will not work!!!! it will be broken!!! even though it is just a dumb empty properties file!!!! don’t forget it!!!!

You will end up with a nasty bit like:

javax.servlet.ServletException: Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectOne(selectGuessRadio).  Found null.

and even if you hack the jspx you will end up with something like:

javax.servlet.ServletException: /numberGuess.jspx(42,82) '#{numberGuess.currentGuess}' Target Unreachable, identifier 'numberGuess' resolved to null

And you may tear your hair out! And you may gnash your teeth! And when you find out the problem you may want to post it on your blog so noone else will have to suffer the same fate! I would imagine… ;-)

jar it up some more

mkdir -p src/main/java/org/jboss/seam/example/numberguess/
cp NumberGuess.java ${_}

I snagged mese’f a copy of NumberGuess.java

The biggest thing the tutorial left to the imagination was the imports:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

Presumably in the interests of brevity…

jar deps

  <dependency>
      <groupId>jboss</groupId>
      <artifactId>jboss-seam</artifactId>
      <version>2.0.0.BETA1</version>
  </dependency>

ear is totally wrong

I dunno what else to say about it… Ended up redoing the whole pom and even then it wasn’t right. The jboss-app.xml was always empty, I couldn’t get it to quit being dumb about being smart about ejbModules…

In the end, I clobbered the generated application.xml and jboss-app.xml with the ones from the seam-numberguess-2.1.0.CR1.ear

cp pageflow.jpdl.xml cheat.jpdl.xml target/in3
cp jboss-app.xml application.xml target/in3/META-INF
jar cf in3.ear -C target/in3 .
mv in3.ear ${JBOSS_HOME}/server/default/deploy/

final product

As I said in the beginning this was not a 100% success, but it was a lot closer than when I started:

% wget http://brianin3d.googlepages.com/in3-project.tgz
% tar xfz in3-project.tgz
% cd in3-project
# this assumes you have JBOSS_HOME defined...
% mvn package &&  ( cd in3-ear/ ;  ./bother.sh )

Hit http://localhost:8080/seam-ng-mvn to see it in action.

A rough start, but at least it’s a start… :-D

Leave a Comment

jar-czar-publisher

Well, if you read some of my other yammering, you know I’ve been doing a lot of publishing to S3 lately. I tried a number of tools, but none of them did exactly what I wanted:

  • recursively upload files
  • push corrent mime type
  • make everything world readable
  • ignore CVS directories

I do a lot of Java mess, so Java seemed like a fitting choice… I had the s3-example-libraries-1.0.0.jar and the whole gestalt is kinda Java-ey and why am I apologizing?

LOL

Java is a perfectly respectable language. It’s not like I wrote it in perl!

jar-czar-publisher expect to find a file called s3.properties that looks something like:

key    = <put your key here>
secret = <put you secret here>
bucket = <name of your bucket>
email  = <your email address>
source = publish

To make things easy, you can use bin/run.sh to kick everything off after running “mvn package“.

The application will recurse all the files under publish and push them up to your bucket. For example:

                         xsl/headers.xsl ->                  publish/xsl/headers.xsl
                         xsl/footers.xsl ->                  publish/xsl/footers.xsl
                            xsl/user.xsl ->                     publish/xsl/user.xsl
                          xsl/search.xsl ->                   publish/xsl/search.xsl

Now the url “http://mybucket.s3.amazonaws.com/xsl/headers.xsl” will pull back the file from “publish/xsl/headers.xsl”.

To keep things easy-peasy, I leave the stuff where ever it is on disk and link it’s directory in publish ala:

% mkdir publish
% cd publish
% ln -s ${HOME}/tmp/somejunk
% cd -
% mvn package
% vim s3.properties
% ./bin/run.sh

using that zip

That jar-czar-publisher.zip contains the source code… Speaking more accurately, it contains the CVS directory. In order to use it you’ll need to setup a local cvs repository.

Just in case you haven’t done this, shame on you! Everything you write should go into CVS!

j/k

These instructions are for bash… Running ‘em in other shells is left as an excercise for the reader… :-P

% mkdir -p /tmp/cvs/a-go-go
% cd ${_}
% export CVSROOT=${PWD}
% cvs init
% wget http://brianin3d.googlepages.com/jar-czar-publisher.zip
% unzip jar-czar-publisher.zip
% mkdir -p /tmp/some/more/dumb/junk/from/brian
% cd ${_}
% cvs co jar-czar-publisher
% cd ${_}
% echo come on.. I think you know what to do from here...

If you don’t…

% mkdir -p ${HOME}/.m2/repository/com/amazon/s3/s3-example-libraries/1.0.0/
% cd ${_}
% wget http://brianin3d.googlepages.com/s3-example-libraries-1.0.0.jar
% cd -
% mvn package
% echo see above

a few things about a few things

OK, I hate to explain a joke, but let me say something about some of that… “${_}” is a bash-ism that means “whatever the last argument of the previous command.” Got it off a good friend of mine name of John Taylor.

The other “cd -” just means change directory back to wherever I just came from.

a word about mime types

I really don’t like mime types. I think they are dumb. Ever use the file command? That’s smart! A list of magic keys to identify file type. Heck, even guessing by file extensions is smarter!

I always wondered why there was no Java equivalent of the file command, but I have things to do, so I went with the still pretty dumb solution of file extension.

It’s just a property file lookup. I made if from /etc/mime.types like so:

% grep -v '#' /etc/mime.types | awk '{ \
    for ( i = 2 ; i <= NF ; i++ ) printf( "%-40s = %s\n", $i, $1 ); \
}' > mimes.properties
% wc -l src/main/resources/mimes.properties
453

mimes.properties seems pretty handy….

Yup, just like that. I used to think I was pretty good at shell scripting, but it always ends up with being pretty ok with awk. I’ve written some fancier awk, believe you me.

If you really care:

public abstract class Util {
    public static final Properties MIME = new Properties();
    static {
        try {
            MIME.load( new FileInputStream( "src/main/resources/mimes.properties" ) );
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }

    public static String mime( String file ) {
        String mime = MIME.getProperty( file.replaceAll( ".*\\.", ""  ) );
        return null == mime ? DEFAULT_MIME_TYPE : mime;
    }

    public static String mime( File file ) {
        return Util.mime( file.toString() );
    }
};

bit of s3 naming advice

I really wish someone had pointed this out earlier. If you want to use S3 to host stuff for your website:

name your bucket something from your domain

For example: ” xml.jar-czar.com” and then point a cname at “xml.jar-czar.com.s3.amazonaws.com”. That way, when you want to give people a URL, you can give them http://xml.jar-czar.com/browse/a/a.page00000035.xml instead of http://xml.jar-czar.com.s3.amazonaws.com/browse/a/a.page00000035.xml

Leave a Comment

Older Posts »