Search in sources :

Example 11 with ManagedReference

use of org.jboss.as.naming.ManagedReference in project wildfly by wildfly.

the class SessionBeanHomeInterceptorFactory method create.

@Override
public Interceptor create(final InterceptorFactoryContext context) {
    return new Interceptor() {

        @Override
        public Object processInvocation(final InterceptorContext context) throws Exception {
            final ComponentView view = viewToCreate.getValue();
            try {
                INIT_METHOD.set(method);
                INIT_PARAMETERS.set(context.getParameters());
                final ManagedReference instance = view.createInstance();
                return instance.getInstance();
            } finally {
                INIT_METHOD.remove();
                INIT_PARAMETERS.remove();
            }
        }
    };
}
Also used : ComponentView(org.jboss.as.ee.component.ComponentView) InterceptorContext(org.jboss.invocation.InterceptorContext) ManagedReference(org.jboss.as.naming.ManagedReference) Interceptor(org.jboss.invocation.Interceptor)

Example 12 with ManagedReference

use of org.jboss.as.naming.ManagedReference in project wildfly by wildfly.

the class ManagedReferenceLifecycleMethodInterceptor method processInvocation.

/**
     * {@inheritDoc}
     */
public Object processInvocation(final InterceptorContext context) throws Exception {
    final ManagedReference reference = (ManagedReference) context.getPrivateData(ComponentInstance.class).getInstanceData(contextKey);
    final Object instance = reference.getInstance();
    try {
        final Method method = this.method;
        if (withContext) {
            final Method oldMethod = context.getMethod();
            try {
                if (this.lifecycleMethod) {
                    // because InvocationContext#getMethod() is expected to return null for lifecycle methods
                    context.setMethod(null);
                    return method.invoke(instance, context.getInvocationContext());
                } else if (this.changeMethod) {
                    context.setMethod(method);
                    return method.invoke(instance, context.getInvocationContext());
                } else {
                    return method.invoke(instance, context.getInvocationContext());
                }
            } finally {
                // reset any changed method on the interceptor context
                context.setMethod(oldMethod);
            }
        } else {
            method.invoke(instance);
            return context.proceed();
        }
    } catch (IllegalAccessException e) {
        final IllegalAccessError n = new IllegalAccessError(e.getMessage());
        n.setStackTrace(e.getStackTrace());
        throw n;
    } catch (InvocationTargetException e) {
        throw Interceptors.rethrow(e.getCause());
    }
}
Also used : ManagedReference(org.jboss.as.naming.ManagedReference) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with ManagedReference

use of org.jboss.as.naming.ManagedReference in project wildfly by wildfly.

the class ResourceReferenceProcessor method getResourceRefEntries.

