Sunday, October 28, 2007

Upgrade to Leopard, the story so far

I was really looking forward to upgrading to Leopard today but I'm not sure if I'm happier now that it's all done. Apple is starting to do some really stupid things lately. I've been a Apple/Next user/developer since 1992, eight years on Next and the last seven years on Apple. I have to ask, what the hell are they thinking? No Java 6 support, key bindings removed for Front Row, you cannot use an AirDisk with Time Machine, AirDisks don't work and so on. I guess I'll have to see how things go over the next week or two, but I'm not going to install Leopard on any other mac in my house till things get better. On a similar note, I bought a Apple TV a few months back and returned it after two days. I've never been unhappy with an Apple product before, but this seems to be becoming a trend :-) I love my iPhone, but Apple's tendency to control every aspect of my computer is really starting to piss me off. BTW, wtf is up with Front Row, when I browse out of the "Music" section the music stops, Front Row 1.x didn't do this, it played music till you picked something else for it to play. I'm going to watch a movie, I'm sure that will be more enjoyable than the last hour has been.

Thursday, October 11, 2007

Getting Maven and Eclipse to work together to filter resources

I don't really care for any of the Eclipse plugins currently available for Maven (m2eclipse or Q4E), however I do like the Maven plugin for eclipse (it seems to be the only one that works). At Overstock.com everyone uses Eclipse, so as we are moving to Maven it is important that Maven work well in the IDE. Most folks checkout their code, develop and then check there code back into svn via Eclipse, never hitting the command line. Obviously, the Maven Eclipse plugin won't be good for them, so currently we are using m2eclipse for Eclipse/Maven integration. One of the problems I've been running into is filtering resources, I believe if you are using m2eclipse and you choose (from the popup menu) Maven -> Update Source Folders all your resources will be filtered (if you have filtering turned on in your pom.xm). I don't want to do that and folks will forget to do it, furthermore, since I do use the Maven Eclipse plugin (requiring the command line) and I don't use m2eclipse, this won't work for me. So how do you get Maven and Eclipse to work together? This feature request for m2eclipse provides some pointers. Eugene suggests using a Maven build(er), but I cannot do that (because I'm not using m2ecilpse and I couldn't get it to work correctly when I tried), so I'll use a Program builder instead. In the Package Explorer, right click on your project and select properties, this brings up the Properties for xxx dialog box. In the list on the left, choose Builders, as shown in the image below (click on any of the images below for a larger version): Next, click the New... button and choose the Program builder: In the Properties for New_Builder (1) dialog box, use the Browse File System... button to locate your copy of the mvn executable. For the Working Directory section, click the Browse Workspace... button and select your project. For the Arguments section add the following:
resources:resources resources:testResources
Here is how the dialog should look like so far: To finish things off, select the Build Options tab at the top of the Properties for New_Build (1) dialog box and make sure the During auto builds check box is selected (actually you want all check boxes under Run the builder to be checked except for During a "Clean"). Next select the Specify working set of relevant resources check box and then click Specify Resources.... In the dialog box that pops up, navigate to your src/main/resources and src/test/resources folders and click the check box to the left so these folders are included in your working set, click Finish. Here is what the Build Options tab should look like: That's it, now when you refresh your workspace, try to run a unit test, etc. all your resources are filtered just as if you were doing it from the command line. If you have a way to do this correctly with m2eclipse or Q4E please let me know. Update: you may want to add the -o command line switch for Maven so that it doesn't try to look for JARs to download. You will want to add -o to the resources:resources... code above. Update 2: If you are using the m2eclipse plugin, check out Eugene Kuleshov's response to this post. Also, if you decide to keep using an external builder, you'll want to make sure the "Refresh resources upon completion." check box is selected on the Refresh tab of the builder dialog box. Update 3/12/08: In the "Build Options" section above you'll need to select all options under "Run the builder", so that it runs both during and after a clean. Also, I no longer use the m2eclipse builder, I only use a Program Builder.

