Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Fabric3
  * Copyright (c) 2009 Metaform Systems
  *
  * Fabric3 is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as
  * published by the Free Software Foundation, either version 3 of
  * the License, or (at your option) any later version, with the
  * following exception:
 *
 * Linking this software statically or dynamically with other
 * modules is making a combined work based on this software.
 * Thus, the terms and conditions of the GNU General Public
 * License cover the whole combination.
 *
 * As a special exception, the copyright holders of this software
 * give you permission to link this software with independent
 * modules to produce an executable, regardless of the license
 * terms of these independent modules, and to copy and distribute
 * the resulting executable under terms of your choice, provided
 * that you also meet, for each linked independent module, the
 * terms and conditions of the license of that module. An
 * independent module is a module which is not derived from or
 * based on this software. If you modify this software, you may
 * extend this exception to your version of the software, but
 * you are not obligated to do so. If you do not wish to do so,
 * delete this exception statement from your version.
 *
 * Fabric3 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 General Public License for more details.
 *
 * You should have received a copy of the
 * GNU General Public License along with Fabric3.
 * If not, see <http://www.gnu.org/licenses/>.
 */
 package org.fabric3.federation.executor;
 
 import java.util.List;
 
 
Processes a ZoneDeploymentCommand. This may result in routing the command locally, to an individual runtime, or to all runtimes in a zone depending on the correlation semantics.

Version:
$Rev: 7441 $ $Date: 2009-07-26 14:38:08 +0200 (Sun, 26 Jul 2009) $
 
     private ZoneManager zoneManager;
     private RuntimeService runtimeService;
     private ScopeRegistry scopeRegistry;
     private EventService eventService;
     private boolean domainSynchronized;
 
     public ZoneDeploymentCommandExecutor(@Reference ZoneManager zoneManager,
                                          @Reference CommandExecutorRegistry executorRegistry,
                                          @Reference RuntimeService runtimeService,
                                          @Reference ScopeRegistry scopeRegistry,
                                          @Reference EventService eventService,
                                          @Reference ClassLoaderRegistry classLoaderRegistry,
                                          @Monitor ZoneDeploymentCommandExecutorMonitor monitor) {
         this. = zoneManager;
         this. = executorRegistry;
         this. = runtimeService;
         this. = scopeRegistry;
         this. = eventService;
         this. = monitor;
        this. = classLoaderRegistry;
    }
    @Init
    public void init() {
        .register(ZoneDeploymentCommand.classthis);
    }
    public void execute(ZoneDeploymentCommand commandthrows ExecutionException {
        String correlationId = command.getCorrelationId();
        if (correlationId != null) {
            // the command is destined to a specific runtime
            routeToRuntime(command);
        } else {
            // route the command to all runtimes in the zone
            routeToZone(command);
        }
         = true;
    }
    private void routeToRuntime(ZoneDeploymentCommand commandthrows ExecutionException {
        String correlationId = command.getCorrelationId();
        String runtimeName = .getRuntimeName();
        // route the command to a specific runtime
        if (correlationId.equals(runtimeName)) {
            if (( && command.isSynchronization())) {
                // the zone is already synchronized, ignore as this may be a duplicate
                return;
            }
            routeLocally(command);
        } else {
            String id = command.getId();
            boolean routed = false;
            for (RuntimeInstance runtime : .getRuntimes()) {
                String target = runtime.getName();
                if (target.equals(correlationId)) {
                    // deploy to the runtime
                    try {
                        byte[] serialized = serializeRuntimeCommand(command);
                        .sendMessage(targetserialized);
                    } catch (IOException e) {
                        throw new ExecutionException(e);
                    } catch (MessageException e) {
                        throw new ExecutionException(e);
                    }
                    .routed(targetid);
                    routed = true;
                }
            }
            if (!routed) {
                throw new NoTargetRuntimeException("Runtime " + runtimeName + " not found for deployment command: " + id);
            }
        }
    }
    private void routeToZone(ZoneDeploymentCommand commandthrows ExecutionException {
        String runtimeName = .getRuntimeName();
        // route the command to all runtimes in the zone
        for (RuntimeInstance runtime : .getRuntimes()) {
            String target = runtime.getName();
            if (runtimeName.equals(target)) {
                routeLocally(command);
            } else {
                try {
                    // deploy to the runtime
                    byte[] serialized = serializeRuntimeCommand(command);
                    .sendMessage(targetserialized);
                } catch (IOException e) {
                    throw new ExecutionException(e);
                } catch (MessageException e) {
                    throw new ExecutionException(e);
                }
                String id = command.getId();
                .routed(targetid);
            }
        }
    }
    private void routeLocally(ZoneDeploymentCommand commandthrows ExecutionException {
        String runtimeName = .getRuntimeName();
        String id = command.getId();
        .routed(runtimeNameid);
        // execute the extension commands first before deserializing the other commands as they may contain extension-specific metadata classes
        byte[] serializedExtensionCommands = command.getExtensionCommands();
        List<CommandextensionCommands = deserialize(serializedExtensionCommands);
        for (Command cmd : extensionCommands) {
            .execute(cmd);
        }
        try {
        } catch (InstanceLifecycleException e) {
            throw new ExecutionException(e);
        }
        byte[] serializedCommands = command.getCommands();
        List<Commandcommands = deserialize(serializedCommands);
        for (Command cmd : commands) {
            .execute(cmd);
        }
        try {
        } catch (InstanceLifecycleException e) {
            throw new ExecutionException(e);
        }
    }
    private byte[] serializeRuntimeCommand(ZoneDeploymentCommand commandthrows IOException {
        ByteArrayOutputStream bas = new ByteArrayOutputStream();
        String id = command.getId();
        byte[] extensionCommands = command.getExtensionCommands();
        byte[] commands = command.getCommands();
        boolean synchronization = command.isSynchronization();
        RuntimeDeploymentCommand runtimeCommand = new RuntimeDeploymentCommand(idextensionCommandscommandssynchronization);
        stream.writeObject(runtimeCommand);
        stream.close();
        return bas.toByteArray();
    }
    @SuppressWarnings({"unchecked"})
    private List<Commanddeserialize(byte[] commandsthrows ExecutionException {
        MultiClassLoaderObjectInputStream ois = null;
        try {
            InputStream stream = new ByteArrayInputStream(commands);
            // Deserialize the command set. As command set classes may be loaded in an extension classloader, use a MultiClassLoaderObjectInputStream
            // to deserialize classes in the appropriate classloader.
            ois = new MultiClassLoaderObjectInputStream(stream);
            return (List<Command>) ois.readObject();
        } catch (IOException e) {
            throw new ExecutionException(e);
        } catch (ClassNotFoundException e) {
            throw new ExecutionException(e);
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                // ignore;
            }
        }
    }
New to GrepCode? Check out our FAQ X