javamongodbosgi-bundlebndtoolsamdatu

How to make MongoDB Service Available?


I am developing OSGi Mongodb bundle I have also added the following dependencies

  1. com.mongodb
  2. org.apache.felix.fileinstal
  3. org.amdatu.mongo
  4. org.apache.felix.configadmin

and all the dependency managers but in gogo console I get the following error message

org.amdatu.mongo
org.osgi.service.cm.ManagedServiceFactory(service.pid=org.amdatu.mongo) registered
org.osgi.service.log.LogService service optional unavailable
[11] agenda.mongodb.mongo_gfs
agenda.mongo.inter.AgendaMongo() unregistered
org.amdatu.mongo.MongoDBService service required unavailable

the main problem is MongoDBService is not available I must require this service for solving this problem I have read the book according to them

From a development perspective, everything seems fine, but when you run the appli‐ cation, it will complain that the MongoDBService is unavailable. You can figure this out with the dmcommand in the shell. We did however set up MongoDB on our system and deployed the necessary dependencies in our runtime. Still, the MongoDBService was unable to start. How come? This is because the MongoDBService needs some mandatory configuration in order to know to what database to connect to. The Amdatu MongoDB Serviceuses the Managed Service Factory pattern (see Chapter 4), and in order to bootstrap it, we need to supply a configuration file. In order to supply the configuration file, we need to create a new folder in our agendaproject. Create a new folder called load. This is the default name that the runtime will look for in order to spot configuration files. Next, add an empty text file and call it something like org.amdatu.mongo-demo.xml. The configuration file needs at least the following information: dbName=demo

I have also apply this but its still unavailable.

This is interface:

package agenda.mongo.inter;
import java.io.InputStream;

public interface AgendaMongo {
   public String store_in_db();
   public InputStream getData(Object file_id);
}

This is the implementation for Mongodb:

package agenda.mongodb.gridfs;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;

import org.amdatu.mongo.MongoDBService;
import org.bson.types.ObjectId;

import agenda.mongo.inter.AgendaMongo;

import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

    public class Gridfs_Mongodb implements AgendaMongo{
        GridFSInputFile gfsinput=null;
        private volatile MongoDBService mongoservice;
        public String store_in_db() {

        /*try {
            GridFS gfsHandler;
            gfsHandler = new GridFS(mongoservice.getDB(), "rest_data");// database
            File uri = new File("f:\\get1.jpg");                                    // name and
            gfsinput = gfsHandler.createFile(uri);
            gfsinput.saveChunks(1000);
            gfsinput.setFilename("new file");
            gfsinput.save();
            //System.out.println(gfsinput.getId());
            //save_filepath("file",gfsinput.getId());
            Object get_id = gfsinput.getId();//get_filename();
            //System.out.println(getData(get_id));
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            //System.out.println("Exception");
            e.printStackTrace();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            //System.out.println("Exception");
            e.printStackTrace();

        }*/
        System.out.println("DB:" + mongoservice.getDB());
        return mongoservice.getDB()+"";
    }

    /*
     * Retrieving the file
     */
    public InputStream getData(Object file_id) {
        GridFS gfsPhoto = new GridFS(mongoservice.getDB(), "rest_data");
        GridFSDBFile dataOutput = gfsPhoto.findOne((ObjectId) file_id);
        DBCursor cursor = gfsPhoto.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
        System.out.println(dataOutput);
        return dataOutput.getInputStream();

    }
    void start(){
        System.out.println("hello");
        System.out.println(store_in_db());
    }


}

Here I was just trying to get database name because every thing can be done after that but I t was returning me NULL because MongoDBService is Unavailable.

At this is Activator class

package agenda.mongodb.gridfs;

import org.amdatu.mongo.MongoDBService;
import org.apache.felix.dm.DependencyActivatorBase;
import org.apache.felix.dm.DependencyManager;
import org.osgi.framework.BundleContext;
import agenda.mongo.inter.AgendaMongo;

public class Activator extends DependencyActivatorBase {

    @Override
    public void init(BundleContext arg0, DependencyManager manager)
            throws Exception {
        manager.add(createComponent()
                .setInterface(AgendaMongo.class.getName(), null)
                .setImplementation(Gridfs_Mongodb.class)
                .add(createServiceDependency()
                .setService(MongoDBService.class)
                .setRequired(true)));
    }
    @Override
    public void destroy(BundleContext arg0, DependencyManager arg1)
            throws Exception {
        // TODO Auto-generated method stub

    }
}

The Interface package is an exported package and the implementation package is private.


Solution

  • The configuration file should have a .cfg extension (not .xml).