Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package org.apache.tuscany.sca.assembly.builder;
 
 import java.util.List;
 import java.util.Map;
 
 
Default implementation of a provider factory extension point.

Version:
$Rev: 963624 $ $Date: 2010-07-13 10:12:08 +0100 (Tue, 13 Jul 2010) $
 
 
     private ExtensionPointRegistry registry;
     private final Map<StringCompositeBuilderbuilders = new HashMap<StringCompositeBuilder>();
     private final Map<QNameBindingBuilderbindingBuilders = new HashMap<QNameBindingBuilder>();
     private final Map<QNameImplementationBuilderimplementationBuilders =
         new HashMap<QNameImplementationBuilder>();
     private final Map<QNamePolicyBuilderpolicyBuilders = new HashMap<QNamePolicyBuilder>();
     private ContractBuilder contractBuilder  = null;
 
     private boolean loaded;
 
         this. = registry;
     }
 
     public void start() {
     }
 
     public void stop() {
         .clear();
         .clear();
         .clear();
          = false;
     }
 
     public void addCompositeBuilder(CompositeBuilder builder) {
         .put(builder.getID(), builder);
     }
 
     public void removeCompositeBuilder(CompositeBuilder builder) {
         .remove(builder.getID());
     }
 
     public CompositeBuilder getCompositeBuilder(String id) {
         loadBuilders();
         return .get(id);
     }
     
     public void addContractBuilder(ContractBuilder contractBuilder){ 
         this. = contractBuilder;
     }
 
     public void removeContractBuilder(ContractBuilder contractBuilder){
         this. = null;
     }
 
        loadBuilders();
        return ;
    }

    
