Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, July 23, 2009

Running Pylons with Jython for Beginners

Well I'm sure someone out there besides me is interested in Pylons running on the JVM. Jython is a project working to bring Python to the JVM. If you are a Java user, and also a Pylons user you may be interested in the following.

To get started you need to install Jython. You can visit the Jython Site or get the 2.5.0 release here. You will need a version >= 2.5.0. The download is a jar based installer. You will need to run this as an application. On some platforms you can run the installer by double clicking the jar file (worked on OS X). On others you may need to go to the command line and issue this command "java -jar <jarname>".

Once Jython is installed setup easy_install for jython. Get ez_setup.py (find it here). Once you have ez_setup.py downloaded run it using "jython ez_setup.py". This will install easy_install in Jython's bin folder.

Now if you work with Python you'll probably want to create an alias for this easy_install. I did. I just put the following in my .profile (note I also am aliasing virutualenv):
alias jeasy_install="$JYTHON_HME/bin/easy_install"
alias jeasy_install2.5="$JYTHON_HME/bin/easy_install-2.5"
alias jvirtualenv="$JYTHON_HME/bin/virtualenv"
Another note, don't use JYTHON_HOME in your bash env if you want virtualenv to work. I substituted JYTHON_HME. If you wan't to see why you can't use JYTHON_HOME check out the jython script. This needs to use JYTHON_HOME to configure the java classpath and run the Jython application.

Get virtualenv installed for Jython. Just type "jeasy_install virtualenv". Once that finishes you should have a 'virtualenv' tool in the Jython installation's bin folder. Note I have an example above of aliasing this to jvirtualenv, so that I can use the Python based version too.

Ok now you are ready for Pylons. You'll download the go-pylons script from PylonsHQ get this script here. Once it's downloaded run it using "jython go-pylons.py jylons". This will create all the virtual environment where we can install packages specific to our app that we do not want polluting the Jython site-packages.

Alright getting closer... Activate your new env. On *nix this is as easy as "source jylons/bin/activate" on Windows "jylons\bin\activate.bat". For more info on virtualenv see here.

You should now be using the 'jylons' virtual environment. We are close. Type "paster create --t pylons". A script will now guide you through creating your Pylons app. For this part I advise you follow the "getting started" guide from PylonsHQ here.

You'll now need to get the bleeding edge SQLAlchemy, which supports the Jython DBAPI implementation 'zxJDBC'. Use this URL to check it out. Once that is done make sure your jylons virtualenv is activated, if not activate it now. Change dir to the checked out project. Run the setup.py file like so: "jython setup.py install". This will install the trunk version of SQLAlchemy under the 'jylons' virtualenv.

Before I go I want to make sure that you can connect to your database. In my case Postgres is the database of choice. You could just as well use MySQL. As far as I can tell no integration exists for sqlite, so when you create a new Pylons project that uses SQLAlchemy it won't start up right away; due to errors stemming from sqlite being the configured database. Just switch to MySQL or Postgres.

In the development.ini file you'll want to set the sqlalchemy URL like so:

sqlalchemy.url = postgresql+zxjdbc://<username>:<password>@localhost:5432/<dbname>

Note the '+zxjdbc'. This is what SQLAlchemy 0.6 is using to designate a Jython zxJDBC based DBAPI. If you are interested check the source code for sqlalchemy.engine.url. This will in turn require the Postgres JDBC driver is on the classpath. I hacked it on by updating the jython script to add the driver. Not an optimal solution but workable for now. I'm hoping for a better mechanism to add jar files to Jython's class path at some point.

You should be ready to go. You can now create tables and such in SA. Have fun!

Friday, February 20, 2009

Memory settings when using Jetty inside of Eclipse

If you like to debug and run your webapps inside of Eclipse you've got two options. First there is Tomcat, which running will require a plugin like Sysdeo. Second, you can write a simple class to start up your web application using Jetty. I like to use Jetty rather than the old school Sysdeo Tomcat plugin. I've found that it is simpler to configure the application to run in a test or stage mode this way inside of Eclipse.

If you run Jetty inside of Eclipse you may need to increase the memory being used by the process (Jetty is fast to start up and doesn't requires much memory, but your web app may!). So if you have a process to start your web app in Eclipse you'll have a run configuration. If you open this (click the item menu drop down to the right of the green circle w/ a triangle in it) configuration in the right pane select the 'Arguments' tab.

As you can see in the image above you then will want to fill in the VM arguments to contain the needed memory settings. In the above configuration I've added settings to increase the minimum heap to 256 mb and the max heap to 512 mb. Thats all I needed for my webapp to run smoothly, you may require more or less.

Tuesday, January 20, 2009

Java on Mac OS X: try Soy Latte

If you are working with Java on OS X then you probably should be trying out SoyLatte. SoyLatte is a port of BSD Java for OS X, it is also part of the OpenJDK initiative. It can help you work around the many issues you'll likely have with the 1.6 Java SDK supported by Apple (I couldn't get Woodstox working with RelaxNG schema support).

First off download it. Then you'll probably want to install it somewhere (/usr/local/soylate-xxx should work). Then make sure you have a JAVA_HOME shell var pointing to this folder. Make sure also you add the bin folder to the path.

Getting Eclipse running on SoyLatte is cake. Open the preferences, select "Java > installed JREs", click the 'add' button select the 'MacOS X VM' option and next. You'll now want to point to the SoyLatte folder and finish things off. Select the SoyLatte JRE and close the preferences.

Last thing I'd like to point out. I use CXF to do web services, and found there was an issue with SoyLatte using JAXB 2.0 when I need JAXB 2.1. To solve this I just downloaded the jaxb-api-2.1.jar and jaxws-api-xx.jar and put them into the soylatte/jre/endorsed folder.

Saturday, January 3, 2009

URL Encoding in Tomcat

I'm working on a new feature at blueleftistconstructor (BLC) that is sorta like del.icio.us. The gist is this feature allows a member to create 'bookmarks' in their account on the site. Bookmarks can then be accessed from anywhere one can access BLC.

I created a bookmarklet that when invoked from any webpage will redirect to BLC and save the page as a bookmark. This is pretty clutch as it makes bookmarking a lot easier. I coded up this bookmarklet awhile ago but found a bug just the other day. Any site that had a title containing Unicode characters would cause the title to freak out. In the save bookmark form the characters where getting butchered.

Well it took a hour or so to troubleshoot the problem. It ends up that Tomcat (the Java webserver I host the site app in) uses ISO-Latin-1 (ISO-8859-1) when decoding URLs. Well that makes sense of why the Unicode characters where lost. As luck would have it I kinda guessed this was the issue and found this great post. If you are experiencing such issues just follow the directions in the post and you'll be fine.