/** {@inheritDoc} */
    @Override
    public void writeContent(XMLExtendedStreamWriter writer, SubsystemMarshallingContext context)
        throws XMLStreamException {
      context.startSubsystemElement(Namespace.CURRENT.getUriString(), false);
      ModelNode scanners = context.getModelNode();
      for (final Property list : scanners.asPropertyList()) {
        final ModelNode node = list.getValue();

        for (final Property scanner : node.asPropertyList()) {

          writer.writeEmptyElement(Element.DEPLOYMENT_SCANNER.getLocalName());
          writer.writeAttribute(Attribute.NAME.getLocalName(), scanner.getName());
          ModelNode configuration = scanner.getValue();
          if (configuration.hasDefined(CommonAttributes.PATH)) {
            writer.writeAttribute(
                Attribute.PATH.getLocalName(), configuration.get(CommonAttributes.PATH).asString());
          }
          if (configuration.hasDefined(CommonAttributes.SCAN_ENABLED)) {
            writer.writeAttribute(
                Attribute.SCAN_ENABLED.getLocalName(),
                configuration.get(CommonAttributes.SCAN_ENABLED).asString());
          }
          if (configuration.hasDefined(CommonAttributes.SCAN_INTERVAL)) {
            writer.writeAttribute(
                Attribute.SCAN_INTERVAL.getLocalName(),
                configuration.get(CommonAttributes.SCAN_INTERVAL).asString());
          }
          if (configuration.hasDefined(CommonAttributes.RELATIVE_TO)) {
            writer.writeAttribute(
                Attribute.RELATIVE_TO.getLocalName(),
                configuration.get(CommonAttributes.RELATIVE_TO).asString());
          }
          if (configuration.hasDefined(CommonAttributes.AUTO_DEPLOY_ZIPPED)) {
            if (!configuration.get(CommonAttributes.AUTO_DEPLOY_ZIPPED).asBoolean()) {
              writer.writeAttribute(
                  Attribute.AUTO_DEPLOY_ZIPPED.getLocalName(), Boolean.FALSE.toString());
            }
          }
          if (configuration.hasDefined(CommonAttributes.AUTO_DEPLOY_EXPLODED)) {
            if (configuration.get(CommonAttributes.AUTO_DEPLOY_EXPLODED).asBoolean()) {
              writer.writeAttribute(
                  Attribute.AUTO_DEPLOY_EXPLODED.getLocalName(), Boolean.TRUE.toString());
            }
          }
          if (configuration.hasDefined(CommonAttributes.DEPLOYMENT_TIMEOUT)) {
            writer.writeAttribute(
                Attribute.DEPLOYMENT_TIMEOUT.getLocalName(),
                configuration.get(CommonAttributes.DEPLOYMENT_TIMEOUT).asString());
          }
        }
        writer.writeEndElement();
      }
    }
/**
 * @author Tomaz Cerar
 * @created 25.1.12 17:24
 */
public class DeploymentScannerDefinition extends SimpleResourceDefinition {

  DeploymentScannerDefinition(final PathManager pathManager) {
    super(
        DeploymentScannerExtension.SCANNERS_PATH,
        DeploymentScannerExtension.getResourceDescriptionResolver("deployment.scanner"),
        new DeploymentScannerAdd(pathManager),
        DeploymentScannerRemove.INSTANCE);
  }

  protected static final SimpleAttributeDefinition NAME =
      new SimpleAttributeDefinitionBuilder(CommonAttributes.NAME, ModelType.STRING, false)
          .setXmlName(Attribute.NAME.getLocalName())
          .setAllowExpression(false)
          .setValidator(new StringLengthValidator(1))
          .setDefaultValue(new ModelNode().set(DeploymentScannerExtension.DEFAULT_SCANNER_NAME))
          .build();

  protected static final SimpleAttributeDefinition PATH =
      new SimpleAttributeDefinitionBuilder(CommonAttributes.PATH, ModelType.STRING, false)
          .setXmlName(Attribute.PATH.getLocalName())
          .setAllowExpression(true)
          .setValidator(new StringLengthValidator(1, Integer.MAX_VALUE, false, true))
          .build();
  protected static final SimpleAttributeDefinition RELATIVE_TO =
      new SimpleAttributeDefinitionBuilder(CommonAttributes.RELATIVE_TO, ModelType.STRING, true)
          .setXmlName(Attribute.RELATIVE_TO.getLocalName())
          .setValidator(new StringLengthValidator(1, Integer.MAX_VALUE, true, false))
          .build();
  protected static final SimpleAttributeDefinition SCAN_ENABLED =
      new SimpleAttributeDefinitionBuilder(CommonAttributes.SCAN_ENABLED, ModelType.BOOLEAN, true)
          .setXmlName(Attribute.SCAN_ENABLED.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode(true))
          .build();
  protected static final SimpleAttributeDefinition SCAN_INTERVAL =
      new SimpleAttributeDefinitionBuilder(CommonAttributes.SCAN_INTERVAL, ModelType.INT, true)
          .setXmlName(Attribute.SCAN_INTERVAL.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode().set(0))
          .build();
  protected static final SimpleAttributeDefinition AUTO_DEPLOY_ZIPPED =
      new SimpleAttributeDefinitionBuilder(
              CommonAttributes.AUTO_DEPLOY_ZIPPED, ModelType.BOOLEAN, true)
          .setXmlName(Attribute.AUTO_DEPLOY_ZIPPED.getLocalName())
          .setDefaultValue(new ModelNode().set(true))
          .setAllowExpression(true)
          .build();
  protected static final SimpleAttributeDefinition AUTO_DEPLOY_EXPLODED =
      new SimpleAttributeDefinitionBuilder(
              CommonAttributes.AUTO_DEPLOY_EXPLODED, ModelType.BOOLEAN, true)
          .setXmlName(Attribute.AUTO_DEPLOY_EXPLODED.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode().set(false))
          .build();

