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.blocker;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUBSYSTEM;
 
 import java.util.Set;
 
Extension that can block threads.

Author(s):
Brian Stansberry (c) 2014 Red Hat Inc.
 
 public class BlockerExtension implements Extension {
 
     public static final String MODULE_NAME = "org.wildfly.extension.blocker-test";
     public static final String SUBSYSTEM_NAME = "blocker-test";
     public static final AttributeDefinition CALLER = SimpleAttributeDefinitionBuilder.create("caller".true)
             .setDefaultValue(new ModelNode("unknown")).build();
     public static final AttributeDefinition TARGET_HOST = SimpleAttributeDefinitionBuilder.create(.true).build();
     public static final AttributeDefinition TARGET_SERVER = SimpleAttributeDefinitionBuilder.create(.true).build();
     public static final AttributeDefinition BLOCK_POINT = SimpleAttributeDefinitionBuilder.create("block-point".)
             .setValidator(EnumValidator.create(BlockPoint.classfalsefalse))
             .build();
     public static final AttributeDefinition BLOCK_TIME = SimpleAttributeDefinitionBuilder.create("block-time".true)
             .setDefaultValue(new ModelNode(20000))
             .build();
 
     public static final AttributeDefinition FOO = SimpleAttributeDefinitionBuilder.create("foo".true).build();
     public static final String REGISTERED_MESSAGE = "Registered blocker-test operations";
 
     private static final EmptySubsystemParser PARSER = new EmptySubsystemParser("urn:wildfly:extension:blocker-test:1.0");
     private static final Logger log = Logger.getLogger(BlockerExtension.class.getCanonicalName());
 
     @Override
     public void initialize(ExtensionContext context) {
         SubsystemRegistration subsystem = context.registerSubsystem(, ModelVersion.create(1));
         subsystem.registerXMLElementWriter();
     }
    @Override
    public void initializeParsers(ExtensionParsingContext context) {
    }
    private static class BlockerSubsystemResourceDefinition extends SimpleResourceDefinition {
        private final boolean forHost;
        private BlockerSubsystemResourceDefinition(boolean forHost) {
            super(PathElement.pathElement(), new NonResolvingResourceDescriptionResolver(),
                    new AbstractAddStepHandler(),
                    .);
            this. = forHost;
        }
        @Override
        public void registerOperations(ManagementResourceRegistration resourceRegistration) {
            super.registerOperations(resourceRegistration);
            resourceRegistration.registerOperationHandler(.new BlockHandler());
            if () {
            }
            // Don't remove this as some tests check for it in the log
            .info();
        }
        @Override
        public void registerAttributes(ManagementResourceRegistration resourceRegistration) {
            super.registerAttributes(resourceRegistration);
            resourceRegistration.registerReadWriteAttribute(nullnew ModelOnlyWriteAttributeHandler());
        }
    }
    public static enum BlockPoint {
        MODEL,
        RUNTIME,
        SERVICE_START,
        SERVICE_STOP,
        VERIFY,
        COMMIT,
        ROLLBACK
    }
    private static class BlockHandler implements OperationStepHandler {
        private static final OperationDefinition DEFINITION = new SimpleOperationDefinitionBuilder("block"new NonResolvingResourceDescriptionResolver())
                .setParameters()
                .build();
        @Override
        public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
            ModelNode targetServer = .resolveModelAttribute(contextoperation);
            ModelNode targetHost = .resolveModelAttribute(contextoperation);
            final BlockPoint blockPoint = BlockPoint.valueOf(.resolveModelAttribute(contextoperation).asString());
            .info("block requested by " + .resolveModelAttribute(contextoperation).asString() + " for " +
                targetHost.asString() + "/" + targetServer.asString() + "(" + blockPoint + ")");
            boolean forMe = false;
            if (context.getProcessType() == .) {
                forMe = true;
            } else {
                context.readResourceForUpdate(.); // To help with WFCORE-263 testing, get the exclusive lock on this process
                Resource rootResource = context.readResourceFromRoot(.);
                if (targetServer.isDefined()) {
                    if (context.getProcessType().isServer()) {
                        String name = System.getProperty(.);
                        forMe = targetServer.asString().equals(name);
                    }
                } else if (context.getProcessType() == .) {
                    Set<Stringhosts = rootResource.getChildrenNames();
                    String name;
                    if (hosts.size() > 1) {
                        name = "master";
                    } else {
                        name = hosts.iterator().next();
                    }
                    if (!targetHost.isDefined()) {
                        throw new OperationFailedException("target-host required");
                    }
                    forMe = targetHost.asString().equals(name);
                }
            }
            if (forMe) {
                final long blockTime = .resolveModelAttribute(contextoperation).asLong();
                .info("will block at " + blockPoint + " for " + blockTime);
                switch (blockPoint) {
                    case : {
                        block(blockTime);
                        break;
                    }
                    case : {
                        context.addStep(new BlockStep(blockTime), ..);
                        break;
                    }
                    case :
                    case : {
                        context.addStep(new OperationStepHandler() {
                            @Override
                            public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                                BlockingService service = new BlockingService(blockTimeblockPoint == .);
                                context.getServiceTarget().addService(.service).install();
                                context.completeStep(new OperationContext.ResultHandler() {
                                    @Override
                                    public void handleResult(OperationContext.ResultAction resultActionOperationContext contextModelNode operation) {
                                        .info("BlockingService step completed: result = " + resultAction);
                                        context.removeService(.);
                                    }
                                });
                            }
                        }, ..);
                        break;
                    }
                    case : {
                        context.addStep(new BlockStep(blockTime), ..);
                        break;
                    }
                    case :
                        context.addStep(new OperationStepHandler() {
                            @Override
                            public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                                context.getFailureDescription().set("rollback");
                                context.setRollbackOnly();
                            }
                        }, ..);
                        break;
                    case :
                        break;
                    default:
                        throw new IllegalStateException(blockPoint.toString());
                }
                context.completeStep(new OperationContext.ResultHandler() {
                    @Override
                    public void handleResult(OperationContext.ResultAction resultActionOperationContext contextModelNode operation) {
                        if ((blockPoint == . && resultAction == ..)
                            || (blockPoint == . && resultAction == ..)) {
                            block(blockTime);
                        }
                    }
                });
            }
        }
        private static void block(long time) {
            try {
                .info("blocking");
                Thread.sleep(time);
            } catch (InterruptedException e) {
                .info("interrupted");
                throw new RuntimeException(e);
            }
        }
        private static class BlockStep implements OperationStepHandler {
            private final long blockTime;
            private BlockStep(long blockTime) {
                this. = blockTime;
            }
            @Override
            public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                block();
            }
        }
    }
    private static class BlockingService implements Service<BlockingService> {
        private static final ServiceName SERVICE_NAME = ServiceName.of("jboss""test""blocking-service");
        private final long blockTime;
        private final boolean blockStart;
        private final Object waitObject = new Object();
        private BlockingService(long blockTimeboolean blockStart) {
            this. = blockTime;
            this. = blockStart;
        }
        @Override
        public void start(final StartContext contextthrows StartException {
            if () {
//                Runnable r = new Runnable() {
//                    @Override
//                    public void run() {
                        try {
                            synchronized () {
                                .info("BlockService blocking in start");
                                .wait();
                            }
                            context.complete();
                        } catch (InterruptedException e) {
                            .info("BlockService interrupted");
//                            context.failed(new StartException(e));
                            throw new StartException(e);
                        }
//                    }
//                };
//                Thread thread = new Thread(r);
//                thread.start();
//                context.asynchronous();
            } else {
                // Not yet used
                throw new UnsupportedOperationException();
            }
        }
        @Override
        public void stop(final StopContext context) {
            if (!) {
                // Not yet used
                throw new UnsupportedOperationException();
            } else {
                synchronized () {
                    .info("BlockService Stopping");
                    .notifyAll();
                }
            }
        }
        @Override
            return this;
        }
    }
New to GrepCode? Check out our FAQ X