Export package in OSGi Bundle - servlets

I have an OSGi Bundle and a servlet. Now I want to access the bundle from the servlet. For that purpose I use the following in the servlet:
#Resource
BundleContext context
...
ServiceReference ref = context.getServiceReference("package.MyOSGiServiceInterface");
MyOSGiServiceInterface service = context.getService(ref);
The Problem is that my servlet doesn't know MyOSGiServiceInterface 'cause that is defined in the OSGiBundle. In Eclipse I added a reference to the bundle Project in my Build Path. But at runtime it obviously can't find it.
To solve that Problem I played around with
(in bundle manifest)
Export-Package: package-of-osgi-service-interface
(in servlet manifest)
Import-Package: package-of-osgi-service-Interface
Dependencies: ...,deployment.MyBundle
But I couldn't solve it.
Whats the missing step? How can I tell JBoss to add the package containing MyOSGiServiceInterface in OSGiBundle to the class path?
Thanks for answers!
(JBoss AS 7.1.1)
--> error message <--

Eventually I solved it. I had to put the right combination of settings together to reach my goal:
Deploy the bundle per: File - Export - "Deployable plug-ins and fragments" into folder: "jboss/standalone/deployments"
Bundle-Manifest:
Bundle-SymbolicName: TestBundle
Bundle-Version: 1.0.0
Export-Package: "package-which-includes-my-service"
Servlet-Manifest:
Dependencies: org.osgi.core,org.jboss.osgi.framework,deployment.TestBundle:1.0.0
Import-Package: "package-which-includes-my-service"

Related

Servlet class is not a javax.servlet.Servlet while deploying a bundle in felix jetty

I am trying to deploy an OSGI bundle in felix jetty. BootStrap is my class which extends HttpServlet.
I am getting the below Exception at deployment stage :
ins.server.servlet.HttpServlet30Dispatcher is not assignable from javax.servlet.http.HttpServlet
2018-07-11T07:46:55,044 WARN sure-rest-neo [] web-reactor - unavailable
javax.servlet.UnavailableException: Servlet class com.nokia.mdf.sure.neo.utils.Bootstrap is not a javax.servlet.Servlet
at org.eclipse.jetty.servlet.ServletHolder.checkServletType(ServletHolder.java:519) ~[jetty-servlet-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:379) ~[jetty-servlet-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:874) ~[jetty-servlet-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:349) ~[jetty-servlet-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1404) ~[jetty-webapp-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1366) ~[jetty-webapp-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:778) ~[jetty-server-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:262) ~[jetty-servlet-9.3.10.v20160621.jar:9.3.10.v20160621]
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:520) ~[jetty-webapp-9.3.10.v20160621.jar:9.3.10.v20160621]
I have embedded all dependencies including the transitive ones. How to resolve this, which I assume is due to jar conflict ?
The problem is exactly with embedding dependencies. When you embed the javax.servlet package then your bundle will use the embedded class while jetty will use the class available from an exported package. So while these classes are named the same there are different instances in the classloaders which leads to exactly this kind of error.
To generally solve this you make sure that ideally only one bundle exports each package and all bundles that need it import the package. So the easiest solution is to not embed dependencies.
If that does not work for you then you can try to import and export the javax.servlet package in your bundle. This allows the OSGi environment to decide which package it will actually wire and avoid having the same class names with different instances in bundles.

Need to provide addtional jars to for xsbt-web-plugin container

I am trying to run my war file using xsbt-web-plugin. My war itself does not contain tomcat-jdbc-pool jar and javax-servlet-api. I tried using container configuration. But sbt complains configuration does not exist.
If I use provided configuration, webapp-runner fails.
I am sure I am missing something. But unable to figure it out.
WARNING: Failed to register in JMX: [javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory]]
What version of xsbt-web-plugin are you using? Can you post the contents of your sbt build configuration?
The Tomcat plugin in xsbt-web-plugin relies on webapp-runner, which does not appear to bundle the tomcat-jdbc library. You can add it via containerLibs in Tomcat:
enablePlugins(TomcatPlugin)
containerLibs in Tomcat += "org.apache.tomcat" % "tomcat-jdbc" % "8.5.15"

NoClassDefFoundError: org/jbox2d/collision/shapes/Shape in netbeans

I am trying to add jbox2d as a dependency to my project in netbeans (playn-jbox2d-1.1-SNAPSHOT.jar), but at run time, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jbox2d/collision/shapes/Shape
All other classes can be accessed fine, just not Shape.
I simply right clicked dependencies in my core directory and did add dependency and searched for jbox2d.
Anyone know what could be wrong?
What version of jbox2d do you have, as a dependency? You can crosscheck in the dependency jar file, if that class (Shape.class) exists or not?
EDIT: Most of the times, 'NoClassDefFoundError' happens due to incorrect classpath settings or when there's more than one class (or jar file) in the classpath, with the same namespace.

seam solder (former weld-extensions project) is not initialized

I want to use logger in my java web application.
I'm using JBossAS 6.0.0.final, cdi (weld), jsf ... etc. Seam solder proposes to use an abstract logger is not tying to a concrete implementation (slf4j, log4j, etc) using jboss-logging api.
In order to get this logger in your code will need to write
# Inject
org.jboss.logging.Logger log
seam-solder.jar has the producer for this logger.
package org.jboss.seam.solder.log;
...
class LoggerProducers
{
# Produces
org.jboss.logging.Logger produceLog (InjectionPoint injectionPoint) {}
}
When I deploying my application, I get an error
15:51:18,300 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] Error installing to Start: name=vfs:///C:/Java/jboss-6.0.0.Final/server/default/deploy/kamis-web-client.5.0.0-SNAPSHOT.ear_WeldBootstrapBean state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [#Default] at injection point [[field] #Inject private ru.kamis.suite.webclient.web.breadcrumbs.BreadcrumbsManager.log]
This is due to the seam-solder.jar has not META-INF/beans.xml file, and it is necessary for cdi container.
If to add beans.xml file in seam-solder.jar manually, then the application works WELL.
How to do without hacks?
To build my application I use maven, so my solution is not comfortable and NOT fine.
PS: Former weld-extensions project contained META-INF/beans.xml file in jar.
with seam-solder-3.0.0.Beta1 there should be no need to modify the jar

