Monday, January 4, 2010

Groovy on Grails & Sqlite

By default grails uses HSQLDB for development. I switched it to use file based, and it was alright for the most part, but I would find inconsistencies where I would delete a record, and the app would query db fine, but querying with HSQL manager would not return the correct results. Just getting the manager up was a pain HSQL file with manager (I used this guide).
Using sqlite with rails development, I've always liked it better and thought the gui tools were better. Following are the steps to get it working for those interested.
  1. Go to the link hibernate-sqlite for the project and sqlite driver. These two places is where you will find documentation, and pretty much all the info you'll need.
  2. In order to add the jars to your grails project you have to first install the ivy plugin using the command -
    grails install-plugin ivy
  3. add this dependency - dependency org="org.xerial" name="sqlite-jdbc" rev="3.6.16"
  4. now run grails get-dependencies
  5. 4 jars will be added in your lib folder you will need to delete the jdbc-api-1.4 jar. ( I haven't really looked into more details and hopefully someone working with grails longer can help but leaving it there causes a compilation exception)
  6. Now from the hibernate-sqlite page download the Local project either build yourself or with jars. Move the file from src/java/dialect/SQLiteDialect.java to your src/java/dialect/ folder
  7. Create a sqlite file somewhere in your path ( I put it in the project) and use the command from terminal -- sqlite3 dev.sqlite3 -- and just make sure the file gets created, you can run the command -- .databases -- .
  8. In the DataSource.groovy file just configure your datasource now .. example. dbCreate="create"
    url="jdbc:sqlite:/(path to file)/dev.sqlite3"
    logSql="true"
    dialect="dialect.SQLiteDialect"
    driverClassName="org.sqlite.JDBC"
  9. For good measure I just commented out the global hsqldb stuff at the top of the file.

That should be it to get your grails project working with sqlite. For a gui to the database, I like to use SQLite Manager a firefox plugin. If I left anything out let me know. Note: I haven't tested it out extensively but the crud tests I did run ran fine.
One thing to note is dbCreate option 'update' didn't work. I first had to use create, and then comment it out after initial db creation. Minor annoyance.

Thursday, September 24, 2009

Review of Google Sidewiki

If you haven't given Google Sidewiki a try yet, I highly recommend it.

Quickly what it is... For quite some time people have been coming up with ways to annotate the web, sidewiki opens up a sidebar, and allows you to leave comments, and read comments left by other people. Really I have seen this type of implementation but their are a few differentiators that make it much better.

The Good
1. Google's ranking algorithm to put the best comments to the top.
- what makes a good comment or not ? The use of language, reputation and longevity of your profile. There are also voting options, a simple yes and no option, and somehow adds to the secret sauce
2. Its easy to use and unobtrusive. You just hide the sidebar, and when the color changes you know that there's comments on this page.
You're able to highlight text, and create a bookmark on the page, and comment on areas of text. When you click on the image next to your comment, it automatically jumps to that area of the page and highlights the text.
3. Blog integration. If there are no comments, it will suggest other articles sometimes, and it allows you to post your comments to your blog.
4. Allows owners of site to own the first comment.

The Bad
1. Forces you to install google toolbar. More of an annoyance.


So go and try it out.

Friday, May 8, 2009

GenericDao w/ Spring, JDO, and GAE/J

In case anyone is interested. The only gotcha I've encountered so far, is that using the JdoCallback is preferred by GAE vs using some of the Spring defined implementations.


package jdo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.jdo.support.JdoDaoSupport;

import GenericDao;
import com.google.appengine.api.datastore.Key;

public class GenericDaoJdo <T, PK extends Serializable> extends JdoDaoSupport implements GenericDao<T, PK> {
/**
* Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
*/
protected final Logger log = LoggerFactory.getLogger(getClass());
private Class<T> persistentClass;

/**
* Constructor that takes in a class for easy creation of DAO
*/
public GenericDaoJdo(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}

public boolean exists(PK id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);
return entity != null;
}

public T get(PK id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);

if (entity == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(this.persistentClass, id);
}

return entity;
}

public T getByKey(Key id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);

if (entity == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(this.persistentClass, id);
}

return entity;
}

public List<T> getAll() {
return new ArrayList<T>(getJdoTemplate().find(persistentClass));
}

