All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.archive.crawler.framework.BeanLookupBindings Maven / Gradle / Ivy

The newest version!
package org.archive.crawler.framework;

import java.util.Map;

import javax.script.SimpleBindings;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

/**
 * Provides syntactic sugar for H3 scripts to reference beans without adding a
 * line like {@code def scope = appCtx.getBean("scope");}. Instead, the script
 * may simply reference {@code scope}. Caveat: access to beans is read only.
 * 
 * @author travis
 */
public class BeanLookupBindings extends SimpleBindings {

    private final ApplicationContext appCtx;

    public BeanLookupBindings(ApplicationContext appCtx) {
        if (appCtx == null) throw new NullPointerException("appCtx");
        this.appCtx = appCtx;
    }

    public BeanLookupBindings(ApplicationContext appCtx, Map m) {
        super(m);
        if (appCtx == null) throw new NullPointerException("appCtx");
        this.appCtx = appCtx;
    }
    
    @Override
    public Object get(Object key) {
        Object ret = super.get(key);
        if (ret == null && key instanceof String) {
            try {
                ret = appCtx.getBean((String) key);
            } catch (BeansException e) {}
        }
        return ret;
    }

    @Override
    public boolean containsKey(Object key) {
        try {
            return super.containsKey(key) || appCtx.containsBean((String) key);
        } catch (Exception e) {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy