Sunday, January 28, 2007

Using Apache as a Proxy for Tomcat Applications

I spent the day trying some configurations to make the integration between Apache HTTP Server and Tomcat.

The configuration that worked best for me basically was to setup a VirtualHost in Apache as a proxy with the same name as the application in Tomcat engine. As a resume, below tou will find some of the steps I went through and what I changed in the configuration files (I am using Apache 2.2 and Tomcat 5.5).

In Apache it is necessary to enable the mod_proxy Module and setup a NameVirtualHost. The httpd.conf (found in %APACHE_HOME%/conf) configuration file should be changed as below.

#Uncomment or add the lines below into the general session of your httpd.conf file.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

# Add the Virtual Host for your application at the end of the file
NameVirtualHost *

<VirtualHost *>
  ServerAdmin serveradmin@domain.com
  DocumentRoot "C:/java/Tomcat5.5/webapps/"
  ServerName myapplication.domain.com
  ServerAlias *.myapplication.domain.com

  ProxyPass /foo-app http://localhost:8080/foo-app
  ProxyPassReverse /foo-app http://localhost:8080/foo-app
</VirtualHost>

After setting this file in your apache server, restart it and proceed to configuring the Tomcat Server, by editing the server.xml file (found in %CATALINA_HOME%/conf). Simply add the lines below at the end of the file, but inside the <server> tag.

<Service name="Tomcat-Apache">
    
    <Engine name="Apache" defaultHost="myapplication.domain.com" debug="0">
    <Host name="myapplication.domain.com" debug="0"
            appBase="c:/java/Tomcat5.5/webapps/foo-app" 
            unpackWARs="true" autoDeploy="true">

        <Context path="/foo-app" docBase="c:/java/Tomcat5.5/webapps/foo-app" debug="1" reloadable="true"/>
    </Host>

    </Engine>

  </Service>  

Now, open your browser and type the URL http://myapplication.domain.com/foo-app and you should be redirected to your application after passing by Apache and Tomcat.

Tomcat RMI Unmarshalling Exception

This is one of those posts that shows how frustrating sometimes the developer job can be! :)

I had a WAR component that was delivered successfully and worked fine in Jetty 6.0 rc2, but the same WAR file would not work under Tomcat 5.5. This application uses some session beans that are available in my JBoss Server. When I tried to execute the lookup() command in my client application running in Tomcat I had an exception thrown:


java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.net.MalformedURLException: no protocol:


Then I asked: Why? Why does it work in Jetty like a charm? Reading ahead at the message of the exception I noticed the MalformedURL message included this: "5.5/temp/0-fisio-web-1.0-SNAPSHOT/WEB-INF/classes/" which made me realize that this was just part of the full URL where my Tomcat Server was running. Searching through the net I found this post, which led me to this other post (and there is also this other post on this subject).

For my surprise this is a known bug since JDK 1.2, marked as WON´T FIX by SUN! Is it unbelievable or not??

There is a workaround suggested by Sun that says that one should use

file.toURI().toURL()

instead of

file.toURL()

but many times, this is not in our hands, as is my case because Tomcat is taking care of this for me, so unless me (or some other generous soul) change and test (and do a backward compatibility test which seems to be what is frightening SUN into not changing this behavior) this way of implementing the RMI communication, the best thing to avoid this problem is to install all your applications in a path that does not include white spaces or illegal characters.

Ignoring Files in Subversion

As I told before, I am using windows as a development environment, and one problem I was facing is that my GUI client SmartSVN was showing binary files produced by compilation (*.class, *.jar) and other uninteristing files do be versioned (like *.log).

I read documentation in the SVN Book Chapter 7, all sort of sites throughout the internet and there was only one solution that worked for me!

This Works - In folder "C:\Documents and Settings\[your_username]\Application Data\Subversion" you will find a file named config (with no extension). In this file there should be a section called [miscellany] and in there you will find the key "global-ignores". Uncomment it (remove the "#" char from the beginning) if necessary, add the extensions you would like to be ignored and you are good to go!

This DOES NOT work - Although it is the solution pointed out by the SVN Book and in many sites, changing the windows registry will not work for SmartSVN.

Still could not find a way to set the default global-ignores for the whole server.

Saturday, January 27, 2007

