Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Kura » Add external Jar to Kura OSGI Bundle(Adding external jars to Kura OSGI bundle)
Add external Jar to Kura OSGI Bundle [message #1751400] Mon, 09 January 2017 19:33 Go to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hello,

I'm trying to build a Kura Bundle with two external Jars (mongo-java-driver-3.4.1.jar
& jdom-2.0.4.jar). I'm not sure I've done it correctly. I'm able to build an OSGI
bundle and install on a Kura 2.0 installation using the OSGI console. I see no errors.

The base package is Kura example: org.eclipse.kura.example.ble.tisensortag. I have
added a few more class files and references to the above two .jar files.

The Kura OSGI bundle is communicating with Ti Sensors Tags as per normal. I don't seem to be able to reference Mongo specific methods. (no errors anywhere). I did pre-test my project as a standalone Java app before including with: org.eclipse.kura.example.ble.tisensortag

How can I verify the Bundle (jar) has been built correctly. I have attached a copy of the Manifest and OSGI console output. I don't see any reference to the external Jars in any of the osgi console output. Should I?

Thanks,
Re: Add external Jar to Kura OSGI Bundle [message #1751506 is a reply to message #1751400] Tue, 10 January 2017 22:08 Go to previous messageGo to next message
Matteo Maiero is currently offline Matteo MaieroFriend
Messages: 423
Registered: July 2015
Location: Italy
Senior Member
Hi Tom,
how are you referencing the Mongo specific methods?
Could you post some snippets?

Thanks.
Matteo
Re: Add external Jar to Kura OSGI Bundle [message #1751508 is a reply to message #1751506] Tue, 10 January 2017 22:25 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi,

I've simplified the code down to this. In the below code snippet,
the "return" in getMongoClient() never completes.

Do you know of any good tutorial on implementing Mongodb with Kura?

Thank you for helping!
--Tom

....
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class MongoWeatherWrapper {

private static final Logger s_logger = LoggerFactory.getLogger(BluetoothLe.class);

MongoClient mongo = null;
DB db = null;

public MongoWeatherWrapper() {
if(null == mongo) {
connectMongo();
}
if(null == db) {
db = getDBFromMongo(mongo);
}
}

private void connectMongo() {
mongo = getMongoClient();
db = getDBFromMongo(mongo);
}

private MongoClient getMongoClient() {
return new MongoClient("localhost", 27017);
}
Re: Add external Jar to Kura OSGI Bundle [message #1751512 is a reply to message #1751506] Wed, 11 January 2017 00:34 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Matteo,

I'm relatively new to working with Eclipse/Java/Kura. I thought perhaps we could go back to basics. I'm not sure the mongodb Jar files have been correctly added to the project. If we could get the following example working as a Kura Bundle it should help to resolve my issues.

I'm attempting to add a "mongo-java-driver-3.4.1.jar" to a hello world example. I have place the mongo.xx.jar in a root level "lib" folder and added the lib/*.JAR to the Build Path. There does not seem to be a reference to the JAR in the Manifest. Is this correct?

At the moment I have these errors when I create the JAR.

The code snippet is below along with the Manifest.

# 1/10/17 7:09:59 PM EST
# Eclipse Compiler for Java(TM) xx-201604081629-e45, 3.11.0, Copyright IBM Corp 2000, 2015. All rights reserved.
----------
1. ERROR in /Users/tommorocz/Documents/workspaceJanEE/kura/org.eclipse.kura.example.hello_osg1/src/org/eclipse/kura/example/hello_osg1/HelloOsgi.java (at line Cool
import com.mongodb.MongoClient;
^^^^^^^^^^^
The import com.mongodb cannot be resolved
----------
2. ERROR in /Users/tommorocz/Documents/workspaceJanEE/kura/org.eclipse.kura.example.hello_osg1/src/org/eclipse/kura/example/hello_osg1/HelloOsgi.java (at line 26)
MongoClient client = new MongoClient("localhost", 27017);
^^^^^^^^^^^
MongoClient cannot be resolved to a type
----------
3. ERROR in /Users/tommorocz/Documents/workspaceJanEE/kura/org.eclipse.kura.example.hello_osg1/src/org/eclipse/kura/example/hello_osg1/HelloOsgi.java (at line 26)
MongoClient client = new MongoClient("localhost", 27017);
^^^^^^^^^^^
MongoClient cannot be resolved to a type
----------
3 problems (3 errors)


------------------
package org.eclipse.kura.example.hello_osg1;

//import java.util.logging.Logger;

import org.osgi.service.component.ComponentContext;
import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;

import org.slf4j.Logger;



public class HelloOsgi {

private static final Logger s_logger = LoggerFactory.getLogger(HelloOsgi.class);

private static final String APP_ID = "org.eclipse.kura.example.hello_osgi";

protected void activate(ComponentContext componentContext) {

s_logger.info("Bundle " + APP_ID + " has started!");

s_logger.debug(APP_ID + ": This is a debug message.");

MongoClient client = new MongoClient("localhost", 27017);
String connectPoint = client.getConnectPoint();
s_logger.info("Mongo has started!");
//System.out.println(connectPoint);
client.close();

}

protected void deactivate(ComponentContext componentContext) {

s_logger.info("Bundle " + APP_ID + " has stopped!");

}

}


-----------------
Manifest.mf

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Hellow World Examlple with Logger
Bundle-SymbolicName: org.eclipse.kura.example.hello_osg1
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.osgi.service.component;version="1.2.0",
org.slf4j
Service-Component: component.xml,
OSGI-INF/component.xml


Thanks,
Tom
Re: Add external Jar to Kura OSGI Bundle [message #1751604 is a reply to message #1751512] Wed, 11 January 2017 19:55 Go to previous messageGo to next message
Matteo Maiero is currently offline Matteo MaieroFriend
Messages: 423
Registered: July 2015
Location: Italy
Senior Member
Hi Tom,
I'm not aware of projects specifically integrating mongodb.
What I can suggest you is to take a look at how we integrated Paho client in the kura.core project [1].
The core project references also the hsqldb integrated into the Kura target-platform.
Maybe you can have look at it too.

Matteo

[1] https://github.com/eclipse/kura/tree/develop/kura/org.eclipse.kura.core
Re: Add external Jar to Kura OSGI Bundle [message #1751608 is a reply to message #1751604] Wed, 11 January 2017 20:21 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Matteo,

I had a quick look at the project and yes adding something like the following does help:
+Bundle-ClassPath: .,
+ lib/org.eclipse.paho.client.mqttv3-1.0.1.2.jar

in my case:
+Bundle-ClassPath: .,
+ lib/mongo-java-driver-3.4.1.jar

I can create a bundle for Kura without error. However I still believe I have
an eclipse related build problem. I'm convinced the above mongo jar is a) not included
with the Kura Bundle or b) I've packaged the Kura Bundle incorrectly using Eclipse IDE.

Please..can you help me with setting up the project build options to create a Kura Bundle with an External Jar. At this point I'm a dead duck, 16+ hours with nothing left to try. All I have to work with is the "Kura Hello World Example".

Please consider my Eclipse/Kura skills as entry level.

Thanks,
Tom


Re: Add external Jar to Kura OSGI Bundle [message #1751621 is a reply to message #1751608] Wed, 11 January 2017 23:00 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hi Tom,

It would help if you could post your MANIFEST.MF file and your build.properties file. As Matteo mentioned, I would look at both of these files very closely in the org.eclipse.kura.core project to make sure you are including everything you need.

Also, the bundle produced by Eclipse is simply a Jar file. You can extract the bundle with jar xf <bundle_name>.jar and examine the contents. This may help answer some of your questions.

Thanks,
--Dave
Re: Add external Jar to Kura OSGI Bundle [message #1751623 is a reply to message #1751621] Wed, 11 January 2017 23:58 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Dave,

Thank you for helping.

To recap, I am modifying the org.eclipse.kura example.BLE.tisesortag project
to write the MQTT publish data to a Mondgodb. This requires adding two external *.Jars (mondo-java-driver + Jdom).

I do not have much experience building an OSGI project other than following the "Kura Hello World" example. I suspect this may the problem -- how to set up Eclipse to do the build, there are so many options.

At the moment I cannot connect to the Mongodb (the problem). I can create a *.jar and start the OSGI bundle. The Kura OSGI bundle is communicating with Ti Sensors Tags as per normal. I don't seem to be able to reference Mongo specific methods. (no errors anywhere). I did pre-test my project as a standalone Java app before including with: org.eclipse.kura.example.ble.tisensortag. Kura Debugger did not reveal much.

I've attached the OSGI bundle jar. (thanks for the tip jar xf <file>!)
and the project details (manifest, etc.) + code snippet.

Please let me know if there is anything else you need.

Very much appreciated,
Tom
Re: Add external Jar to Kura OSGI Bundle [message #1751716 is a reply to message #1751621] Thu, 12 January 2017 20:07 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Dave,

I've done some more testing and created the attached Kura Project
in an attempt to zero in on the problem of not being able to connect
to Mongo from within Kura. I'm now wondering if Kura is blocking something.
I did open port 27017 using Kura firewall.

Hope this helps.

Thanks you for helping.
Tom
Re: Add external Jar to Kura OSGI Bundle [message #1751898 is a reply to message #1751716] Mon, 16 January 2017 16:25 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hi Tom,

I posted a reply on Friday, but for some reason it didn't post. Anyway, I will try to recap what I posted.

What I found based on your attached bundle and project is that you are attempting to embed an OSGi bundle within your application bundle. If you want to embed the MongoDB Jar, you should use a standard Java Jar file and not the MongoDB OSGi bundle. If you want to use the MongoDB OSGi bundle, you should install that directly in the Kura framework and use the proper "Import-Package" statements in your MANIFEST.MF file. Let me know if this is not clear and I can explain more. I can also explain why you were not seeing errors in the Eclipse IDE if interested.

For the quickest fix, I think you can just replace the MongoDB OSGi bundle with a standard MongoDB Jar file. Everything else in your setup looks okay. It is unfortunate that OSGi bundles and standard Java Jar files use the same *.jar extension, this does tend to cause confusion.

Hope this helps.
--Dave
Re: Add external Jar to Kura OSGI Bundle [message #1751943 is a reply to message #1751898] Tue, 17 January 2017 01:27 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Dave,

I don't fully understand the "Quick Fix" scenario. The Mongo-java-driver was the recommended Jar to use for Mongodb connectivity. Our test shows the JAR has been
included successfully, but seems to fail on connection attempts to a Mongodb instance.

In testing I have NOT been able to connect to ANY web resources from within Kura.

What path should I follow to achieve external connectivity, of any type, from within Kura?
Back to basics...

Thank you for all your help.
Tom

PS: Sorry to hear about the fate of the original post.

Re: Add external Jar to Kura OSGI Bundle [message #1751986 is a reply to message #1751943] Tue, 17 January 2017 14:41 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hi Tom,

Let's take a step back.

Regarding the correct *.jar file to use, I was following this guide [1]. It appears you are using "Uber MongoDB Java Driver" or mongo-java-driver. While this may work by embedding in your bundle, I would recommend installing this bundle directly in the Kura framework since it is already an OSGi bundle. If you are intending to embed the jar in your bundle, I would use "MongoDB Driver" or "mongodb-driver". This is not an OSGi bundle, but rather a standard Java Jar file. I will try both of these options later in the week and let you know what I find.

All that said, is your application bundle working within Kura? Meaning, from within Kura, are you able to make a connection to MongoDB?

I am not sure what you mean by "In testing I have NOT been able to connect to ANY web resources from within Kura". To which resources are you referring? Are you trying to externally connect to the MongoDB instance? Are you trying to connect to your Kura application? If you are trying to connect to the MongoDB instance, you will need to open the appropriate ports in the Kura firewall [2].

As I said, I will try this later in the week and report back.

[1] http://mongodb.github.io/mongo-java-driver/3.0/driver/getting-started/installation-guide/
[2] https://docs.mongodb.com/manual/reference/default-mongodb-port/

Thanks,
--Dave
Re: Add external Jar to Kura OSGI Bundle [message #1752205 is a reply to message #1751986] Thu, 19 January 2017 20:59 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hello,

I have created a working example of using the MongoDB client within Kura. I am using the mongo-java-driver embedded in my bundle as you are. The issue I found is indeed in embedding the OSGi bundle. To get this to work, you need to add the same "Import-Package" statements to your manifest that the Mongo OSGi bundle is using. You can view my project here [1] and my MANIFEST.MF file here [2].

I still think the correct approach is to install the MongoDB bundle directly in Kura, but my example also works.

Hopefully this helps.

[1] https://github.com/dwoodard1/kura_examples/tree/master/org.dwoodard.kura.example.mongodb
[2] https://github.com/dwoodard1/kura_examples/blob/master/org.dwoodard.kura.example.mongodb/META-INF/MANIFEST.MF

Thanks,
--Dave
Re: Add external Jar to Kura OSGI Bundle [message #1752275 is a reply to message #1752205] Fri, 20 January 2017 14:07 Go to previous messageGo to next message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Dave,

Thank you for helping me work thru this problem. Couple of questions:
1- What is the best way to import the example project(s) into Eclipse IDE?
2- What is the purpose of the ...example.can project?

Tests against (org.dwoodard.kura.example.mongodb) is shown below.

Thanks,
Tom

-------

1) Tested my project with the the supplied driver "mongo-java-driver-3.2.2.jar". Same results -- can not connect to Mongodb.
2) I cannot import directly into Eclipse IDE using "Import Existing Project Into Workspace".
3) Therefore, build a new version of the project using example code. I have one error "cryptoService cannot be resolve". I have done my best to resolve with no avail.
3(a) blocking out that section (URI Builder) and hard coding the ...mongoURI result is a compiled project that yield the same results. Can not connect to Mongodbd.
Note: Previous comment: "I have NOT been able to connect to ANY web resources from within Kura". I need to connect to an external web resource and parse XML. We cannot connect to the resource using an imported library (jdom2). However we can by using java.net.URLConnection; Razz

There seems to be a commonality between my two problems -- using external Libraries for use within an OSGI Kura bundle. Is it possible the problems are related?
Re: Add external Jar to Kura OSGI Bundle [message #1752292 is a reply to message #1752275] Fri, 20 January 2017 16:39 Go to previous messageGo to next message
David Woodard is currently offline David WoodardFriend
Messages: 420
Registered: July 2014
Senior Member
Hi Tom,

1. The "mongo-java-driver" will not work by itself. I tried, but it itself depends on many other libraries. I guess this is why they recommend the "Uber Jar", so I would stick with this. One thing to note, a lot of the errors being generated are showing in /var/log/kura-console.log not /var/log/kura.log. Errors sent to standard out show in kura-console.log not kura.log. This may help in your long term debug efforts.
2. You cannot use "Import Existing Project..." because I did not include the .project file in my repo. You could try "Import File System", but this may cause other issues. I would just try and pull the pieces of my code you need to get your project working. My guess is you should only need the MANIFEST.MF "Import-Package" items. You do not need the example CAN project, that is separate and has nothing to do with this.
3. I am only using the CryptoService so I can make the password dynamic, if you have the password hard coded you don't need this. If you want to use this, you will need to bind the service [1].
3(a). Yes, this is likely the same problem. Let's get Mongo working first.

If you continue to have problems, please post the output of /var/log/kura-console.log.

[1] https://github.com/dwoodard1/kura_examples/blob/master/org.dwoodard.kura.example.mongodb/OSGI-INF/mongoDbExample.xml

Thanks,
--Dave
Re: Add external Jar to Kura OSGI Bundle [message #1752359 is a reply to message #1752292] Mon, 23 January 2017 02:36 Go to previous message
Tom Morocz is currently offline Tom MoroczFriend
Messages: 9
Registered: January 2017
Junior Member
Hi Dave,

Problem resolved. Thank you ever so much! Lining up each of our Manifests did the trick. I now have a much better understanding of what's going on. Kura-console.log was very useful as well.

Last question, can you recommend any Kura documentation sources in addition to https://eclipse.github.io/kura/?

Thanks again,
Tom
Previous Topic:Web Application in KURA
Next Topic:modbus TCP/IP ADU send problem
Goto Forum:
  


Current Time: Fri May 10 02:19:04 GMT 2024

Powered by FUDForum. Page generated in 0.04429 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top