You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jeff Wishnie <je...@deluxebiz.com> on 2002/09/14 03:11:17 UTC

HELP--Programmatically accessing the UserDatabase?

Tomcat: 4.1.10
JDK: 1.4.0
OS: Redhat 7.2


Hi,

I have a vanilla Tomcat config (pretty much the example server.xml that ships with 1.4.10) and am using the UserDatabaseRealm in
conjunction with the tomcat-users file for authentication.

This works fine, but now I want to programmatically access the Userdatabase to get some info in one of my servlets.

I read through the JNDI How-to and it seems to me that what I'm doing should work, but the resource can't be found at runtime.

Any help would be greatly appreciated.

Here are relevent sections of code/config:

------------------
Server XML
------------------
<GlobalNamingResources>
        <!-- Editable user database that can also be used by
        UserDatabaseRealm to authenticate users -->
        <Resource name="UserDatabase" auth="Container"
            type="org.apache.catalina.UserDatabase"
            description="User database that can be updated and saved">
        </Resource>
        <ResourceParams name="UserDatabase">
            <parameter>
                <name>factory</name>
                <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
            </parameter>
            <parameter>
                <name>pathname</name>
                <value>conf/tomcat-users.xml</value>
            </parameter>
        </ResourceParams>
    </GlobalNamingResources>

 ...

 <!-- realm, defined as default for me Engine -->

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                debug="0" resourceName="UserDatabase"/>


-------------------
Code in servlet
---------------------
 try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            UserDatabase udb = (UserDatabase) envCtx.lookup("UserDatabase");

            Iterator it=udb.getUsers();
            User use=null;
            while (it.hasNext()) {
                use=(User) it.next();
             }
        } catch (NamingException nameEx) {
            // log error
        }


-----------
Exception generated
--------------
javax.naming.NameNotFoundException: Name UserDatabase is not bound in this Context
        at org.apache.naming.NamingContext.lookup(NamingContext.java:811)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
        ...
        ....


Thanks!!

- Jeff


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>