OSGi bundle doesn't start after inserting a Bundle Activator - maven

I'm implementing a maven web project in Netbeans (version 8.0.2) with some build profiles (jetty, OSGi..) managed by POM file using plugins and custom goals of project in Netbeans.
I need to manage the lifecycle of the generated OSGi bundle, so I created an activator with the following class:
package org.activiti.explorer.conf;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class OSGiActivator implements BundleActivator {
#Override
public void start(BundleContext context) throws Exception {
System.out.println("BUNDLE STARTED");
}
#Override
public void stop(BundleContext context) throws Exception {
System.out.println("BUNDLE STOPPED");
}
}
After that I updated the maven bundle <plugin> element in the POM file adding the informaton about the activator, so the plugin is:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-Activator>org.activiti.explorer.conf.OSGiActivator</Bundle-Activator>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Embed-Dependency>*</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Web-ContextPath>activiti-explorer</Web-ContextPath>
<Import-Package> org.eclipse.jetty.servlet.listener,
org.apache.jasper.servlet,
javax.naming,
javax.el;resolution:=optional,
com.sun.el;resolution:=optional,
org.apache.el;resolution:=optional,
javax.servlet.jsp;resolution:=optional,
org.apache.jasper.runtime;resolution:=optional,
org.apache.tomcat;resolution:=optional,
javax.servlet;version="[2.6.0,4.0.1]",
javax.servlet.http;version="[2.6.0,4.0.1]",
org.apache.log4j,
javax.inject,
org.slf4j.impl,
org.slf4j,
org.slf4j.spi,
org.apache.commons.io;version="[1.4,2)",
org.apache.commons.io.output;version="[1.4,2)",
org.apache.commons.lang3;version="[3.1,4)",
org.apache.commons.lang3.builder;version="[3.1,4)",
org.apache.commons.lang3.exception;version="[3.1,4)"
</Import-Package>
<Export-Package>!*</Export-Package>
</instructions>
<manifestLocation>${basedir}/target/META-INF</manifestLocation>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when I build the bundle and load it on OSGi with Felix, the bundle is activated but it doesn't start, whereas if I delete the row about the Bundle-Activator from POM file it works properly.
Note that from Felix console no error is displayed about Build classpath, imported packages and exported packages and if I try to start bundle from osgi line command, this exception is logged:
gogo: BundleException: Error starting module.
Can someone help me? If I don't use the class which implements the Activator and I don't refer it in the POM file, the bundle created runs properly in OSGi.

You are listing all the Import-Package statements explicitly. This means that the maven-bundle-plugin does not add auto detected package usages. You are missing the Import-Package for org.osgi.framework. So the bundle resolves but the package is not wired and when loading the OSGiActivator class it fails.
So the simple solution is to add the package import.
I rather recommend though to not define all the imports and let the maven bundle plugin figure them out automatically. You can do this by adding a ",*". You should then remove all the packages it figures out by itself.

Related

Unable to use spring boot application as a dependency to another spring boot application

Unable to use spring boot application as a dependency to another spring boot application. Can anyone please point me to the correct source? I want to implement a central logging application using AspectJ (which is again spring boot application ) which will listen to different spring boot applications(maybe 6 other applications) method executions, and it will persist the data into DB.
I was using the below config in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
And add this as dependency on other spring boot application POM.XML file but still getting No qualifying bean of type 'com.client.client.NewClass' available*
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.client.client.NewClass;
#SpringBootApplication
public class DemoApplication implements CommandLineRunner{
#Autowired
private NewClass newClass;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(newClass.getValue());`enter code here`
}
}
IMO you have two choices:
A) Write the AspectJ code in a common module (not a full spring boot app). You can make it optional dependency(research about optional dependency in maven if you don't know already) if you like.
Then use that common module as library in other apps. If you want to write the logs to database, configure the log database as secondary database(again google on how to create two datasources in spring).
You will just need to update the properties file and pom.xml if you want to use this option.
B) Write a full spring boot app with REST endpoints to receive messages containing the info to log. Update each of your app to call the REST api with the info to log.