Tomcat Hot Deployment in Windows

The post about hot deploying an application to Tomcat using maven brought me a problem: the struts.jar was always left in the WEB-INF/lib of the exploded directory in the server.

This is a know issue with Tomcat working under Windows and I found an article which I quoted here below with the solution for this problem:

If you try to hot-deploy a WAR file to Tomcat on Windows, you often encounter file locking issues. Windows won’t let Tomcat undeploy the old app because files are locked by the OS. Here is a fix:

Edit %CATALINA_HOME%\conf\context.xml. Find the root <context> and add these two attributes:

<context antijarlocking="true" antiresourcelocking="true">

Now you can copy updated WAR files to your deploy directory and Tomcat will remove the old app and hot-deploy your new app. I’m using this on Tomcat 5.5.11 without any trouble.

There were other people having this kind of problem. Please note that this has nothing to do with using Maven, but with the combination of Tomcat 5.5 + Windows and Hot Deploys! :)

Display HTML tags in HTML pages

If you want to display an HTML tag in a web page (including posts in this blogger), you have to replace the tag markers "<" and ">" by their equivalent ASCII entities "&lt;" (which stands fot "less than") and "&gt;" (which stands for "greater than") respectively.

Check the W3 Schools to learn about other HTML entities.

Just another note: if you want to indent your tags on the page, a simple way to do this is to use the char "&nbsp;" (which stands for "non blocking space"). This char is not compressed by the browser when rendering a page.

Using Maven to deploy application to Apache Tomcat Server

For those who are not familiar with Maven, it is (as stated in the project site) a "software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."

Based on the concept of "convention over configuration" developers may use Maven to compile, build, test, deploy, generate reports and use a whole bunch of other features to manage their software development project. In another oppportunity I will post an article about Maven, for now I will write about my most immediate need: deploy a war file to a tomcat server.

As I am using maven to manage my project from test to build, all I need is to configure the Cargo plugin. Cargo is able to download, install and start Tomcat, but this is not our focus right now, so I will assume you have Tomcat installed and running with no problems.

Using Maven´s Cargo plugin it is possible to install an application into Tomcat in two ways:

  1. Use the Tomcat Manager application, which is deployed by default with Tomcat 5.x and is nothing more than a web interface to manage your server. It has the ability to install a WAR into the server from a remote location.
  2. Copy the WAR File of your application directly into the File System where Tomcat is expecting applications to be, normally %CATALINE_HOME%/webapps.
As one can find strong and weak points in each of these options, I will tell a little bit about the first one.

Using the Tomcat Manager

Now we will deploy an application to Tomcat while it is still running. This is called Hot Deploying.

You should add some comands to your pom file, and they should look like the excerpt of code below. Actually it should be positioned between the tags:
<build>
<plugins>
...
</plugins>
</build>

<plugin>
<groupid>org.codehaus.cargo</groupid>
<artifactid>cargo-maven2-plugin</artifactid>

<configuration>

<!-- Container configuration -->
<container>
<containerid>tomcat5x</containerid>
<type>remote</type>
</container>

<!-- Configuration to use with the Container -->
<configuration>
<type>runtime</type>
<properties>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>admin</cargo.remote.password>
</properties>
</configuration>

<!-- Deployer configuration -->
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupid>com.company</groupid>
<artifactid>web-component</artifactid>
<type>war</type>
</deployable>
</deployables>
</deployer>

</configuration>

</plugin>
You should need to change only the values in the and tags with the values that describe your component WAR file you wanto to install to Tomcat.

Having this in your pom file, you need to issue the following command:

mvn cargo:deploy
If you already have the application running in tomcat it is necessary that you undeploy it before deploying. So, issue the command below and you should be ok:

mvn cargo:undeploy

You also have the option to execute a command that will do an undeploy before attempting to deploy:

mvn cargo:deployer-redeploy
That should be enough to get the application running in Tomcat, deploying from a remote server.

I would just like to add that the configuration for the tags
and are values that must match the ones in the tomcat-users.xml file with the role "manager". Check this before trying to do the deploy.

SDWest 2007 Conferece

The registration for SDWest 2007 is now open.
Early bird discounts goes until February 23rd.

Change default View Source Editor in Internet Explorer (IE)