Wednesday, October 10, 2007

Welcome to the Twilight Zone

Since I'm caught up on the latest Java Posse episode, I thought I'd listen to the radio on my way into work today. It turns out my local NPR station just started it's fall membership drive. After about five minutes of listening, one of the local sponsor's (a car dealership) joked about programming everyone's car radios to always tune to an NPR station. Laughing, I decided to switch stations, well guess what? Every station I turned to was NPR. No kidding, it was so bizarre that I had to call my wife to tell her. When I turned the radio back on, it was on another station. Then when I went back to NPR, all stations played NPR. I'm not sure what was going on but it seems that my radio only wants NPR ;-)

Friday, September 28, 2007

Annotated JAXB Classes

Over the last week or so, I've started to use JAXB along with the Restlet framework. We are actively developing RESTful web services here at Overstock.com. So being new to the Restlet framework, I was eager to get started. One type of representation supported by Restlet is of course XML. To generate XML representations we are using JAXB 2. Being an advocate of annotations, I thought I'd start with annotated POJO's and let the JAXB provider do the rest (I assumed this would be a lot like JPA). I ran into a problem however, trying to create a JAXBContext for my package, I got this error:
WARNING: Problem creating Marshaller
javax.xml.bind.JAXBException: "com.overstock" doesnt contain ObjectFactory.class or jaxb.index
It took me a while to figure out what went wrong. So now that I've got things working correctly, I thought I'd post this example and solution to hopefully save you some time. Given this class:
 1 package com.overstock;
 2
 3 import javax.xml.bind.annotation.XmlAccessType;
 4 import javax.xml.bind.annotation.XmlAccessorType;
 5 import javax.xml.bind.annotation.XmlElement;
 6 import javax.xml.bind.annotation.XmlRootElement;
 7
 8 @XmlRootElement(name="example", namespace="http://overstock.com/example")
 9 @XmlAccessorType(XmlAccessType.FIELD)
10 public class ExampleJaxbClass {
11
12   @XmlElement(required=true)
13   private String elementOne;
14   private String elementTwo;
15
16   protected ExampleJaxbClass() {
17     super();
18   }
19
20   public String getElementOne() {
21     return elementOne;
22   }
23   public void setElementOne(String elementOne) {
24     this.elementOne = elementOne;
25   }
26   public String getElementTwo() {
27     return elementTwo;
28   }
29   public void setElementTwo(String elementTwo) {
30     this.elementTwo = elementTwo;
31   }
32 }
You can easily convert it to XML via the javax.xml.bind.Marshaller class, like this:
 1 public class ExampleTest {
 2
 3   @Test
 4   public void generateXml() throws JAXBException {
 5     ExampleJaxbClass ex = new ExampleJaxbClass();
 6     ex.setElementOne("first Element Value");
 7     ex.setElementTwo("second Element Value");
 8
 9     // Get a JAXB Context for the object we created above
10     JAXBContext context = JAXBContext.newInstance(ex.getClass());
11
12     // To convert ex to XML, I need a JAXB Marshaller
13     Marshaller marshaller = context.createMarshaller();
14
15     // Make the output pretty
16     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
17     StringWriter sw = new StringWriter();
18
19     // marshall the object to XML
20     marshaller.marshal(ex, sw);
21
22     // print it out for this example
23     System.out.println(sw.toString());
24   }
25 }
Here is the XML generated by the annotations above:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:example xmlns:ns2="http://overstock.com/example">
<elementOne>first Element Value</elementOne>
<elementTwo>second Element Value</elementTwo>
</ns2:example>
Now for the problem. To create a marshaller, you first need to create a JAXBContext via its newInstance() factory method. You can create a context for a specific JAXB class, as in the example above, or you can create a context for a list of packages (check out the javadoc page for more). When using the Restlet class JaxbRepresentation (only available in Restlet 1.1m1), it uses the package version of newInstance(), that's when I got my error above. I didn't want to create an ObjectFactory (apparently this is another way to get around the above error), at least not yet if I could help it, so I wanted to get some more info on the jaxb.index file. I couldn't find out much, I even looked at the JSR-222 spec. Well, it turns out that all you need to do is add class names to the file and place the file in the package (directory) where your JAXB annotated classes reside (it's similar in one way to a jpa persistence.xml file but without the xml). Here is the content of my jaxb.index file for the example class above:
ExampleJaxbClass
As you can see, its just the class name, not the fully qualified name (the package name is determined by the directory you placed the file in) or the .class name. If you want to test this out, we need to slightly change the test above. Modify line 10 in the unit test above to look like this:
JAXBContext context = JAXBContext.newInstance(ex.getClass().getPackage().getName());
If the package com.overstock does not have jaxb.index file, this change will cause the test to throw the JAXBException. Add the file and everything works great. If you know where there is good documentation on this let me know I couldn't find any :-)
Code formatting courtesy of Code2HTML.

