Search in sources :

Example 1 with ResourceEditor

use of org.springframework.core.io.ResourceEditor in project geronimo-xbean by apache.

the class SpringInitialContextFactory method getInitialContext.

public Context getInitialContext(Hashtable environment) throws NamingException {
    if (singleton != null) {
        return singleton;
    }
    Resource resource = null;
    Object value = environment.get(Context.PROVIDER_URL);
    String key = "jndi.xml";
    if (value == null) {
        resource = new ClassPathResource(key);
    } else {
        if (value instanceof Resource) {
            resource = (Resource) value;
        } else {
            ResourceEditor editor = new ResourceEditor();
            key = value.toString();
            editor.setAsText(key);
            resource = (Resource) editor.getValue();
        }
    }
    BeanFactory context = loadContext(resource, key);
    Context answer = (Context) context.getBean("jndi");
    if (answer == null) {
        log.warn("No JNDI context available in JNDI resource: " + resource);
        answer = new DefaultContext(environment, new ConcurrentHashMap());
    }
    return answer;
}
Also used : Context(javax.naming.Context) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) BeanFactory(org.springframework.beans.factory.BeanFactory) XBeanXmlBeanFactory(org.apache.xbean.spring.context.impl.XBeanXmlBeanFactory) ResourceEditor(org.springframework.core.io.ResourceEditor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 2 with ResourceEditor

use of org.springframework.core.io.ResourceEditor in project spring-framework by spring-projects.

the class ResourceEditorRegistrar method registerCustomEditors.

/**
 * Populate the given {@code registry} with the following resource editors:
 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
 * URIEditor, ClassEditor, ClassArrayEditor.
 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
 * a ResourceArrayPropertyEditor will be registered as well.
 * @see org.springframework.core.io.ResourceEditor
 * @see org.springframework.beans.propertyeditors.InputStreamEditor
 * @see org.springframework.beans.propertyeditors.InputSourceEditor
 * @see org.springframework.beans.propertyeditors.FileEditor
 * @see org.springframework.beans.propertyeditors.URLEditor
 * @see org.springframework.beans.propertyeditors.URIEditor
 * @see org.springframework.beans.propertyeditors.ClassEditor
 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
 */
@Override
public void registerCustomEditors(PropertyEditorRegistry registry) {
    ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
    doRegisterEditor(registry, Resource.class, baseEditor);
    doRegisterEditor(registry, ContextResource.class, baseEditor);
    doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
    doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
    doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
    doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
    doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
    doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
    ClassLoader classLoader = this.resourceLoader.getClassLoader();
    doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
    doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
    doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));
    if (this.resourceLoader instanceof ResourcePatternResolver) {
        doRegisterEditor(registry, Resource[].class, new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
    }
}
Also used : FileEditor(org.springframework.beans.propertyeditors.FileEditor) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStreamEditor(org.springframework.beans.propertyeditors.InputStreamEditor) URIEditor(org.springframework.beans.propertyeditors.URIEditor) ContextResource(org.springframework.core.io.ContextResource) Resource(org.springframework.core.io.Resource) ClassArrayEditor(org.springframework.beans.propertyeditors.ClassArrayEditor) ResourceEditor(org.springframework.core.io.ResourceEditor) ClassEditor(org.springframework.beans.propertyeditors.ClassEditor) ResourceArrayPropertyEditor(org.springframework.core.io.support.ResourceArrayPropertyEditor) InputSourceEditor(org.springframework.beans.propertyeditors.InputSourceEditor) ReaderEditor(org.springframework.beans.propertyeditors.ReaderEditor) PathEditor(org.springframework.beans.propertyeditors.PathEditor) URLEditor(org.springframework.beans.propertyeditors.URLEditor)

Example 3 with ResourceEditor

use of org.springframework.core.io.ResourceEditor in project spring-framework by spring-projects.

the class GenericFilterBean method init.

/**
 * Standard way of initializing this filter.
 * Map config parameters onto bean properties of this filter, and
 * invoke subclass initialization.
 * @param filterConfig the configuration for this filter
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 * @see #initFilterBean
 */
@Override
public final void init(FilterConfig filterConfig) throws ServletException {
    Assert.notNull(filterConfig, "FilterConfig must not be null");
    this.filterConfig = filterConfig;
    // Set bean properties from init parameters.
    PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
            Environment env = this.environment;
            if (env == null) {
                env = new StandardServletEnvironment();
            }
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, env));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            String msg = "Failed to set bean properties on filter '" + filterConfig.getFilterName() + "': " + ex.getMessage();
            logger.error(msg, ex);
            throw new NestedServletException(msg, ex);
        }
    }
    // Let subclasses do whatever initialization they like.
    initFilterBean();
    if (logger.isDebugEnabled()) {
        logger.debug("Filter '" + filterConfig.getFilterName() + "' configured for use");
    }
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) NestedServletException(org.springframework.web.util.NestedServletException) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Environment(org.springframework.core.env.Environment) ResourceEditor(org.springframework.core.io.ResourceEditor) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Example 4 with ResourceEditor

use of org.springframework.core.io.ResourceEditor in project spring-framework by spring-projects.

the class HttpServletBean method init.

/**
 * Map config parameters onto bean properties of this servlet, and
 * invoke subclass initialization.
 * @throws ServletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws ServletException {
    // Set bean properties from init parameters.
    PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
    if (!pvs.isEmpty()) {
        try {
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        } catch (BeansException ex) {
            if (logger.isErrorEnabled()) {
                logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            }
            throw ex;
        }
    }
    // Let subclasses do whatever initialization they like.
    initServletBean();
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeanWrapper(org.springframework.beans.BeanWrapper) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyValues(org.springframework.beans.PropertyValues) ResourceEditor(org.springframework.core.io.ResourceEditor) ServletContextResourceLoader(org.springframework.web.context.support.ServletContextResourceLoader) BeansException(org.springframework.beans.BeansException)

Aggregations

ResourceEditor (org.springframework.core.io.ResourceEditor)4 BeanWrapper (org.springframework.beans.BeanWrapper)2 BeansException (org.springframework.beans.BeansException)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 PropertyValues (org.springframework.beans.PropertyValues)2 Resource (org.springframework.core.io.Resource)2 ResourceLoader (org.springframework.core.io.ResourceLoader)2 ServletContextResourceLoader (org.springframework.web.context.support.ServletContextResourceLoader)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Context (javax.naming.Context)1 XBeanXmlBeanFactory (org.apache.xbean.spring.context.impl.XBeanXmlBeanFactory)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ClassArrayEditor (org.springframework.beans.propertyeditors.ClassArrayEditor)1 ClassEditor (org.springframework.beans.propertyeditors.ClassEditor)1 FileEditor (org.springframework.beans.propertyeditors.FileEditor)1 InputSourceEditor (org.springframework.beans.propertyeditors.InputSourceEditor)1 InputStreamEditor (org.springframework.beans.propertyeditors.InputStreamEditor)1 PathEditor (org.springframework.beans.propertyeditors.PathEditor)1 ReaderEditor (org.springframework.beans.propertyeditors.ReaderEditor)1 URIEditor (org.springframework.beans.propertyeditors.URIEditor)1