0

I have created Maven based project in which I am tring to launch an OSGi container(Felix) by using the below code but everytime, it is giving me below exception-

Exception in thread "main" java.util.NoSuchElementException
    at java.util.ServiceLoader$ServiceIterator.next(ServiceLoader.java:187)
    at com.ebay.personalization.osgi.test.osgitest.App.main(App.java:28)

Below is my code:

import org.apache.commons.io.FileUtils;
import org.apache.felix.framework.FrameworkFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;

public class App {
    public static void main(String[] args) throws BundleException, IOException {

        FileUtils.deleteDirectory(new File("felix-cache"));
        FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();

        Framework framework = frameworkFactory.newFramework(new HashMap<String, String>());
        framework.start();

        BundleContext context = framework.getBundleContext();
        List<Bundle> installedBundles = new LinkedList<Bundle>();

        installedBundles.add(context.installBundle("C:\\Store\\TestingModel-1.0.0.jar"));

        for (Bundle bundle : installedBundles) {
            bundle.start();
        }

        ServiceReference reference = context.getServiceReference(ITestService.class.getName());
        ITestService service = (ITestService) context.getService(reference);
        service.speak();
    }
}

As soon as I run the above code, I always get the above mentioned exception-

Below is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <groupId>com.host.domain</groupId>
        <artifactId>DomainParent</artifactId>
        <version>1.6.1-RELEASE</version>
    </parent>

    <!-- POM Information about the Project -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.host.personalization.osgi.test</groupId>
    <artifactId>OsgiTest</artifactId>
    <version>1.0.0</version>

    <!-- Packing Type is bundle for OSGI Library Bundle -->
    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>org.springframework.core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
            <artifactId>org.apache.servicemix.bundles.cglib</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-mvn</artifactId>
            <version>1.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <!-- Build Configration -->
    <build>
        <plugins>
            <!-- Apache Felix Bundle Plugin - For Generation of Manifest after Compile 
                phase -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <!-- Configuration for generating the Manifest.mf -->
                <configuration>
                    <manifestLocation>src/main/resources/META-INF</manifestLocation>
                    <!-- Manifest Headers which need to customized during manifest generation -->
                    <instructions>
                        <Bundle-SymbolicName>OsgiTest</Bundle-SymbolicName>
                    <instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- Configuration of repositories for dependency resolution -->
    <repositories>
        <!-- domain Bundles Repository -->
        <!-- This is needed to locate the domain Parent project. Other repositories 
            come from the parent. -->
        <repository>
            <id>releases</id>
            <url>http://nxdomain/content/repositories/releases/</url>
            <releases>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>thirdparty</id>
            <url>http://nxdomain/content/repositories/thirdparty/</url>
            <releases>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

Can anybody tell me what wrong I am doing here? I know ServiceLoader searches the FrameworkFactory interface in the classpath and if it is not able to find then it will throw an exception. But in my case, I already have an dependency on felix framework so it should be there in my classpath.. Right? Then what wrong I am doing?

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • Is apache felix framework on your classpath? – Rohit Jain Aug 18 '13 at 19:55
  • If I am adding a maven dependency of felix framework in my pom.xml file. Then it won't go into my classpath? – AKIWEB Aug 18 '13 at 19:57
  • Before closing the question. Please see that question, in that also, they are saying to add the felix framework in classpath. But I already have felix framework in my classpath.. Dont be in so hurry in closing the post. – AKIWEB Aug 18 '13 at 20:11
  • Did you see the answer and the comments in that clearly? It clearly says, maven dependency won't help. Try to add the Bundles to classpath, and see if it works. – Rohit Jain Aug 18 '13 at 20:13
  • I have already done that as well. By adding jar file in the classpath using buildpath. But no luck.. – AKIWEB Aug 18 '13 at 20:13
  • No you have not done that. You need to put Felix on the **runtime** classpath. This is NOT the "build path" and it has nothing to do with Maven or the sodding POM. I'm voting to close this question since it's an exact duplicate. – Neil Bartlett Aug 18 '13 at 23:16
  • @Neil:Thanks Neil. Any suggestion how can I put Felix on the runtime classpath? – AKIWEB Aug 19 '13 at 00:06
  • When you run your application, add Felix to the list of JARs/directories that you specify with `-classpath`. The Felix framework JAR can be downloaded from http://felix.apache.org/downloads.cgi – Neil Bartlett Aug 19 '13 at 00:35

0 Answers0