简体   繁体   中英

Struts 2.3: runtime add Interceptor

I have a task to add/change interceptors runtime (with a plugin, do not have access to the parent configuration).

In previous version of Struts (2.0) that was pretty simple: classes InterceptorStackConfig and ActionConfig had methods addInterceptor and addInterceptors .

In newer version (2.3) that methods moved into Builder static sub-class and I can't use them like before.

So that is a problem. Already spent several days trying to avoid it. Anyone can help?

My previous code example:

public class IpLoggingInterceptorConfiguration implements ConfigurationProvider {

private Interceptor interceptor;
private Configuration configuration;

@Override
public void init(Configuration configuration) throws ConfigurationException {
    this.configuration = configuration;
}

@Override
public void loadPackages() throws ConfigurationException {

    for (Object packageConfigName : configuration.getPackageConfigNames()) {
        try {
            String name = (String) packageConfigName;
            PackageConfig packageConfig = configuration.getPackageConfig(name);
            updatePackage(packageConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public void updatePackage(PackageConfig packageConfig) {
    Map interceptorConfigs = packageConfig.getInterceptorConfigs();

    for (Object stack : interceptorConfigs.keySet()) {

        if (!(interceptorConfigs.get(stack) instanceof InterceptorStackConfig)) continue;

        InterceptorStackConfig interceptorStackConfig = (InterceptorStackConfig) interceptorConfigs.get(stack);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(interceptorStackConfig.getInterceptors());
        interceptorStackConfig.getInterceptors().clear();
        interceptorStackConfig.addInterceptor(interceptorMapping);
        interceptorStackConfig.addInterceptors(list);
    }

    for (String key : packageConfig.getActionConfigs().keySet()) {
        ActionConfig actionConfig = packageConfig.getActionConfigs().get(key);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(actionConfig.getInterceptors());
        actionConfig.getInterceptors().clear();
        actionConfig.addInterceptor(interceptorMapping);
        actionConfig.addInterceptors(list);
    }
}


@Override
public void destroy() {
}

@Override
public boolean needsReload() {
    return false;
}

@Override
public void register(ContainerBuilder arg0, LocatableProperties arg1)
        throws ConfigurationException {
}

public Interceptor getInterceptor() {
    return interceptor;
}

public void setInterceptor(Interceptor interceptor) {
    this.interceptor = interceptor;
}
}

I found and know, this solution is ugly, but simple and works... Maybe someone have better.

try {
            Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField("interceptors");
            interceptorsListField.setAccessible(true);
            List<InterceptorMapping> interceptorsList= (List<InterceptorMapping>) interceptorsListField.get(interceptorStackConfig);

            List<InterceptorMapping> list = new ArrayList<>();
            list.add(interceptorMapping);
            list.addAll(interceptorStackConfig.getInterceptors());
            interceptorsListField.set(interceptorStackConfig,list);
        } catch (Exception e) {
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM