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.provider;
 
 import java.util.List;
 import java.util.Map;
 
Default implementation of a provider factory extension point.

Version:
$Rev: 986876 $ $Date: 2010-08-18 22:27:13 +0300 (Wed, 18 Aug 2010) $
 
 
     private ExtensionPointRegistry registry;
     private final Map<Class<?>, ProviderFactoryproviderFactories = new HashMap<Class<?>, ProviderFactory>();
     private boolean loaded;

    
The default constructor. Does nothing.
 
         this. = registry;
     }

    
Add a provider factory.

Parameters:
providerFactory The provider factory
 
     public void addProviderFactory(ProviderFactory providerFactory) {
         if(providerFactory instanceof PolicyProviderFactory) {
             .add((PolicyProviderFactory)providerFactory);
         } 
         // Only add the 1st instance so that the ones with higher ranking with prevail
         if (!.containsKey(providerFactory.getModelType())) {
             .put(providerFactory.getModelType(), providerFactory);
         }
     }

    
Remove a provider factory.

Parameters:
providerFactory The provider factory
 
     public void removeProviderFactory(ProviderFactory providerFactory) {
         if(providerFactory instanceof PolicyProviderFactory) {
             .remove((PolicyProviderFactory)providerFactory);
         }
         .remove(providerFactory.getModelType());
     }

    
Returns the provider factory associated with the given model type.

Parameters:
modelType A model type
Returns:
The provider factory associated with the given model type
 
     public ProviderFactory getProviderFactory(Class<?> modelType) {
         loadProviderFactories();
 
         Class<?>[] classes = modelType.getInterfaces();
         for (Class<?> c : classes) {
             ProviderFactory factory = .get(c);
             if (factory != null) {
                 return factory;
             }
        }
        return .get(modelType);
    }
        loadProviderFactories();
        return ;
    }
    
    public <P extends ProviderFactoryCollection<P> getProviderFactories(Class<P> factoryType) {
        loadProviderFactories();
        List<P> factories = new ArrayList<P>();
        for (ProviderFactory pf : .values()) {
            if (factoryType.isInstance(pf)) {
                factories.add(factoryType.cast(pf));
            }
        }
        return factories;
    }

    
Load provider factories declared under META-INF/services.

Parameters:
registry
    private synchronized void loadProviderFactories() {
        if ()
            return;
         = true;
    }

    
Load provider factories declared under META-INF/services.

