lili
lili

Reputation: 1906

Spring + Felix war = FileNotFoundException

Is it possible to use spring inside war bundle on felix? I use spring 3.0.5 with felix on glassfish 3.1

I tried to enter component-scan tag inside OSGI war bundle in felix, and I'm getting the below exception.

I saw that a similar bug was solved for Equinox, and what about felix? Is there a workaround or solution to this problem?

P.S: the same exception is thrown if I define path with * in web.xml contextConfigLocation, for example:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/*.xml</param-value>
</context-param>

Exception:

2012-02-08 18:30:23,194 [pool-28-thread-1] (PathMatchingResourcePatternResolver.java:532) WARN - Cannot search for matching files underneath URL [bundle://275.0:2/examples/services/] because it does not correspond to a directory in the file system 
java.io.FileNotFoundException: URL [bundle://275.0:2/examples/services/] cannot be resolved to absolute file path because it does not reside in the file system: bundle://275.0:2/examples/services/ 
at org.springframework.util.ResourceUtils.getFile(Res ourceUtils.java:204) 
at org.springframework.core.io.AbstractFileResolvingR esource.getFile(AbstractFileResolvingResource.java :52) 
at org.springframework.core.io.UrlResource.getFile(Ur lResource.java:168) 
at org.springframework.core.io.support.PathMatchingRe sourcePatternResolver.doFindPathMatchingFileResour ces(PathMatchingResourcePatternResolver.java:528) 
... 
2012-02-08 18:30:23,194 [pool-28-thread-1] (PathMatchingResourcePatternResolver.java:353) DEBUG - Resolved location pattern [classpath*:examples/services/**/*.class] to resources [] 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx">

    <context:annotation-config/>

    <tx:annotation-driven />

    <context:component-scan base-package="examples.services" />

</beans>

thank you for any tip

Upvotes: 3

Views: 846

Answers (2)

pdubs
pdubs

Reputation: 2848

A slightly more elegant solution if you don't mind tying yourself to Felix.

package org.eclipse.core.runtime;

import java.net.URL;

import org.apache.felix.framework.BundleRevisionImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class FileLocator {
    private FileLocator() {}

    public static URL resolve(URL url) {
        if(url == null) {
            return null;
        }
        Bundle bundle = FrameworkUtil.getBundle(FileLocator.class).getBundleContext().getBundle();
        BundleRevisionImpl bundleRevision = bundle.adapt(BundleRevisionImpl.class);
        return bundleRevision.getLocalURL(url.getPort(), url.getPath());
    }
}

This requires that you export the framework on Felix startup with the following parameter:

-Dorg.osgi.framework.system.packages.extra=org.apache.felix.framework

Upvotes: 0

lili
lili

Reputation: 1906

There are 2 workarounds I found to solve it. Still I'll be glad to hear a real solution for felix.

The workarounds are:

  1. Use equinox instead of felix :)
  2. (an ugly one) Create a temporary folder and extract all the classes into it. Create a class org.eclipse.core.runtime.FileLocator with a function

public static URL resolve(URL url) throws IOException

and return the url of content from this folder, for example:

    String path = url.getPath(); 
    String str;
    if(path.contains("com")){
        str = path.substring(path.indexOf("com"));
    }
    return new URL("file:\\c:\\temp_classes\\"+str);

Upvotes: 1

Related Questions