Search in sources :

Example 26 with ManagedReference

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

the class ServerInterceptorCache method createInterceptorFactoryForServerInterceptor.

private InterceptorFactory createInterceptorFactoryForServerInterceptor(final Method method, final Constructor interceptorConstructor) {
    // The managed reference is going to be ConstructedValue, using the container-interceptor's constructor
    final ConstructedValue interceptorInstanceValue = new ConstructedValue(interceptorConstructor, Collections.<Value<?>>emptyList());
    // we *don't* create multiple instances of the container-interceptor class, but we just reuse a single instance and it's *not*
    // tied to the Jakarta Enterprise Beans component instance lifecycle.
    final CachedValue cachedInterceptorInstanceValue = new CachedValue(interceptorInstanceValue);
    // ultimately create the managed reference which is backed by the CachedValue
    final ManagedReference interceptorInstanceRef = new ValueManagedReference(cachedInterceptorInstanceValue);
    // which can invoke the container-interceptor's around-invoke/around-timeout methods
    return new ContainerInterceptorMethodInterceptorFactory(interceptorInstanceRef, method);
}
Also used : ValueManagedReference(org.jboss.as.naming.ValueManagedReference) ContainerInterceptorMethodInterceptorFactory(org.jboss.as.ejb3.component.ContainerInterceptorMethodInterceptorFactory) ConstructedValue(org.jboss.msc.value.ConstructedValue) ValueManagedReference(org.jboss.as.naming.ValueManagedReference) ManagedReference(org.jboss.as.naming.ManagedReference) CachedValue(org.jboss.msc.value.CachedValue)

Example 27 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 28 with ManagedReference

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

the class InstanceNameBindingProcessor method bindServices.

private void bindServices(DeploymentUnit deploymentUnit, ServiceTarget serviceTarget, ServiceName contextServiceName) {
    final ServiceName instanceNameServiceName = contextServiceName.append("InstanceName");
    final BinderService instanceNameService = new BinderService("InstanceName");
    serviceTarget.addService(instanceNameServiceName, instanceNameService).addDependency(contextServiceName, ServiceBasedNamingStore.class, instanceNameService.getNamingStoreInjector()).addDependency(ServerEnvironmentService.SERVICE_NAME, ServerEnvironment.class, new Injector<ServerEnvironment>() {

        @Override
        public void inject(final ServerEnvironment serverEnvironment) throws InjectionException {
            instanceNameService.getManagedObjectInjector().inject(new ManagedReferenceFactory() {

                @Override
                public ManagedReference getReference() {
                    return new ManagedReference() {

                        @Override
                        public void release() {
                        }

                        @Override
                        public Object getInstance() {
                            final String nodeName = serverEnvironment.getNodeName();
                            return nodeName == null ? "" : nodeName;
                        }
                    };
                }
            });
        }

        @Override
        public void uninject() {
            instanceNameService.getManagedObjectInjector().uninject();
        }
    }).install();
    final Map<ServiceName, Set<ServiceName>> jndiComponentDependencies = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPONENT_JNDI_DEPENDENCIES);
    Set<ServiceName> jndiDependencies = jndiComponentDependencies.get(contextServiceName);
    if (jndiDependencies == null) {
        jndiComponentDependencies.put(contextServiceName, jndiDependencies = new HashSet<>());
    }
    jndiDependencies.add(instanceNameServiceName);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ManagedReference(org.jboss.as.naming.ManagedReference) BinderService(org.jboss.as.naming.service.BinderService) ServiceName(org.jboss.msc.service.ServiceName) Injector(org.jboss.msc.inject.Injector) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ServerEnvironment(org.jboss.as.server.ServerEnvironment) HashSet(java.util.HashSet)

Example 29 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) {
        if (resourceRef.isDependencyIgnored()) {
            continue;
        }
        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 30 with ManagedReference

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

the class EJBComponent method createViewInstanceProxy.

protected <T> T createViewInstanceProxy(final Class<T> viewInterface, final Map<Object, Object> contextData, final ServiceName serviceName) {
    final ServiceController<?> serviceController = currentServiceContainer().getRequiredService(serviceName);
    final ComponentView view = (ComponentView) serviceController.getValue();
    final ManagedReference instance;
    try {
        if (WildFlySecurityManager.isChecking()) {
            instance = WildFlySecurityManager.doUnchecked(new PrivilegedExceptionAction<ManagedReference>() {

                @Override
                public ManagedReference run() throws Exception {
                    return view.createInstance(contextData);
                }
            });
        } else {
            instance = view.createInstance(contextData);
        }
    } catch (Exception e) {
        // TODO: do we need to let the exception propagate here?
        throw new RuntimeException(e);
    }
    return viewInterface.cast(instance.getInstance());
}
Also used : ComponentView(org.jboss.as.ee.component.ComponentView) ManagedReference(org.jboss.as.naming.ManagedReference) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) NamingException(javax.naming.NamingException) SystemException(javax.transaction.SystemException)

Aggregations

ManagedReference (org.jboss.as.naming.ManagedReference)33 ComponentView (org.jboss.as.ee.component.ComponentView)11 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)9 NamingException (javax.naming.NamingException)4 ProcessApplicationInterface (org.camunda.bpm.application.ProcessApplicationInterface)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 StartException (org.jboss.msc.service.StartException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 InitialContext (javax.naming.InitialContext)2 ProcessEngine (org.camunda.bpm.engine.ProcessEngine)2 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)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