/**
   * SecurityAssociation.getSubject() == authSubject SecurityAssociation.getPrincipal() ==
   * authPrincipal
   */
  private void validateSettings(boolean expectNull) {
    Subject s = SecurityAssociation.getSubject();
    Principal p = SecurityAssociation.getPrincipal();

    if (expectNull) {
      assertNull("getSubject() == null", s);
      assertNull("getPrincipal() == null", p);
    } else {
      assertTrue("getSubject() == authSubject", authSubject.equals(s));
      assertTrue("getPrincipal() == authPrincipal", authPrincipal.equals(p));
    }
  }
 /** Test the expected security context exists via the SecurityAssociation accessors */
 public void testSecurityContext() {
   authPrincipal = new SimplePrincipal("jduke");
   authSubject = new Subject();
   authSubject.getPrincipals().add(authPrincipal);
   SecurityAssociation.pushSubjectContext(authSubject, authPrincipal, "theduke");
   validateSettings(false);
 }
  /** Validate that a child thread sees its parent */
  public void testInheritableThreadLocal() throws Exception {
    authPrincipal = new SimplePrincipal("jduke");
    authSubject = new Subject();
    authSubject.getPrincipals().add(authPrincipal);
    SecurityAssociation.pushSubjectContext(authSubject, authPrincipal, "theduke");
    validateSettings(false);

    TestThread t = new TestThread("testInheritableThreadLocal", false);
    t.start();
    t.join();
    if (t.error != null) {
      t.error.printStackTrace();
      fail("TestThread saw an error");
    }
  }
 protected void setUp() {
   System.setProperty("org.jboss.security.SecurityAssociation.ThreadLocal", "false");
   SecurityAssociation.setServer();
 }
 public Jbpapp6469TestCase(String name) {
   super(name);
   SecurityAssociation.setPrincipal(new SimplePrincipal("admin"));
   SecurityAssociation.setCredential("admin");
 }
  public static void main(final String[] args) throws Exception {
    if (args.length == 0) {
      displayUsage();
      System.exit(0);
    }

    String sopts = "-:hD:s:n:a:u:p:Se:H:";
    LongOpt[] lopts = {
      new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
      new LongOpt("server", LongOpt.REQUIRED_ARGUMENT, null, 's'),
      new LongOpt("adapter", LongOpt.REQUIRED_ARGUMENT, null, 'a'),
      new LongOpt("serverName", LongOpt.REQUIRED_ARGUMENT, null, 'n'),
      new LongOpt("shutdown", LongOpt.NO_ARGUMENT, null, 'S'),
      new LongOpt("exit", LongOpt.REQUIRED_ARGUMENT, null, 'e'),
      new LongOpt("halt", LongOpt.REQUIRED_ARGUMENT, null, 'H'),
      new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
      new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'p'),
    };

    Getopt getopt = new Getopt(PROGRAM_NAME, args, sopts, lopts);
    int code;
    String arg;

    String serverURL = null;
    String adapterName = "jmx/rmi/RMIAdaptor";
    String username = null;
    String password = null;
    ObjectName serverJMXName = ObjectNameFactory.create("jboss.system:type=JVMShutdown");
    boolean exit = false;
    boolean halt = false;
    int exitcode = -1;

    while ((code = getopt.getopt()) != -1) {
      switch (code) {
        case ':':
        case '?':
          // for now both of these should exit with error status
          System.exit(1);
          break;

        case 1:
          // this will catch non-option arguments
          // (which we don't currently care about)
          System.err.println(PROGRAM_NAME + ": unused non-option argument: " + getopt.getOptarg());
          break;
        case 'h':
          displayUsage();
          System.exit(0);
          break;
        case 'D':
          {
            // set a system property
            arg = getopt.getOptarg();
            String name, value;
            int i = arg.indexOf("=");
            if (i == -1) {
              name = arg;
              value = "true";
            } else {
              name = arg.substring(0, i);
              value = arg.substring(i + 1, arg.length());
            }
            System.setProperty(name, value);
            break;
          }
        case 's':
          serverURL = getopt.getOptarg();
          break;
        case 'n':
          serverJMXName = new ObjectName(getopt.getOptarg());
          break;
        case 'S':
          // nothing...
          break;
        case 'a':
          adapterName = getopt.getOptarg();
          break;
        case 'u':
          username = getopt.getOptarg();
          SecurityAssociation.setPrincipal(new SimplePrincipal(username));
          break;
        case 'p':
          password = getopt.getOptarg();
          SecurityAssociation.setCredential(password);
          break;
        case 'e':
          exitcode = Integer.parseInt(getopt.getOptarg());
          exit = true;
          break;
        case 'H':
          exitcode = Integer.parseInt(getopt.getOptarg());
          halt = true;
          break;
      }
    }

    InitialContext ctx;

    // If there was a username specified, but no password prompt for it
    if (username != null && password == null) {
      System.out.print("Enter password for " + username + ": ");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      password = br.readLine();
      SecurityAssociation.setCredential(password);
    }

    if (serverURL == null) {
      ctx = new InitialContext();
    } else {
      Hashtable env = new Hashtable();
      env.put(Context.PROVIDER_URL, serverURL);
      env.put(NamingContext.JNP_DISABLE_DISCOVERY, "true");
      // This is a hack
      if (serverURL.startsWith("http:") || serverURL.startsWith("https:"))
        env.put(Context.INITIAL_CONTEXT_FACTORY, HttpNamingContextFactory.class.getName());
      ctx = new InitialContext(env);
    }

    Object obj = ctx.lookup(adapterName);
    if (!(obj instanceof MBeanServerConnection)) {
      throw new RuntimeException(
          "Object not of type: MBeanServerConnection, but: "
              + (obj == null ? "not found" : obj.getClass().getName()));
    }

    MBeanServerConnection adaptor = (MBeanServerConnection) obj;
    ServerProxyHandler handler = new ServerProxyHandler(adaptor, serverJMXName);
    Class<?>[] ifaces = {JVMShutdownMBean.class};
    ClassLoader tcl = Thread.currentThread().getContextClassLoader();
    JVMShutdownMBean server = (JVMShutdownMBean) Proxy.newProxyInstance(tcl, ifaces, handler);

    if (exit) {
      server.exit(exitcode);
    } else if (halt) {
      server.halt(exitcode);
    } else {
      server.shutdown();
    }
    System.out.println("Shutdown message has been posted to the server.");
    System.out.println("Server shutdown may take a while - check logfiles for completion");
  }