Load builders declared under META-INF/services.
    private synchronized void loadBuilders() {
        if ()
            return;
        InterfaceContractMapper mapper = utils.getUtility(InterfaceContractMapper.class);
        // Get the provider factory service declarations
        Collection<ServiceDeclarationbuilderDeclarations;
        ServiceDiscovery serviceDiscovery = .getServiceDiscovery();
        try {
            builderDeclarations = serviceDiscovery.getServiceDeclarations(CompositeBuilder.class.getName());
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        for (ServiceDeclaration builderDeclaration : builderDeclarations) {
            Map<StringStringattributes = builderDeclaration.getAttributes();
            String id = attributes.get("id");
            CompositeBuilder builder = new LazyCompositeBuilder(idbuilderDeclarationthisfactoriesmapper);
            .put(idbuilder);
        }
        try {
            builderDeclarations = serviceDiscovery.getServiceDeclarations(BindingBuilder.class);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        for (ServiceDeclaration builderDeclaration : builderDeclarations) {
            BindingBuilder<?> builder = new LazyBindingBuilder(builderDeclaration);
            .put(builder.getBindingType(), builder);
        }
        try {
            builderDeclarations = serviceDiscovery.getServiceDeclarations(ImplementationBuilder.class);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        for (ServiceDeclaration builderDeclaration : builderDeclarations) {
            ImplementationBuilder<?> builder = new LazyImplementationBuilder(builderDeclaration);
            .put(builder.getImplementationType(), builder);
        }
        
        try {
            builderDeclarations = serviceDiscovery.getServiceDeclarations(PolicyBuilder.class);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        for (ServiceDeclaration builderDeclaration : builderDeclarations) {
            PolicyBuilder<?> builder = new LazyPolicyBuilder(builderDeclaration);
            .put(builder.getPolicyType(), builder);
        }
        
        try {
            builderDeclarations = serviceDiscovery.getServiceDeclarations(ContractBuilder.class);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        
        for (ServiceDeclaration builderDeclaration : builderDeclarations) {
             = new LazyContractBuilder(builderDeclaration);
        }
         = true;
    }
    public void addBindingBuilder(BindingBuilder<?> bindingBuilder) {
        .put(bindingBuilder.getBindingType(), bindingBuilder);
    }
    
    public <B extends BindingBindingBuilder<B> getBindingBuilder(QName bindingType) {
        loadBuilders();
        return (BindingBuilder<B>).get(bindingType);
    }
    public <B extends Bindingvoid removeBindingBuilder(BindingBuilder<B> builder) {
        .remove(builder.getBindingType());
    }
    public void addImplementationBuilder(ImplementationBuilder<?> implementationBuilder) {
        .put(implementationBuilder.getImplementationType(), implementationBuilder);
    }
    public <I extends ImplementationImplementationBuilder<I> getImplementationBuilder(QName implementationType) {
        loadBuilders();
        return (ImplementationBuilder<I>).get(implementationType);
    }
    public <I extends Implementationvoid removeImplementationBuilder(ImplementationBuilder<I> builder) {
    }
    public void addPolicyBuilder(PolicyBuilder<?> policyBuilder) {
        .put(policyBuilder.getPolicyType(), policyBuilder);
    }
    public <B> PolicyBuilder<B> getPolicyBuilder(QName policyType) {
        loadBuilders();
        return (PolicyBuilder<B>).get(policyType);
    }
    
        loadBuilders();
        return .values();
    }
    public <B> void removePolicyBuilder(PolicyBuilder<B> builder) {
        .remove(builder.getPolicyType());
    }
    
    
A wrapper around a composite builder allowing lazy loading and initialization of implementation providers.
    private class LazyCompositeBuilder implements CompositeBuilder {
        private FactoryExtensionPoint factories;
        private InterfaceContractMapper mapper;
        private String id;
        private ServiceDeclaration builderDeclaration;
        private CompositeBuilder builder;
        private BuilderExtensionPoint builders;
        private LazyCompositeBuilder(String id,
                                     ServiceDeclaration factoryDeclaration,
                                     BuilderExtensionPoint builders,
                                     FactoryExtensionPoint factories,
                                     InterfaceContractMapper mapper) {
            this. = id;
            this. = factoryDeclaration;
            this. = builders;
            this. = factories;
            this. = mapper;
        }
        public String getID() {
            return ;
        }
        public Composite build(Composite compositeBuilderContext context)
            throws CompositeBuilderException {
            return getBuilder().build(compositecontext);
        }
        private CompositeBuilder getBuilder() {
            if ( == null) {
                try {
                    Class<CompositeBuilderbuilderClass = (Class<CompositeBuilder>).loadClass();
                    try {
                        Constructor<CompositeBuilderconstructor =
                            builderClass.getConstructor(FactoryExtensionPoint.classInterfaceContractMapper.class);
                         = constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        try {
                            Constructor<CompositeBuilderconstructor =
                                builderClass.getConstructor(BuilderExtensionPoint.class,
                                                            FactoryExtensionPoint.class,
                                                            InterfaceContractMapper.class);
                             = constructor.newInstance();
                        } catch (NoSuchMethodException ex) {
                            Constructor<CompositeBuilderconstructor =
                                builderClass.getConstructor(ExtensionPointRegistry.class);
                             = constructor.newInstance();
                        }
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }
    private class LazyBindingBuilder implements BindingBuilder {
        private ServiceDeclaration sd;
        private BindingBuilder<?> builder;
        private QName qname;;

        

Parameters:
sd
        public LazyBindingBuilder(ServiceDeclaration sd) {
            super();
            this. = sd;
            this. = ServiceDeclarationParser.getQName(sd.getAttributes().get("qname"));
        }
        public void build(Component componentContract contractBinding bindingBuilderContext contextboolean rebuild) {
            getBuilder().build(componentcontractbindingcontextrebuild);
        }
        public QName getBindingType() {
            return ;
        }
        private synchronized BindingBuilder getBuilder() {
            if ( == null) {
                try {
                    Class<?> builderClass = .loadClass();
                    try {
                        Constructor<?> constructor = builderClass.getConstructor(ExtensionPointRegistry.class);
                         = (BindingBuilder)constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<?> constructor = builderClass.getConstructor();
                         = (BindingBuilder)constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }
    private class LazyImplementationBuilder implements ImplementationBuilder {
        private ServiceDeclaration sd;
        private ImplementationBuilder<?> builder;
        private QName qname;;

        

Parameters:
sd
        public LazyImplementationBuilder(ServiceDeclaration sd) {
            super();
            this. = sd;
            this. = ServiceDeclarationParser.getQName(sd.getAttributes().get("qname"));
        }
        public void build(Component componentImplementation implementationBuilderContext context) {
            getBuilder().build(componentimplementationcontext);
        }
        public QName getImplementationType() {
            return ;
        }
        private synchronized ImplementationBuilder getBuilder() {
            if ( == null) {
                try {
                    Class<?> builderClass = .loadClass();
                    try {
                        Constructor<?> constructor = builderClass.getConstructor(ExtensionPointRegistry.class);
                         = (ImplementationBuilder)constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<?> constructor = builderClass.getConstructor();
                         = (ImplementationBuilder)constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }
    private class LazyPolicyBuilder implements PolicyBuilder {
        private ServiceDeclaration sd;
        private PolicyBuilder<?> builder;
        private QName qname;;

        

Parameters:
sd
        public LazyPolicyBuilder(ServiceDeclaration sd) {
            super();
            this. = sd;
            this. = ServiceDeclarationParser.getQName(sd.getAttributes().get("qname"));
        }
        public boolean build(Component componentImplementation implementationBuilderContext context) {
            return getBuilder().build(componentimplementationcontext);
        }
        public QName getPolicyType() {
            return ;
        }
        
        public List<QNamegetSupportedBindings() {
            return getBuilder().getSupportedBindings();
        }
        
        private synchronized PolicyBuilder getBuilder() {
            if ( == null) {
                try {
                    Class<?> builderClass = .loadClass();
                    try {
                        Constructor<?> constructor = builderClass.getConstructor(ExtensionPointRegistry.class);
                         = (PolicyBuilder)constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<?> constructor = builderClass.getConstructor();
                         = (PolicyBuilder)constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        public boolean build(Endpoint endpointBuilderContext context) {
            return getBuilder().build(endpointcontext);
        }
        public boolean build(org.apache.tuscany.sca.assembly.EndpointReference endpointReferenceBuilderContext context) {
            return getBuilder().build(endpointReferencecontext);
        }
        public boolean build(EndpointReference endpointReferenceEndpoint endpointBuilderContext context) {
            return getBuilder().build(endpointReferenceendpointcontext);
        }      
    }
    
    private class LazyContractBuilder implements ContractBuilder {
        private ServiceDeclaration sd;
        private ContractBuilder builder;

        

Parameters:
sd
        public LazyContractBuilder(ServiceDeclaration sd) {
            super();
            this. = sd;
        }
        
/*        
        public boolean build(EndpointReference endpointReference, BuilderContext context){
            return getBuilder().build(endpointReference, context);
        }
        
        public boolean build(Endpoint endpoint, BuilderContext context){
            return getBuilder().build(endpoint, context);
        }
*/        
        
        public boolean build(InterfaceContract interfaceContractBuilderContext context){
            return getBuilder().build(interfaceContractcontext);
        }        
        
        private synchronized ContractBuilder getBuilder() {
            if ( == null) {
                try {
                    Class<?> builderClass = .loadClass();
                    try {
                        Constructor<?> constructor = builderClass.getConstructor(ExtensionPointRegistry.class);
                         = (ContractBuilder)constructor.newInstance();
                    } catch (NoSuchMethodException e) {
                        Constructor<?> constructor = builderClass.getConstructor();
                         = (ContractBuilder)constructor.newInstance();
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        } 
    }    
New to GrepCode? Check out our FAQ X