views:

58

answers:

2

Hi to everyone.

With your help I have successfully resolved a question that I have asked here. I have developed a custom Tomcat authenticator for the web application, and currently the authenticator and its configuration file are located in the %CATALINA_HOME%\lib\ directory. Unfortunately, the authenticator configuration file is almost a duplicate for the configuration file of the web application (located in %CATALINA_HOME%\webapps\myapp) the authenticator was developed for, and the configuration files both, obviously, share the same DB-connection settings. This is inconvenient because sharing the only configuration file would be the best.

I think there could be two ways to resolve the issue:

  1. Find the webapps directory for myapp presence somehow at Tomcat startup, and then read the application configuration file (proceed from the current situation, and the myapp\WEB-INF\web.xml is configured properly). Sure, unfortunately this requires Tomcat restart.
  2. There might be a way to put the form authenticator into the corresponding web application directory to be able to read the shared configuration file directly. Perhaps this might eliminate Tomcat restart and allow simple redeployment.

I would like to prefer the second solution if it's possible, but I'm not sure. Which way is better, if they both exist? Or is there any other and even better solution not putting the web application specific authenticator into %CATALINA_HOME\lib%?

Thanks in advance and sorry for my English.

A: 

You have a case of code duplication.

I would suggest refactoring the code, so that Tomcat has all the necessary code to to its authentification, and then refactor your application to solely use the Tomcat authentification code.

If your application cannot use the Tomcat authentification you can at least move the common code, including its configuration files, up in Tomcat, and then use the common code.

Thorbjørn Ravn Andersen
The only duplicated thing is multiple (two) configurations located in `CATALINA_HOME\lib\myapp.conf` (I believe there should be JAR files only) and `CATALINA_HOME\webapps\myapp\WEB-INF\myapp.conf` respectively. Both contain the same DB-connection properties. But recently I had an obscure (for a first look) bug, because I just forgot update both files after deployment. So I would like to put the authenticator configuration into the corresponding web applicatio directory somehow to make the configuration file unique. Sorry, I can't expain better. :(
Lyubomyr Shaydariv
Have you considered loading the configuration into a PRoperties object in the container, and making it available to your application through JNDI?
Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen: No, actually no. I'm not familiar with JNDI, and for the moment cannot figure out how it could help me to share DB-connection settings between the authenticator and the web application.
Lyubomyr Shaydariv
JNDI is basically a HashMap into a data structure kept inside the web container. Hence you can define your object in the web container, and then look it up in the web application. Use only classes available in the JRE for casting the object coming out of JNDI to avoid class loader issues.
Thorbjørn Ravn Andersen
So, if I understand you correctly: 1) create a context object in the web application using JNDI API; 2) populate that context with e.g. a connection string; 3) after the first login attempt just try to retrieve the connection string at the custom authenticator side. Is it right?
Lyubomyr Shaydariv
If all you need is a string, then look up the string every time you need it. Otherwise it sounds pretty much like it, yes.
Thorbjørn Ravn Andersen
Thanks. I'll try.
Lyubomyr Shaydariv
A: 

Ah, it's all much easier! The main idea is just to get connection string through the (JDBCRealm) context.getRealm() object in the authenticate(...). That's enough to resolve my issue.

Lyubomyr Shaydariv