Search in sources :

Example 26 with Resource

use of javax.annotation.Resource in project wildfly by wildfly.

the class ResourceInjectionUtilities method getResourceName.

public static String getResourceName(InjectionPoint injectionPoint, PropertyReplacer propertyReplacer) {
    Resource resource = getResourceAnnotated(injectionPoint).getAnnotation(Resource.class);
    String mappedName = resource.mappedName();
    if (!mappedName.equals("")) {
        return propertyReplacer == null ? mappedName : propertyReplacer.replaceProperties(mappedName);
    }
    String name = resource.name();
    if (!name.equals("")) {
        name = propertyReplacer == null ? name : propertyReplacer.replaceProperties(name);
        // see if this is a prefixed name
        // and if so just return it
        int firstSlash = name.indexOf("/");
        int colon = name.indexOf(":");
        if (colon != -1 && (firstSlash == -1 || colon < firstSlash)) {
            return name;
        }
        return RESOURCE_LOOKUP_PREFIX + "/" + name;
    }
    String propertyName;
    if (injectionPoint.getMember() instanceof Field) {
        propertyName = injectionPoint.getMember().getName();
    } else if (injectionPoint.getMember() instanceof Method) {
        propertyName = getPropertyName((Method) injectionPoint.getMember());
        if (propertyName == null) {
            throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) injectionPoint.getMember());
        }
    } else {
        throw WeldLogger.ROOT_LOGGER.cannotInject(injectionPoint);
    }
    String className = injectionPoint.getMember().getDeclaringClass().getName();
    return RESOURCE_LOOKUP_PREFIX + "/" + className + "/" + propertyName;
}
Also used : Field(java.lang.reflect.Field) Resource(javax.annotation.Resource) Method(java.lang.reflect.Method) ParameterInjectionPoint(org.jboss.weld.injection.ParameterInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint)

Example 27 with Resource

use of javax.annotation.Resource in project tomee by apache.

the class Assembler method validateCdiResourceProducers.

private void validateCdiResourceProducers(final AppContext appContext, final AppInfo info) {
    if (appContext.getWebBeansContext() == null) {
        return;
    }
    // validate @Produces @Resource/@PersistenceX/@EJB once all is bound to JNDI - best case - or with our model
    if (appContext.isStandaloneModule() && !appContext.getProperties().containsKey("openejb.cdi.skip-resource-validation")) {
        final Map<String, Object> bindings = appContext.getWebContexts().isEmpty() ? appContext.getBindings() : appContext.getWebContexts().iterator().next().getBindings();
        if (bindings != null && appContext.getWebBeansContext() != null && appContext.getWebBeansContext().getBeanManagerImpl().isInUse()) {
            for (final Bean<?> bean : appContext.getWebBeansContext().getBeanManagerImpl().getBeans()) {
                if (ResourceBean.class.isInstance(bean)) {
                    final ResourceReference reference = ResourceBean.class.cast(bean).getReference();
                    String jndi = reference.getJndiName().replace("java:", "");
                    if (reference.getJndiName().startsWith("java:/")) {
                        jndi = jndi.substring(1);
                    }
                    Object lookup = bindings.get(jndi);
                    if (lookup == null && reference.getAnnotation(EJB.class) != null) {
                        final CdiPlugin plugin = CdiPlugin.class.cast(appContext.getWebBeansContext().getPluginLoader().getEjbPlugin());
                        if (!plugin.isSessionBean(reference.getResourceType())) {
                            // local beans are here and access is O(1) instead of O(n)
                            boolean ok = false;
                            for (final BeanContext bc : appContext.getBeanContexts()) {
                                if (bc.getBusinessLocalInterfaces().contains(reference.getResourceType()) || bc.getBusinessRemoteInterfaces().contains(reference.getResourceType())) {
                                    ok = true;
                                    break;
                                }
                            }
                            if (!ok) {
                                throw new DefinitionException("EJB " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
                            }
                        }
                    }
                    if (Reference.class.isInstance(lookup)) {
                        try {
                            lookup = Reference.class.cast(lookup).getContent();
                        } catch (final Exception e) {
                            // surely too early, let's try some known locations
                            if (JndiUrlReference.class.isInstance(lookup)) {
                                checkBuiltInResourceTypes(reference, JndiUrlReference.class.cast(lookup).getJndiName());
                            }
                            continue;
                        }
                    } else if (lookup == null) {
                        // TODO: better validation with lookups in tomee, should be in TWAB surely but would split current code
                        final Resource r = Resource.class.cast(reference.getAnnotation(Resource.class));
                        if (r != null) {
                            if (!r.lookup().isEmpty()) {
                                checkBuiltInResourceTypes(reference, r.lookup());
                            } else if (!r.name().isEmpty()) {
                                final String name = "comp/env/" + r.name();
                                boolean done = false;
                                for (final WebAppInfo w : info.webApps) {
                                    for (final EnvEntryInfo e : w.jndiEnc.envEntries) {
                                        if (name.equals(e.referenceName)) {
                                            if (e.type != null && !reference.getResourceType().getName().equals(e.type)) {
                                                throw new DefinitionException("Env Entry " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
                                            }
                                            done = true;
                                            break;
                                        }
                                    }
                                    if (done) {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (lookup != null && !reference.getResourceType().isInstance(lookup)) {
                        throw new DefinitionException("Resource " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast, instance is " + lookup);
                    }
                }
            }
        }
    }
}
Also used : CdiPlugin(org.apache.openejb.cdi.CdiPlugin) ResourceBean(org.apache.webbeans.component.ResourceBean) Resource(javax.annotation.Resource) DestroyableResource(org.apache.openejb.api.resource.DestroyableResource) JndiUrlReference(org.apache.openejb.core.ivm.naming.JndiUrlReference) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) ResourceReference(org.apache.webbeans.spi.api.ResourceReference) DefinitionException(javax.enterprise.inject.spi.DefinitionException)

Example 28 with Resource

use of javax.annotation.Resource in project tomee by apache.

the class ResourceManagerImpl method setBus.

@Resource
public final void setBus(Bus b) {
    if (bus != b) {
        bus = b;
        firstCalled = false;
        super.addResourceResolver(new ObjectTypeResolver(bus));
        if (null != bus) {
            bus.setExtension(this, ResourceManager.class);
        }
    }
}
Also used : ObjectTypeResolver(org.apache.cxf.resource.ObjectTypeResolver) Resource(javax.annotation.Resource)

Example 29 with Resource

use of javax.annotation.Resource in project tomee by apache.

the class ResourceInjector method visitMethod.

public final void visitMethod(final Method method, final Annotation annotation) {
    assert annotation instanceof Resource : annotation;
    Resource res = (Resource) annotation;
    String resourceName = getResourceName(res, method);
    Class<?> clz = getResourceType(res, method);
    Object resource = resolveResource(resourceName, clz);
    if (resource == null && "".equals(res.name())) {
        resource = resolveResource(null, clz);
    }
    if (resource != null) {
        invokeSetter(method, resource);
    } else {
        LOG.log(Level.FINE, "RESOURCE_RESOLVE_FAILED", new Object[] { resourceName, clz });
    }
}
Also used : Resource(javax.annotation.Resource)

Example 30 with Resource

use of javax.annotation.Resource in project tomee by apache.

the class LegacyAnnotationProcessor method processAnnotations.

/**
 * Inject resources in specified instance.
 * @param instance
 * @throws java.lang.IllegalAccessException
 * @throws java.lang.reflect.InvocationTargetException
 * @throws javax.naming.NamingException
 */
public void processAnnotations(final Object instance) throws IllegalAccessException, InvocationTargetException, NamingException {
    if (context == null) {
        // No resource injection
        return;
    }
    Class<?> clazz = instance.getClass();
    while (clazz != null) {
        // Initialize fields annotations
        final Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].isAnnotationPresent(Resource.class)) {
                final Resource annotation = fields[i].getAnnotation(Resource.class);
                lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
            }
            if (fields[i].isAnnotationPresent(EJB.class)) {
                final EJB annotation = fields[i].getAnnotation(EJB.class);
                lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
            }
            if (fields[i].isAnnotationPresent(WebServiceRef.class)) {
                final WebServiceRef annotation = fields[i].getAnnotation(WebServiceRef.class);
                lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
            }
            if (fields[i].isAnnotationPresent(PersistenceContext.class)) {
                final PersistenceContext annotation = fields[i].getAnnotation(PersistenceContext.class);
                lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
            }
            if (fields[i].isAnnotationPresent(PersistenceUnit.class)) {
                final PersistenceUnit annotation = fields[i].getAnnotation(PersistenceUnit.class);
                lookupFieldResource(context, instance, fields[i], annotation.name(), clazz);
            }
        }
        // Initialize methods annotations
        final Method[] methods = clazz.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].isAnnotationPresent(Resource.class)) {
                final Resource annotation = methods[i].getAnnotation(Resource.class);
                lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
            }
            if (methods[i].isAnnotationPresent(EJB.class)) {
                final EJB annotation = methods[i].getAnnotation(EJB.class);
                lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
            }
            if (methods[i].isAnnotationPresent(WebServiceRef.class)) {
                final WebServiceRef annotation = methods[i].getAnnotation(WebServiceRef.class);
                lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
            }
            if (methods[i].isAnnotationPresent(PersistenceContext.class)) {
                final PersistenceContext annotation = methods[i].getAnnotation(PersistenceContext.class);
                lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
            }
            if (methods[i].isAnnotationPresent(PersistenceUnit.class)) {
                final PersistenceUnit annotation = methods[i].getAnnotation(PersistenceUnit.class);
                lookupMethodResource(context, instance, methods[i], annotation.name(), clazz);
            }
        }
        clazz = clazz.getSuperclass();
    }
}
Also used : Field(java.lang.reflect.Field) PersistenceUnit(javax.persistence.PersistenceUnit) Resource(javax.annotation.Resource) PersistenceContext(javax.persistence.PersistenceContext) Method(java.lang.reflect.Method) EJB(javax.ejb.EJB) WebServiceRef(javax.xml.ws.WebServiceRef)

Aggregations

Resource (javax.annotation.Resource)56 Collectors (java.util.stream.Collectors)22 Set (java.util.Set)20 List (java.util.List)18 Subject (alien4cloud.security.Subject)17 Sets (com.google.common.collect.Sets)17 Arrays (java.util.Arrays)17 ArrayUtils (org.apache.commons.lang3.ArrayUtils)15 IGenericSearchDAO (alien4cloud.dao.IGenericSearchDAO)14 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)12 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)12 Audit (alien4cloud.audit.annotation.Audit)11 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)11 RestResponse (alien4cloud.rest.model.RestResponse)11 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)11 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)11 ApiOperation (io.swagger.annotations.ApiOperation)11 IOException (java.io.IOException)11 MediaType (org.springframework.http.MediaType)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11