/**
 * Jaas service to install a {@code Configuration}
 *
 * @author <a href="mailto:[email protected]">Marcus Moyses</a>
 */
public class JaasConfigurationService implements Service<Configuration> {

  public static final ServiceName SERVICE_NAME = SecurityExtension.JBOSS_SECURITY.append("jaas");

  private static final Logger log = Logger.getLogger("org.jboss.as.security");

  private final Configuration configuration;

  public JaasConfigurationService(Configuration configuration) {
    this.configuration = configuration;
  }

  /** {@inheritDoc} */
  @Override
  public void start(StartContext arg0) throws StartException {
    if (log.isDebugEnabled()) log.debug("Starting JaasConfigurationService");

    // set new configuration
    Configuration.setConfiguration(configuration);
  }

  /** {@inheritDoc} */
  @Override
  public void stop(StopContext arg0) {
    // restore configuration to null
    Configuration.setConfiguration(null);
  }

  /** {@inheritDoc} */
  @Override
  public Configuration getValue() throws IllegalStateException, IllegalArgumentException {
    return configuration;
  }
}
/**
 * SubjectFactory service for the security container
 *
 * @author <a href="mailto:[email protected]">Marcus Moyses</a>
 */
public class SubjectFactoryService implements Service<SubjectFactory> {

  public static final ServiceName SERVICE_NAME =
      SecurityExtension.JBOSS_SECURITY.append("subject-factory");

  private static final Logger log = Logger.getLogger("org.jboss.as.security");

  private final InjectedValue<ISecurityManagement> securityManagementValue =
      new InjectedValue<ISecurityManagement>();

  private SubjectFactory subjectFactory;

  private String subjectFactoryClassName;

  public SubjectFactoryService(String subjectFactoryClassName) {
    this.subjectFactoryClassName = subjectFactoryClassName;
  }

  /** {@inheritDoc} */
  @Override
  public void start(StartContext context) throws StartException {
    if (log.isDebugEnabled()) log.debug("Starting SubjectFactoryService");
    final ISecurityManagement injectedSecurityManagement = securityManagementValue.getValue();
    int i = subjectFactoryClassName.lastIndexOf(":");
    if (i == -1)
      throw new StartException("Missing module name for the subject-factory-class-name attribute");
    String moduleSpec = subjectFactoryClassName.substring(0, i);
    String className = subjectFactoryClassName.substring(i + 1);
    JBossSecuritySubjectFactory subjectFactory = null;
    try {
      Class<?> subjectFactoryClazz =
          SecurityActions.getModuleClassLoader(moduleSpec).loadClass(className);
      subjectFactory = (JBossSecuritySubjectFactory) subjectFactoryClazz.newInstance();
    } catch (Exception e) {
      throw new StartException(e);
    }
    subjectFactory.setSecurityManagement(injectedSecurityManagement);
    this.subjectFactory = subjectFactory;
  }

  /** {@inheritDoc} */
  @Override
  public void stop(StopContext context) {
    // nothing to do
  }

  /** {@inheritDoc} */
  @Override
  public SubjectFactory getValue() throws IllegalStateException {
    return subjectFactory;
  }

  /**
   * Target {@code Injector}
   *
   * @return target
   */
  public Injector<ISecurityManagement> getSecurityManagementInjector() {
    return securityManagementValue;
  }
}