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.core.context.impl;
 
 import java.util.List;
 
Implementation of ComponentContext that delegates to a ComponentContextProvider.

Version:
$Rev: 1058955 $ $Date: 2011-01-14 13:34:37 +0200 (Fri, 14 Jan 2011) $
 
 public class ComponentContextImpl implements RuntimeComponentContext {
     private final RuntimeComponent component;
 
     private final CompositeContext compositeContext;
     private final CompositeActivator compositeActivator;
     private final ProxyFactory proxyFactory;
     private final AssemblyFactory assemblyFactory;
     private final JavaInterfaceFactory javaInterfaceFactory;
     private final PropertyValueFactory propertyFactory;
     private final EndpointReferenceBinder eprBinder;
     private final ExtensionPointRegistry registry;
 
     public ComponentContextImpl(ExtensionPointRegistry registry,
                                 CompositeContext compositeContext,
                                 RuntimeComponent component) {
         this. = component;
         FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class);
         this. = factories.getFactory(AssemblyFactory.class);
         this. = factories.getFactory(JavaInterfaceFactory.class);
 
         UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class);
         this. = compositeContext;
 
         this. = utilities.getUtility(CompositeActivator.class);
 
         this. =
             registry.getExtensionPoint(ContextFactoryExtensionPoint.class).getFactory(RequestContextFactory.class);
         this. = ExtensibleProxyFactory.getInstance(registry);
         this. = factories.getFactory(PropertyValueFactory.class);
 
        this. = utilities.getUtility(EndpointReferenceBinder.class);
        this. = registry;
    }
    public String getURI() {
        return .getURI();
    }
    public <B> ServiceReference<B> cast(B targetthrows IllegalArgumentException {
        return .cast(target);
    }
    public <B> B getService(Class<B> businessInterfaceString referenceNamethrows IllegalArgumentException {
    	B service = null;
    	
     	ServiceReference<B> serviceRef = getServiceReference(businessInterfacereferenceName);
     	if(serviceRef != null) {
            service = serviceRef.getService();
     	}
        return service;
    }
    public <B> ServiceReference<B> getServiceReference(Class<B> businessInterfaceString referenceNamethrows IllegalArgumentException {
        for (ComponentReference ref : .getReferences()) {
            if (referenceName.equals(ref.getName())) {
                Multiplicity multiplicity = ref.getMultiplicity();
                if (multiplicity == . || multiplicity == .) {
                    throw new IllegalArgumentException("Reference " + referenceName
                        + " has multiplicity "
                        + multiplicity);
                }
                if (ref.getEndpointReferences().size() < 1) {
                    return null;
                }
                ServiceReference<B> sr = getServiceReference(businessInterface, (RuntimeEndpointReference)getEndpointReference(ref));
                if (sr == null) {
                    throw new IllegalArgumentException("Reference " + referenceName + " is null");
                }
                return sr;
            }
        }
        throw new IllegalArgumentException("[JCA80011] Reference not found: " + referenceName);
    }

    
Select an endpoint reference from the component reference

Parameters:
ref
Returns:
        List<EndpointReferenceeprs = ref.getEndpointReferences();
        if (eprs.size() == 1) {
            // Return 1st one
            return eprs.get(0);
        } else {
            for (EndpointReference epr : eprs) {
                // Try to see if there is an EPR using binding.sca
                if (epr.getBinding().getType().equals(.)) {
                    return epr;
                }
            }
            return eprs.get(0);
        }
    }

    
Select an endpoint reference from the component reference

Parameters:
ref
Returns:
    private Endpoint getEndpoint(ComponentService serviceString bindingName) {
        if (bindingName == null) {
            // The default binding name is the name of the promoted service
            bindingName = getPromotedService(service).getName();
        }
        Endpoint returnEp = null;
        List<Endpointeps = service.getEndpoints();
        for (Endpoint ep : eps) {
            Binding binding = ep.getBinding();
            if (bindingName.equals(binding.getName()) || binding.getName() == null) {
                returnEp = ep;
                break;
            }
        }
        //TUSCANY-3543
        if(returnEp == null) {
            returnEp = eps.get(0);
        }
        
        return returnEp;
    }
    private ComponentService getPromotedService(ComponentService componentService) {
        Service service = componentService.getService();
        if (service instanceof CompositeService) {
            return getPromotedService(((CompositeService)service).getPromotedService());
        } else {
            return componentService;
        }
    }

    
Gets the value for the specified property with the specified type.

Parameters:
type The type of the property value we are getting
propertyName The name of the property we are getting
B The class of the property value we are getting
Throws:
org.oasisopen.sca.ServiceRuntimeException If a Property for the specified propertyName is not found
See also:
setPropertyValueFactory(org.apache.tuscany.sca.context.PropertyValueFactory)
    public <B> B getProperty(Class<B> typeString propertyName) {
        for (ComponentProperty p : .getProperties()) {
            if (propertyName.equals(p.getName())) {
            	return .createPropertyValue(ptype);           
            }
        }
        throw new IllegalArgumentException("Property not found: " + propertyName);
    }

    

Parameters:
component
    public static ComponentService getSingleService(Component component) {
        ComponentService targetService;
        List<ComponentServiceservices = component.getServices();
        List<ComponentServiceregularServices = new ArrayList<ComponentService>();
        for (ComponentService service : services) {
            if (service.isForCallback()) {
                continue;
            }
            String name = service.getName();
            if (!name.startsWith("$") || name.startsWith("$dynamic$")) {
                regularServices.add(service);
            }
        }
        if (regularServices.size() == 0) {
            throw new ServiceRuntimeException("No service is declared on component " + component.getURI());
        }
        if (regularServices.size() != 1) {
            throw new ServiceRuntimeException("More than one service is declared on component " + component.getURI()
                + ". Service name is required to get the service.");
        }
        targetService = regularServices.get(0);
        return targetService;
    }
    public <B> ServiceReference<B> createSelfReference(Class<B> businessInterface) {
        ComponentService service = getSingleService();
        try {
            return createSelfReference(businessInterfaceservice);
        } catch (IllegalArgumentException iae) {
        	throw iae;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e.getMessage(), e);
        }
    }
    public <B> ServiceReference<B> createSelfReference(Class<B> businessInterfaceString serviceName) {
        if (serviceName == null) {
            return createSelfReference(businessInterface);
        }
        try {
            String bindingName = null;
            int index = serviceName.indexOf('/');
            if (index != -1) {
                bindingName = serviceName.substring(index + 1);
                serviceName = serviceName.substring(0, index);
            }
            for (ComponentService service : .getServices()) {
                if (serviceName.equals(service.getName())) {
                    Endpoint endpoint = getEndpoint(servicebindingName);
                    if (endpoint == null) {
                        break;
                    }
                    return getServiceReference(businessInterface, (RuntimeEndpoint)endpoint);
                }
            }
            throw new IllegalArgumentException("Service not found: " + serviceName);
        } catch (IllegalArgumentException iae) {
        	throw iae;
        } catch (ServiceRuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e.getMessage(), e);
        }
    }

    

