Saturday, May 12, 2012

Getting Started with OpenShift Paas Cloud

Getting Started

Couldn't be easier, in a nutshell:
  1. Create an OpenShift account.
  2. Add your public key to the OpenShift website.
  3. Setup the OpenShift Eclipse plugin.
That is about it! Deploying couldn't be easier as well. When you sign up OpenShift (very nicely) creates you a free git repo. All you need to do to deploy to your server is to push your changes to your repo, it does the rest! All very easy and very nice.

Changing Your GIT Upstream

I don't really want to deploy with every push, so I push to an 'inbetween' repo. I then push to 'upstream' (my OpenShift repo) only when I want to deploy.

To add an extra repo in between, run from your GIT folder:
  1. git remote rename origin upstream
  2. git remote add origin git@github.com/{your-account/{your-repo-name}.git
  3. git push -u origin master
After that your OpenShift project will be in GitHub, and any future pushes to GitHub you can just use 'git push'.

Now, to deploy just use:
git push upstream

I hope this has helped someone out there...

Tuesday, May 08, 2012

PaaS (Platform as as Service) Solutions Review

Well, I have decided to start (finally) playing around with PaaS solutions. I have briefly looked at GAE vs OpenShift, my initial impressions of each:

GAE (Google App Engine)

Strengths
  • Integrates well with Maven, and POM is nicely setup with a Maven Archetype
  • Will soon be offering MySQL solution, so you can have proper transactional apps on it
  • Plays well with Spring
  • Offers a NoSQL datastore (BigTable)
  • Automatically scales well with not much effort/monitoring on a devs behalf
  • Ties in well with other Google offerings, such as Drive, Maps, and even user management
Weaknesses
  • No full Java EE stack offering
  • Hard to avoid vender lock in

OpenShift

Strengths
  • Zero vendor lock-in
  • Can run basically any application. You can deploy straight to a JBoss 7.1 server easily (or PHP, Perl, whatever tickles your fancy)
  • Has a nice Eclipse plugin
  • Deploys by checking into a specific GIT repo (haven't tried yet but looks fairly easy)
  • Automatically scales well with not much effort/monitoring on a devs behalf
  • Can use MySQL, PostgreSQL or even MongoDB out of the box
Weaknesses
  • The default MySQL install doesn't automatically scale, I am sure you can some how set up replication but haven't looked into yet.
  • No built in niceties that you get with GAE, such as automatic user management
  • Relatively new, and not as well used and therefore tested as GAE (yet)

Verdict

I like both offerings to be honest, it would just depend on your requirements and personal likes/dislikes. I think I am leaning towards setting up a new application on OpenShift, mainly due to the fact you can deploy to a REAL application server (as opposed to a lightweight one such as Jetty etc). Stay tuned for how I get on...

Friday, September 16, 2011

Why does f:validateDoubleRange only work for @SessionScoped?

Can someone explain to me why Foo in my example is always null when it gets to the validateDoubleRange class? The end result is the min value for the validator is always 0. The number 3 displays just fine on the page when in the outputText element.
It validates fine if I make the bean `@SessionScoped` instead of `@ViewScoped`

Controller:
import java.io.Serializable;
    import java.math.BigDecimal;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    @ViewScoped
    @ManagedBean(name = "fooController")
    public class FooController implements Serializable {
    
        private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FooController.class);
        private static final long serialVersionUID = 1L;
        private Foo foo;
        private BigDecimal amount;
        private Long fooId;
    
        public Long getFooId() {
            return fooId;
        }
    
        public void setFooId(Long fooId) {
            this.fooId = fooId;
            this.foo = new Foo();
            foo.setFooId(fooId);
            foo.setMinAmount(Double.valueOf(3));
            foo.setMaxAmount(Double.valueOf(10));
        }
    
        public Foo getFoo() {
            return foo;
        }
    
        public void sendAmount() {
            log.debug("sendAmount: " + amount);
        }
    
        public BigDecimal getAmount() {
            return amount;
        }
    
        public void setAmount(BigDecimal amount) {
            this.amount = amount;
        }
    
        public static class Foo {
    
            private Long fooId;
            private Double minAmount;
            private Double maxAmount;
    
            public Foo() {
            }
    
            public void setFooId(Long fooId) {
                this.fooId = fooId;
            }
    
            public void setMinAmount(Double minAmount) {
                this.minAmount = minAmount;
            }
    
            public void setMaxAmount(Double maxAmount) {
                this.maxAmount = maxAmount;
            }
    
            public Long getFooId() {
                return fooId;
            }
    
            public Double getMaxAmount() {
                return maxAmount;
            }
    
            public Double getMinAmount() {
                return minAmount;
            }
        }
    }

JSP:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                >
    <f:metadata>
        <f:viewParam name="fooId" value="#{fooController.fooId}" />        
    </f:metadata>
    <h:form id="myForm">
        <h:outputText value="This is correctly displayed: '#{fooController.foo.minAmount}'"/><br/>
        <h:outputText value="My Input:" />
        <h:inputText id="myInput"
                     value="#{fooController.amount}" 
                     required="true"
                     >
            <f:validateDoubleRange minimum="#{fooController.foo.minAmount}" maximum="80"/>
        </h:inputText>
        <h:message for="myInput"/>
        <br/>
        <h:commandButton id="bidButton"
                         value="Place Bid"
                         action="#{fooController.sendAmount}"
                         >
        </h:commandButton>
    </h:form>
</ui:composition>


I am using JSF 2 on JBoss 6.1

------------------------------------------- UPDATE-------------------------------------------
 Thanks to BalusC on StackOverflow I have the answer. "This problem is related to JSF issue 1492." See http://stackoverflow.com/questions/7445417/why-does-fvalidatedoublerange-only-work-for-sessionscoped/7447265#7447265 for the full answer and work arounds.

Wednesday, August 10, 2011

Search Strings and the Levenshtein Distance

I would just like to thank my friend Mark for reminding about the
Levenshtein Distance. What is the Levenshtein Distance? Well, it is simply a measure of how different two strings are. Very useful returning useful search results, dictionary suggestions, that kind of thing.

Friday, July 08, 2011

JSF Bug - Number Based Input Field Magically Changes From 'null' To Zero

The problem:
Submitting a form and the number based fields (BigDecimal/Number/Integer etc) that had an empty string (null) on the form get populated with zero ("0") values.

The problem domain:
Tomcat/JBoss servers. I am running JBoss 5.1.0.GA.

The solution:
It appears the team at Tomcat didn't want us to be able to have nullable number fields in a JSF form! The solution is to set this java system variable: "org.apache.el.parser.COERCE_TO_ZERO=false", e.g.:

run.bat -Dorg.apache.el.parser.COERCE_TO_ZERO=false

This was a bit of a nightmare to track down, I tried writing and stepping through custom converters and thought I was going insane. Hopefully this helps someone out there.

P.S. I know I said this is a bug, but technically (crazily?) the spec does say to convert null's to 0's. See: http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html.

Friday, July 01, 2011

JEE 6 Security - Part Two (the implementation)

Alright, I have set up authentication up in JBoss AS 6 using JAAS database (jdbc) authentication. The steps:

1) Setup the logging levels, this will save you some headaches, trust me. Don't forget to change it back once you are setup. Open up jboss-logging.xml under server/default/deploy, under the 'console-handler' tag, there is a 'level' tag, replace 'INFO' with 'TRACE'. Also, add by the other loggers the following:

      
   