What Build Module in IntelliJ actually does for maven module

When I run
mvn clean install
for my maven module then it compiles fine. No issues.
But when I open my pom.xml file in IntelliJ and I choose to Build -> Build module then I get following issues:
Information:javac 1.8.0_144 was used to compile java sources
Information:Module "mymodule" was fully rebuilt due to project configuration/dependencies changes
Information:09.10.2017 21:16 - Compilation completed with 3 errors and 3 warnings in 23s 991ms
C:\somepath\mymodule\pom.xml
Error:Error:osgi: [mymodule] Exception: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent: java.net.URLClassLoader#29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin
Error:Error:osgi: [mymodule] Failed to load plugin org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true, error: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent: java.net.URLClassLoader#29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin
Error:Error:osgi: [mymodule] Cannot load the plugin org.apache.sling.bnd.models.ModelsScannerPlugin
This is a module with AEM code and it uses maven-sling-plugin. It works fine for other developers in the project. Because it's working when executed directly from maven I'm trying to understand what IntelliJ does in the background. But actually, my problem is those compilation issues.
From what I've found IntelliJ does not call maven when Build is done. Any ideas how can I find differences between running from IntelliJ and directly from Maven?
What happens here is that the ModelScanner plugin can't be found using the current ClassLoader. The reason for this can be that you are using IntelliJ IDEA Ultimate which comes with a OSGI plugin already pre-installed called 'Osmorc'. If this OSGI plugin is active it will determine the classloader to be used for building OSGI related projects.
So simply de-activating this Osmorc plugin in IntelliJ should allow your build to revert to the classloader from the ModelScannerPlugin mentioned in the configuration of your the maven-bundle-plugin in your projects POM.xml file which should solve the problem.
If this still results in a similar Maven build error, then make sure to add a Maven dependency 'org.apache.sling.bnd.model' to your maven-bundle-plugin in your POM.xml file.
<!-- Apache Felix Bundle Plugin -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.3.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<executions>
<!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
<execution>
<id>scr-metadata</id>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<supportIncrementalBuild>true</supportIncrementalBuild>
</configuration>
</execution>
</executions>
<configuration>
<exportScr>true</exportScr>
<instructions>
<!-- Enable processing of OSGI DS component annotations -->
<_dsannotations>*</_dsannotations>
<!-- Enable processing of OSGI metatype annotations -->
<_metatypeannotations>*</_metatypeannotations>
<_plugin>org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true</_plugin>
</instructions>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.bnd.models</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
could you please check your core pom file. it should contain a plugin section like this:
<plugin> <!-- Enable registration of Sling Models classes via bnd plugin --> org.apache.sling.bnd.models.ModelsScannerPlugin, <!-- Allow the processing of SCR annotations via a bnd plugin --> org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin;destdir=${project.build.outputDirectory} </plugin>
but if you created a project using aem archetype the tag looks like' <_plugin>

Kotlin JSR-223 ScriptEngineFactory within the fat jar - Cannot find kotlin compiler jar