Parameters:
<B>
businessInterface
service
Returns:
    public <B> ServiceReference<B> createSelfReference(Class<B> businessInterfaceComponentService service) {
        try {
            RuntimeEndpointReference ref =
                (RuntimeEndpointReference)createEndpointReference(servicenullbusinessInterface);
            ref.setComponent();
            return getServiceReference(businessInterfaceref);
        } catch (IllegalArgumentException iae) {
        	throw iae;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }
    public RequestContext getRequestContext() {
        if ( != null) {
            return .createRequestContext();
        } else {
            return new RequestContextImpl();
        }
    }

    
    public <B> ServiceReference<B> getServiceReference(Class<B> businessInterface,
                                                       RuntimeEndpointReference endpointReference) {
    	ServiceReference<B> result = null;
    	
        try {
            InterfaceContract interfaceContract = endpointReference.getComponentTypeReferenceInterfaceContract();
            if (businessInterface == null) {
                businessInterface = (Class<B>)((JavaInterface)interfaceContract.getInterface()).getJavaClass();
            }
            RuntimeComponentReference ref = (RuntimeComponentReference)endpointReference.getReference();
            InterfaceContract refInterfaceContract = getInterfaceContract(interfaceContractbusinessInterface);
            if (refInterfaceContract != null) {
	            if (refInterfaceContract != interfaceContract) {
	                ref = (RuntimeComponentReference)ref.clone();
	                if (interfaceContract != null) {
	                    ref.setInterfaceContract(interfaceContract);
	                } else {
	                    ref.setInterfaceContract(refInterfaceContract);
	                }
	            }
 
	            ref.setComponent();
	            result = new ServiceReferenceImpl<B>(businessInterfaceendpointReference.getComponentContext().getCompositeContext());
            }
        } catch (IllegalArgumentException iae ) {
        	throw iae;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
        
        return result;
    }
    public <B> ServiceReference<B> getServiceReference(Class<B> businessInterfaceRuntimeEndpoint endpoint) {
        try {
            if (businessInterface == null) {
                InterfaceContract contract = endpoint.getComponentTypeServiceInterfaceContract();
                businessInterface = (Class<B>)((JavaInterface)contract.getInterface()).getJavaClass();
            }
            RuntimeEndpointReference ref =
                (RuntimeEndpointReference)createEndpointReference(endpointbusinessInterface);
            ref.setComponent();
            return new ServiceReferenceImpl<B>(businessInterfaceref);
        } catch (IllegalArgumentException iae) {
        	throw iae;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }

    
Create a self-reference for a component service

                                                      ComponentService service,
                                                      String bindingName,
                                                      Class<?> businessInterfacethrows CloneNotSupportedException,
        InvalidInterfaceException {
        Endpoint endpoint = getEndpoint(servicebindingName);
        return createEndpointReference(endpointbusinessInterface);
    }
    private EndpointReference createEndpointReference(Endpoint endpointClass<?> businessInterface)
        Component component = endpoint.getComponent();
        ComponentService service = endpoint.getService();
        ComponentReference componentReference = .createComponentReference();
        componentReference.setName("$self$." + service.getName());
        componentReference.setCallback(service.getCallback());
        componentReference.getTargets().add(service);
        componentReference.getPolicySets().addAll(service.getPolicySets());
        componentReference.getRequiredIntents().addAll(service.getRequiredIntents());
        componentReference.getBindings().add(endpoint.getBinding());
        InterfaceContract interfaceContract = service.getInterfaceContract();
        Service componentTypeService = service.getService();
        if (componentTypeService != null && componentTypeService.getInterfaceContract() != null) {
            interfaceContract = componentTypeService.getInterfaceContract();
        }
        interfaceContract = getInterfaceContract(interfaceContractbusinessInterface);
        componentReference.setInterfaceContract(interfaceContract);
        componentReference.setMultiplicity(.);
        // component.getReferences().add(componentReference);
        // create endpoint reference
        EndpointReference endpointReference = .createEndpointReference();
        endpointReference.setComponent(component);
        endpointReference.setReference(componentReference);
        endpointReference.setBinding(endpoint.getBinding());
        endpointReference.setUnresolved(false);
        endpointReference.setTargetEndpoint(endpoint);
        componentReference.getEndpointReferences().add(endpointReference);
        ((RuntimeComponentReference)componentReference).setComponent((RuntimeComponent)component);
        ((RuntimeEndpointReference)endpointReference).bind();
        return endpointReference;
    }

    
    private InterfaceContract getInterfaceContract(InterfaceContract interfaceContractClass<?> businessInterface)
        if (businessInterface == null) {
            return interfaceContract;
        }
        if (interfaceContract == null) {
            ic.setInterface(.createJavaInterface(businessInterface));
            return ic;
        }
        boolean compatible = false;
        if (interfaceContract != null && interfaceContract.getInterface() != null) {
            Interface interfaze = interfaceContract.getInterface();
            if (interfaze instanceof JavaInterface) {
                Class<?> cls = ((JavaInterface)interfaze).getJavaClass();
                if (businessInterface.isAssignableFrom(cls)) {
                    compatible = true;
                }
                if(!compatible) {
                    InterfaceContract biContract = .createJavaInterfaceContract();
                    JavaInterface callInterface = .createJavaInterface(businessInterface);
                    biContract.setInterface(callInterface);
                    if (callInterface.getCallbackClass() != null) {
                        biContract.setCallbackInterface(.createJavaInterface(callInterface
                            .getCallbackClass()));
                    }
                	InterfaceContractMapper ifcm = .getExtensionPoint(InterfaceContractMapper.class);
                	compatible = ifcm.isCompatibleSubset(biContract , interfaceContract);
                }
            }
        }
        
        if(!compatible) {
        	// JCA-9011
        	throw new IllegalArgumentException("Business interface " + businessInterface.getName() + " is not compatible with " + interfaceContract.getInterface());
        }
        return interfaceContract;
    }
    /* ******************** Contribution for issue TUSCANY-2281 ******************** */

    

See also:
ComponentContext.getServices(Class, String)
    public <B> Collection<B> getServices(Class<B> businessInterfaceString referenceName) {
        ArrayList<B> services = new ArrayList<B>();
        Collection<ServiceReference<B>> serviceRefs = getServiceReferences(businessInterfacereferenceName);
        for (ServiceReference<B> serviceRef : serviceRefs) {
            services.add(serviceRef.getService());
        }
        return services;
    }

    

See also:
ComponentContext.getServiceReferences(Class, String)
    public <B> Collection<ServiceReference<B>> getServiceReferences(Class<B> businessInterfaceString referenceName) {
        try {
            for (ComponentReference ref : .getReferences()) {
                if (referenceName.equals(ref.getName())) {
                	if ( ref.getMultiplicity() == . ) 
                		throw new IllegalArgumentException("Reference " + referenceName + " is not a valid argument for getServiceReferences because it has a multiplicity of 1..1");
                	if (ref.getMultiplicity() == .)
                		throw new IllegalArgumentException("Reference " + referenceName + " is not a valid argument for getServiceReferences because it has a multiplicity of 0..1");
                		
                    ArrayList<ServiceReference<B>> serviceRefs = new ArrayList<ServiceReference<B>>();
                    for (EndpointReference endpointReference : ref.getEndpointReferences()) {
                        RuntimeEndpointReference epr = (RuntimeEndpointReference)endpointReference;
                        serviceRefs.add(getServiceReference(businessInterfaceepr));
                    }
                    return serviceRefs;
                }
            }
            throw new IllegalArgumentException("Reference not found: " + referenceName);
        } catch (IllegalArgumentException iae) {
        	throw iae;
        } catch (ServiceRuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceRuntimeException(e.getMessage(), e);
        }
    }
    /* ******************** Contribution for issue TUSCANY-2281 ******************** */
        return ;
    }
    }
New to GrepCode? Check out our FAQ X