Jetty websocket class loading issue - websocket

I have implemented a basic websocket server in Jetty(Standalone mode).
MyWebSocketServlet.java
public class MyWebSocketServlet extends WebSocketServlet {
#Override
public void configure(WebSocketServletFactory webSocketServletFactory){
webSocketServletFactory.getPolicy().setIdleTimeout(1000 * 10 * 60);
webSocketServletFactory.setCreator(new MyWebSocketFactory());
}
}
MyWebSocketFactory.java
public class MyWebSocketFactory implements WebSocketCreator {
public Object createWebSocket(
ServletUpgradeRequest servletUpgradeRequest
, ServletUpgradeResponse servletUpgradeResponse) {
return new MyWebSocketListener();
}
}
MyWebSocketListener.java
public class MyWebSocketListener implements WebSocketListener {
private Session sessionInstance;
public void onWebSocketBinary(byte[] bytes, int i, int i1) {
ByteBuffer data = ByteBuffer.wrap(bytes, i, i1);
try {
sessionInstance.getRemote().sendBytes(data);
} catch (IOException e) {
e.printStackTrace();
}
}
public void onWebSocketClose(int i, String s) {
}
public void onWebSocketConnect(Session session) {
sessionInstance = session;
}
public void onWebSocketError(Throwable throwable) {
throwable.printStackTrace(System.err);
}
public void onWebSocketText(String s) {
try {
sessionInstance.getRemote().sendString(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false"
version="3.1">
<servlet>
<servlet-name>WsEcho</servlet-name>
<servlet-class>org.test.sanket.MyWebSocketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WsEcho</servlet-name>
<url-pattern>/echo/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HttpEcho</servlet-name>
<servlet-class>org.test.sanket.MyHttpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HttpEcho</servlet-name>
<url-pattern>/httpecho/*</url-pattern>
</servlet-mapping>
</web-app>
Instead of using a Standalone Jetty if I use embedded jetty and programatically configure the server and add the Servlets then this sample runs fine.
But if I am packaging the same as a war, and then deploying the same in a standalone jetty instance I am having the following observation:
I am able to hit the HttpServlet , i.e. MyHttpServlet and receive a response
But when I try to hit the websocket servlet, i.e. MyWebSocketServlet, I am seeing the following error:
exception
java.lang.ClassCastException: org.eclipse.jetty.server.HttpConnection cannot be cast to org.eclipse.jetty.server.HttpConnection
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:175)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:148)
at org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:151)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:751)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:566)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:578)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
I did come across the following link:
Jetty - stand alone WebSocket server
From the above link it seems to be a class loading issue, because jetty websocket package is treated as system class package and shouldn't be loaded by the WebApp if already loaded by the system.
So as referenced in the above link, I looked into the details suggested at:
http://www.eclipse.org/jetty/documentation/9.2.10.v20150310/jetty-classloading.html
From this link, one of the ways to get around this issue is to call the org.eclipse.jetty.webapp.WebAppContext.setSystemClasses(String Array) or org.eclipse.jetty.webapp.WebAppContext.addSystemClass(String) to allow fine control over which classes are considered System classes.
So for being able to do that, I should be able to get an Instance of WebAppContext, when Jetty is initializing and add the WebSocket classes as system classes.
I tried searching for how one would be able to achieve the same but no luck so far ? Can anybody kindly point me to a reference implementation as to how this can be achieved ?
Java Version: OpenJDK 7(latest)
Jetty: 9.2.10.v20150310
Operating System: Ubuntu 14.04
Thanks in advance!

If you have followed this link to setup the Jetty Standalone Instance, then you might have run the following command:
[/opt/web/mybase]# java -jar /opt/jetty/jetty-distribution-9.2.10.v20150310/start.jar --add-to-start=deploy,http,logging
If so, then when you try to hit the websocket servlet you will see the exception that you are noticing.
All you need to do is, instead of that command, you as well need to initialize the websocket module as shown below:
[/opt/web/mybase]# java -jar /opt/jetty/jetty-distribution-9.2.10.v20150310/start.jar --add-to-start=deploy,http,logging,websocket
Hope this helps!

Don't include the org.eclipse.jetty.* classes in your war's WEB-INF/lib or WEB-INF/classes directories.

Related

Error Code 500 on Embedded Glassfish 4.1.1 with Arquillian

I am quite new to Java EE and Arquillian. I am getting problems if I want to deploy my simple project into the embedded glassfish 4.1.1 server. I can succesfully pass the deployment, but I can not access the methods that i implemented. I am getting error code 500, while i expect 200.
my deployment:
public class AuthTest
{
#Deployment(testable = false)
public static WebArchive createDeployment()
{
System.out.println("\nin createDeployment");
// File[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeAndTestDependencies().asFile();
WebArchive archive = ShrinkWrap.create(WebArchive.class, "ArquillianHepInterface.war")
.addClasses(AuthEndpoint.class)
.addPackage(Database.class.getPackage())
.addPackage(DeviceAccessDBEntry.class.getPackage())
.addPackage(WebsiteAccessDBEntry.class.getPackage())
.addPackage(DeviceInfo.class.getPackage())
.addPackage(LoginInfo.class.getPackage())
.addPackage(ResponseBoolean.class.getPackage())
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource(new File("src/main/webapp", "WEB-INF/web.xml"))
.addAsWebInfResource(new File("src/main/webapp", "WEB-INF/glassfish-web.xml"))
.addAsResource("META-INF/persistence.xml")
// .addAsLibraries(libs)
;
System.out.println("\nende createDeployment");
return archive;
}
#ArquillianResource
protected URL contextPath;
#Test
public void registerUUIDTest()
{
DeviceInfo newUser = new DeviceInfo();
newUser.setDate("2014-12-12");
newUser.setUuid("xyz");
Response i = expect().statusCode(200).when().given().
contentType("application/json").body(newUser)
.post(this.contextPath + "AUTHENTICATION/CREATEMOBILELOGIN")
;
}
my AuthEndpoint class:
#Path("/AUTHENTICATION")
public class AuthEndpoint
{
#Inject
private DatabaseInterface database;
#POST
#Produces({ "application/json" })
#Consumes({ "application/json" })
#Path("CREATEMOBILELOGIN")
public LoginInfo createMobileLogin(DeviceInfo info)
{
System.out.println("RICHTIGES FILE");
System.err.println();
LoginInfo response = null;
if (info != null && info.getUuid() != null)
{
String password = this.database.registerDevice(info.getUuid());
if (password != null)
{
response = new LoginInfo();
response.setPw(password);
response.setUuid(info.getUuid());
}
}
return response;
}
}
I am getting:
SEVERE: WebModule[/ArquillianHepInterface]StandardWrapper.Throwable
Why is that happening? I do not understand where my problem is...
I supposed that I have problems while I am using glassfish 4.1.1...? I want simply to call the Methods in the AuthEndpoint class throught the Arquillian TestClass to verify that a single have been created...
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Arquillian Rest Demo</display-name>
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>REST-Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>REST-Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
glassfish-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/ArquillianHepInterface</context-root>
<class-loader delegate="true" />
</glassfish-web-app>
My beans.xml file is empty.
The methods that AuthEndpoint uses are in another maven project that I have compiled and added in the maven dependencies...
I would appreciate any help...

Jetty - stand alone WebSocket server

in these days I try to implement a WebSocket server by using Jetty.
I've created a Jetty project called "WebSocketServer" as demo-base in the distribution of Jetty 9.2.0.v20140526.
After that, I write some codes to implement the WebSocket mechanism and export all codes to a war file to push it to the webapps folder of "WebSocketServer". When I java -jar ..<jetty.home>/start.jar, it is all workable. But, after I create a new connection to this WebSocket project, there are some error codes happened.
java.lang.ClassCastException: org.eclipse.jetty.server.HttpConnection cannot be cast to org.eclipse.jetty.server.HttpConnection
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:175)
at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:148)
at org.eclipse.jetty.websocket.servlet.WebSocketServlet.service(WebSocketServlet.java:151)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:751)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:566)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:578)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1111)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:498)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1045)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:199)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:98)
at org.eclipse.jetty.server.Server.handle(Server.java:461)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:284)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:244)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:534)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Thread.java:744)
I have no idea what's going on? The following codes are what I write to build a simple WebSocket server.
Servlet:
#SuppressWarnings("serial")
public class XYZWebSocketServlet extends WebSocketServlet{
#Override
public void configure(WebSocketServletFactory factory) {
factory.getPolicy().setIdleTimeout(600000);
factory.register(XYZWebSocketEvent.class);
}
}
Event:
#WebSocket
public class XYZWebSocketEvent {
private Session session;
#OnWebSocketConnect
public void onConnect(Session sess) {
session = sess;
// Get parameters while client connect to server
Map<String,List<String>> parameters = session.getUpgradeRequest().getParameterMap();
String encyptedID = parameters.get("ID").get(0);
System.out.println("Connect: " + session.getRemoteAddress().getPort());
try {
session.setIdleTimeout(600000);
session.getRemote().sendString("Hello!");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
#OnWebSocketMessage
public void onMessage(String message) {
try {
session.getRemote().sendString("Message: " + message);
}
catch (Exception ex) {
}
}
#OnWebSocketClose
public void onClose(int statusCode, String reason) {
try {
session.getRemote().sendString("Close: statusCode=" + statusCode + ", reason=" +reason);
}
catch (Exception ex) {
}
}
#OnWebSocketError
public void onError(Throwable t) {
System.out.println("Error: " + t.getMessage());
}
public Session getSession() {
return this.session;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>WebSocket application</display-name>
<servlet>
<servlet-name>XYZWebSocketServlet</servlet-name>
<servlet-class>com.newKinpo.servlet.XYZWebSocketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XYZWebSocketServlet</servlet-name>
<url-pattern>/events/*</url-pattern>
</servlet-mapping>
</web-app>
Is there something wrong? Thanks for your attention.
I had similiar problem and I have found the cause and the solution. Embeded jetty server is loaded by SUN class loader (will call it system class loader later) after webapp directory is scanned my app is loaded by WebApp class loader and when it comes to WebSocketServerFactory it is loaded by WebApp. However org.eclipse.jetty.server.HttpConnection object obtained from the request is loaded by the system class loader.
According to https://wiki.eclipse.org/Jetty/Reference/Jetty_Classloading jetty websocket package is treated as system class package and shouldn't be loaded by the WebApp if already loaded by the system.
The solution is to force loading of the package org.eclipse.jetty.websocket at the time jetty server is initialized.
I just create dummy instance of WebSocketHandler for example. There are many options to force package loading but they are irrelevant to this question.

Jetty 9 WebSocket Server Max Message Size on Session

I ran into this issue and had some difficulty finding answers for this anywhere so I thought I would enter it here for future programmers.
In Jetty 9, if you try to set the maximum message size on a session object to handle large data packets, it will not work. You will still get disconnected if your client tries to send large data. I'm talking about setMaximimumMessageSize on this object: http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html
Instead, what you have to do is set the max message size on the policy object acquired from the WebSocketServletFactory.
public final class MyWebSocketServlet extends WebSocketServlet
{
private static final long MAX_MESSAGE_SIZE = 1000000;
#Override
public void configure(WebSocketServletFactory factory)
{
factory.getPolicy().setMaxMessageSize(MAX_MESSAGE_SIZE);
factory.setCreator(new MyWebSocketCreator());
}
}
This will work as intended and your server will be able to handle large messages up to the maximum size you set.
The way you are setting the maximum message, in the WebSocketServlet is correct.
The Session.setMaximumMessageSize(long) as you pointed out in the javadoc is an unfortunately leaking of an early draft of JSR-356 (javax.websocket API) effort.
That method on the Jetty side API should not be there, and has been removed in Jetty 9.1
Bug has been filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=412439
Note: Jetty 9.1 will have the JSR-356 (javax.websocket API) support in it. Where the javax.websocket.Session has 2 methods of similar behavior.
javax.websocket.Session.setMaxBinaryMessageBufferSize(int)
javax.websocket.Session.setMaxTextMessageBufferSize(int)
I had this problem when sending files (binary data) with more than 64KB. I was using the javax.websocket-example from the Embedded Jetty WebSocket Examples.
Finally the only thing I need to do was to setMaxBinaryMessageBufferSize in the Session argument from the #OnOpen annotated method.
#ClientEndpoint
#ServerEndpoint(value = "/ws")
public class EventSocket {
#OnOpen
public void onWebSocketConnect(Session sess) {
sess.setMaxBinaryMessageBufferSize(1 * 1024 * 1024); // 1MB
}
#OnMessage
public void processUpload(byte[] b, boolean last, Session session) {
...
}
}
If anybody wants configurable alternative, setting servlet parameter maxTextMessageSize in web.xml also works -
<servlet>
<servlet-name>MyWebSocketServlet</servlet-name>
<servlet-class>test.MyWebSocketServlet</servlet-class>
<init-param>
<param-name>maxTextMessageSize</param-name>
<param-value>1048576</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyWebSocketServlet</servlet-name>
<url-pattern>/MyWebSocket/*</url-pattern>
</servlet-mapping>

Internalization of JSF project on WebSphere 7

I'm trying to implement loading Resource Bundles for JSF application from DB, following the sample: internationalization in JSF with ResourceBundle entries which are loaded from database
For the test I coded getItSomehow() just as create HashMap and fill it with key "hello_world" and value "["+locale+"]"+"hello world"
The sample works fine when I deploy it on Glassfish3.
But when I use WebSphere AS 7, the jsf page is displayed correctly only for the first time. Opening the jsf page in other browsers (with other prefered language selected) I receive the respond always in the locale of first run.
While debugging, I found the difference in implementation of ResourceBundle.java: Glassfish uses this class provided in rt.jar of the JDK1.6; but WebSphere has this class inside java.util.jar
The ResourceBundle (of WebSphere) called from ApplicationResourceBundle.getResourceBundle() calls handleGetBundle() and finally invokes my.i18n.DbResourceBundle$DBControl.newBundle() .
Called second (and further) time with different locale it doesn't invoke my override but just returns the same bundle created for first locale.
The question: is it possible to code internalizable jsf web-application deployed on WebSphere AS 7.0.07, not digging nor hacking into internals of the AS?
(environment: Windows XP, WebSphere AS 7.0.0.7, jdk1.6.0_24, jsf 2.1.4)
You can provide a specific implementation of ResourceBundle.
Here an example that gets the current locale each time JSF invokes the ResourceBundle methods:
package my.company.jsf.util;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
public class MyBundle extends ResourceBundle {
private static final Map<Locale, ResourceBundle> RB_CACHE = new HashMap<Locale, ResourceBundle>();
private static final String BUNDLE_NAME = "my-messages";
public MyBundle() {
}
#Override
public Enumeration<String> getKeys() {
ResourceBundle rb = getResourceBundle();
final Iterator<String> it = rb.keySet().iterator();
return new Enumeration<String>() {
#Override
public boolean hasMoreElements() {
return it.hasNext();
}
#Override
public String nextElement() {
return it.next();
}
};
}
#Override
protected Object handleGetObject(String key) {
ResourceBundle rb = getResourceBundle();
return rb.getObject(key);
}
private ResourceBundle getResourceBundle() {
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
ResourceBundle rb = RB_CACHE.get(locale);
if (rb == null) {
rb = ResourceBundle.getBundle(BUNDLE_NAME, locale);
RB_CACHE.put(locale, rb);
}
return rb;
}
}
and in your faces-config.xml put:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<resource-bundle>
<base-name>my.company.jsf.util.MyBundle</base-name>
<var>MSG</var>
</resource-bundle>
</application>
</faces-config>
We had your same problem and this solution worked for us with Windows Server 2008, WebSphere AS 7.0.0.19, jdk1.6.0_29, jsf 2.1.5

OSGi Declarative Services - NullPointer Exception

I have a problem with my Declarative Services. I have 2 bundles, one is a server provider and another the user interface that consumes the service.
On server side, the implementation is:
public boolean checkUser(){
return true;
}
And the XML file inside OSGi-INF folder:
<component name="ZBService">
<implementation class="service.ZBService" />
<service>
<provide interface="service.IZBService" />
</service>
</component>
On client side, the implementation is:
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService{
IZBService zb;
public void setZBService(IZBService eventAdmin) {
this.zb = eventAdmin;
}
public void unsetZBService(IZBService eventAdmin){
if(this.zb == eventAdmin){
this.zb = null;}
}
public boolean greetServer(String input, String input2) throws Exception {
return zb.checkUser();
}
}
And XML file:
<component name="ZBService">
<implementation class="main.java.com.gwt.app.server.GreetingServiceImpl" />
<service>
<provide interface="main.java.com.gwt.app.client.GreetingService"/>
</service>
<reference name="zb" interface="service.IZBService" bind="setZBService" unbind="unsetZBService" cardinality="0..n" policy="dynamic" />
</component>
Also, I have included the tag Service-Component on manifest file and I have deployed the equinox ds bundle that is ACTIVE.
The client is a GWT user interface, then I inject the service reference into server side of GWT. Well, when I deploy the application on Equinox it runs, but when I push the button, I launch an event to call ZBService. I have debugged the application and the error is zb attribute is null. It is to say, the dependence is nos injected. However the services are exposed on Equinox. If I write services on Equinox console, the services are deployed. Then, my conclusion is the error is due to the injection does not perform.
I would like to know if someone knows what is the reason??
Thanks a lot in advance!!
Nice day
EDIT:
I did your suggestions but it doesn't run. I change the component names and condinality/policy. The result is the same --> NullPointerException due to the injection isn't done.
Also I have debug the application to see if the methods bind and/or unbind are called, but they aren't.
The complete class is:
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService{
static protected IZBService zb;
public GreetingServiceImpl(){
System.out.println("Constructor GreetingServiceImpl");
}
public IZBService getZb() {
return zb;
}
public void setZb(IZBService zb) {
GreetingServiceImpl.zb = zb;
}
public void unsetZb(IZBService zb) {
GreetingServiceImpl.zb = zb;
}
#Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Cache the current thread
Thread currentThread = Thread.currentThread();
// We are going to swap the class loader
ClassLoader oldContextClassLoader = currentThread.getContextClassLoader();
currentThread.setContextClassLoader(this.getClass().getClassLoader());
super.service(req, resp);
currentThread.setContextClassLoader(oldContextClassLoader);
}
public void activate(ComponentContext context) {
System.out.println("Creating new greeter for " + context.getProperties().get("name")
+ ": " + context.getComponentInstance().toString());
}
public void activate() {
System.out.println("Activando la referencia al servicio");
}
public void deactivate(ComponentContext context) {
System.out.println("Deactivating greeter for " + context.getProperties().get("name")
+ ": " + context.getComponentInstance().toString());
}
public boolean greetServer(String input, String input2) throws Exception {
return zb.checkUser();
}
}
And the XML client is:
<?xml version="1.0" encoding="UTF-8" ?>
<scr:component name="serviceZB" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<implementation class="main.java.com.gwt.app.server.GreetingServiceImpl" />
<!-- <service>
<provide interface="main.java.com.gwt.app.client.GreetingService"/>
</service> -->
<reference name="zb" interface="service.IZBService"
bind="setZb" unbind="unsetZb" cardinality="1..1"
policy="static" />
</scr:component>
Why isn't the service injected if the service is deployed???
Here is a list of things you can try:
First, remove the "static" of zb, that could be the problem.
If you are using Equinox, add the -Dequinox.ds.print=true flag to the VM arguments and see more information about parsing XMLs and so
Of course, add sysouts to setZB and unsetZB :)
Remember that IZBService implementation needs a constructor without arguments
If you are using Equinox use the "list -c" command to obtain information of each component (it's cool because says exactly why a component is not registered).
Set the "inmediate=true" in XMLs to force to inmediatly activation.
You have both components with the same name, , which is kind of awkward when discussing them.
The reference on the client side has: cardinality="0..n" policy="dynamic". Which means it can be activated with zero to n references. Yet your code does not handle this. It seems to expect exactly one reference. Perhaps you should use cardinality="1..1" policy="static".

Resources