I have a fat jar where I'm trying to get the instance of Kotlin's ScriptEngine.
For the debugging purposes I'm iterating through available Script Engine Factories and getting the engines.
val scriptEngineManager = ScriptEngineManager()
for (factory in scriptEngineManager.engineFactories) {
val scriptEngine = factory.scriptEngine
}
When it hits the Kotlin's engine, it fails with following exception:
Exception in thread "main" java.io.FileNotFoundException: Cannot find kotlin compiler jar, set kotlin.compiler.jar property to proper location
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt:100)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt$kotlinCompilerJar$2.invoke(KotlinJsr223ScriptEngineFactoryExamples.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:130)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.getKotlinCompilerJar(KotlinJsr223ScriptEngineFactoryExamples.kt)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223ScriptEngineFactoryExamplesKt.access$getKotlinCompilerJar$p(KotlinJsr223ScriptEngineFactoryExamples.kt:1)
at org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory.getScriptEngine(KotlinJsr223ScriptEngineFactoryExamples.kt:56)
at davidsiro.invoices.InvoiceGeneratorKt.generateInvoice(invoiceGenerator.kt:16)
at davidsiro.invoices.MainKt.main(main.kt:11)
My fat jar contains all of the dependencies (though unpacked), including Kotlin Compiler. I'm using Maven Assembly Plugin to build it, which configured like these:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
Any ideas?
Update
For the record, I tried to both KotlinJsr223JvmLocalScriptEngineFactory and KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory with the same result.
The JSR223 factories in the kotlin-script-util are trying to find the compiler jar in order to setup the compilation. In your case, you'll need to write your own factory to supply the script compilation classpath explicitly, e.g.
class MyScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
override fun getScriptEngine(): ScriptEngine =
KotlinJsr223JvmLocalScriptEngine(
Disposer.newDisposable(),
this,
classpath, // !!! supply the script classpath here
KotlinStandardJsr223ScriptTemplate::class.qualifiedName!!,
{ ctx, types -> ScriptArgsWithTypes(arrayOf(ctx.getBindings(ScriptContext.ENGINE_SCOPE)), types ?: emptyArray()) },
arrayOf(Bindings::class)
)
}
You need to put the following jars into classpath:
kotlin-script-util.jar - it contains the template class used as a superclass for the script
kotlin-script-runtime.jar - for base classes used in scripting
any other jars you'll need to use in your scripts, quite likely - kotlin-stdlib.jar
You may put your fat jar there instead, but that would mean that everything from it will be accessible from your scripts. Not talking about the overheads for the compiler.

ClassCastException with OSGI bundle

I am working on OSGi bundle, which uses javax.ws.rs-api (2.0.1). Karaf is already having jsr311-api (1.1.1) loaded as bundle. When I try to load my OSGi bundle, I see the following exception. Is there a way we can ignore the previously loaded bundle?
The activate method has thrown an exception
java.lang.LinkageError: ClassCastException: attempting to castbundle://137.0:1/javax/ws/rs/ext/RuntimeDelegate.class to bundle://177.0:1/javax/ws/rs/ext/RuntimeDelegate.class
at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:146)[137:javax.ws.rs.jsr311-api:1.1.1]
at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)[137:javax.ws.rs.jsr311-api:1.1.1]
at javax.ws.rs.core.UriBuilder.newInstance(UriBuilder.java:95)[137:javax.ws.rs.jsr311-api:1.1.1]
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)[137:javax.ws.rs.jsr311-api:1.1.1]
Your bundle must import only the packages you need versions.
You have to create META-INF\MANIFEST.MF with Import-Package header, which will contain the list of packages required only versions.
Import-Package: javax.ws.rs.ext,version="2.0.1"
List all the packages that cause the conflict.
I think here they are:
javax.ws.rs,version="2.0.1"
javax.ws.rs.client,version="2.0.1"
javax.ws.rs.container,version="2.0.1"
javax.ws.rs.core,version="2.0.1"
javax.ws.rs.ext,version="2.0.1"
You can specify a range of versions : [2.0.1, 3) and so on.
Real example:
Import-Package: org.osgi.service.blueprint; version="[1.0.0, 2.0.0)"
You can use maven-bundle-plugin to create requered MANIFEST.MF:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Description>${project.description}</Bundle-Description>
<Import-Package>
javax.ws.rs;version=2.0.1,
javax.ws.rs.client;version=2.0.1,
javax.ws.rs.container;version=2.0.1,
javax.ws.rs.core;version=2.0.1,
javax.ws.rs.ext;version=2.0.1,
*,
org.apache.camel.osgi
</Import-Package>
<Export-Package>
your.package
</Export-Package>
</instructions>
</configuration>
</plugin>
Don't forget to install bundle version 2.0.1

