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.synthesizer;
 
 import java.net.URI;
 
 
 import static org.fabric3.host.Names.BOOT_CONTRIBUTION;
Implementation that synthesizes a singleton component from an existing object instance.

Version:
$Rev: 8435 $ $Date: 2009-12-20 00:24:46 +0100 (Sun, 20 Dec 2009) $
 
 public class SingletonComponentSynthesizer implements ComponentSynthesizer {
 
     private LogicalComponentManager lcm;
     private ScopeContainer scopeContainer;
 
     @Constructor
                                          @Reference AtomicComponentInstantiator instantiator,
                                          @Reference LogicalComponentManager lcm,
                                          @Reference ComponentManager componentManager,
                                          @Reference JavaContractProcessor contractProcessor,
                                          @Reference ScopeRegistry registry) {
         this(implementationProcessorinstantiatorlcmcomponentManagercontractProcessorregistry.getScopeContainer(.));
     }
                                         AtomicComponentInstantiator instantiator,
                                         LogicalComponentManager lcm,
                                         ComponentManager componentManager,
                                         JavaContractProcessor contractProcessor,
                                         ScopeContainer scopeContainer) {
        this. = implementationProcessor;
        this. = instantiator;
        this. = lcm;
        this. = componentManager;
        this. = contractProcessor;
        this. = scopeContainer;
    }
    public <S, I extends S> void registerComponent(String nameClass<S> type, I instanceboolean introspectthrows ComponentRegistrationException {
        try {
            LogicalComponent<?> logical = createLogicalComponent(nametypeinstanceintrospect);
            AtomicComponent<I> physical = createPhysicalComponent(logicalinstance);
            .register(physical);
            .register(physical);
        } catch (RegistrationException e) {
            throw new ComponentRegistrationException(e);
        } catch (AssemblyException e) {
            throw new ComponentRegistrationException(e);
        }
    }
    private <S, I extends S> LogicalComponent<?> createLogicalComponent(String nameClass<S> type, I instanceboolean introspect)
            throws InvalidServiceContractExceptionAssemblyException {
        LogicalCompositeComponent domain = .getRootComponent();
        ComponentDefinition<Implementation<?>> definition = createDefinition(nametypeinstanceintrospect);
        InstantiationContext context = new InstantiationContext();
        LogicalComponent<?> logical = .instantiate(definitiondomaincontext);
        if (context.hasErrors()) {
            throw new AssemblyException(context.getErrors());
        }
        // mark singleton components as provisioned since instances are not created
        logical.setState(.);
        // all references are initially resolved since they are manually injected
        for (LogicalReference reference : logical.getReferences()) {
            reference.setResolved(true);
            for (LogicalWire wire : reference.getWires()) {
                wire.setState(.);
            }
        }
        return logical;
    }
    private <S, I extends S> ComponentDefinition<Implementation<?>> createDefinition(String nameClass<S> type, I instanceboolean introspect)
            throws InvalidServiceContractException {
        String implClassName = instance.getClass().getName();
        ClassLoader loader = getClass().getClassLoader();
        IntrospectionContext context = new DefaultIntrospectionContext(loader);
        if (introspect) {
            // introspect the instance so it may be injected by the runtime with additional services
            SystemImplementation implementation = new SystemImplementation();
            implementation.setImplementationClass(implClassName);
            .introspect(implementationcontext);
            ComponentDefinition<Implementation<?>> def = new ComponentDefinition<Implementation<?>>(name);
            SingletonImplementation singletonImplementation = new SingletonImplementation(implementation.getComponentType(), implClassName);
            def.setImplementation(singletonImplementation);
            def.setContributionUri();
            return def;
        } else {
            // instance does not have any services injected
            ServiceContract contract = .introspect(typecontext);
            if (context.hasErrors()) {
                throw new InvalidServiceContractException(context.getErrors());
            }
            String serviceName = contract.getInterfaceName();
            ServiceDefinition service = new ServiceDefinition(serviceNamecontract);
            InjectingComponentType componentType = new InjectingComponentType(implClassName);
            componentType.add(service);
            SingletonImplementation implementation = new SingletonImplementation(componentTypeimplClassName);
            implementation.setComponentType(componentType);
            ComponentDefinition<Implementation<?>> def = new ComponentDefinition<Implementation<?>>(name);
            def.setImplementation(implementation);
            def.setContributionUri();
            return def;
        }
    }
    private <I> AtomicComponent<I> createPhysicalComponent(LogicalComponent<?> logicalComponent, I instance) {
        URI uri = logicalComponent.getUri();
        InjectingComponentType type = (InjectingComponentTypelogicalComponent.getDefinition().getComponentType();
        type.getInjectionSites();
        SingletonComponent<I> component = new SingletonComponent<I>(uriinstancetype.getInjectionSites());
        component.setClassLoaderId();
        return component;
    }
New to GrepCode? Check out our FAQ X