Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, 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.domain.controller.plan;
 
 
 import java.util.List;
 import java.util.Map;

Author(s):
Emanuel Muckenhuber
 
 public abstract class ServerTaskExecutor {
 
     private final OperationContext context;
 
     protected ServerTaskExecutor(OperationContext contextMap<ServerIdentityExecutedServerRequestsubmittedTasksList<ServerPreparedResponsepreparedResults) {
         this. = context;
         this. = submittedTasks;
         this. = preparedResults;
     }

    
Execute

Parameters:
listener the transactional operation listener
identity the server identity
operation the operation
Returns:
whether the operation was executed or not
Throws:
org.jboss.as.controller.OperationFailedException
 
     protected abstract boolean execute(final TransactionalProtocolClient.TransactionalOperationListener<ServerOperationlistenerfinal ServerIdentity identityfinal ModelNode operationthrows OperationFailedException;

    
Execute a server task.

Parameters:
listener the transactional server listener
task the server task
Returns:
whether the task was executed or not
 
     public boolean executeTask(final TransactionalProtocolClient.TransactionalOperationListener<ServerOperationlistenerfinal ServerUpdateTask task) {
         try {
             return execute(listenertask.getServerIdentity(), task.getOperation());
         } catch (OperationFailedException e) {
             // Handle failures operation transformation failures
             final ServerIdentity identity = task.getServerIdentity();
             final ServerOperation serverOperation = new ServerOperation(identitytask.getOperation(), nullnull.);
             final TransactionalProtocolClient.PreparedOperation<ServerOperationresult = BlockingQueueOperationListener.FailedOperation.create(serverOperatione);
             listener.operationPrepared(result);
         }
         return true;
     }

    
Execute the operation.

Parameters:
listener the transactional operation listener
client the transactional protocol client
identity the server identity
operation the operation
transformer the operation result transformer
Returns:
whether the operation was executed
    protected boolean executeOperation(final TransactionalProtocolClient.TransactionalOperationListener<ServerOperationlistenerTransactionalProtocolClient clientfinal ServerIdentity identityfinal ModelNode operationfinal OperationResultTransformer transformer) {
        if(client == null) {
            return false;
        }
        final OperationMessageHandler messageHandler = new DelegatingMessageHandler();
        final OperationAttachments operationAttachments = new DelegatingOperationAttachments();
        final ServerOperation serverOperation = new ServerOperation(identityoperationmessageHandleroperationAttachmentstransformer);
        try {
            final Future<ModelNoderesult = client.execute(listenerserverOperation);
            recordExecutedRequest(new ExecutedServerRequest(identityresulttransformer));
        } catch (IOException e) {
            final TransactionalProtocolClient.PreparedOperation<ServerOperationresult = BlockingQueueOperationListener.FailedOperation.create(serverOperatione);
            listener.operationPrepared(result);
            recordExecutedRequest(new ExecutedServerRequest(identityresult.getFinalResult(), transformer));
        }
        return true;
    }

    
Record an executed request.

Parameters:
task the executed task
    void recordExecutedRequest(final ExecutedServerRequest task) {
        synchronized () {
            .put(task.getIdentity(), task);
        }
    }

    
Record a prepare operation.

Parameters:
preparedOperation the prepared operation
        recordPreparedTask(new ServerTaskExecutor.ServerPreparedResponse(preparedOperation));
    }

    
Record a prepared operation.

Parameters:
task the prepared operation
        synchronized () {
            .add(task);
        }
    }
        @Override
            super.operationPrepared(prepared);
        }
        @Override
        public void operationComplete(ServerOperation operationModelNode result) {
            super.operationComplete(operationresult);
        }
        @Override
        protected void drainTo(Collection<TransactionalProtocolClient.PreparedOperation<ServerOperation>> preparedOperations) {
            super.drainTo(preparedOperations);
        }
    }
    public static class ServerOperation extends TransactionalOperationImpl {
        private final ServerIdentity identity;
        private final OperationResultTransformer transformer;
        ServerOperation(ServerIdentity identityModelNode operationOperationMessageHandler messageHandlerOperationAttachments attachmentsOperationResultTransformer transformer) {
            super(operationmessageHandlerattachments);
            this. = identity;
            this. = transformer;
        }
        public ServerIdentity getIdentity() {
            return ;
        }
        ModelNode transformResult(ModelNode result) {
            return .transformResult(result);
        }
    }

    
The prepared response.
    public static class ServerPreparedResponse {
            this. = preparedOperation;
        }
            return ;
        }
        public ServerIdentity getServerIdentity() {
            return .getOperation().getIdentity();
        }
        public String getServerGroupName() {
            return getServerIdentity().getServerGroupName();
        }

        
Finalize the transaction. This will return false in case the local operation failed, but the overall state of the operation is commit=true.

Parameters:
commit true to commit, false to rollback
Returns:
whether the local proxy operation result is in sync with the overall operation
        public boolean finalizeTransaction(boolean commit) {
            final boolean failed = .isFailed();
            if(commit && failed) {
                return false;
            }
            if(commit) {
                .commit();
            } else {
                if(!failed) {
                    .rollback();
                }
            }
            return true;
        }
    }

    
The executed request.
    public static class ExecutedServerRequest implements OperationResultTransformerCancellable {
        private final ServerIdentity identity;
        private final Future<ModelNodefinalResult;
        private final OperationResultTransformer transformer;
        public ExecutedServerRequest(ServerIdentity identityFuture<ModelNodefinalResult) {
            this(identityfinalResult.);
        }
        public ExecutedServerRequest(ServerIdentity identityFuture<ModelNodefinalResultOperationResultTransformer transformer) {
            this. = identity;
            this. = finalResult;
            this. = transformer;
        }
        public ServerIdentity getIdentity() {
            return ;
        }
        public Future<ModelNodegetFinalResult() {
            return ;
        }
        @Override
        public ModelNode transformResult(final ModelNode result) {
            return .transformResult(result);
        }
        @Override
        public boolean cancel() {
            return .cancel(false);
        }
    }
    private static class DelegatingMessageHandler implements OperationMessageHandler {
        private final OperationContext context;
        DelegatingMessageHandler(final OperationContext context) {
            this. = context;
        }
        @Override
        public void handleReport(MessageSeverity severityString message) {
            .report(severitymessage);
        }
    }
    private static class DelegatingOperationAttachments implements OperationAttachments {
        private final OperationContext context;
        private DelegatingOperationAttachments(final OperationContext context) {
            this. = context;
        }
        @Override
        public boolean isAutoCloseStreams() {
            return false;
        }
        @Override
        public List<InputStreamgetInputStreams() {
            int count = .getAttachmentStreamCount();
            List<InputStreamresult = new ArrayList<InputStream>(count);
            for (int i = 0; i < counti++) {
                result.add(.getAttachmentStream(i));
            }
            return result;
        }
        @Override
        public void close() throws IOException {
            //
        }
    }
New to GrepCode? Check out our FAQ X