Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » registering servlet using httpService via blueprint declaration in Virgo(registering servlet using httpService via blueprint declaration in Virgo)
registering servlet using httpService via blueprint declaration in Virgo [message #1387975] Wed, 25 June 2014 21:33 Go to next message
Johnson Abraham is currently offline Johnson AbrahamFriend
Messages: 3
Registered: November 2011
Junior Member
Hi,
Using Eclipse Virgo Tooling in eclipse juno, I have created a Bundle project which which uses JRE 7.0. runtime environment. It is not a web application bundle

I am trying to register a HttpServlet using Equinox HttpService using Gemini Blueprint and then deploy it in Virgo Jetty Server 3.6.0. Below I have provided the details of the project I had created along with the exception that I get. Not sure what is causing the class loading issue. How I can fix this issue ? I am new to OSGI and Virgo please can you provide me some details and possible solution to the issue.

Attached the log file.

thanks.
Johnson Abraham


///---------------------------------------SimpleBean-------------------------------------------------------

import java.util.logging.Level;
import java.util.logging.Logger;

import org.osgi.service.http.HttpService;

public class SimpleBean {

private static final String SERVLET_ALIAS = "/hellods";
private HttpService httpService;
private static final Logger _logger = Logger.getLogger(SimpleBean.class.getName());


public void setHttpService (HttpService httpService) {
this.httpService = httpService;

}

public void start(){
SimpleServlet rs = new SimpleServlet();
ClassLoader old = Thread.currentThread().getContextClassLoader();

try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
httpService.registerServlet(SERVLET_ALIAS,rs, null, null);
} catch (Exception se) {
se.printStackTrace();
_logger.log(Level.SEVERE, "SimpleBean unable to register servlet", se);
}finally{
Thread.currentThread().setContextClassLoader(old);
}
System.out.println ("SimpleServlet registered at /hellods");
}

public void stop(){
httpService.unregister(SERVLET_ALIAS);
httpService.unregister("/*");

}

}

///------------------------------------SimpleServlet----------------------------------------
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
resp.getWriter().write("Hello from the cloud!");
}
}