2) Add your security domain. To do this create a new XML file, name it something like my-app-name-jaas-jboss-beans.xml and place it in the server/default/deploy folder. Add the following to the file:

  
    
      
        java:/jdbc/myapp
        
          select PASSWORD from USER where USERNAME=?
        
          SELECT r.NAME, 'Roles' FROM ROLE r, USER_ROLE ur, USER u WHERE
          u.USERNAME=? AND u.USERNAME=ur.USERNAME AND ur.ROLE_NAME=r.NAME
        
      
    
  

IMPORTANT: The role query has to have two coloums, the second of which is always 'Roles' exactly like stated. It's a JBoss quirk.

3) You have a security domain, now you have to tell your application to use it. Create or edit jboss-web.xml in your applications WEB-INF folder, it needs the security doman specified like so:

    /myapp
    java:/jaas/myapp-realm


4) Your application is set to use the domain now, but you haven't told it what needs securing and when to authenticate. For testing purposes I have used BASIC authentication, in reality you should use FORM based. I will leave you to research the difference. To test your security add the following to your web.xml:

        Secure Pages
        
            secure-pages
            
            /test/*
        
        
            
            MANAGER
            USER
        
    
    
        BASIC
        myapp-realm
    
    
        
        MANAGER
    
    
        
        USER
    

So I have covered authentication, and EJB 3.1 JEE authorization is covered tons everywhere. One thing lacking in the JEE world is full Identity management, i.e. something where you say identity.createUser("bob") and it will create a user in any abstracted back end user store, LDAP, Database or whatever. This tool does exist, and it is PicketLink. I haven't had a chance to play with it, but it looks promising. I would dare say it is overkill for most applications though, at least until it becomes more mainstream and simple to use.

JEE 6 Security - Part One (the research)

I am starting to look at security for my JEE 6 /EJB 3.1 application. I have to say there is plenty of information on authorization for EJB 3.x (for instance what methods/urls a user has access to etc) but very few simple articles on authentication and identity management (creating new users etc). Authentication isn't really part of the JEE spec as such, although all JEE 6 servers support JAAS.

Using a new library that handles identity management (PicketLink) sounds great, but I don't want to be stuck in a couple of years time replacing it because it is out of fashion. I also want to try not to use Spring Security, I want to stick with JEE standards (even though Spring Security is a pseudo standard).

I hope to follow this up with how I managed to get authentication setup using JAAS and probably JDBC/Database credentials, and how I create new users.

As a side note, I found a good article for Seven Security (Mis)Configurations in Java web.xml Files. Definitely worth a read.

Thursday, June 09, 2011

JRebel

I normally just try and post up useful code snippets and howto's. I am breaking this rule to rave about how great JRebel is! If you haven't heard of it, it INSTANTLY deploys code from your workspace onto your server, yes INSTANTLY. So, not just your JSP/XHTML changes but your CODE changes. So, say good-bye to your code and re-deployment cycles, and give JRebel a try.

P.S. I am no way affiliated with them, just love the idea of skipping the deployment cycle and wasting my precious time.

Monday, May 30, 2011

Internationalized h:selectOneMenu using a Enum

Using JSF2.0 it is now very easy to add internalionsed enums to your drop down (h:selectOneMenu) options. All you need to do is this:

<h:selectOneMenu id="colourSelect" value="#{colourController.colour}">
    <f:selectItems value="#{colourController.colours}"
                   var="colour"
                   itemValue="#{colour}"
                   itemLabel="#{enumBundle[colour.name()]}">
    </f:selectItems>
</h:selectOneMenu>

Simples! That may be enough for you to get going. Read on if you need more config info.

In faces config, add:
    <application>
        <resource-bundle>
            <base-name>/enumBundle</base-name>
            <var>enumBundle</var>
        </resource-bundle>
    </application>

Create a file '/src/main/resources/enumBundle.properties' (if using maven, otherwise place it in your default package) and add this to it:

RED=Colour Red
BLUE=Colour Blue
GREEN=Colour Green

Thats the resource bundle setup, now you just need the bean:

import java.io.Serializable;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;


@ConversationScoped
@Named("colourController")
public class ColourController implements Serializable {

    public enum Colour {
        RED, BLUE, GREEN
    }
    
    public Colour[] getColours(){
        return Colour.values();
    }

    public void colour(Colour colour) {
        System.out.println("colour: '" + colour.name() + "' was selected.");
    }
}

You DO NOT need to write your own converter (I have seen this recommended elsewhere) as JSF has it's own built in converter. This is all you need to do. I wasted a couple of hours on this, hopefully saves someone out there some time, let me know!

Tuesday, May 24, 2011

Adding Code Syntax Highlighting To Your Blog

Thanks to http://heisencoder.net I can now use code snippets and syntax highlighting in my blog. Click here to see how you can do it.

Using CDI Beans in your Faces Converter

Unfortunately, you cannot use @Inject within your FacesConverter. This becomes a problem when you want to convert, say, countries loaded from your database and cached in a context such as @ApplicationScoped. There are two answers here:

Option 1: If using JBoss
If you are using JBoss, just use Seam Faces. Just by including the library you can use the @Inject annotation, as well as @PostConstruct. Very Handy. If you are using Maven, just add this to your pom.xml:
<dependency>
<groupId>org.jboss.seam.faces</groupId>
<artifactId>seam-faces</artifactId>
<version>3.0.1.Final</version>
</dependency>
That is it! You can stop reading now :).

Option 2: If using GlassFish
Unfortunately, Glass fish 3.1 has a bug with the class loader, see:
Using Seam Faces exposes the bug. According to the bug report, 'you need to add dependencies to the classpath to satisfy any class that is referenced in a bean archive'. Not only annoying, but I couldn't get it to work even when dependencies where in the pom, it was one issue after another. Until GlassFish 3.1.1 I would recommend creating a simple Service Locator to lookup the CDI beans.

public class CDIServiceLocator {
    private static BeanManager getBeanManager() {
        try {
            InitialContext initialContext = new InitialContext();
            return (BeanManager) initialContext.lookup("java:comp/BeanManager");
        }
        catch (NamingException e) {
            throw new RuntimeException("Couldn't find BeanManager in JNDI");
        }
    }

    public static Object getBeanByName(String beanName) {
        BeanManager bm = getBeanManager();
        Set beans = bm.getBeans(beanName);
        if (beans == null || beans.isEmpty()) {
            return null;
        }
        Bean bean = beans.iterator().next();
        CreationalContext ctx = bm.createCreationalContext(bean);
        Object o = bm.getReference(bean, bean.getClass(), ctx);
        return o;
    }
}

You can then use the ServiceLocator to lookup beans like so:
countries = (List<country>) CDIServiceLocator.getBeanByName("countries");

This is a hack, no doubt about it. But until GlassFish 3.1.1 comes out, I believe its the best option - unless you can switch to JBoss. Big thanks to dominickdorn.com whose code and info this is largely based from.

HyperSonic/HyperSQL Quickstart

HyperSQL is a fantastic tool for local rapid development and testing. A lot of people don't seem to know about it, or just how useful it is. Some advantages:
  1. You do not need to install it, so you do not need local admin rights (a hurdle in corporate environments).
  2. It saves everything in a readable, and editable text file. In other words, the database IS the text file (see below for example).
  3. It starts up in no time (an empty DB for me starts up in 385 ms!).
  4. It is fully JDBC compatible.
As a quick start (based on release 2.2.2, which is the latest as of this post):
  • Download the latest HyperSQL/HSQLDB zip file
  • extract it to your local drive, in this example C:\bin\hsql
  • cd C:\bin\hsqldb-2.2.2\hsqldb\bin
  • make a new batch file, call it say hsqlStart.bat
  • edit the batch file, and put this in it:
  • java -cp ../lib/hsqldb.jar org.hsqldb.server.Server --database.0 file:C:\bin\hsqldb-2.2.2\hsqldb\data\myDb --dbname.0 test
  • Run the batch file from the comman prompt from the bin folder
  • Connect to the database using the jdbc compatible SQL editor of your choice, using the connection string: jdbc:hsqldb:hsql://localhost/test", "SA", "", and the driver found at: C:\bin\hsqldb-2.2.2\hsqldb\lib\hsqldb.jar
  • Refer to the HyperSQL/HSQLDB User Guide for anything not covered by this quick start.

As mentioned it saves all updates in a plain text file. So if you run:
    create table my_tbl(
    my_key INT primary key,
    some_text VARCHAR(256)
    )
and then open the file: C:\bin\hsqldb-2.2.2\hsqldb\data\temp.log, you will see it has the create table directly in there. This is VERY useful for troubleshooting and sanity checks during rapid development.

To shutdown the server cleanly type 'SHUTDOWN' in your SQL editor (such as Squiral SQL). You will notice it shutdown, and copies the data cleanly from the temp.log file into temp.script. This file is actually editable, so you can edit the SQL driectly in the script file, cool huh?

Hope this is useful for someone, shout out if it is!

Tuesday, September 05, 2006

CSS and Images not loading

Alrighty, my first (hopefully) helpful blog :).

I am using a hosting company for my webapp. They are using plain, good old Apache for hosting the static content such as images, css files and html pages. Requests are passed to Tomcat for processing any of the jsp files etc.

I noticed that the first time I went to my website after clearing the browser cache, the images and the CSS files were not being displayed or even found. This kept me up a few nights pulling my hair out, but I eventually noticed that when I disabled cookies the images and CSS NEVER got displayed!

The problem? After viewing the page source I noticed that without cookies enabled, the URLs included the session id in them, like so: "http://www.stroke-education.com/image/cart.gif;jsessionid=24A03F5B45814B192ABF9F7B1918B8AB.tomcat36". Of course Apache doesn't know how to deal with the jsessionid, it treats the whole thing as one URL, and it cannot find the file. The session id gets included the first time because tomcat isn't sure if cookies are enabled/disabled.

The solution? Tomcat has to get passed the requests that have jsessionid in the URL. My host company ended up forwarding all requests to Tomcat, which is overkill but it works.

A better solution is talked about here:
http://www.jguru.com/faq/view.jsp?EID=53878

They suggest adding the following to Apaches config:
<IfModule mod_rewrite.c>
RewriteEngine on
# Force URLs with a jsessionid to go to Tomcat. Necessary because
# Apache doesn't recognise that the semi-colon is special.
RewriteRule ^(/.*;jsessionid=.*)$ $1 [T=jserv-servlet]
</IfModule>

Welcome


After spending numerous hours searching blogs and the like for help with weird, wonderful, and just plain strange Java Web Development issues and bugs, I thought I should return the favor and post my findings.

The latest site I have made is http://www.stroke-education.com. It was an interesting one for me, as I hadn't dealt with credit card processing, or a website with high accessibility needs before. It was also my first contract based website.

The main technologies I used were:
  • Struts MVC
  • Hibernate
  • Spring
  • MySQL
I will post up some issues I had with it, and the solutions I came up with as I remember them, or come across new ones :).

I hope my postings will help a few people out there, so stay tuned!