public List<T> getAllDistinct() {
Collection result = new LinkedHashSet(getAll());
return new ArrayList(result);
}

public void remove(PK id) {
getJdoTemplate().deletePersistent(this.get(id));
}

public T save(T object) {
return (T) getJdoTemplate().makePersistent(object);
}


}

Wednesday, April 29, 2009

Managing dependencies with Spring 3.0 M2 and DataNucleus

ASM this library is major p.i.t.a cause releases aren't backwards compatible, and is done for the performance improvements. Spring doesn't plan to upgrade as from this post and this post but instead will reference a packaged version of asm, due to release with Spring 3.0 M3 . This is all part of the cost of working with milestones.

Google app engine / java uses Data Nucleus to enhance class files for persistence and relies on asm 3.1. Currently I am using the maven-datanucleus-plugin to enhance my class files. With maven I couldn't figure out how to include both libraries. The solution was to define the asm 2.3.2 libraries with scope runtime, and change ant-macros.xml file to not failonerror to false. I haven't tested on gae/j itself and just hosted mode, and this seemed to work. Also I had to define the datanucleus-core dependency as runtime scope as well, because the plugin requires 1.1.2, and gae only works with 1.1.1 now.

Tuesday, April 28, 2009

GWT, Spring 3.0 MVC, and REST on Google App Engine / Java - Part 2

From Part 1 of GWT, Spring 3.0 MVC and REST on Gae/J posting, you should now have a spring application with REST up and running and should be deployable on google app engine.

Integrating GWT with your backend now is pretty straight forward for the most part. Restlet is a REST framework, they released a module called Restlet-GWT module. For more understanding about it and why use rest with GWT, here is a link to blog posting on release Restlet ported to GWT. I'll be referring to the Greeting demo when you create a new application with the Google eclipse plugin. In what ever you named your Entry point class, look for the function

private void sendNameToServer()

your going to replace everything from greetService.greetServer(textToServer, with this..

final Client client = new Client(Protocol.HTTP);
client.get("http://localhost:/greet.json", new Callback() {
public void onEvent(Request request, Response response) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel.removeStyleName("serverResponseLabelError");
JSONValue json;

json = JSONParser.parse(response.getEntity().getText());
JSONValue value = json.isObject().get("hello");

serverResponseLabel.setHTML(value.toString());
dialogBox.center();
closeButton.setFocus(true);
}
});

