参考 https://github.com/ceefour/paxwicket-shiro ;
创建filter:
package com.liming.core.web.bootup.wicket.filter;
import org.apache.log4j.Logger;
import javax.servlet.*;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: sunlf
* Date: 14-2-20
* Time: 下午6:33
* To change this template use File | Settings | File Templates.
*/
public class TestFilter implements Filter {
private static Logger logger = Logger.getLogger(TestFilter.class);
private Filter filter;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
logger.error("doFilter start---> Address is " + request.getRemoteAddr());
chain.doFilter(request, response);
}
private Filter getFilter() {
if (filter == null) {
throw new IllegalStateException("Filter property is required");
}
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
}
@Override
public void destroy() {
//To change body of implemented methods use File | Settings | File Templates.
}
}
创建DelegatingFilterFactory类
package com.liming.core.web.bootup.wicket.filter;
import org.ops4j.pax.wicket.api.ConfigurableFilterConfig;
import org.ops4j.pax.wicket.api.support.AbstractFilterFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationException;
import javax.servlet.Filter;
import java.util.Dictionary;
public class DelegatingFilterFactory extends AbstractFilterFactory {
private Filter filter;
public DelegatingFilterFactory(BundleContext bundleContext, String applicationName, Integer priority) {
super(bundleContext, applicationName, priority);
}
public Filter createFilter(ConfigurableFilterConfig filterConfig) {
ClassLoader oldClassloader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(TestFilter.class.getClassLoader());
TestFilter delegatingFilter = TestFilter.class.newInstance();
delegatingFilter.setFilter(filter);
delegatingFilter.init(filterConfig);
return delegatingFilter;
} catch (Exception e) {
throw new IllegalStateException(String.format("Filter %s could not be created for application {}",
TestFilter.class.getName(), getApplicationName()), e);
} finally {
Thread.currentThread().setContextClassLoader(oldClassloader);
}
}
public void setFilter(Filter filter) {
this.filter= filter;
}
@Override
public void updated(Dictionary properties) throws ConfigurationException {
}
}
增加osig的Activator类
package com.liming.core.web.bootup.wicket.internal;
import com.liming.core.web.bootup.wicket.filter.DelegatingFilterFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* @类描述:OSGI初始化类
* @创建人:sunlf
* @创建时间:2014-04-16 下午3:05
* @修改人:
* @修改时间:
* @修改备注:
*/
public class InitActivator implements BundleActivator {
private static BundleContext context;
/**
* 获取Bundle上下文
* @return Bundle上下文
*/
public static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
* @see org.osgi.tp.BundleActivator#start(org.osgi.tp.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
InitActivator.context = bundleContext;
DelegatingFilterFactory delegatingFilterFactory=new DelegatingFilterFactory(bundleContext,"blueprint.simple.default",2);
delegatingFilterFactory.register();
}
/*
* (non-Javadoc)
* @see org.osgi.tp.BundleActivator#stop(org.osgi.tp.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
InitActivator.context = null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<service interface="org.ops4j.pax.wicket.api.WebApplicationFactory">
<service-properties>
<entry key="pax.wicket.applicationname" value="blueprint.simple.default"/>
<entry key="pax.wicket.mountpoint" value="/"/>
</service-properties>
<bean class="org.ops4j.pax.wicket.api.support.SimpleWebApplicationFactory">
<property name="wicketApplication" value="com.liming.core.web.bootup.wicket.IrisApplication"/>
</bean>
</service>