Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.test.integration.management.extension.streams;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SYSTEM_PROPERTY;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.VALUE;
 
 import java.io.File;
 
Extension that can provide log files as a stream.

Author(s):
Brian Stansberry (c) 2014 Red Hat Inc.
 
 public class LogStreamExtension implements Extension {
 
     public static final String MODULE_NAME = "org.wildfly.extension.log-stream-test";
     public static final String SUBSYSTEM_NAME = "log-stream-test";
     public static final String STREAM_LOG_FILE = "stream-log-file";
     public static final String LOG_MESSAGE_PROP = "wildfly.test.stream.response.key";
 
     private static final EmptySubsystemParser PARSER = new EmptySubsystemParser("urn:wildfly:extension:log-stream-test:1.0");
     private static final Logger log = Logger.getLogger(LogStreamExtension.class.getCanonicalName());
 
     public static final AttributeDefinition LOG_FILE = SimpleAttributeDefinitionBuilder.create("log-file".)
             .setStorageRuntime()
             .setAllowNull(true)
             .build();
 
     @Override
     public void initialize(ExtensionContext context) {
         SubsystemRegistration subsystem = context.registerSubsystem(, ModelVersion.create(1));
         subsystem.registerXMLElementWriter();
     }
 
     @Override
     public void initializeParsers(ExtensionParsingContext context) {
     }
 
     public static String getLogMessage(String key) {
         return "LogStreamHandler invoked with key " + key;
     }
 
     private static class LogStreamSubsystemResourceDefinition extends SimpleResourceDefinition {
 
         private final OperationStepHandler handler;
         private LogStreamSubsystemResourceDefinition() {
             super(PathElement.pathElement(), new NonResolvingResourceDescriptionResolver(),
                    new AbstractAddStepHandler(),
                    .);
            this. = new LogStreamHandler();
        }
        @Override
        public void registerOperations(ManagementResourceRegistration resourceRegistration) {
            super.registerOperations(resourceRegistration);
            resourceRegistration.registerOperationHandler(.);
            .info("Registered log-stream-test operations");
        }
        @Override
        public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
            resourceRegistration.registerReadOnlyAttribute();
            .info("Registered log-stream-test attributes");
        }
    }
    private static class LogStreamHandler implements OperationStepHandler {
        private static final OperationDefinition DEFINITION = new SimpleOperationDefinitionBuilder(,
                new NonResolvingResourceDescriptionResolver())
                .setReplyType(.)
                .build();
        @Override
        public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
            // Put an entry in the log that the test driver can use to verify a complete log
            logPropValue(context);
            String path;
            if (context.getProcessType().isServer()) {
                path = System.getProperty("jboss.server.log.dir") + . + "server.log";
            } else {
                path = System.getProperty("jboss.domain.log.dir") + . + "host-controller.log";
            }
            File f = new File(path);
            try {
                String uuid = context.attachResultStream("text/plain"new FileInputStream(f));
                context.getResult().set(uuid);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        private void logPropValue(OperationContext context) {
            Resource res = context.readResourceFromRoot(PathAddress.pathAddress());
            String key = res.getModel().get().asString();
            .info(getLogMessage(key));
        }
    }
New to GrepCode? Check out our FAQ X