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.fabric.generator.extension;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
Default ExtensionGenerator implementation.

Version:
$Rev: 7435 $ $Date: 2009-07-26 12:23:58 +0200 (Sun, 26 Jul 2009) $
 
 public class ExtensionGeneratorImpl implements ExtensionGenerator {
     private MetaDataStore store;
     private HostInfo info;
     private ContributionUriEncoder encoder;
 
     public ExtensionGeneratorImpl(@Reference MetaDataStore store, @Reference HostInfo info) {
         this. = store;
         this. = info;
     }

    
Setter for injecting the service for encoding contribution URIs so they may be derferenced in a domain. This is done lazily as the encoder is supplied by an extension which is intialized after this component which is needed during bootstrap.

Parameters:
encoder the encoder to inject
 
     @Reference(required = false)
    public void setEncoder(ContributionUriEncoder encoder) {
        this. = encoder;
    }
    public Map<StringCommandgenerate(Map<StringList<Contribution>> contributions,
                                         List<LogicalComponent<?>> components,
                                         CommandMap commandMap,
                                         GenerationType typethrows GenerationException {
        if (. != .getRuntimeMode()) {
            // short circuit this unless running in distributed mode
            return null;
        }
        Map<StringCommandcommands = new HashMap<StringCommand>();
        // evaluate contributions being provisioned for required capabilities
        evaluateContributions(contributionscommandstype);
        // evaluate components for required capabilities
        evaluateComponents(componentscommandstype);
        // evaluate policies on wires
        evaluatePolicies(commandscontributionscommandMaptype);
        if (commands.isEmpty()) {
            return null;
        }
        return commands;
    }

    
Evaluates contributions for required capabilities, resolving those capabilities to extensions.

Parameters:
contributions the contributions to evaluate
commands the list of commands to update with un/provison extension commands
type the generation type
Throws:
org.fabric3.spi.generator.GenerationException if an exception occurs
    private void evaluateContributions(Map<StringList<Contribution>> contributions,
                                       Map<StringCommandcommands,
                                       GenerationType typethrows GenerationException {
        for (Map.Entry<StringList<Contribution>> entry : contributions.entrySet()) {
            String zone = entry.getKey();
            if (zone == null) {
                // skip local runtime
                continue;
            }
            AbstractExtensionsCommand command;
            if (type == .) {
                command = new UnProvisionExtensionsCommand();
            } else {
                command = new ProvisionExtensionsCommand();
            }
            List<ContributionzoneContributions = entry.getValue();
            Set<Contributionextensions = new HashSet<Contribution>();
            for (Contribution contribution : zoneContributions) {
                Set<Contributionrequired = .resolveCapabilities(contribution);
                extensions.addAll(required);
            }
            for (Contribution extension : extensions) {
                URI encoded = encode(extension.getUri());
                command.addExtensionUri(encoded);
            }
            if (!command.getExtensionUris().isEmpty()) {
                commands.put(zonecommand);
            }
        }
    }

    
Evaluates components for required capabilities, resolving those capabilities to extensions.

Parameters:
components the components to evaluate
commands the list of commands to update with un/provison extension commands
type the generation type
Throws:
org.fabric3.spi.generator.GenerationException if an exception occurs
    private void evaluateComponents(List<LogicalComponent<?>> components,
                                    Map<StringCommandcommands,
                                    GenerationType typethrows GenerationException {
        for (LogicalComponent<?> component : components) {
            String zone = component.getZone();
            if (zone == null) {
                // skip local runtime
                continue;
            }
            AbstractExtensionsCommand command = getExtensionsCommand(commandszonetype);
            evaluateComponent(componentcommandtype);
            if (!command.getExtensionUris().isEmpty()) {
                commands.put(zonecommand);
            }
        }
    }

    
Evaluates a component for required capabilities.

Parameters:
component the component
command the command to update
type the generation type
Throws:
org.fabric3.spi.generator.GenerationException if an exception during evaluation is encountered
    private void evaluateComponent(LogicalComponent<?> componentAbstractExtensionsCommand commandGenerationType typethrows GenerationException {
        Implementation<?> impl = component.getDefinition().getImplementation();
        AbstractComponentType<?, ?, ?, ?> componentType = impl.getComponentType();
        Set<Contributionextensions = new HashSet<Contribution>();
        if (isGenerate(component.getState(), type)) {
            for (String capability : componentType.getRequiredCapabilities()) {
                extensions.addAll(.resolveCapability(capability));
            }
            for (String capability : impl.getRequiredCapabilities()) {
                extensions.addAll(.resolveCapability(capability));
            }
        }
        // evaluate services
        for (LogicalService service : component.getServices()) {
            for (LogicalBinding<?> binding : service.getBindings()) {
                if (isGenerate(binding.getState(), type)) {
                    for (String capability : binding.getDefinition().getRequiredCapabilities()) {
                        extensions.addAll(.resolveCapability(capability));
                    }
                }
            }
        }
        // evaluate references
        for (LogicalReference reference : component.getReferences()) {
            for (LogicalBinding<?> binding : reference.getBindings()) {
                if (isGenerate(binding.getState(), type)) {
                    for (String capability : binding.getDefinition().getRequiredCapabilities()) {
                        extensions.addAll(.resolveCapability(capability));
                    }
                }
            }
        }
        for (Contribution extension : extensions) {
            URI uri = extension.getUri();
            URI encoded = encode(uri);
            if (!command.getExtensionUris().contains(encoded) && !..equals(uri) && !..equals(uri)) {
                command.addExtensionUri(encoded);
            }
        }
    }
    private boolean isGenerate(LogicalState stateGenerationType type) {
        if (. == type && . != state) {
            return true;
        } else if (. == type && . == state) {
            return true;
        } else if (. == type && . == state) {
            return true;
        }
        return false;
    }

    
Evaluates policy interceptors added to wires for required capabilities, resolving those capabilities to extensions.

Parameters:
contributions the contributions to evaluate
commandMap the generated command map to introspect for policy interceptors
type the generation type
commands the list of commands to update with un/provison extension commands
Throws:
org.fabric3.spi.generator.GenerationException if an exception occurs
    private void evaluatePolicies(Map<StringCommandcommands,
                                  Map<StringList<Contribution>> contributions,
                                  CommandMap commandMap,
                                  GenerationType typethrows GenerationException {
        for (Map.Entry<StringList<Command>> entry : commandMap.getCommands().entrySet()) {
            String zone = entry.getKey();
            if (zone == null) {
                // skip local runtime
                continue;
            }
            for (Command generatedCommand : entry.getValue()) {
                if (generatedCommand instanceof ConnectionCommand) {
                    ConnectionCommand connectionCommand = (ConnectionCommandgeneratedCommand;
                    if (. == type) {
                        // undeployment, evaluate wire detach commands
                        for (DetachWireCommand command : connectionCommand.getDetachCommands()) {
                            evaluateWireCommand(commandcommandscontributionszonetype);
                        }
                    } else {
                        // a deployment, evaluate the wire attach commands
                        for (AttachWireCommand command : connectionCommand.getAttachCommands()) {
                            evaluateWireCommand(commandcommandscontributionszonetype);
                        }
                    }
                }
            }
        }
    }
    private void evaluateWireCommand(WireCommand wireCommand,
                                     Map<StringCommandcommands,
                                     Map<StringList<Contribution>> contributions,
                                     String zoneGenerationType typethrows GenerationException {
        for (PhysicalOperationDefinition operation : wireCommand.getPhysicalWireDefinition().getOperations()) {
            for (PhysicalInterceptorDefinition interceptor : operation.getInterceptors()) {
                URI contributionUri = interceptor.getPolicyClassLoaderId();
                Contribution contribution = .find(contributionUri);
                if (findContribution(contributioncontributions)) {
                    // the interceptor is bundled with user contribution so skip
                    continue;
                }
                AbstractExtensionsCommand command = getExtensionsCommand(commandszonetype);
                URI encoded = encode(contributionUri);
                if (!command.getExtensionUris().contains(encoded)
                        && !..equals(contributionUri)
                        && !..equals(contributionUri)) {
                    command.addExtensionUri(encoded);
                }
                commands.put(zonecommand);
                addDependencies(contributioncommand);
            }
        }
    }

    
Finds a contribution in the list of contributions.

Parameters:
contribution the contribution to find
contributions the contribution to search
Returns:
true if found
    private boolean findContribution(Contribution contributionMap<StringList<Contribution>> contributions) {
        for (List<Contributionlist : contributions.values()) {
            if (list.contains(contribution)) {
                return true;
            }
        }
        return false;
    }

    
Transitively calculates imported contributions and required capabilities. These are then added to the extension un/provision command.

Parameters:
contribution the contribution to calculate imports for
command the command to update
Throws:
org.fabric3.spi.generator.GenerationException if an exception occurs
    private void addDependencies(Contribution contributionAbstractExtensionsCommand commandthrows GenerationException {
        List<ContributionWire<?, ?>> contributionWires = contribution.getWires();
        for (ContributionWire<?, ?> wire : contributionWires) {
            URI importedUri = wire.getExportContributionUri();
            Contribution imported = .find(importedUri);
            addDependencies(importedcommand);
            URI encoded = encode(importedUri);
            if (!command.getExtensionUris().contains(encoded)
                    && !..equals(importedUri)
                    && !..equals(importedUri)) {
                command.addExtensionUri(encoded);
            }
        }
        Set<Contributioncapabilities = .resolveCapabilities(contribution);
        for (Contribution capability : capabilities) {
            URI encoded = encode(capability.getUri());
            if (!command.getExtensionUris().contains(encoded)) {
                command.addExtensionUri(encoded);
            }
        }
    }

    
Gets or creates un/provision extension commands from the command map.

Parameters:
commands the command map
zone the zone extensions are provisioned to
type the generation type
Returns:
the command
    private AbstractExtensionsCommand getExtensionsCommand(Map<StringCommandcommandsString zoneGenerationType type) {
        AbstractExtensionsCommand command;
        command = (AbstractExtensionsCommandcommands.get(zone);    // safe cast
        if (command == null) {
            if (. == type) {
                command = new UnProvisionExtensionsCommand();
            } else {
                command = new ProvisionExtensionsCommand();
            }
        }
        return command;
    }


    
Encodes a contribution URI to one that is derferenceable from a runtime in the domain.

Parameters:
uri the contribution URI
Returns:
a URI that is derferenceable in the domain
Throws:
org.fabric3.spi.generator.GenerationException if the URI cannot be encoded
    private URI encode(URI urithrows GenerationException {
        if ( != null) {
            try {
                return .encode(uri);
            } catch (URISyntaxException e) {
                throw new GenerationException(e);
            }
        }
        return uri;
    }
New to GrepCode? Check out our FAQ X