Remote OSGI using ECF: Bundle Requirements For BND - osgi

Can some one point what bundles am I missing in this bndrun file for registering a remote service in OSGI framework. I followed the following example in the link Building your first remote osgi service.
I have registered a service like this:
public void start(BundleContext context) throws Exception {
Dictionary<String, String> props = new Hashtable<>();
props.put("service.exported.interfaces", "*");
props.put("service.exported.configs", "ecf.generic.server");
context.registerService(ITimeService.class.getName(),
new TimeServiceImpl(), props);
}
Here is my runrequires section of bndrun file:
runrequires: \
osgi.identity;filter:='(osgi.identity=org.siu.casa.timeservice.host.org.siu.casa.timeservice.host.api.impl)',\
osgi.identity;filter:='(osgi.identity=org.siu.casa.timeservice.host.org.siu.casa.timeservice.api)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.command)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.runtime)',\
osgi.identity;filter:='(osgi.identity=org.apache.felix.gogo.shell)',\
osgi.identity;filter:='(&(osgi.identity=org.osgi.service.event)(version>=1.3.1))',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf)',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf.discovery)',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf.identity)',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf.osgi.services.remoteserviceadmin)',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf.osgi.services.remoteserviceadmin.proxy)',\
osgi.identity;filter:='(osgi.identity=org.eclipse.ecf.provider.remoteservice)',\
osgi.identity;filter:='(&(osgi.identity=org.eclipse.equinox.registry)(version>=3.5.400))'

Related

Share application.properties files in different project

Below showing the project structure
Core Project
|-config project
|
|-Service project
After building the core project we get Service.jar file.
While running the service.jar am passing spring.config.additional.location as command line argument.
java -jar Service-1.0.jar --spring.config.additional-location=C:/Users/Administrator/Desktop/Springboot/
above spring.config.additional.location path having application.property file and some xml files.
I can able to read application property file in service project ,following logic
Application.propertes
external.config=C:/Users/Administrator/Desktop/Springboot/config/
Mian Class
#ImportResource(locations = {
"${external.config}"+"/spring/service-config.xml",
"${external.config}"+"/spring/datasource-config.xml"
})
public class ServiceMain {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ServiceMain.class)
.build()
.run(args);
for (String name : applicationContext.getBeanDefinitionNames()) {
}
}
}
Similar kind of logic applied in config project is given below,its not working
#Configuration
public class ConfigurationFactory
{
#Value("${external.config}")
public String extConfPath;
public String REQ_CONF = extConfPath+"/Configuration.xml";
public static final String FILTER_XML_CONF = extConfPath+"/DocFilter.xml";
}
Is there any better way to do this? How can i read external application.properties in config project
Do we have any better way to do this in spring boot
As you are cleary developing a distributed web system the best practice is to used externalised configuration used by your different services allowing you to update settings without redeployment. Take a look at Spring Cloud Config

Refresh Package Bundles

I've stumbled upon a problem, that can be summarized as follows:
I have an application which is embedded with OSGI. In this OSGI container, I have installed a bundle A of version v1 which registers that service object. This service object is used by the host application. while the service object is in use, I uninstalled the bundle A and installed a new version(v2) of the bundle A. Here are my findings.
Old service object works fine even if we uninstall the bundle A(v1).
The new service object gives the new functionality of bundle A(v2).
My question here is when we refresh the bundle packages by using
List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle local; // points to the bundle
refreshbundles.add(local);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)
and when we try to get the service object registered by the new version of the bundle A, it is returning null. I tried looking at the source code but it didn't help. can you please help me with this?
EDIT:
HostApplication
public Felix m_felix = null;
m_felix = new Felix(config); //sending some config to felix.
m_felix.start();
//installing bundle
Bundle bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.0.jar")
bundle.start()
//getting the service object registered by bundle A.
ServiceReference sr = m_felix.getBundleContext()
.getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();
//now uninstalling the bundle installing the new version of it say version 1.1
bundle.uninstall()
bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.1.jar")
bundle.start()
//getting the service object registered by bundleA1.1
ServiceReference sr = m_felix.getBundleContext()
.getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();
//the above line is working fine but after refreshing the packages, Service object is returned as null
List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle bundle; // points to the bundle
refreshbundles.add(bundle);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)
//refresh done
ServiceReference sr = m_felix.getBundleContext()
.getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();
//throwing null pointer exception because getService() is returning null.
what exactly is happening when we refresh the bundles?
BundleA_Activator.java
public class BundleA_Activator extends BundleActivator{
public class BundleActivatorInterfaces implements BundleActivator{
ServiceRegistration SR;
#Override
public void start(BundleContext bundleContext) {
SR = bundleContext.registerService(SampleInterface.class.getName(), new ExposedClass(), null);
}
#Override
public void stop(BundleContext bundleContext) {
SR.unregister();
}
}

Removing/shutdown Firebase in Java app (for hot redepoy)

I tried to use org.springframework.boot:spring-boot-devtools to speed up development.
My project uses Firebase to authenticate some requests. Firebase initialized via:
#PostConstruct
public void instantiateFirebase() throws IOException {
FirebaseOptions options = new FirebaseOptions.Builder()
.setDatabaseUrl(String.format("https://%s.firebaseio.com", configuration.getFirebaseDatabase()))
.setServiceAccount(serviceJson.getInputStream())
.build();
FirebaseApp.initializeApp(options);
}
After context reloading on changing .class file Spring reports error:
Caused by: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
at com.google.firebase.internal.Preconditions.checkState(Preconditions.java:173)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:180)
at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:160)
What Firebase API allow deregister/destroy FirebaseApp that I should use in #PreDestroy?
Looks like it is not possible to disable/shutdown/reinitialize Firebase app.
In my case it is fine to keep that instance in memory without changes.
Depending on your requirements you may use as simple as:
#PostConstruct
public void instantiateFirebase() throws IOException {
// We use only FirebaseApp.DEFAULT_APP_NAME, so check is simple.
if ( ! FirebaseApp.getApps().isEmpty())
return;
Resource serviceJson = applicationContext.getResource(String.format("classpath:firebase/%s", configuration.getFirebaseServiceJson()));
FirebaseOptions options = new FirebaseOptions.Builder()
.setDatabaseUrl(String.format("https://%s.firebaseio.com", configuration.getFirebaseDatabase()))
.setServiceAccount(serviceJson.getInputStream())
.build();
FirebaseApp.initializeApp(options);
}
or filter data like:
for (FirebaseApp app : FirebaseApp.getApps()) {
if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME))
return;
}

