Embedded Jetty ResourceBase Path URL
I am implementing Jetty in a Spring based application. I am setting up my Jetty server in a Spring context file. The specific part of the configuration I ran into is this:
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/" />
<property name="resourceBase" value="????????" />
<property name="parentLoaderPriority" value="true" />
</bean>
If you see above where I put the ????????, I ideally want the resourceBase to refer to a folder in my classpath. I am deploying my application in a single JAR executable and have a folder config/web/WEB-INF
in my classpath.
It seems that Jetty can handle URLs defined in resourceBase (for example jar:file:/myapp.jar!/config/web
), but doesn't seem to support class URLs. I get an IllegalArgumentException if I define something like classpath:config/web
.
This is a real pain for me. Does anyone know how to achieve this functionality?
Thanks,
Andrew
a source to share
You need to get your resource as Spring Resource
and call getURI().toString()
on it, something like this:
public class ResourceUriFactoryBean extends AbstractFactoryBean<String> {
private Resource resource;
public ResourceUriFactoryBean(Resource resource) {
this.resource = resource;
}
@Override
protected String createInstance() throws Exception {
return resource.getURI().toString();
}
@Override
public Class<? extends String> getObjectType() {
return String.class;
}
}
-
<property name="resourceBase">
<bean class = "com.metatemplating.sample.test.ResourceUriFactoryBean">
<constructor-arg value = "classpath:config/web" />
</bean>
</property>
-
Or a more elegant approach with the Spring 3.0 expression language:
<property name="resourceBase"
value = "#{new org.springframework.core.io.ClassPathResource('config/web').getURI().toString()}" />
a source to share