Friday, September 14, 2007

Goodbye consulting, hello Overstock.com

It would have been much better if I'd posted this month's ago but I've been so busy with work and the UJUG (Utah Java User Group) that I haven't had much time to blog. I'm hopping this will change from today onward. So my first bit of news is that after being a consultant for most of the last eight years or so I've finally decided to take a full-time job with Overstock.com. I actually started back in April of this year, so I've been there for a while now. I must say that I'm having more fun at Overstock.com than I've had in years. That is part of the reason I haven't been blogging, my day job is so satisfying that when I get home I don't need to challenge myself with something interesting, I get to do interesting stuff all day long. I'm hoping to start blogging a little more often (like more than every two months) and talk a little bit about what we are doing, from a technology standpoint that is. On another note, I just finished an article on JPA for TheServerSide.com. I think it will be posted on Tuesday of next week (Sept. 18th). If you are interested in looking at the source, you can check out the project page here.

Friday, July 20, 2007

Salsa and the iPhone

On Tuesday my wife and I (along with our three boys) went out for dinner. At some point my wife asked what time it was, so I reached down, grabbed my mobile phone (SLVR) and much to my surprise, it slipped out of my hands and right into the salsa in front of me. Once I cleaned it off, I told my wife it was 6 PM or something. Then I realized that the USB port was full of salsa. I cleaned it out the best I could and hoped that I hadn't toasted another phone (the last time I had a "cool" phone was about three years ago and that one was washed with the rest of my clothes about a week after I got it). When we got home, I plugged it in and let it charge all night. The next day, around 9 AM or so, I started to get the low-batter warning. Around 10 AM, it shut off. Another phone, gone. Joking around I said, now it's time for an iPhone. After talking with my wife for a while I realized that it would be really cool to get an iPhone. After all, I was now phoneless (never mind the beaten up, featureless one I had at home). A quick run down to our local Apple store, and I had an 8GB iPhone.

Friday, July 13, 2007

I was on the Java Posse?

I was really surprised last week wen I was listening to the Java Posse and who are they interviewing at the java.net booth? Java User Group folks, and low and behold there I am. Check out episode #129 at the 39:29 mark to hear about the Utah Java User Group. BTW, if you don't already listen to the Posse, I'd highly recommend it, it's a great source for the latest news and interviews with the who's who of the Java community.

Tuesday, June 12, 2007

Safari 3.0 Public Beta

