Ludovic Pénet
Ludovic Pénet

Reputation: 1136

Accessing a context specific realm with Tomcat 7

I am using a "home made" realm in a JSF webapp.

<Context docBase="senateurs" jndiExceptionOnFailedWrite="false" path="/senateurs" reloadable="true">
    <Resource auth="Container" description="Extraction des roles" maxActive="3" maxIdle="1" maxWait="60" name="jdbc/authSen" type="javax.sql.DataSource" validationQuery="select * from senateurs.qua"/>
    <Realm className="org.apache.catalina.realm.SenatLdapDataSourceRealm" dataSourceName="jdbc/authSen" instanceName="realm-senateurs" localDataSource="true" roleNameCol="rolcod" userNameCol="perlogldap" userRoleTable="senateurs.perrol"/>
</Context>

In the following question, it could be just any realm.

I would like to access this realm, to expose its configuration property in my JSF webapp.

To access the DataSource, I can use the following function :

public static BasicDataSource getDataSource(String dataSourceName) throws NamingException {
    InitialContext ic = new InitialContext();
    ic.createSubcontext("java:");
    ic.createSubcontext("java:comp");
    ic.createSubcontext("java:comp/env");
    String subContext = "java:comp/env";
    String[] split = dataSourceName.split("/");
    for(int i = 0 ; i < split.length -1 ; i++) {
        subContext += "/" + split[i];
        ic.createSubcontext(subContext);
    }
    return (BasicDataSource) ic.lookup("java:comp/env/" + dataSourceName);
}

To access a realm declared in host configuration, I can use :

public static Realm getTomcatRealm() {
    try {
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        ObjectName name = new ObjectName("Catalina", "type", "Server");
        Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
        Service service = server.findService("Catalina");
        Engine engine = (Engine) service.getContainer();
        Host host = (Host) engine.findChild(engine.getDefaultHost());
        return host.getRealm();
    } catch (MBeanException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (AttributeNotFoundException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (InstanceNotFoundException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (ReflectionException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } catch (MalformedObjectNameException ex) {
        log.error("Erreur lors de l'accès a serveur Tomcat",ex);
    } finally {
        return null;
    }
}

What should I use to access the realm declared in the webapp context ?

Thanks in advance.

Upvotes: 0

Views: 1745

Answers (2)

Mark Thomas
Mark Thomas

Reputation: 16625

You can get the instance with a small extension to the code you already have.

Context context = (Context) host.findChild("/senateurs");
return context.getRealm();

Upvotes: 1

Ludovic P&#233;net
Ludovic P&#233;net

Reputation: 1136

I still did not find the instance, but can access its attributes with

public static Object getTomcatContextRealmAttribute(String attr) {
    try {
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        Set<ObjectInstance> realms = mBeanServer.queryMBeans(new ObjectName("Catalina:context=*,host=*,realmPath=*,type=*"), null);
        if(realms.size() != 1) {
            // unhandled case...
            return null;
        }
        return mBeanServer.getAttribute(realms.iterator().next().getObjectName(),attr);
    } catch (MalformedObjectNameException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        return null;
    }
}

which fits my need.

Upvotes: 1

Related Questions