I believe it is common sense that Windows Notepad is not among the best text editors in the market, right?

So, if you wanna change the editor that will display the source html from a web page being displayed in internet explorer to another editor you use, here´s how to do it:

1 - Open the registry editor. You should find it under C:\WINDOWS\system32\regedt32.exe or simply type "regedit" in the run command box in Start Menu.

2 - Navigate in the tree to find the following key

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\View Source Editor\Editor Name

3 - The default value for this key is "C:\windows\notepad.exe". Change this text to whatever editor you like, with the full path to it. If you want to use UltraEdit, for example, the new value should be changed to "C:\Arquivos de programas\UltraEdit\UEdit32.exe".

Thursday, January 18, 2007

SVN as a service under Windows

Found this great article on how to run subversion as a service (check my other post on installing subversion (SVN)).

Basically it explains how to use the "sc" (Service Control) which is provided by Windows OS to crete a service to run the svnserve command. This should work on Windows NT, XP, 2000 and 2003 Server.

It should look like something like this:

sc create [name]
binpath= "c:\svn\bin\svnserve.exe --service [svn-args]"
displayname= "Subversion Repository"
depend= Tcpip

If you are not comfortable in doing this, you can always try running svnservice, which is a project from tigris intended to "offer a comfortable way to run svn server (svnserve.exe) as MS Windows service".

Friday, January 12, 2007

The Cygwin sshd as a service under Windows

My other post on called "Running a ssh server on Windows" gave some steps to start a ssh daemon on Windows using Cygwin.

The interesting thing is that when you setup the ssh environment on Cygwin (executing the command ssh-host-config), it also creates a service in windows. This service can be accessed through the Services Management Windows standard administration tool and be setup to start automatically when windows starts.

Neat! :)

Check this article with some interesting observations on setting up the users who will and will not be allowed access to the ssh server.

Running a SSH server on Windows

This post is not essentially about software development but is about something I found very useful: having an ssh connection to the machines where I work.

SSH daemon in available in linux from the start and there are no big matters about it other than security issues.

Running an ssh server on a Windows machine is a little bit trickier. Thanks to the guys at Cygnus we have Cygwin, which is, as stated in their web site "a Linux-like environment for Windows".

This environment comes with a ssh server daemon which can be run as a service in Windows, allowing you to have ssh connections to your machine using any valid ssh client.

First step would obiously be to donwload and install Cygwin on your machine. You can find a quick reference on installing it under Windows.

There is an official document distributed with Cygwin on setting up a secure environment using ssh. You will find it entering the following commands on your cygwin prompt:

cd /usr/share/doc/Cygwin

less openssh.README

This document can also be found online and the tips below are intended to be a quick start guide.

So now that you have Cygwin installed, you should open your cygwin prompt and type:

ssh-host-config

The output should be something like this:

$ ssh-host-config
Overwrite existing /etc/ssh_config file? (yes/no) yes
Generating /etc/ssh_config file
Overwrite existing /etc/sshd_config file? (yes/no) yes
Privilege separation is set to yes by default since OpenSSH 3.3.
However, this requires a non-privileged account called 'sshd'.
For more info on privilege separation read /usr/share/doc/openssh/README.privsep.

Should privilege separation be used? (yes/no) yes
Generating /etc/sshd_config file

Host configuration finished. Have fun!

This should be enough to run the server daemon. Run one of the following two commands to start it.

net start sshd
cygrunsrv -S sshd

Check if the server is actually running:

$ ps -ef | grep ssh
SYSTEM 760 3508 ? 00:29:11 /usr/sbin/sshd

Now test if the connection was successfull:

$ ssh localhost
dambrosio@localhost's password:
Last login: Sat Jan 13 00:31:53 2007 from 192.168.1.1
Fanfare!!!
You are successfully logged in to this server!!!

There are a lot of security issues about ssh and remote connections. Read a bit more about the subject, and there is an interesting article which is a good starting point.

This other article will give you some hints on installing cygwin, setting it up as a service on Windows 2003 and some issues about security as well.

Remember:
ssh runs by default at port 22, so remember to open this TCP port on your firewall. You do have one, right? :)

Sunday, January 7, 2007

Installing Subversion, a Version Control System