///////--------------------------------------MANIFEST.MF---------------------------------------------
Manifest-Version: 1.0
Bundle-Description: OSGI
Bundle-Name: Demo1
Bundle-SymbolicName: com.osgi.httpservice.bp.demo1
Bundle-Version: 1.0.0
Bundle-ManifestVersion: 2
Import-Package: javax.servlet;version="[2.6.0,2.6.0]",
javax.servlet.http;version="[2.6.0,2.6.0]",
org.osgi.service.device;version="[1.1.0,1.1.0]",
org.osgi.service.event;version="[1.3.0,1.3.0]",
org.osgi.service.http;version="[1.2.1,1.2.1]",
org.osgi.service.io;version="[1.0.0,1.0.0]",
org.osgi.service.metatype;version="[1.2.0,1.2.0]",
org.osgi.service.provisioning;version="[1.2.0,1.2.0]",
org.osgi.service.upnp;version="[1.2.0,1.2.0]",
org.osgi.service.useradmin;version="[1.1.0,1.1.0]",
org.osgi.service.wireadmin;version="[1.0.1,1.0.1]"
Bundle-Blueprint: OSGI-INF/blueprint/*.xml

//----------------------------OSGI-INF/blueprint/blueprint.xml---------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<blueprint>
<reference id="simpleService" interface="org.osgi.service.http.HttpService"/>

<bean id="simpleBean" class="com.osgi.httpservice.bp.demo1.SimpleBean" init-method="start" destroy-method="stop">
<property name="httpService" ref="simpleService"/>
</bean>

</blueprint>


I have included the following jar in the ${VIRGO_HOME}/repository/ext
org.eclipse.equinox.http.servlet
org.eclipse.equinox.http.jetty

I have created a plan to include the org.eclipse.osgi.services and the above bundle which has been added to the initialArtifacts property in org.eclipse.virgo.kernel.userregion.properties

<plan name="httpservice.blueprint" version="1.0.0" scoped="false" atomic="false">
<artifact type="bundle" name="javax.servlet" version="0.0.0"/>
<artifact type="bundle" name="org.eclipse.osgi.services" version="0.0.0"/>
<artifact type="bundle" name="org.eclipse.equinox.http.servlet" version="0.0.0"/>
<artifact type="bundle" name="org.eclipse.equinox.http.jetty" version="0.0.0"/>

</plan>

Changed the osgi http port to the folllowing.
and set the -Dorg.eclipse.osgi.service.http.port=9090.


After all the above change, when I deploy my bundle in VJS in eclipse IDE. I get the following exception

[2014-06-24 15:34:23.119] start-signalling-2 <DE0006E> Start failed for bundle 'com.osgi.httpservice.bp.demo1' version '1.0.0'. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleService': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: javax.servlet.ServletException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1441)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:581)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:60)
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:325)
at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290)
at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137)
at org.eclipse.virgo.kernel.agent.dm.ContextPropagatingTaskExecutor$2.run(ContextPropagatingTaskExecutor.java:95)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1156)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:626)
at java.lang.Thread.run(Thread.java:804)
Caused by: java.lang.NoClassDefFoundError: javax.servlet.ServletException
at java.lang.Class.getDeclaredMethodsImpl(Native Method)
at java.lang.Class.getDeclaredMethods(Class.java:783)
at org.springframework.aop.framework.JdkDynamicAopProxy.findDefinedEqualsAndHashCodeMethods(JdkDynamicAopProxy.java:127)
at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:116)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
at org.eclipse.gemini.blueprint.service.util.internal.aop.ProxyUtils.createProxy(ProxyUtils.java:68)
at org.eclipse.gemini.blueprint.service.util.internal.aop.ProxyUtils.createProxy(ProxyUtils.java:37)
at org.eclipse.gemini.blueprint.service.importer.support.AbstractServiceProxyCreator.createServiceProxy(AbstractServiceProxyCreator.java:105)
at org.eclipse.gemini.blueprint.service.importer.support.OsgiServiceProxyFactoryBean.createProxy(OsgiServiceProxyFactoryBean.java:181)
at org.eclipse.gemini.blueprint.service.importer.support.AbstractServiceImporterProxyFactoryBean.getObject(AbstractServiceImporterProxyFactoryBean.java:95)
at org.eclipse.gemini.blueprint.service.importer.support.OsgiServiceProxyFactoryBean.getObject(OsgiServiceProxyFactoryBean.java:125)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 15 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletException
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:455)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:707)
... 27 common frames omitted


Re: registering servlet using httpService via blueprint declaration in Virgo [message #1388549 is a reply to message #1387975] Thu, 26 June 2014 15:53 Go to previous messageGo to next message
Chris Frost is currently offline Chris FrostFriend
Messages: 230
Registered: January 2010
Location: Southampton, England
Senior Member

Hi Johnson,

My blog post about the HTTPService is a little old now and I suspect something has changed. Please be aware that Virgo already provides a HTTPService separate from Jetty and this may be causing some issues when you try and run two at one. The config for the built in one is in configuration/config.ini and includes 'org.osgi.service.http.port=8080'. I can't remember if the built in HTTPService is used in the user region with the Jetty distribution, I just had a quick look and it dosn't look like it. I can't see the equinox.httpservice bundle at all actually. This would mean the admin console is running on Jetty as a Web Bundle but you should still be aware that the config is still there.

The exception you posted isn't the real error, it's masking the actual error that needed to lookup the 'ServletError' class which then swallowed your error and spat out that ClassNotFound exception. I would try and simplify your example, start with a bundle that works then see if you can pull in the httpService without error and then finally try and use the HttpService. Also, try expanding the version ranges for javax.servlet in your manifest. javax.servlet;version="[2.6.0,3.1.0)", javax.servlet.http;version="[2.6.0,3.1.0)", ..." Virgo ships with Servlet 3.0, this could be why ServletException isn't getting found.

Hope that helps.
Chris.


------------------------------------------------
Chris Frost, Twitter @cgfrost
Springsource, a divison of VMware.
Re: registering servlet using httpService via blueprint declaration in Virgo [message #1388703 is a reply to message #1388549] Thu, 26 June 2014 20:52 Go to previous message
Johnson Abraham is currently offline Johnson AbrahamFriend
Messages: 3
Registered: November 2011
Junior Member
Hi Chris,
Thanks for the reply.
I tried simplifying my example by removing the Gemini blueprint dependency injection and introducing a ServiceTracker to consume the org.eclipse.osgi.service.httpService . It works fine.

Now that I was able to consume the org.eclipse.osgi.service.httpService using ServiceTracker. I tried using blueprint dependency injection as well. The servlet was registered in a separate context path to ensure there is no conflict.

Though the httpService is available I am not able to consume it using the blueprint definition. It still throws the exception reported in my previous email.

I extended the version range of javax.servlet and javax.servlet.http but it had no affect on the exception.

I repeated the example by replacing the blueprint dependency injection with component based dependency injection (i.e. OSGI declarative service) It works fine. I am able to use both the HttpServiceTracker as well as the OSGI declarative service on two different context path.


I have pasted my HttpServiceTracker code below.

I have also attached my example project as well.



thanks,
Johnson Abraham
//----------------------HttpServiceTracker----------------------------------
public class HttpServiceTracker extends ServiceTracker {

private static Logger _logger = Logger.getLogger(HttpServiceTracker.class.getName());

public HttpServiceTracker(final BundleContext context) {
super(context, HttpService.class.getName(), null);
_logger.info("Creating tracker");
}

@Override
public Object addingService(final ServiceReference reference) {
_logger.info("adding Service " + reference);
HttpService httpService = (HttpService) super.addingService(reference);
if (httpService == null)
return null;

try {
_logger.info("registering servlet");
_logger.info("Registering servlet at /hellods");

httpService.registerServlet("/hellods1",
new SimpleServlet(), null, null);
} catch (Exception e) {
_logger.log(Level.SEVERE, "Exception registering anotherhelloworldservlet", e);
}

return httpService;
}

@Override
public void removedService(final ServiceReference reference,
final Object service) {
HttpService httpService = (HttpService) service;

_logger.log(Level.INFO, "Unregistering /hellods");
httpService.unregister("/hellods1");

super.removedService(reference, service);
}
}

Previous Topic:new version of virgo
Next Topic:Virgo Tomcat and static web content context
Goto Forum:
  


Current Time: Thu Dec 18 00:56:40 GMT 2014

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

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