Got NoClassDefFoundError when invoking Axis2 webservice deployed with maven in weblogic

I'm trying to deploy axis2 webservices in weblogic server using maven. The project has maven modules and one of that is a war that i have defined the axis servlet in. The wsdl was there, so i used wsdl2code plugin to generate the xmlbean and schema and put that in a jar module. Structure is as below.
--lv-ear (ear with dependency on war)
|
--lv-ws
|
--lv-ws-ccid (jar module with skeleton and xmlbeans)
|
--lv-ws-ecs (jar module with skeleton and xmlbeans)
|
--lv-ws-web (war module with dep on jar modules)
|
--WEB-INF
|
--conf/axis2.xml
--services/ccid/services.xml
I built and deployed the ear to weblogic domain. The war was deployed successfully as part of ear and services were deployed. I am able to access wsdl files. When I tried to call the service, i got the below ClassNotFoundException for a schema file.
Caused by: java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.s2104B1E6E09A2A85656B3E630BA151C1.TypeSystemHolder
I saw that the random string in that path differed fo me. So I tried to call again and got below NoClassDefFoundError, which persists even after I tried with different approaches.
java.lang.NoClassDefFoundError: Could not initialize class com.lv.ws.ccid.xmlbean.InputDocument
at com.lv.ws.ccid.xmlbean.InputDocument$Factory.parse(InputDocument.java:463)
at com.lv.ws.ccid.CcidMessageReceiverInOut.fromOM(CcidMessageReceiverInOut.java:332)
at com.lv.ws.ccid.CcidMessageReceiverInOut.invokeBusinessLogic(CcidMessageReceiverInOut.java:46)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:173)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:144)
I searched for this and found something that told to configure app server for axis2 based on http://axis.apache.org/axis2/java/core/docs/app_server.html . When I tried it I got below error.
weblogic.xml.stax.XmlStreamInputFactory can not be cast to javax.xml.stream.XmlInputFactory
After discarding that configuration, I did some other possible deployments by having the webservice skeleton and xmlbean files in an aar and put the aar inside WEB-INF/services. I also tried putting Class-Path entry in MANIFEST.MF in ear / war for the jar files, to no avail. Still I got the same NoClassDefFoundError. Can you please give me suggestions on fixing that?
Fixed it now. This was due to my lack of experience with Axis. Issue was that, I had the generated schema and xmlbean files moved to src folder and then just tried to use normal jar function and dependency to deploy.
Now, I removed them from src folder and use wsdl2code and axis2-aar plugins to generate the xmlbean and schema files dynamically and then package them in aar. Then I deployed the aar to webapp and it worked fine. I have listed the plugin configuration inside <build> below.
<plugins>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.4</version>
<executions>
<execution>
<id>ccid-ws</id>
<goals>
<goal>wsdl2code</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.lv.ws.ccid</packageName>
<wsdlFile>${basedir}/src/main/resources/META-INF/ccid.wsdl</wsdlFile>
<databindingName>xmlbeans</databindingName>
<syncMode>sync</syncMode>
<unpackClasses>true</unpackClasses>
<namespaceToPackages>https://mdm.com/portal/ws/services/ccid=com.lv.ws.ccid.xmlbean</namespaceToPackages>
<outputDirectory>${basedir}/target/generated-sources</outputDirectory> <generateServerSide>false</generateServerSide>
<generateServicesXml>true</generateServicesXml>
<skipWSDL>true</skipWSDL>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.6.2</version>
<extensions>true</extensions>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>aar</goal>
</goals>
</execution>
</executions>
<configuration>
<aarName>ccid</aarName>
<includeDependencies>false</includeDependencies>
<outputDirectory>${project.build.directory}/aar</outputDirectory>
</configuration>
</plugin>
</plugins>

Resources