I am trying to go through tutorial from OSGI and Apache Felix 3.0 book and
one of the the examples is to use IPOJO Annotation and Meta.xml. When I
deploy application to Felix the application runs fine but if i try to lookup
help iside Felix Gogo shell, I get error: gogo: ClassCastException:
java.lang.String cannot be cast to [Ljava.lang.String;

Here is the code with annotation:

package com.packtpub.felix.bookshelf.service.tui;

import com.packtpub.felix.bookshelf.inventory.api.Book;
import
com.packtpub.felix.bookshelf.inventory.api.BookAlreadyExistsException;
import com.packtpub.felix.bookshelf.inventory.api.BookNotFoundException;
import com.packtpub.felix.bookshelf.inventory.api.InvalidBookException;
import com.packtpub.felix.bookshelf.service.api.BookshelfService;
import com.packtpub.felix.bookshelf.service.api.InvalidCredentialsException;
import com.packtpub.felix.bookshelf.service.tui.api.BookshelfServiceProxy;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.service.command.Descriptor;
//import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.util.HashSet;
import java.util.Set;


@Component(name = "BookshelfServiceProxy")
@Provides
public class BookshelfServiceProxyImpl implements BookshelfServiceProxy{
    public static final String SCOPE = "book";

    @ServiceProperty(name = "osgi.command.scope", value = SCOPE)
    public String gogoScope;

    public static final String[] FUNCTIONS = new String[] {
            "add", "search"
    };

    public static final String FUNCTIONS_STR ="search";
    @ServiceProperty(name = "osgi.command.function", value = FUNCTIONS_STR)
    public String gogoFunctions;

    //private BundleContext context;
    @Requires
    private BookshelfService bookshelf;

//    public BookshelfServiceProxyImpl(BundleContext context){
//        this.context = context;
//    }
    public BookshelfServiceProxyImpl(){
    }

    @Descriptor("Search books by author, title, or category")
    public Set<Book> search(
            @Descriptor("username")
            String username,
            @Descriptor("password")
            String password,
            @Descriptor(
                    "search on attribute: author, title, or category"
            )
            String attribute,
            @Descriptor(
                    "match like (use % at the beginning or end of <like>" +
                            " for wild-card)")
            String filter) throws InvalidCredentialsException{

        BookshelfService service =  lookupService();

        String sessionid = service.login(username,password.toCharArray());
        Set<String> results;

        if("title".equals(attribute)){
            results = service.searchBooksByTitle(sessionid,filter);
        }
        else if("author".equals(attribute)){
            results = service.searchBooksByAuthor(sessionid,filter);
        }
        else if("category".equals(attribute)){
            results = service.searchBooksByCategory(sessionid,filter);
        }
        else{
            throw new RuntimeException(
                    "Invalid attribute, expecting one of { 'title', " +
                            "'author', 'category' } got '"+attribute+"'");
        }
        return  getBooks(sessionid,service,results);

    }

    @Descriptor("Search books by rating")
    public Set<Book> search(
            @Descriptor("username")
            String username,
            @Descriptor("password")
            String password,
            @Descriptor("search on attribute rating")
            String attribute,
            @Descriptor("lower rating limit (inclusive)")
            int lower,
            @Descriptor("upper rating limit (inclusive)")
            int upper) throws InvalidCredentialsException {

        if(!"rating".equals(attribute)){
            throw new RuntimeException(
                    "Invalid attribute, expecting 'rating' got '"+
                            attribute+"'");
        }

        BookshelfService service = lookupService();
        String sessionid = service.login(username,password.toCharArray());
        Set<String> results =
service.searchBooksByRating(sessionid,lower,upper);
        return  getBooks(sessionid,service,results);
    }

    @Descriptor("Add book to the inventory")
    public String add(
            @Descriptor("username") String username,
            @Descriptor("password") String password,
            @Descriptor("ISBN") String isbn,
            @Descriptor("Title") String title,
            @Descriptor("Author") String author,
            @Descriptor("Category") String category,
            @Descriptor("Rating (0..10)") int rating)
            throws InvalidCredentialsException,
            BookAlreadyExistsException,
            InvalidBookException{
            BookshelfService service = lookupService();
        String sessionId = service.login(username,password.toCharArray());
        service.addBook(sessionId,isbn,title,author,category,rating);
        return isbn;
    }

    private BookshelfService lookupService(){
//        ServiceReference reference = context.getServiceReference(
//                BookshelfService.class.getName());
//        if(reference==null){
//            throw new RuntimeException("BookshelfService not
registered,cannot invoke "+
//                    "operation");
//        }
//        BookshelfService service = (BookshelfService)
this.context.getService(reference);
//        if(service==null){
//            throw new RuntimeException(
//                    "BookshelfService not registered, cannot invoke "+
//                            "operation");
//        }
//        return service;
        return this.bookshelf;
    }

    private Set<Book> getBooks(
            String sessionid, BookshelfService service,
            Set<String> results) {
        Set<Book> books = new HashSet<Book>();
        for(String isbn: results){
            Book book;
            try{
                book = service.getBook(sessionid,isbn);
                books.add(book);
            } catch (BookNotFoundException e) {
                System.out.println("ISBN "+ isbn +
                        " referenced but not found");
            }
        }
        return books;
    }
}


Here is meta.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
           xsi:schemaLocation="org.apache.felix.ipojo
http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd";
           xmlns="org.apache.felix.ipojo">

    <instance component="BookshelfServiceProxy"
            name="bookshelf.service.tui"/>
</ipojo>

Reply via email to