After hearing about the various Mac OS X Leopard announcements, I was happy to see a public beat for Safari. I downloaded it yesterday and started to play around with it. I was really surprised, it is a lot faster than Safari 2.x. Not only is it faster, but I cannot live without the new inline find feature - it is so cool. Yes Firefox has had this for years, but not as usable as Safari's implementation. As a longtime emacs user, I've always liked inline searching way more than the popup finder window most applications have. If you haven't tried out the new Safari yet, I'd suggest you give it a try. If you are a windows user, have you tried it yet? The closest I get to windows is Parallels on my mac and I try to avoid that if possible too. Oh yeah, Apple also fixed the annoying quit "feature" where Safari would just quit, no matter how many windows you had open, if you hit apple-q (or Safari->Quit Safair). Again, another Firefox feature but a welcome addition nonetheless. Go windows? Let me know how Safari performs. Update: I just installed Safari in Windows via Parallels, and to my surprise not only did Safari get installed but so did Software Update. Wow, Software Update is an integral part of Mac OS X. Does this mean that Apple is starting to take over the Windows desktop? What do you think?

Tuesday, May 8, 2007

CommunityOne comments so far

After JavaOne last year, I told myself I'd attend NetBeans day in 2007. A month or so ago I registered for JavaOne and signed up for NetBeans day (not a very smooth process, I actually had to call someone to register for NB days because I'd already registered for J1) anyway, just after that Sun started to promote CommunityOne Day. So here I am at the first CommunityOne day, the day before JavaOne starts. The reason I wanted to come to NetBeans day (now CommunityOne day) was because it is so small. James Gosling just walked by me and he wasn't mobbed :-) I attended a presentation earlier today where Jonathan Schwartz was speaking and he was standing right behind me before the session started. Where else could you talk to Jonathan Schwartz with only 100+ people in the room? The message today has been pretty clear, Sun is going to open source all of its SW and innovation is the key. Almost every talk I went to today emphasized innovation. The argument goes like this, if Sun isn't trying to control everything, then their engineers are free to think about and work on innovation, it's what will set Sun apart from the others. Jonathan described the market as a really big pie and that there is plenty of room for many players. He also described how Sun will make money with this strategy, pull-through business by selling support, services and hardware. I think for me personally, I'm really excited about what Sun is doing. By making everything open source and freely available, means I can use the SW at home and on my own personal projects and in turn I can use it at work. I can see CommunityOne day being really big in a year or three so if you get a chance, I'd suggest trying to attend next year, before it's as big as JavaOne itself. For me, being able to talk to Jonathan or stop James in the hall is reason enough to come to CommunityOne. The content is somewhat different from J1. Instead of only being about Java (you could argue J1 is also going this way) you get exposure to everything Sun is doing in the open source space.

Java SE: Present and Future