Where is class weblogic.jndi.WLInitialContextFactory?

when trying to execute my jar file I get an exception:
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory
[Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
I guess this is some kind of missing library on the classpath.
Can anyone tell me which jar-file is missing? I can't find the class weblogic.jndi.WLInitialContextFactory anywhere...
Thanks!
P.S.: I already have weblogic 10.0 jar included.
Check your server/lib/ folder to find wliclient.jar.
With Weblogic 12.1.3, you can find it here:
${INSTALL_DIR}/inventory/wlserver/server/lib/wlclient.jar
Step 1:
Go to E:\weblogic81\user_projects\domains\mydomain. Then type Setenv command. As follows
E:\weblogic81\user_projects\domains\mydomain>setenv
Step 2:
Weblogic.jar file is needed by your client application. It may contain in the following path E:\weblogic81\weblogic81\server\lib\weblogic.jar. so set the classpath for the this folder or copy this weblogic.jar file into your application-folder so that weblogic.jar file is available to your application first.
E:\weblogic81\user_projects\domains\mydomain>set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Step 3:
Go to domain folder in command prompt as shown above and set classpath.
To not to disturb other classpaths set classpath as:
set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Here (.) dot represents set classpath to current directory.
Step 4:
After classpath set run command STARTWEBLOGIC as follows:
E:\weblogic81\user_projects\domains\mydomain>STARTWEBLOGIC
Step 5:
Do not login to weblogic server. If you are already login just log out and write the following code in myeclipse or some other IDE.
Step 6:
package directory.service;
import java.util.*;
import weblogic.jndi.*;
import java.io.FileInputStream;
import javax.naming.*;
public class GetInitContext {
/**
* #param args
*/
public static void main(String[] args) {
try{
weblogic.jndi.Environment env=new weblogic.jndi.Environment();
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory(
weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
env.setProviderUrl("t3://localhost:7001");
env.setSecurityPrincipal("agni");
env.setSecurityCredentials("agnidevam");
Context context=env.getInitialContext();
System.out.println("got the initial context for weblogic server---> "+context);
context.createSubcontext("sone");
context.bind("agni one",new Integer(10));
context.createSubcontext("sone/sctwo");
context.bind("agni two",new Integer(20));
context.createSubcontext("sone/sctwo/scthree");
context.bind("agni three",new Integer(30));
System.out.println("subcontex object created please check in admin server for more details");
}
catch(Exception e){
System.out.println("file inputstream exception ---> "+e);
}
}
}
Step 7:
Execute the above code and login to weblogic and right click on myserver>view jndi tree> you find the bound objects information.
it looks you are doing a JNDI lookup outside of WLS.
You need to use wlfulclient.jar or if your machine has a WLS installation then add to your classpath project: WL_HOME/server/lib/weblogic.jar
I faced the same issue and it's fixed now :)
The fix is, to go to WebLogic server and navigate to /Oracle/Middleware/wlserver_10.3/server/lib/ and execute the below command.
Command: java -jar wljarbuilder.jar -profile wlfullclient5
The above command creates a jar file with all the jar's inside WebLogic server /lib folder and place it in your client java code build path Eclipse and craetes runnable JAR file and place this wlfullclient5.jar file in server/lib folder as well.
Hope this helps! Kindly let me know if you have any issues.
Adding wlserver/server/lib/weblogic.jar is enough. I test it.
Check the following tag in your build.xml
property name="WLS_HOME" value="${env.WLS_HOME}"
where WLS_HOME=c:\weblogic\wls\wlserver if running on windows
i kept trying to run a simple hello world program and it kept throwing
*run:
[echo] Executing client class
[java] javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]*
once i changed the above mentioned tag it in the build.xml it worked fine
It is packaged inside of the weblogic.jar under your server/lib.
in version 12c it is located in weblogic-classes.jar in your lib directory:
C:\wls1213\wlserver\server\lib
For WLS 12.2, where WL_HOME is The BEA home directory of your WebLogic installation
(as defualt WL_HOME is Middleware\Oracle_Home\wlserver)
%WL_HOME%\server\lib\wlclient.jar
%WL_HOME%\server\lib\wls-api.jar
%WL_HOME%\server\lib\wls-api-part.jar
%WL_HOME%\server\lib\wlthint3client.jar
all these libs contains the: jar: weblogic\jndi\WLInitialContextFactory.class
see WLS doc.: https://docs.oracle.com/en/middleware/fusion-middleware/weblogic-server/12.2.1.4/wlprg/overview.html#GUID-FC14CC53-DE49-456F-B54C-D73CC6DBF818
I've faced the issue stated here and I've managed to solved by fixing WL_HOME enviroment variable.
In my case the wlserver_10.3 folder was moved to another drive (From D to E) and the guy who did the disk "migration" forgot to change the WL_HOME value at PATH\TO\Oracle\Middleware\wlserver_10.3\common\bin
By fixing the wlserver_10.3 path I was able to deploy JAR's at WebLogic

Resources