Parameters:
registry
factoryClass
Returns:
    private List<ProviderFactoryloadProviderFactories(Class<?> factoryClass) {
        // Get the provider factory service declarations
        Collection<ServiceDeclarationfactoryDeclarations;
        ServiceDiscovery serviceDiscovery = .getServiceDiscovery();
        try {
            factoryDeclarations = serviceDiscovery.getServiceDeclarations(factoryClass.getName(), true);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
        List<ProviderFactoryfactories = new ArrayList<ProviderFactory>();
        for (ServiceDeclaration factoryDeclaration : factoryDeclarations) {
            Map<StringStringattributes = factoryDeclaration.getAttributes();
            // Load an implementation provider factory
            if (factoryClass == ImplementationProviderFactory.class) {
                String modelTypeName = attributes.get("model");
                // Create a provider factory wrapper and register it
                ImplementationProviderFactory factory =
                    new LazyImplementationProviderFactory(modelTypeNamefactoryDeclaration);
                addProviderFactory(factory);
                factories.add(factory);
            } else if (factoryClass == BindingProviderFactory.class) {
                // Load a binding provider factory
                String modelTypeName = attributes.get("model");
                // Create a provider factory wrapper and register it
                BindingProviderFactory factory =
                    new LazyBindingProviderFactory(modelTypeNamefactoryDeclaration);
                addProviderFactory(factory);
                factories.add(factory);
            } else if (factoryClass == PolicyProviderFactory.class) {
                // Load a policy provider factory
                String modelTypeName = attributes.get("model");
                // Create a provider factory wrapper and register it
                PolicyProviderFactory factory =
                    new LazyPolicyProviderFactory(modelTypeNamefactoryDeclaration);
                addProviderFactory(factory);
                factories.add(factory);
            } else if (factoryClass == WireFormatProviderFactory.class) {
                // Load a wire format provider factory
                String modelTypeName = attributes.get("model");
                // Create a provider factory wrapper and register it
                WireFormatProviderFactory factory =
                    new LazyWireFormatProviderFactory(modelTypeNamefactoryDeclaration);
                addProviderFactory(factory);
                factories.add(factory);
            } else if (factoryClass == OperationSelectorProviderFactory.class) {
                // Load a wire format provider factory
                String modelTypeName = attributes.get("model");
                // Create a provider factory wrapper and register it
                OperationSelectorProviderFactory factory =
                    new LazyOperationSelectorProviderFactory(modelTypeNamefactoryDeclaration);
                addProviderFactory(factory);
                factories.add(factory);
            }
        }
        return factories;
    }

    
A wrapper around an implementation provider factory allowing lazy loading and initialization of implementation providers.
    private static class LazyBindingProviderFactory implements BindingProviderFactory {
        private ExtensionPointRegistry registry;
        private String modelTypeName;
        private ServiceDeclaration factoryDeclaration;
        private BindingProviderFactory factory;
        private Class<?> modelType;
        private LazyBindingProviderFactory(ExtensionPointRegistry registry,
                                           String modelTypeName,
                                           ServiceDeclaration factoryDeclaration) {
            this. = registry;
            this. = modelTypeName;
            this. = factoryDeclaration;
        }
        @SuppressWarnings("unchecked")
        private BindingProviderFactory getFactory() {
            if ( == null) {
                try {
                    Class<BindingProviderFactoryfactoryClass =
                        (Class<BindingProviderFactory>).loadClass();
                    Constructor<BindingProviderFactoryconstructor =
                        factoryClass.getConstructor(ExtensionPointRegistry.class);
                     = constructor.newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        @SuppressWarnings("unchecked")
            return getFactory().createReferenceBindingProvider(endpointReference);
        }
        @SuppressWarnings("unchecked")
            return getFactory().createServiceBindingProvider(endpoint);
        }
        public Class<?> getModelType() {
            if ( == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }

    
A wrapper around an implementation provider factory allowing lazy loading and initialization of implementation providers.
        private ExtensionPointRegistry registry;
        private String modelTypeName;
        private ServiceDeclaration providerClass;
        private ImplementationProviderFactory factory;
        private Class<?> modelType;
                                                  String modelTypeName,
                                                  ServiceDeclaration providerClass) {
            this. = registry;
            this. = modelTypeName;
            this. = providerClass;
        }
        @SuppressWarnings("unchecked")
        private ImplementationProviderFactory getFactory() {
            if ( == null) {
                try {
                    Class<ImplementationProviderFactoryfactoryClass =
                        (Class<ImplementationProviderFactory>).loadClass();
                    Constructor<ImplementationProviderFactoryconstructor =
                        factoryClass.getConstructor(ExtensionPointRegistry.class);
                     = constructor.newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
        @SuppressWarnings("unchecked")
                                                                   Implementation Implementation) {
            return getFactory().createImplementationProvider(componentImplementation);
        }
        public Class<?> getModelType() {
            if ( == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }

    
A wrapper around an policy provider factory allowing lazy loading and initialization of policy providers.
    private class LazyPolicyProviderFactory implements PolicyProviderFactory {
        private ExtensionPointRegistry registry;
        private String modelTypeName;
        private ServiceDeclaration providerClass;
        private PolicyProviderFactory factory;
        private Class<?> modelType;
        private LazyPolicyProviderFactory(ExtensionPointRegistry registry,
                                          String modelTypeName,
                                          ServiceDeclaration providerClass) {
            this. = registry;
            this. = modelTypeName;
            this. = providerClass;
        }
        @SuppressWarnings("unchecked")
        private PolicyProviderFactory getFactory() {
            if ( == null) {
                try {
                    Class<PolicyProviderFactoryfactoryClass = (Class<PolicyProviderFactory>).loadClass();
                    Constructor<PolicyProviderFactoryconstructor =
                        factoryClass.getConstructor(ExtensionPointRegistry.class);
                     = constructor.newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
            return getFactory().createImplementationPolicyProvider(component);
        }
        public PolicyProvider createReferencePolicyProvider(EndpointReference endpointReference) {
            return getFactory().createReferencePolicyProvider(endpointReference);
        }
        public PolicyProvider createServicePolicyProvider(Endpoint endpoint) {
            return getFactory().createServicePolicyProvider(endpoint);
        }
        public Class<?> getModelType() {
            if ( == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }

    
A wrapper around a wire format provider factory allowing lazy loading and initialization of wire format providers.
    private class LazyWireFormatProviderFactory implements WireFormatProviderFactory {
        private ExtensionPointRegistry registry;
        private String modelTypeName;
        private ServiceDeclaration providerClass;
        private WireFormatProviderFactory factory;
        private Class<?> modelType;
        private LazyWireFormatProviderFactory(ExtensionPointRegistry registry,
                                              String modelTypeName,
                                              ServiceDeclaration providerClass) {
            this. = registry;
            this. = modelTypeName;
            this. = providerClass;
        }
        @SuppressWarnings("unchecked")
        private WireFormatProviderFactory getFactory() {
            if ( == null) {
                try {
                    Class<WireFormatProviderFactoryfactoryClass =
                        (Class<WireFormatProviderFactory>).loadClass();
                    Constructor<WireFormatProviderFactoryconstructor =
                        factoryClass.getConstructor(ExtensionPointRegistry.class);
                     = constructor.newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
            return getFactory().createReferenceWireFormatProvider(endpointReference);
        }
            return getFactory().createServiceWireFormatProvider(endpoint);
        }
        public Class<?> getModelType() {
            if ( == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }

    
A wrapper around a operation selector provider factory allowing lazy loading and initialization of operation selector providers.
        private ExtensionPointRegistry registry;
        private String modelTypeName;
        private ServiceDeclaration providerClass;
        private OperationSelectorProviderFactory factory;
        private Class<?> modelType;
                                                     String modelTypeName,
                                                     ServiceDeclaration providerClass) {
            this. = registry;
            this. = modelTypeName;
            this. = providerClass;
        }
        @SuppressWarnings("unchecked")
        private OperationSelectorProviderFactory getFactory() {
            if ( == null) {
                try {
                    Class<OperationSelectorProviderFactoryfactoryClass =
                        (Class<OperationSelectorProviderFactory>).loadClass();
                    Constructor<OperationSelectorProviderFactoryconstructor =
                        factoryClass.getConstructor(ExtensionPointRegistry.class);
                     = constructor.newInstance();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
            return getFactory().createReferenceOperationSelectorProvider(endpointReference);
        }
            return getFactory().createServiceOperationSelectorProvider(endpoint);
        }
        public Class<?> getModelType() {
            if ( == null) {
                try {
                     = .loadClass();
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
            return ;
        }
    }
    
New to GrepCode? Check out our FAQ X