private List<BindingConfiguration> getResourceRefEntries(final DeploymentUnit deploymentUnit, DeploymentDescriptorEnvironment environment, ClassLoader classLoader, DeploymentReflectionIndex deploymentReflectionIndex, ResourceInjectionTarget resourceInjectionTarget) throws DeploymentUnitProcessingException {
    List<BindingConfiguration> bindings = new ArrayList<BindingConfiguration>();
    final EEResourceReferenceProcessorRegistry registry = deploymentUnit.getAttachment(Attachments.RESOURCE_REFERENCE_PROCESSOR_REGISTRY);
    final ResourceReferencesMetaData resourceRefs = environment.getEnvironment().getResourceReferences();
    if (resourceRefs == null) {
        return bindings;
    }
    for (final ResourceReferenceMetaData resourceRef : resourceRefs) {
        final String name;
        if (resourceRef.getName().startsWith("java:")) {
            name = resourceRef.getName();
        } else {
            name = environment.getDefaultContext() + resourceRef.getName();
        }
        Class<?> classType = null;
        if (resourceRef.getType() != null) {
            try {
                classType = classLoader.loadClass(resourceRef.getType());
            } catch (ClassNotFoundException e) {
                throw EeLogger.ROOT_LOGGER.cannotLoad(e, resourceRef.getType());
            }
        }
        // our injection (source) comes from the local (ENC) lookup, no matter what.
        InjectionSource injectionSource = new LookupInjectionSource(name);
        classType = processInjectionTargets(resourceInjectionTarget, injectionSource, classLoader, deploymentReflectionIndex, resourceRef, classType);
        if (!isEmpty(resourceRef.getLookupName())) {
            injectionSource = new LookupInjectionSource(resourceRef.getLookupName(), classType != null && JAVAX_NAMING_CONTEXT.equals(classType.getName()));
        } else if (!isEmpty(resourceRef.getResUrl())) {
            final String url = resourceRef.getResUrl();
            if (classType != null && classType.equals(URI.class)) {
                try {
                    //we need a newURI every time
                    injectionSource = new FixedInjectionSource(new ManagedReferenceFactory() {

                        @Override
                        public ManagedReference getReference() {
                            try {
                                return new ImmediateManagedReference(new URI(url));
                            } catch (URISyntaxException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }, new URI(url));
                } catch (URISyntaxException e) {
                    throw EeLogger.ROOT_LOGGER.cannotParseResourceRefUri(e, resourceRef.getResUrl());
                }
            } else {
                try {
                    injectionSource = new FixedInjectionSource(new ManagedReferenceFactory() {

                        @Override
                        public ManagedReference getReference() {
                            try {
                                return new ImmediateManagedReference(new URL(url));
                            } catch (MalformedURLException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }, new URL(url));
                } catch (MalformedURLException e) {
                    throw EeLogger.ROOT_LOGGER.cannotParseResourceRefUri(e, resourceRef.getResUrl());
                }
            }
        } else {
            if (classType == null) {
                throw EeLogger.ROOT_LOGGER.cannotDetermineType(name);
            }
            //check if it is a well known type
            final String lookup = ResourceInjectionAnnotationParsingProcessor.FIXED_LOCATIONS.get(classType.getName());
            if (lookup != null) {
                injectionSource = new LookupInjectionSource(lookup);
            } else {
                final EEResourceReferenceProcessor resourceReferenceProcessor = registry.getResourceReferenceProcessor(classType.getName());
                if (resourceReferenceProcessor != null) {
                    injectionSource = resourceReferenceProcessor.getResourceReferenceBindingSource();
                } else if (!resourceRef.getResourceRefName().startsWith("java:")) {
                    injectionSource = new LookupInjectionSource("java:jboss/resources/" + resourceRef.getResourceRefName());
                } else {
                    //if we cannot resolve it just log
                    ROOT_LOGGER.cannotResolve("resource-env-ref", name);
                    continue;
                }
            }
        }
        bindings.add(new BindingConfiguration(name, injectionSource));
    }
    return bindings;
}
Also used : ResourceReferencesMetaData(org.jboss.metadata.javaee.spec.ResourceReferencesMetaData) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) ManagedReference(org.jboss.as.naming.ManagedReference) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource) InjectionSource(org.jboss.as.ee.component.InjectionSource) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) EnvEntryInjectionSource(org.jboss.as.ee.component.EnvEntryInjectionSource) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ResourceReferenceMetaData(org.jboss.metadata.javaee.spec.ResourceReferenceMetaData) FixedInjectionSource(org.jboss.as.ee.component.FixedInjectionSource) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) LookupInjectionSource(org.jboss.as.ee.component.LookupInjectionSource)

Example 14 with ManagedReference

use of org.jboss.as.naming.ManagedReference in project wildfly by wildfly.

the class NamingBindingAdd method installExternalContext.