Sorry for formatting, but you can see
client.get("http://localhost:/greet.json", new Callback() {

here is where I'm calling the rest url to retrieve the json string returned from the backend and GWT already provides functionality to parse JSON. For more information on Restlet-GWT. Restlet Maven Repository.

I found an issue with running Spring MVC 3.0 on google app engine ( could be only hosted mode). GWT compiles and generates a bunch of css, and javascript files. To get the nice REST style url's all requests need to be mapped to context
( / )


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>



The servlet mapping will match the longest matching map first. So when something like


<script type="text/javascript" language="javascript" src="/static/app/app.nocache.js"></script>



is given .. this should go directly to the mapping default servlet (above example), and not the main spring dispatcher. The issue is with jetty vs tomcat and how they handle paths. From the posting I found on stackoverflow (servlet for serving static content) "The problem is that Tomcat's default servlet does not take the ServletPath into account (so it looks for the static files in the main folder), while Jetty does (so it looks in the static folder)." Luckily the user provides the file of a servlet. In the servlet I made one change in the servlet class provided.
In the function protected String getPath(HttpServletRequest req).. I changed it to return pathInfo; and not return servletPath + pathInfo; . This changes makes the behavior like Tomcat, and so you won't have to move all your files inside of a folder called static. This basically ignores the /static/ and looks for path after that. Now in your web.xml just define new servlet, and define for static mapping.

Thats it.. You should now have your GWT front end talking to your Spring 3.0 MVC application running in hosted mode, and GAE/J. The cool thing is now you can switch between regular spring mvc application functionality (exmaple: submitting forms) using html, css and GWT. Coming up - Nightmare dependencies with Spring 3.0 M2 and Data nucleus GAE/J, GenericDao with Spring and JDO, and Spring Security.

Tuesday, April 21, 2009

GWT, Spring 3.0 MVC, and REST on Google App Engine / Java - Part 1

When googleing how to integrate GWT and Spring/Spring mvc, the most popular answer is to use the GWT- Widget/Server Library. The solutions are wrapping pojos as an RPC service or for better performance taking a spring controller and adding RPC functionality by extension.

With the intoduction of REST features in Spring MVC 3.0, this should make integration between GWT and Spring more natural. This is how to get it all working together on Google App Engine / Java.

First use Spring 3.0 M2. To include this in maven


<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.spring-library</artifactId>
<type>libd</type>
<version>3.0.0.M2</version>
</dependency>



Add the milestone repository
SpringSource Enterprise Bundle Repository - External Bundle Milestones
http://repository.springsource.com/maven/bundles/milestone
Note: ensure your using asm 2.2.3

Now in the web application you need to create the REST support with MVC. The most important feature added is the ContentNegotiatingViewResolver, this will determine which view to present from Accept header or extension, and from media type can resolve appropriate view. Probably the more important type will be for JSON support. In the blog post from Spring about REST in Spring 3 they hint at the JacksonJsonView and its inclusion in WebFlow or Spring Js, and at the time of this posting, unavailable. Anyways.. I really don't care to add web flow, or spring js to my project when using GWT. The project json-lib-ext-spring : details provides a json view that works seamlessly. Ensure that you exclude all spring libraries in your maven dependency.

In you spring configuration , it will look like this now..


<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>





<bean id="greet" class="path.to.GreetJsonView"/>



In the GreetJsonView just :

import net.sf.json.spring.web.servlet.view.JsonView;
public class GreetJsonView extends JsonView {
}

In the future it should be as easy and changing the class that you extend when spring releases a proper JSON view.

@Controller
@RequestMapping("/greet")
public class GreetController {

@RequestMapping(value = "greet", method = RequestMethod.GET)
public String list(ModelMap modelMap) {
modelMap.put("hello","REST rocks");

return "greet";
}
}

Now when you go to /path/greet it will display the jsp or if you go to /path/greet.json it will return the json string.

For additional details on how to get Spring MVC started and configured -
The most helpful is the pet clinic demo for Spring 3 M2 found in the subversion repository.

AtomView and REST Support

Example program called Spring Finance

Thats it for part 1, in my next post, I will show how to get GWT working with your spring mvc back end through REST calls, and putting it all together to get it working nicely on GAE/J

Monday, April 13, 2009

Google App Engine / Java and Maven

When first creating a project with the elipse google plugin, your presented with a project with gwt and gae/j ( google app engine / java) support. The first question I asked is how to get this all working with maven. The best archetype I have found is here gae-mvn-archetype. Instructions as followed:

1 create GAE/J project from maven archetype, artifactId is the app id.
> mvn archetype:generate -DarchetypeCatalog=http://www.mvnsearch.org/maven2
maven2
2 Build project
> mvn package
3 Test
> dev_appserver target\app-name
4 Deploy
> appcfg update target\app-name

But better to go here for specific details Maven for Google Appengine
Two things I've found from first use. First I was receiving ResourceDoesNotExistException: Unable to locate resource in repository. To get around this, in your settings.xml, usually found in /Users/.m2 .. there's a node called interactiveMode. Just comment this line out. As to the exact details I haven't looked into it yet. Second the META-INF directory is created in the resources directory. I would move this to the webapps, directory so the files jdoconfig.xml and persistence.xml are included in final build. Now in the google groups in the posting found here GAE/J Maven Support there's an alternative that doesn't rely on ant or google app engine SDK. I've tried this one out as well but found it to be not as easy to follow or use. I guess its a matter of what happens in the future on support and features, as to which one will be the better choice.

GWT support:
There were two maven plugins I found for this found here : gwt-maven google code and here : Google Web toolkit plugin. Long story short, the contributors from the google code hosted project will now be contributing to the Gwt plugin found on codehaus.org. So using this plugin Google Web toolkit plugin, should be good. The issues I've found is most of the documentation found on the site, refer to version 1.1. Using their plugin repository there was no 1.1 at the time of this post only 1.0 is found on there plugin repository So the solution is to use there sandbox repository found here Sandbox repository. Here you will be able to find the 1.1 release of GWT maven plugin.