Search in sources :

Example 6 with ManagedReference

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

the class ApplicationClientComponentDescription method createConfiguration.

@Override
public ComponentConfiguration createConfiguration(final ClassReflectionIndex classIndex, final ClassLoader moduleClassLoader, final ModuleLoader moduleLoader) {
    final ComponentConfiguration configuration = super.createConfiguration(classIndex, moduleClassLoader, moduleLoader);
    configuration.setInstanceFactory(new ComponentFactory() {

        @Override
        public ManagedReference create(final InterceptorContext context) {
            return new ManagedReference() {

                @Override
                public void release() {
                }

                @Override
                public Object getInstance() {
                    return null;
                }
            };
        }
    });
    return configuration;
}
Also used : ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) ComponentFactory(org.jboss.as.ee.component.ComponentFactory) InterceptorContext(org.jboss.invocation.InterceptorContext) ManagedReference(org.jboss.as.naming.ManagedReference)

Example 7 with ManagedReference

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

the class LookupInjectionSource method getResourceValue.

/**
     * {@inheritDoc}
     */
public void getResourceValue(final ResolutionContext resolutionContext, final ServiceBuilder<?> serviceBuilder, final DeploymentPhaseContext phaseContext, final Injector<ManagedReferenceFactory> injector) {
    final String applicationName = resolutionContext.getApplicationName();
    final String moduleName = resolutionContext.getModuleName();
    final String componentName = resolutionContext.getComponentName();
    final boolean compUsesModule = resolutionContext.isCompUsesModule();
    final String scheme = org.jboss.as.naming.InitialContext.getURLScheme(lookupName);
    if (scheme == null) {
        // relative name, build absolute name and setup normal lookup injection
        if (componentName != null && !compUsesModule) {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:comp/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        } else if (compUsesModule) {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:module/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        } else {
            ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:jboss/env/" + lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
        }
    } else {
        if (scheme.equals("java")) {
            // an absolute java name, setup normal lookup injection
            if (compUsesModule && lookupName.startsWith("java:comp/")) {
                // switch "comp" with "module"
                ContextNames.bindInfoFor(applicationName, moduleName, componentName, "java:module/" + lookupName.substring(10)).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
            } else {
                ContextNames.bindInfoFor(applicationName, moduleName, componentName, lookupName).setupLookupInjection(serviceBuilder, injector, phaseContext.getDeploymentUnit(), optional);
            }
        } else {
            // an absolute non java name
            final ManagedReferenceFactory managedReferenceFactory;
            if (URL_SCHEMES.contains(scheme)) {
                // a Java EE Standard Resource Manager Connection Factory for URLs, using lookup to define value of URL, inject factory that creates URL instances
                managedReferenceFactory = new ManagedReferenceFactory() {

                    @Override
                    public ManagedReference getReference() {
                        try {
                            return new ImmediateManagedReference(new URL(lookupName));
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
            } else {
                // lookup for a non java jndi resource, inject factory which does a true jndi lookup
                managedReferenceFactory = new ManagedReferenceFactory() {

                    @Override
                    public ManagedReference getReference() {
                        try {
                            return new ImmediateManagedReference(new InitialContext().lookup(lookupName));
                        } catch (NamingException e) {
                            EeLogger.ROOT_LOGGER.tracef(e, "failed to lookup %s", lookupName);
                            return null;
                        }
                    }
                };
            }
            injector.inject(managedReferenceFactory);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) ManagedReference(org.jboss.as.naming.ManagedReference) NamingException(javax.naming.NamingException) ImmediateManagedReference(org.jboss.as.naming.ImmediateManagedReference) URL(java.net.URL) InitialContext(javax.naming.InitialContext)

Example 8 with ManagedReference

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

the class ManagedReferenceMethodInterceptor 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 {
        return method.invoke(instance, context.getParameters());
    } 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) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with ManagedReference

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

the class EjbLookupInjectionSource method getResourceValue.

@Override
public void getResourceValue(final ResolutionContext resolutionContext, final ServiceBuilder<?> serviceBuilder, final DeploymentPhaseContext phaseContext, final Injector<ManagedReferenceFactory> injector) throws DeploymentUnitProcessingException {
    final Class<?> type;
    if (targetType != null) {
        type = targetType;
    } else if (targetTypeName != null) {
        try {
            type = ClassLoadingUtils.loadClass(targetTypeName, phaseContext.getDeploymentUnit());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    } else {
        type = null;
    }
    injector.inject(new ManagedReferenceFactory() {

        @Override
        public ManagedReference getReference() {
            try {
                final Object value = new InitialContext().lookup(lookup);
                return new ManagedReference() {

                    @Override
                    public void release() {
                    }

                    @Override
                    public Object getInstance() {
                        if (type != null && value instanceof org.omg.CORBA.Object) {
                            return PortableRemoteObject.narrow(value, type);
                        } else {
                            return value;
                        }
                    }
                };
            } catch (NamingException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) ManagedReference(org.jboss.as.naming.ManagedReference) PortableRemoteObject(javax.rmi.PortableRemoteObject) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 10 with ManagedReference

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

the class ComponentInstantiatorInterceptor method processInvocation.

public Object processInvocation(final InterceptorContext context) throws Exception {
    final ComponentInstance componentInstance = context.getPrivateData(ComponentInstance.class);
    final ManagedReference existing = (ManagedReference) componentInstance.getInstanceData(contextKey);
    if (existing == null) {
        final ManagedReference reference = componentFactory.create(context);
        boolean ok = false;
        try {
            componentInstance.setInstanceData(contextKey, reference);
            if (setTarget) {
                context.setTarget(reference.getInstance());
            }
            Object result = context.proceed();
            ok = true;
            return result;
        } finally {
            if (!ok) {
                reference.release();
                componentInstance.setInstanceData(contextKey, reference);
            }
        }
    } else {
        return context.proceed();
    }
}
Also used : 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