void installExternalContext(final OperationContext context, final String name, final ModelNode model) throws OperationFailedException {
    final String moduleID = NamingBindingResourceDefinition.MODULE.resolveModelAttribute(context, model).asString();
    final String className = NamingBindingResourceDefinition.CLASS.resolveModelAttribute(context, model).asString();
    final ModelNode cacheNode = NamingBindingResourceDefinition.CACHE.resolveModelAttribute(context, model);
    boolean cache = cacheNode.isDefined() ? cacheNode.asBoolean() : false;
    final ObjectFactory objectFactoryClassInstance = new ExternalContextObjectFactory();
    final ServiceTarget serviceTarget = context.getServiceTarget();
    final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(name);
    final Hashtable<String, String> environment = getObjectFactoryEnvironment(context, model);
    environment.put(ExternalContextObjectFactory.CACHE_CONTEXT, Boolean.toString(cache));
    environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_CLASS, className);
    environment.put(ExternalContextObjectFactory.INITIAL_CONTEXT_MODULE, moduleID);
    final ExternalContextBinderService binderService = new ExternalContextBinderService(name, objectFactoryClassInstance);
    binderService.getManagedObjectInjector().inject(new ContextListAndJndiViewManagedReferenceFactory() {

        @Override
        public ManagedReference getReference() {
            try {
                final Object value = objectFactoryClassInstance.getObjectInstance(name, null, null, environment);
                return new ImmediateManagedReference(value);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String getInstanceClassName() {
            return className;
        }

        @Override
        public String getJndiViewInstanceValue() {
            final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
            try {
                WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(objectFactoryClassInstance.getClass().getClassLoader());
                return String.valueOf(getReference().getInstance());
            } finally {
                WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(cl);
            }
        }
    });
    serviceTarget.addService(bindInfo.getBinderServiceName(), binderService).addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, binderService.getNamingStoreInjector()).addDependency(ExternalContextsService.SERVICE_NAME, ExternalContexts.class, binderService.getExternalContextsInjector()).install();
}
Also used : ServiceTarget(org.jboss.msc.service.ServiceTarget) ManagedReference(org.jboss.as.naming.ManagedReference) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ContextListAndJndiViewManagedReferenceFactory(org.jboss.as.naming.ContextListAndJndiViewManagedReferenceFactory) ModuleLoadException(org.jboss.modules.ModuleLoadException) MalformedURLException(java.net.MalformedURLException) OperationFailedException(org.jboss.as.controller.OperationFailedException) ObjectFactory(javax.naming.spi.ObjectFactory) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ExternalContexts(org.jboss.as.naming.context.external.ExternalContexts) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ModelNode(org.jboss.dmr.ModelNode) ExternalContextObjectFactory(org.jboss.as.naming.ExternalContextObjectFactory) ContextNames(org.jboss.as.naming.deployment.ContextNames) ExternalContextBinderService(org.jboss.as.naming.service.ExternalContextBinderService)

Example 15 with ManagedReference

use of org.jboss.as.naming.ManagedReference in project wildfly by wildfly.

the class SessionObjectReferenceImpl method getBusinessObject.

@Override
@SuppressWarnings({ "unchecked" })
public synchronized <S> S getBusinessObject(Class<S> businessInterfaceType) {
    final String businessInterfaceName = businessInterfaceType.getName();
    ManagedReference managedReference = null;
    if (businessInterfaceToReference == null) {
        businessInterfaceToReference = new HashMap<String, ManagedReference>();
    } else {
        managedReference = businessInterfaceToReference.get(businessInterfaceName);
    }
    if (managedReference == null) {
        if (viewServices.containsKey(businessInterfaceType)) {
            final ServiceController<?> serviceController = currentServiceContainer().getRequiredService(viewServices.get(businessInterfaceType));
            final ComponentView view = (ComponentView) serviceController.getValue();
            try {
                managedReference = view.createInstance();
                businessInterfaceToReference.put(businessInterfaceType.getName(), managedReference);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw WeldLogger.ROOT_LOGGER.viewNotFoundOnEJB(businessInterfaceType.getName(), ejbName);
        }
    }
    return (S) managedReference.getInstance();
}
Also used : ComponentView(org.jboss.as.ee.component.ComponentView) ManagedReference(org.jboss.as.naming.ManagedReference)

Aggregations

ManagedReference (org.jboss.as.naming.ManagedReference)25 ComponentView (org.jboss.as.ee.component.ComponentView)7 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)7 NamingException (javax.naming.NamingException)4 ImmediateManagedReference (org.jboss.as.naming.ImmediateManagedReference)4 InterceptorContext (org.jboss.invocation.InterceptorContext)4 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2 InitialContext (javax.naming.InitialContext)2 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)2 Component (org.jboss.as.ee.component.Component)2 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)2 ComponentFactory (org.jboss.as.ee.component.ComponentFactory)2 InjectionSource (org.jboss.as.ee.component.InjectionSource)2 ExtendedEntityManager (org.jboss.as.jpa.container.ExtendedEntityManager)2 Injector (org.jboss.msc.inject.Injector)2 ServiceName (org.jboss.msc.service.ServiceName)2