Niklas,
What changes are you making for the Spring-like configuration to the ftp
server? Any code yet [I have not looked at the latest svn stuff]?
One of the reasons for asking is that I added a database "bean" to the
code. This allows the ability to save items to a database. My initial
use for this was provide support for a virtual file system implemented
using a database. Later I expanded it to include configurable items.
The database manager bean must be one of the first ones loaded. I put it
right after the message resource bean. Not everyone would like or need a
database bean, so it becomes an interesting task of how to provide a
generic process to add it if it is present. Remember it has to be loaded
right after the message resource bean, as the db bean can be used to
pull configuration and other information from a database.
Just as an FYI, the "bean" actually makes a generic call using Java
Persistence. This way nothing database-specific is in the code. In order
to use this without passing the Persistence object down through a
context or constructor, I also load the Apache OpenEJB. I use the
OpenEJB as a local "object" directory server.
The database manager code looks like this:
configure() {
LOG.info("Configuring DBManager: ");
try {
JAXPConfigurator.configure(getProxoolPath(), false);
} catch (ProxoolException ex) {
LOG.error("DB Pool setup failed",ex);
}
// over-ride the default "test" database with the real one :-)
Map<String, String> configOverrides = new HashMap<String, String>();
// setup the property to over-ride
configOverrides.put("database.hibernate.connection.url",
getConnectionUrl());
this.emf = Persistence.createEntityManagerFactory(
getPersistenceUnit(), configOverrides);
}
Right now it's tied to the proxool pool code [much better for db pools
than anything Apache (pool stuff) has in my personal opinion :-O], and
is also tied to hibernate. I am in the process of making this generic.
Notice there are no specific database's mentioned. I use MySQL, and
sometimes H2, however the db-specific code is left to an external
persistence configuration file.
In my FileObject class I do this to get access to the Persistence:
public EntityManagerFactory getEmf() {
// see if the emf object needs to be fetched
if (this.emf == null) {
try {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
InitialContext localContext = new InitialContext(properties);
PersistenceManager pm = (PersistenceManager)
localContext.lookup("PersistenceManagerRemote");
this.emf = pm.getEmf();
} catch (NamingException ex) {
LOG.error("DBFileObject.unable to access the Persistence Layer",
ex);
}
}
return emf;
}
No need to pass it through the constructor, simply use the OpenEJB to
lookup the resource and provide access to it.
To be compatible with the configuration(s) in-use, I added simple
name-value columns to some configuration tables.
The table looks like this:
+-------+----------------------------+------------------+
| index | name | value |
+-------+----------------------------+------------------+
| 1 | config.create-default-user | 1 |
+-------+----------------------------+------------------+
| 2 | config.message.languages | en,de |
+-------+----------------------------+------------------+
This way you can just search using the already defined properties. The
index is not used, but it is required in-order for Persistence to manage it.
---
I also put in a new DB User Manager which uses the generic Persistence
for methods. There is no SQL code in the class, it's plain Java.
---
Not sure how to introduce all of this into the ftp server project. I
suspect it will be a very popular addition, maybe not for the user
manager, but for the generic database ability, and EJB3 capabilities.
Andy Thomson