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.implementation.java.invocation;
 
 import java.util.List;
 import java.util.Map;
 
The runtime instantiation of Java component implementations

Version:
$Rev: 986678 $ $Date: 2010-08-18 16:53:47 +0300 (Wed, 18 Aug 2010) $
 
 public class JavaComponentContextProvider {
     private RuntimeComponent component;
     private ProxyFactory proxyFactory;
     private InstanceFactory instanceFactory;
 	private JavaScopeImpl scope;
 
     public JavaComponentContextProvider(RuntimeComponent component,
                                         JavaInstanceFactoryProvider configuration,
                                         DataBindingExtensionPoint dataBindingExtensionPoint,
                                         PropertyValueFactory propertyValueObjectFactory,
                                         ComponentContextFactory componentContextFactory,
                                         RequestContextFactory requestContextFactory) {
         super();
         this. = configuration;
         this. = configuration.getProxyFactory();
         //        if (componentContextFactory != null) {
         //            this.componentContext = componentContextFactory.createComponentContext(component, requestContextFactory);
         //        } else {
         //            this.componentContext = new ComponentContextImpl(this, requestContextFactory, this.proxyService);
         //        }
         this. = component;
         this. = (JavaPropertyValueObjectFactorypropertyValueObjectFactory;
        this. = ((JavaImplementation)component.getImplementation()).getJavaScope();
    }
        if ( == null) {
            start();
        }
        return .newInstance();
    }
    void configureProperties(List<ComponentPropertydefinedProperties) {
        for (ComponentProperty p : definedProperties) {
            configureProperty(p);
        }
    }
    private void configureProperty(ComponentProperty configuredProperty) {
        JavaElementImpl element =
            .getImplementation().getPropertyMembers().get(configuredProperty.getName());
        if (element != null && configuredProperty.getValue() != null) {
            if (!(element.getAnchor() instanceof Constructor)) {
                if(element.getElementType() == .) {
                    // Field field = (Field)element.getAnchor();
                    .getInjectionSites().add(element);
                    /*
                    if(Modifier.isPublic(field.getModifiers())) {
                        instanceFactoryProvider.getInjectionSites().add(element);
                    } else if(field.getAnnotation(org.oasisopen.sca.annotation.Property.class) != null) {
                        instanceFactoryProvider.getInjectionSites().add(element);
                    }
                    */
                } else {
                    .getInjectionSites().add(element);
                }
            }
            //Class propertyJavaType = JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType());
            ObjectFactory<?> propertyObjectFactory =
                createPropertyValueFactory(configuredPropertyconfiguredProperty.getValue(), element);
            .setObjectFactory(elementpropertyObjectFactory);
            JavaConstructorImpl<?> constructor = .getImplementation().getConstructor();
            for(JavaElementImpl pconstructor.getParameters()){
                if(element.getName().equals(p.getName())) {
                    .setObjectFactory(ppropertyObjectFactory);
                }
            }
        }
    }
    void start() {
    	List<JavaElementImplcallbackInjectionList = null;
    	
    	// If the component implementation is stateless, we need to inject the callbacks on service invocation
    	// rather than doing it once at the component level. 
    	if ( .equals(.)) {
    		callbackInjectionList = .getCallbackInjectionSites();
    	} else {
    		callbackInjectionList = .getInjectionSites();
    	}
    		
            Map<StringList<EndpointReference>> callbackWires = new HashMap<StringList<EndpointReference>>();
            for (ComponentService service : .getServices()) {
                RuntimeComponentReference callbackReference = (RuntimeComponentReference)service.getCallbackReference();
                if (callbackReference != null) {
                    List<EndpointReferencewires = callbackReference.getEndpointReferences();
                    if (!wires.isEmpty()) {
                        RuntimeEndpointReference epr = (RuntimeEndpointReferencewires.get(0);
                        callbackWires.put(epr.getComponentTypeReferenceInterfaceContract().getInterface().toString(),
                                          wires);
                    }
                }
            }
                .getCallbackMembers().entrySet()) {
                List<EndpointReferencewires = callbackWires.get(entry.getKey());
                if (wires == null) {
                    // this can happen when there are no client wires to a
                    // component that has a callback
                    continue;
                }
                for(JavaElementImpl element : entry.getValue()) {
                    Class<?> businessInterface = element.getType();
                    ObjectFactory<?> factory = null;
                    if (ServiceReference.class.isAssignableFrom(element.getType())) {
                        businessInterface =
                            JavaIntrospectionHelper.getBusinessInterface(element.getType(), element.getGenericType());
                        factory =
                            new CallbackReferenceObjectFactory(businessInterfacewires);
                    } else {
                        factory = new CallbackWireObjectFactory(businessInterfacewires);
                    }
                    if (!(element.getAnchor() instanceof Constructor)) {
                        callbackInjectionList.add(element);
                    }
                    .setObjectFactory(elementfactory);
                }
            }
        }
            JavaElementImpl element =
                .getImplementation().getReferenceMembers().get(ref.getName());
            if (element != null) {
                if (!(element.getAnchor() instanceof Constructor)) {
                    if(element.getElementType() == .) {
                        Field field = (Field)element.getAnchor();
                        if(Modifier.isPublic(field.getModifiers())) {
                            .getInjectionSites().add(element);
                        } else if(field.getAnnotation(org.oasisopen.sca.annotation.Reference.class) != null) {
                            .getInjectionSites().add(element);
                        }
                    } else {
                        .getInjectionSites().add(element);
                    }
                }
                ComponentReference componentReference = null;
                List<EndpointReferencewireList = null;
                for (ComponentReference reference : .getReferences()) {
                    if (reference.getName().equals(ref.getName())) {
                        wireList = ((RuntimeComponentReference)reference).getEndpointReferences();
                        componentReference = reference;
                        break;
                    }
                }
                if (ref.getMultiplicity() == . || ref.getMultiplicity() == .) {
                    List<ObjectFactory<?>> factories = new ArrayList<ObjectFactory<?>>();
                    Class<?> baseType =
                        JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType());
                    for (int i = 0; i < wireList.size(); i++) {
                        ObjectFactory<?> factory = null;
                        if (ServiceReference.class.isAssignableFrom(baseType)) {
                            Type callableRefType = JavaIntrospectionHelper.getParameterType(element.getGenericType());
                            // Type businessType = JavaIntrospectionHelper.getParameterType(callableRefType);
                            Class<?> businessInterface =
                                JavaIntrospectionHelper.getBusinessInterface(baseTypecallableRefType);
                            factory = new CallableReferenceObjectFactory(businessInterface, (RuntimeEndpointReferencewireList.get(i));
                        } else {
                            factory = createObjectFactory(baseTypewireList.get(i));
                        }
                        factories.add(factory);
                    }
                    .setObjectFactories(elementfactories);
                    JavaConstructorImpl<?> constructor = .getImplementation().getConstructor();
                    for(JavaElementImpl pconstructor.getParameters()){
                        if(element.getName().equals(p.getName())) {
                            .setObjectFactories(pfactories);
                        }
                    }
                } else {
                    if (wireList == null && ref.getMultiplicity() == .) {
                        throw new IllegalStateException("Required reference is missing: " + ref.getName());
                    }
                    if (wireList != null && !wireList.isEmpty()) {
                        ObjectFactory<?> factory = null;
                        if (ServiceReference.class.isAssignableFrom(element.getType())) {
                            Class<?> businessInterface =
                                JavaIntrospectionHelper.getBusinessInterface(element.getType(), element
                                    .getGenericType());
                            factory =
                                new CallableReferenceObjectFactory(businessInterface, (RuntimeEndpointReferencewireList.get(0));
                        } else {
                            factory = createObjectFactory(element.getType(), wireList.get(0));
                        }
                        .setObjectFactory(elementfactory);
                        JavaConstructorImpl<?> constructor = .getImplementation().getConstructor();
                        for(JavaElementImpl pconstructor.getParameters()){
                            if(element.getName().equals(p.getName())) {
                                .setObjectFactory(pfactory);
                            }
                        }
                    }
                }
            }
        }
        //setUpPolicyHandlers();
    }
    void addResourceFactory(String nameObjectFactory<?> factory) {
        if (resource != null && !(resource.getElement().getAnchor() instanceof Constructor)) {
            .getInjectionSites().add(resource.getElement());
        }
        .setObjectFactory(resource.getElement(), factory);
    }
        return createInstanceWrapper().getInstance();
    }
        return ;
    }
    void stop() {
        //cleanUpPolicyHandlers();
    }
    Invoker createInvoker(Operation operationInterfaceContract interfaceContractthrows NoSuchMethodException {
        Class<?> implClass = .getImplementationClass();
        Method method = JavaInterfaceUtil.findMethod(implClassoperation);
        if (operation instanceof JavaOperation &&
            ((JavaOperationoperation).isAsyncServer() ) {
        	return new JavaAsyncImplementationInvoker(operationmethodinterfaceContract);
        } else {
        	return new JavaImplementationInvoker(operationmethodinterfaceContract);
        } // end if
    } // end 
    private static class OptimizedObjectFactory<T> implements ObjectFactory<T> {
        private ScopeContainer scopeContainer;
        public OptimizedObjectFactory(ScopeContainer scopeContainer) {
            super();
            this. = scopeContainer;
        }
        public T getInstance() throws ObjectCreationException {
            try {
                return (T).getWrapper(null).getInstance();
            } catch (TargetResolutionException e) {
                throw new ObjectCreationException(e);
            }
        }
    }
    private <B> ObjectFactory<B> createObjectFactory(Class<B> interfazeEndpointReference wire) {
        // FIXME: [rfeng] Disable the optimization for new as it needs more discussions
        /*
        boolean conversational = wire.getSource().getInterfaceContract().getInterface().isConversational();
        Binding binding = wire.getSource().getBinding();
        // Check if it's wireable binding for optimization
        if (!conversational && binding instanceof OptimizableBinding) {
            OptimizableBinding optimizableBinding = (OptimizableBinding)binding;
            Component component = optimizableBinding.getTargetComponent();
            if (component != null) {
                Implementation implementation = component.getImplementation();
                // Check if the target component is java component
                if (implementation instanceof JavaImplementation) {
                    JavaImplementation javaImplementation = (JavaImplementation)implementation;
                    if (interfaze.isAssignableFrom(javaImplementation.getJavaClass())) {
                        ScopedRuntimeComponent scopedComponent = (ScopedRuntimeComponent)component;
                        ScopeContainer scopeContainer = scopedComponent.getScopeContainer();
                        Scope scope = scopeContainer.getScope();
                        if (scope == Scope.COMPOSITE || scope == Scope.STATELESS || scope == Scope.SYSTEM) {
                            boolean optimizable = true;
                            for (InvocationChain chain : wire.getInvocationChains()) {
                                if (chain.getHeadInvoker() != chain.getTailInvoker()) {
                                    optimizable = false;
                                    break;
                                }
                            }
                            if (optimizable) {
                                return new OptimizedObjectFactory<B>(scopeContainer);
                            }
                        }
                    }
                }
            }
        }
        */
        return new WireObjectFactory<B>(interfaze, (RuntimeEndpointReferencewire);
    }
                                                        Object propertyValue,
                                                        JavaElementImpl javaElement) {
        return .createValueFactory(propertypropertyValuejavaElement);
    }

    

Returns:
the component
        return ;
    }
    /*private void setUpPolicyHandlers() {
        for (PolicyHandler policyHandler : policyHandlers.values()) {
            policyHandler.setUp(component.getImplementation());
        }
    }
    private void cleanUpPolicyHandlers() {
        for (PolicyHandler policyHandler : policyHandlers.values() ) {
            policyHandler.cleanUp(this);
        }
    }*/
New to GrepCode? Check out our FAQ X