Search in sources :

Example 1 with NullRealm

use of org.apache.catalina.realm.NullRealm in project tomcat by apache.

the class TestRegistration method testMBeanDeregistration.

/*
     * Test verifying that Tomcat correctly de-registers the MBeans it has
     * registered.
     * @author Marc Guillemot
     */
@Test
public void testMBeanDeregistration() throws Exception {
    final MBeanServer mbeanServer = Registry.getRegistry(null, null).getMBeanServer();
    // Verify there are no Catalina or Tomcat MBeans
    Set<ObjectName> onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    assertEquals("Unexpected: " + onames, 0, onames.size());
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    assertEquals("Unexpected: " + onames, 0, onames.size());
    final Tomcat tomcat = getTomcatInstance();
    final File contextDir = new File(getTemporaryDirectory(), "webappFoo");
    addDeleteOnTearDown(contextDir);
    if (!contextDir.mkdirs() && !contextDir.isDirectory()) {
        fail("Failed to create: [" + contextDir.toString() + "]");
    }
    Context ctx = tomcat.addContext(contextName, contextDir.getAbsolutePath());
    CombinedRealm combinedRealm = new CombinedRealm();
    Realm nullRealm = new NullRealm();
    combinedRealm.addRealm(nullRealm);
    ctx.setRealm(combinedRealm);
    tomcat.start();
    getUrl("http://localhost:" + getPort());
    // Verify there are no Catalina MBeans
    onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    assertEquals("Found: " + onames, 0, onames.size());
    // Verify there are the correct Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    ArrayList<String> found = new ArrayList<>(onames.size());
    for (ObjectName on : onames) {
        found.add(on.toString());
    }
    // Create the list of expected MBean names
    String protocol = tomcat.getConnector().getProtocolHandlerClassName();
    if (protocol.indexOf("Nio2") > 0) {
        protocol = "nio2";
    } else if (protocol.indexOf("Apr") > 0) {
        protocol = "apr";
    } else {
        protocol = "nio";
    }
    String index = tomcat.getConnector().getProperty("nameIndex").toString();
    ArrayList<String> expected = new ArrayList<>(Arrays.asList(basicMBeanNames()));
    expected.addAll(Arrays.asList(hostMBeanNames("localhost")));
    expected.addAll(Arrays.asList(contextMBeanNames("localhost", contextName)));
    expected.addAll(Arrays.asList(connectorMBeanNames("auto-" + index, protocol)));
    expected.addAll(Arrays.asList(optionalMBeanNames("localhost")));
    expected.addAll(Arrays.asList(requestMBeanNames("auto-" + index + "-" + getPort(), protocol)));
    // Did we find all expected MBeans?
    ArrayList<String> missing = new ArrayList<>(expected);
    missing.removeAll(found);
    assertTrue("Missing Tomcat MBeans: " + missing, missing.isEmpty());
    // Did we find any unexpected MBeans?
    List<String> additional = found;
    additional.removeAll(expected);
    assertTrue("Unexpected Tomcat MBeans: " + additional, additional.isEmpty());
    tomcat.stop();
    // There should still be some Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    assertTrue("No Tomcat MBeans", onames.size() > 0);
    // add a new host
    StandardHost host = new StandardHost();
    host.setName("otherhost");
    tomcat.getEngine().addChild(host);
    final File contextDir2 = new File(getTemporaryDirectory(), "webappFoo2");
    addDeleteOnTearDown(contextDir2);
    if (!contextDir2.mkdirs() && !contextDir2.isDirectory()) {
        fail("Failed to create: [" + contextDir2.toString() + "]");
    }
    tomcat.addContext(host, contextName + "2", contextDir2.getAbsolutePath());
    tomcat.start();
    tomcat.stop();
    tomcat.destroy();
    // There should be no Catalina MBeans and no Tomcat MBeans
    onames = mbeanServer.queryNames(new ObjectName("Catalina:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    assertEquals("Remaining: " + onames, 0, onames.size());
    onames = mbeanServer.queryNames(new ObjectName("Tomcat:*"), null);
    log.info(MBeanDumper.dumpBeans(mbeanServer, onames));
    assertEquals("Remaining: " + onames, 0, onames.size());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) CombinedRealm(org.apache.catalina.realm.CombinedRealm) ArrayList(java.util.ArrayList) NullRealm(org.apache.catalina.realm.NullRealm) ObjectName(javax.management.ObjectName) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File) CombinedRealm(org.apache.catalina.realm.CombinedRealm) NullRealm(org.apache.catalina.realm.NullRealm) Realm(org.apache.catalina.Realm) MBeanServer(javax.management.MBeanServer) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with NullRealm

use of org.apache.catalina.realm.NullRealm in project tomcat by apache.

the class StandardEngine method getRealm.

// ------------------------------------------------------------- Properties
/**
     * Obtain the configured Realm and provide a default Realm implementation
     * when no explicit configuration is set.
     *
     * @return configured realm, or a {@link NullRealm} by default
     */
@Override
public Realm getRealm() {
    Realm configured = super.getRealm();
    // This can be overridden at engine, context and host level
    if (configured == null) {
        configured = new NullRealm();
        this.setRealm(configured);
    }
    return configured;
}
Also used : NullRealm(org.apache.catalina.realm.NullRealm) NullRealm(org.apache.catalina.realm.NullRealm) Realm(org.apache.catalina.Realm)

Aggregations

Realm (org.apache.catalina.Realm)2 NullRealm (org.apache.catalina.realm.NullRealm)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Context (org.apache.catalina.Context)1 StandardHost (org.apache.catalina.core.StandardHost)1 CombinedRealm (org.apache.catalina.realm.CombinedRealm)1 Tomcat (org.apache.catalina.startup.Tomcat)1 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)1 Test (org.junit.Test)1