NoClassDefFoundError in Osgi environment

I am working with osgi on apache karaf and I am trying to use kafka and debezium to run into an osgi environment.
kafka and debezium were not osgi ready (karaf will not consider them as bundles), so I did osgified them using eclipse "Plug-in project". The jars that I osgified them are the following : debezium-embedded, debezium-core, kafka connect-api, kafka connect-runtime.
At the begining I get alot of "Class not found exception" when I try to run debezium..
In order to resolve this problem, I changed the manifest of the two bundles. I added an import package to the caller and an export package to the called bundle. Using this I can solve the classNotFound issue.
After solving all the classNotfound issues, I get NoClassDefFoundError
NoClassDefFoundError means that the class loader could not find the .class when it tries to load them ... But I did import all the packages and export them as well.
Any thoughts how to deal with NoClassDefFoundError in an osgi environement
[EDIT Added code]
This is the class Monitor :
public class Monitor {
private Consumer<SourceRecord> consumer = new Consumer<SourceRecord>() {
public void accept(SourceRecord t) {
System.out.println("Change Detected !");
}
};
public void connect() {
System.out.println("Engine Starting");
Configuration config = Configuration.create()
/* begin engine properties */
.with("connector.class", "io.debezium.connector.mysql.MySqlConnector")
.with("offset.storage", "org.apache.kafka.connect.storage.FileOffsetBackingStore")
.with("offset.storage.file.filename", "d:/pathTooffset.dat")
.with("offset.flush.interval.ms", 60000)
/* begin connector properties */
.with("name", "my-sql-connector").with("database.hostname", "localhost").with("database.port", 3306)
.with("database.user", "root").with("database.password", "apassword").with("server.id", 10692)
.with("database.server.name", "localhost")
.with("database.history", "io.debezium.relational.history.FileDatabaseHistory")
.with("database.history.file.filename", "d:/pathTOdbhistory.dat")
.build();
try {
// Create the engine with this configuration ...
EmbeddedEngine engine = EmbeddedEngine.create().using(config).notifying(consumer).build();
Executor executor = Executors.newFixedThreadPool(1);
executor.execute(() -> {
engine.run();
});
} catch (Exception e) {
e.printStackTrace();
}
}
And my activator :
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
Monitor monitor = new Monitor();
monitor.connect();
}
public void stop(BundleContext context) throws Exception {
}}
The problem must be inside EmbeddedEngine. The error could not initialize class means that some static initialization of the class did not work. See this related question noclassdeffounderror-could-not-initialize-class-error.
I propose to run karaf in debug mode and debug through the initialization of this class.

Remote JNDI access to a single resource in TomEE

I'm trying to set an object into JNDI and then get remote access to it. I'm using TomEE 1.6.0. I'm setting a sinple string using an servlet like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
try
{
Context ctx=new InitialContext();
ctx.bind("myKey","MY STRING");
}
catch(NamingException ex)
{
ex.printStackTrace();
}
}
After running and invoke this servlet, I try to get remote access through JNDI using this standalone main.
public static void main(String[] args) throws Exception
{
Context ctx = getContext();
String nom = (String)ctx.lookup("java:/comp/env/nombre");
System.out.println(nom);
}
private static Context getContext() throws Exception
{
Hashtable<String,String> t = new Hashtable<>();
t.put("java.naming.factory.initial","org.apache.openejb.client.RemoteInitialContextFactory");
t.put("java.naming.provider.url","http://127.0.0.1:8080/tomee/ejb");
return new InitialContext(t);
}
But it throws an NameNotFoundException like this:
Exception in thread "main" javax.naming.NameNotFoundException: /comp/env/nombre does not exist in the system. Check that the app was successfully deployed.
at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:319)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at demo.TestJNDI.main(TestJNDI.java:13)
So, my question are two:
1 - How can I know the default JNDI name which is using TomEE to publish this string?
2 - How can I set this string into any XML file instead the servlet?
Thanks!
Not sure what you expect to do but remote context is mainly an ejb/resource one. comp/env is clearly local to the application

Resources