Having a Version Control System is almost mandatory for every developer because keeping track of the evolution of his code is highly desirable. Such a system allows the developer to have individually registered version of every file in his system, including source code, documentation, diagrams etc.

Subversion (AKA as SVN) is such a system. It is open source and is considered an evolution of CVS with new features and a new internal architecture which allows SVN to have significant changes and improvements when compared to CVS, such as directories versions, truly atomic commits etc.

After this brief introduction, let’s get to work. As I am using Windows as platform for development (flames expected here ;) this article will give you the steps on installing SVN on this platform. If you are using Windows there are 3 ways to get subversion on your system:

1. Get an installer from Collabnet site.
2. Get a zipped file with binaries from tigris.
3. Get the sources from the repository and compile them on your system.

I’ll stick to the first two situations, because you would normally need the third one only if you need the latest features on the system. The stable releases should be enough for most needs.

Using Collabnet’s installer
Collabnet is the company that designed and created subversion. Using this prepared installation file should be the easiest way to get subversion up and running on your windows box. It comes with the SVN server, a SVN command-line client and an apache server almost ready for running svn.
CollabNet provides a readme file with basic instructions on creating a repository, clearing the firewall to let SVN do his work etc. It is a good basic reference. So, simply download the file (version 1.4 as of the time of the writing) and follow instructions.


Using ZipFile Binaries
This installation option is a little bit more difficult than the one above but everyone should do fine. I will not cover apache installation on this article.

Step 1
First of all you should download the latest version of the server available from subversion project downloads page. As of the time of this writing it is version 1.4.2. You may choose to download the sources and compile them yourself or download the binaries created by contributors to the project, which though not maintained by the project team are normally reliable packages. Now scroll the downloads page down to find the binaries for SVN Server for Windows. When you are redirected to the page find the latest release (should be the last one in the list) and get the file which name looks more or less like this svn-win32-X.X.X.zip (the one I got was svn-win32-1.4.2.zip).

Step 2
Unzip the file somewhere on your hard disk where it would make sense to have the subversion binaries. We will call this folder the subversion home directory (%SVN_HOME%). For example, if you unziped the files for SVN version 1.4.2 under C:\Program Files folder, your %SVN_HOME% dir would be: C:\Program Files\svn-win32-1.4.2. On the %SVN_HOME%\bin folder you will find the executable files which you will use to maintain your server and repositories.

svn.exe - Subversion client
svnadmin.exe - Subversion repository administration
svndumpfilter.exe - Subversion dump fle filter
svnlook.exe - Subversion repository browser
svnserve.exe - Subversion Server
svnsync.exe - Subversion repository replicator
svnversion.exe - Subversion revision extractor

Step 3
Let’s create the repository. Add the %SVN_HOME%\bin folder to your path variable in Windows to make the binaries accessible from anywhere in your computer. Now, create a test repository folder somewhere on your system (let’s say c:\temp\svn-test-rep) and we will call it the %SVN_REP%.
Run the svnadmin command to create a repository called repos1. You will do that running the following command:

svnadmin create c:\temp\svn-test-rep\repos1

As a result you should have a folder called "repos1" under %SVN_REP% with the following content:

DIR --- conf
DIR --- dav
DIR --- db
------- format
DIR --- hooks
DIR --- locks
------- README.txt

Step 4
Now we should test the server. This is as simple as running the command below:

svnserve -d -r c:\temp\svn-test-rep

This will get the server up and running. The "-d" tells the server to run as a daemon and "-r" tells the repository name.
Remember to enable svnserve to run on your firewall if you have one (if you don’t, you should get one ;)).

Now open another command prompt (while the server is still running), go to your "temp" folder and checkout the repository you created on step 3. Checking out a repository means getting a local version (copy with a revision number) from every file present on your repository. You can do that by typing:

svn co svn://localhost/repos1

This command will ask the svn server running on localhost to get the latest revision of every file on repository called "repos1". The "co" in the command line stands for "checkout" and "svn://" specifies the protocol used to access the repository. If had apache installed and setup to run svn mods we could use http or https to access the repository.

Well, this pretty much tells you how to install a SVN server and create repositories for your code. Of course now you will get worried about issues like: who can access my files or how do I specify security constraints? This should be subject for another article, as well as svn GUI clients and apache server.