  protected static final SimpleAttributeDefinition AUTO_DEPLOY_XML =
      new SimpleAttributeDefinitionBuilder(
              CommonAttributes.AUTO_DEPLOY_XML, ModelType.BOOLEAN, true)
          .setXmlName(Attribute.AUTO_DEPLOY_XML.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode().set(true))
          .build();

  protected static final SimpleAttributeDefinition DEPLOYMENT_TIMEOUT =
      new SimpleAttributeDefinitionBuilder(
              CommonAttributes.DEPLOYMENT_TIMEOUT, ModelType.LONG, true)
          .setXmlName(Attribute.DEPLOYMENT_TIMEOUT.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode().set(600))
          .build();

  protected static final SimpleAttributeDefinition RUNTIME_FAILURE_CAUSES_ROLLBACK =
      new SimpleAttributeDefinitionBuilder(
              CommonAttributes.RUNTIME_FAILURE_CAUSES_ROLLBACK, ModelType.BOOLEAN, true)
          .setXmlName(Attribute.RUNTIME_FAILURE_CAUSES_ROLLBACK.getLocalName())
          .setAllowExpression(true)
          .setDefaultValue(new ModelNode().set(false))
          .build();

  protected static final SimpleAttributeDefinition[] ALL_ATTRIBUTES = {
    PATH,
    RELATIVE_TO,
    SCAN_ENABLED,
    SCAN_INTERVAL,
    AUTO_DEPLOY_EXPLODED,
    AUTO_DEPLOY_XML,
    AUTO_DEPLOY_ZIPPED,
    DEPLOYMENT_TIMEOUT,
    RUNTIME_FAILURE_CAUSES_ROLLBACK
  };

  @Override
  public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
    // resourceRegistration.registerReadOnlyAttribute(NAME, null);
    resourceRegistration.registerReadWriteAttribute(
        PATH, null, new ReloadRequiredWriteAttributeHandler(PATH));
    resourceRegistration.registerReadWriteAttribute(
        RELATIVE_TO, null, new ReloadRequiredWriteAttributeHandler(RELATIVE_TO));
    resourceRegistration.registerReadWriteAttribute(
        SCAN_ENABLED, null, WriteEnabledAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        SCAN_INTERVAL, null, WriteScanIntervalAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        AUTO_DEPLOY_ZIPPED, null, WriteAutoDeployZipAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        AUTO_DEPLOY_EXPLODED, null, WriteAutoDeployExplodedAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        AUTO_DEPLOY_XML, null, WriteAutoDeployXMLAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        DEPLOYMENT_TIMEOUT, null, WriteDeploymentTimeoutAttributeHandler.INSTANCE);
    resourceRegistration.registerReadWriteAttribute(
        RUNTIME_FAILURE_CAUSES_ROLLBACK,
        null,
        WriteRuntimeFailureCausesRollbackAttributeHandler.INSTANCE);
  }
}
  /** {@inheritDoc} */
  @Override
  public void initialize(ExtensionContext context) {
    ROOT_LOGGER.debug("Initializing Deployment Scanner Extension");

    final SubsystemRegistration subsystem =
        context.registerSubsystem(CommonAttributes.DEPLOYMENT_SCANNER);
    subsystem.registerXMLElementWriter(parser);
    final ManagementResourceRegistration registration = subsystem.registerSubsystemModel(SUBSYSTEM);
    registration.registerOperationHandler(
        DeploymentScannerSubsystemAdd.OPERATION_NAME,
        DeploymentScannerSubsystemAdd.INSTANCE,
        DeploymentScannerSubsystemAdd.INSTANCE,
        false);
    registration.registerOperationHandler(
        DeploymentScannerSubsystemRemove.OPERATION_NAME,
        DeploymentScannerSubsystemRemove.INSTANCE,
        DeploymentScannerSubsystemRemove.INSTANCE,
        false);
    // Register operation handlers
    final ManagementResourceRegistration scanners =
        registration.registerSubModel(scannersPath, SCANNER);
    scanners.registerOperationHandler(
        DeploymentScannerAdd.OPERATION_NAME,
        DeploymentScannerAdd.INSTANCE,
        DeploymentScannerAdd.INSTANCE,
        false);
    scanners.registerOperationHandler(
        DeploymentScannerRemove.OPERATION_NAME,
        DeploymentScannerRemove.INSTANCE,
        DeploymentScannerRemove.INSTANCE,
        false);
    scanners.registerReadWriteAttribute(
        Attribute.PATH.getLocalName(),
        null,
        WritePathAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.RELATIVE_TO.getLocalName(),
        null,
        WriteRelativeToAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.SCAN_ENABLED.getLocalName(),
        null,
        WriteEnabledAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.SCAN_INTERVAL.getLocalName(),
        null,
        WriteScanIntervalAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.AUTO_DEPLOY_ZIPPED.getLocalName(),
        null,
        WriteAutoDeployZipAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.AUTO_DEPLOY_EXPLODED.getLocalName(),
        null,
        WriteAutoDeployExplodedAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
    scanners.registerReadWriteAttribute(
        Attribute.DEPLOYMENT_TIMEOUT.getLocalName(),
        null,
        WriteDeploymentTimeoutAttributeHandler.INSTANCE,
        Storage.CONFIGURATION);
  }