Search in sources :

Example 1 with ResourceReferencesMetaData

use of org.jboss.metadata.javaee.spec.ResourceReferencesMetaData 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)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)1 EnvEntryInjectionSource (org.jboss.as.ee.component.EnvEntryInjectionSource)1 FixedInjectionSource (org.jboss.as.ee.component.FixedInjectionSource)1 InjectionSource (org.jboss.as.ee.component.InjectionSource)1 LookupInjectionSource (org.jboss.as.ee.component.LookupInjectionSource)1 ImmediateManagedReference (org.jboss.as.naming.ImmediateManagedReference)1 ManagedReference (org.jboss.as.naming.ManagedReference)1 ManagedReferenceFactory (org.jboss.as.naming.ManagedReferenceFactory)1 ResourceReferenceMetaData (org.jboss.metadata.javaee.spec.ResourceReferenceMetaData)1 ResourceReferencesMetaData (org.jboss.metadata.javaee.spec.ResourceReferencesMetaData)1