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/>.
  *
  * ----------------------------------------------------
  *
  * Portions originally based on Apache Tuscany 2007
  * licensed under the Apache 2.0 license.
  *
  */
 package org.fabric3.fabric.generator.context;
 
 import java.util.List;
 import java.util.Map;
 
 
Generates commands to start deployable contexts in a zone. The start commands will be ordered based on a reverse topological sort of collocated wiring dependencies. For example, if component A' in deployable A is wired to component B' in deployable B, and the wire is local (i.e. not via a binding), the command to start deployable context B is ordered before that to start context A.

Version:
$Rev: 7114 $ $Date: 2009-06-14 20:07:59 +0200 (Sun, 14 Jun 2009) $
 
     private TopologicalSorter<QNamesorter;
 
     public StartContextCommandGeneratorImpl() {
          = new TopologicalSorterImpl<QName>();
     }
 
     public Map<StringList<Command>> generate(List<LogicalComponent<?>> componentsCommandMap mapboolean incrementalthrows GenerationException {
         Map<StringList<Command>> commands = new HashMap<StringList<Command>>();
         List<QNamedeployables = new ArrayList<QName>();
         for (LogicalComponent<?> component : components) {
             if (component.getState() == . || !incremental) {
                 StartContextCommand command = new StartContextCommand(component.getDeployable());
                 List<Commandlist = getCommands(component.getZone(), commands);
                 if (!list.contains(command)) {
                     list.add(command);
                 }
                 deployables.add(component.getDeployable());
            }
        }
        try {
            sort(commandsmapdeployables);
        } catch (GraphException e) {
            throw new GenerationException(e);
        }
        return commands;
    }

    
Returns the list of commands by zone, creating one if necessary.

Parameters:
zone the zone
commands the list of commands maped by zone
Returns:
the list of commands for a zone
    private List<CommandgetCommands(String zoneMap<StringList<Command>> commands) {
        List<Commandlist = commands.get(zone);
        if (list == null) {
            list = new ArrayList<Command>();
            commands.put(zonelist);
        }
        return list;
    }

    
Sorts the start context commands by the order of the deployable contexts.

Parameters:
commands the start context commands
map the sorted map of zone id to ordered list of start context commands
deployables the list of deployables
Throws:
org.fabric3.util.graph.GraphException if an error occurs building the graph used to calculatre order occurs
    private void sort(Map<StringList<Command>> commandsCommandMap mapList<QNamedeployablesthrows GraphException {
        for (Map.Entry<StringList<Command>> entry : map.getCommands().entrySet()) {
            Map<QNameIntegerorder = calculateDeployableOrder(entry.getValue(), deployables);
            if (order.isEmpty()) {
                return;
            }
            ContextComparator comparator = new ContextComparator(order);
            List<Commandlist = commands.get(entry.getKey());
            if (list == null) {
                // no commands for zone
                return;
            }
            Collections.sort(listcomparator);
        }
    }


    
Builds a directed acyclic graph of deployable contexts related through collocated forward wires by introspecting the list of AttachWire commands for a zone. This DAG is then sorted and the reverse topological order representing the deployable context dependencies is returned. Callback wires are ignored as depenency ordering is done in the forward direction.

Parameters:
commands the list of commands
deployables the list of deployables
Returns:
a map of deplopyable contexts and their relative ordering
Throws:
org.fabric3.util.graph.GraphException if an error building the graph is raised
    private Map<QNameIntegercalculateDeployableOrder(List<CommandcommandsList<QNamedeployablesthrows GraphException {
        DirectedGraph<QNamedag = new DirectedGraphImpl<QName>();
        // add the contributions as vertices
        for (Command command : commands) {
            if (command instanceof ConnectionCommand) {
                ConnectionCommand connectionCommand = (ConnectionCommandcommand;
                for (AttachWireCommand wireCommand : connectionCommand.getAttachCommands()) {
                    PhysicalWireDefinition definition = wireCommand.getPhysicalWireDefinition();
                    QName source = definition.getSourceDeployable();
                    QName target = definition.getTargetDeployable();
                    Vertex<QNamesourceVertex = null;
                    if (source != null) {
                        sourceVertex = findVertex(dagsource);
                        if (sourceVertex == null) {
                            sourceVertex = new VertexImpl<QName>(source);
                            dag.add(sourceVertex);
                        }
                    }
                    Vertex<QNametargetVertex = null;
                    if (target != null && (source == null || !source.equals(target))) {
                        targetVertex = findVertex(dagtarget);
                        if (targetVertex == null) {
                            targetVertex = new VertexImpl<QName>(target);
                            dag.add(targetVertex);
                        }
                    }
                    if (sourceVertex != null && targetVertex != null) {
                        Edge<QNameedge = dag.getEdge(sourceVertextargetVertex);
                        if (edge == null) {
                            dag.add(new EdgeImpl<QName>(sourceVertextargetVertex));
                        }
                    }
                }
            }
        }
        List<Vertex<QName>> vertices = .reverseSort(dag);
        Map<QNameIntegerdeployableOrder = new HashMap<QNameInteger>(vertices.size());
        int i = 0;
        while (i < vertices.size()) {
            Vertex<QNamevertex = vertices.get(i);
            deployableOrder.put(vertex.getEntity(), i);
            i++;
        }
        // The deployables calculated from the graph of wires may not be all of the deployables since a composite may contain components with no wires
        // Add the rest of the deployables to the end of the list
        for (QName deployable : deployables) {
            if (!deployableOrder.containsKey(deployable)) {
                deployableOrder.put(deployablei);
                i++;
            }
        }
        return deployableOrder;
    }
    private Vertex<QNamefindVertex(DirectedGraph<QNamedagQName name) {
        for (Vertex<QNamevertex : dag.getVertices()) {
            if (vertex.getEntity().equals(name)) {
                return vertex;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X