View Javadoc
1   package info.ejava.examples.secureping;
2   
3   import java.util.Arrays;
4   
5   import javax.management.MBeanServerConnection;
6   import javax.management.ObjectName;
7   import javax.naming.Context;
8   import javax.naming.InitialContext;
9   
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  /**
14   * This class flushes the JBoss JaasSecurityManager authentication cache. 
15   * This needs to be done after changing user login/role information in the 
16   * database when not waiting not waiting for a timeout supplied in 
17   * deploy/security-service.xml. It does so using the RMIAdapter for the 
18   * JMX MBean server.
19   */
20  public class ResetAuthenticationCache {
21      private static final Logger logger = LoggerFactory.getLogger(ResetAuthenticationCache.class); 
22      private static final String SECURITY_MANAGER_SERVICE = 
23          "jboss.security:service=JaasSecurityManager";
24      private static final String FLUSH_OPERATION = 
25          "flushAuthenticationCache";
26      private static final String jndiName = 
27          System.getProperty("jmx.invoker","jmx/invoker/RMIAdaptor");
28      private static final String domainName = 
29          System.getProperty("jmx.domain", "ejavaDomain");
30  
31      
32      public ResetAuthenticationCache() {}
33      
34      public void execute() throws Exception {
35          //get the JMX Adaptor from the JNDI tree
36          Context jndi = new InitialContext(); //rely on a jndi.properties file
37          logger.debug("jndi=" + jndi.getEnvironment());
38          logger.debug("looking up:" + jndiName);
39          Object object = jndi.lookup(jndiName);
40          //RMIAdaptor remote = (RMIAdaptor) object;
41          MBeanServerConnection remote = (MBeanServerConnection) object;
42          
43          
44          //invoke the bean to flush the authentication cache for the domain
45          ObjectName name = new ObjectName(SECURITY_MANAGER_SERVICE);
46          Object[] params = { domainName };
47          String[] signature = { "java.lang.String" };
48          logger.debug(name + "." + FLUSH_OPERATION + "(" + 
49                  Arrays.toString(params) + ")");
50          remote.invoke(name,FLUSH_OPERATION, params, signature);
51      }
52      
53      public static void main(String args[]) {
54          try {
55              new ResetAuthenticationCache().execute();
56          }
57          catch (Exception ex) {
58              ex.printStackTrace();
59              System.exit(-1);
60          }
61      }
62  }