Presented by Danny Coward Java SE Platform Lead, Sun Microsystems (blogs.sun.com/dannycoward) Everything you need to know about what's going on in the Java Platform, Standard Edition (Java SE platform). The goal is to give you an overview of everything that has happened over the last year or so. Danny will talk about things available today and things that will be covered in SE 7 tomorrow. Agenda, it's all about Java SE 6 Platform, the making of Java SE: java speaks in many tongues, breaking up is hard to do, Java SE on the desktop, some important upgrades. Changing the face of java, we are shown a slide with a timeline showing the various Java releases: JDK 1.0 pre 1997, J2SE 1.2 pre '99, J2SE 1.3 around 2000, j2SE 1.4 2002, pre 2005 J2SE 5.0 and just before 07 Java SE 6. Java SE 7 may be available just before 2009. Year in review, JDK 6 adoption, slightly more than half of all downloads of the JDK are SE 6.0. Java 6 JRE was release in April of 07. There are about 50,000,000 JDK downloads a month. The top 10 new features in SE 6, Web Services, Scripting, database, more desktop API's (SwingWorker, JTable sorting and filtering, GroupLayout - the layout manager used by Matisse), monitoring and management, compiler access, pluggable annotations (define your own annotations), desktop deployment, security, the -ilities: quality, compatibility, stability. Tools as part of SE 6: jconsole, jps, jmap, jhat (analyze memory usage), jstack (see BOF-2816), you don't need to start the JVM with any special options to use the probs, they attach dynamically. Java SE 6 is much faster than previous version of Java. Today, Java SE is open sourced, available under GPL. There are a couple of components only available in binary format due to licensing issues. As part of Java SE 6, the team started to do weekly builds and that made the move to open source much easier. Open JDK Community now available at openjdk.java.net. An interim governance board has also been establish (see Opening Session for member names). How Java SE Platform gets developed, the JCP defines API Specifications and OpenJDK implements those specifications. Why go multi-lingual? the Java programming language is the best general purpose language! Many other languages, many other virtues, rapid prototyping and experimentation, particular styles of programming, mixing different types of developers, or just for fun. In Java SE 6, Sun implemented JSR 223, Scripting for the Java Platform, developer APIs to mix script fragments in, Framework APIs for adding script engines, Collecting conforming scripting engines, see scripting.java.net, Sun added a JavaScript technology engine, JavaScript technology works out of the box. Here is a code sample: ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine js = m.getEngineByExtension("js"); js.eval("print('Hello, world!')"); Check out the SE 6 demo directory for examples of how to use the new features. Multiple Languages in JDK Version 7, turbo-charging scripting engines, new bytecode for dynamic method dispatch, 'supporting dynamically typed languages on the Java Platform (JSR 292)', investigate hot swapping. Bundling more dynamic languages and engines, see JSR 274, JRuby, Jython, BeanShell, JavaFX technology script. Java SE is an enabling technology for these new technologies. Changes in Java SE 7 (for a complete list, check out Alex Miller's blog), reading is more important than writing, one language same meaning; everywhere, simplicity matters. Seeking a small number of changes for SE 7. Candidate changes for Java SE 7, superpackages, extensions to the annotation syntax, language support for java technology properties, control abstraction constructs (closures, CICE, first-class methods), operator overloading,rough edges (shorter variable declaration, strings in switch statements, etc. Modularity in Java SE 6, deployment time, interfaces and implementation classes, information hiding, assertions. Deployment time, JAR and Resources framework. One of the problems is with the packaging structure, let's say you have a picture and text package and they in turn use a data package, but the client can access all three package. What if you only want to selectively provide access to packages or classes in the package you can use superpackages. For an indepth discussion see the Strawman Proposal for JSR 294. Packaging, as in ZIP files and later JAR files, was originally put together for applets, but the specification hasn't changed in quite a while. This will allow you to define versions, dependencies, etc. and will likely be called JAM files, JAR file will still be supported however. For modularity see superpackages JSR 294, modularity 277 & 291 OSGi interoperability. They both have open mailing lists. Swing development, swing is a powerful toolkit, some developers are put off, time to make it easier. See JSR 295, 303, and 296 for how Swing will be simplified. However, SE 7 is a ways off, so what can we do for SE 6? How does Java stack-up against Flash? Not well, start-up time is poor, installing the JRE is slow, install tool is not consumer friendly and the size of the JRE keeps getting bigger and bigger (~ 12 MB). To fix these problems, Sun is going to release a consumer JRE release in late 2007 early 2008. Qickstarter, pre-load the cache, before launch, not the same as having a running VM, cooperates with the OS, a radically improved experience. Java Technology-based kernel, modularizing the JRE software, just enough to run "hello world", install the rest in the background: referencing a class, Class.getResource or equivalent, System.loadLibrary() or equivalent, custom JRE version for applications that need it. As a prototype, the team put together some trial, to run "hello world" the JRE was 2 MB, the SwingSet2 demo was just under 4 MB, whereas the whole JRE is 12MB. In SE 7, JMX is going to be revamped, see JSR-255 Java Management Extensions v 2.0 (JMX) and JSR-262 for web services connector for JMX agents. JSR-203, in early draft, a new file system API, java.nio.filesystem.Filesystem, listen for file-system changes. Other Kiri's to note 360 Javadoc Tag Technology Update and JSR 310 Date and Time API. Java SE 6 is available today, Open JDK is being built See planetjdk.org for Java SE blogs.