Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » DI in a OSGI Service
DI in a OSGI Service [message #644097] Thu, 09 December 2010 13:38 Go to next message
Mike Moor is currently offline Mike Moor
Messages: 20
Registered: November 2010
Location: Steffisburg, Switzerland
Junior Member
Hi,

i have a LoginPart. As soon as the "login"-Button has been pressed, a LoginService is called. This Login-Service is a OSGI-Service an is injected into the LoginPart successfully.

In this LoginService i would like to have a reference to an other OSGI-Service (PasswordEncoder). So i tried it with the @Inject annotation too. In the console i can see, that the Service is available, but in my LoginService it isn't.

What i already read is, that if a Class is not part of the e4 Model, the @Inject Annotation doesn't do anything.

Is there a way to get this service programmatically?
Something like this:

public class LoginServiceImpl implements LoginService {

  @Inject
  private PasswordEncoder encoder; // <- available as an OSGI-Service

  public LoginServiceImpl() {
    // what should stand here???
    encoder = MagicClass.getService("encoder");
  }

}



thanx a lot!
mike
Re: DI in a OSGI Service [message #644108 is a reply to message #644097] Thu, 09 December 2010 14:45 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5344
Registered: July 2009
Senior Member
Using DS you can let your LoginService reference the PasswordEncoder
Service and so you don't need e4 DI.

Tom

Am 09.12.10 14:38, schrieb Mike Moor:
> Hi,
>
> i have a LoginPart. As soon as the "login"-Button has been pressed, a
> LoginService is called. This Login-Service is a OSGI-Service an is
> injected into the LoginPart successfully.
>
> In this LoginService i would like to have a reference to an other
> OSGI-Service (PasswordEncoder). So i tried it with the @Inject
> annotation too. In the console i can see, that the Service is available,
> but in my LoginService it isn't.
>
> What i already read is, that if a Class is not part of the e4 Model, the
> @Inject Annotation doesn't do anything.
>
> Is there a way to get this service programmatically?
> Something like this:
>
>
> public class LoginServiceImpl implements LoginService {
>
> @Inject
> private PasswordEncoder encoder; // <- available as an OSGI-Service
>
> public LoginServiceImpl() {
> // what should stand here???
> encoder = MagicClass.getService("encoder");
> }
>
> }
>
>
>
> thanx a lot!
> mike
Re: DI in a OSGI Service [message #644142 is a reply to message #644108] Thu, 09 December 2010 16:45 Go to previous messageGo to next message
Mike Moor is currently offline Mike Moor
Messages: 20
Registered: November 2010
Location: Steffisburg, Switzerland
Junior Member
this is right, but my services come from a spring configuration and are published as OSGI Service through the spring-osgi-extender. so i think i can't define them as reference? is there no possibility to get the service programmatically?
Re: DI in a OSGI Service [message #644169 is a reply to message #644142] Thu, 09 December 2010 17:40 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5344
Registered: July 2009
Senior Member
Sure always but really Spring does take care of those things I'm quite
sure I saw this.

Anyways you can always use pure OSGi but the bad thing is that your
services bundles then have a dependency on OSGi which I normally try to
avoid.

Programmatically you'd write (it's a head dumb so method names, ...
might not be 100% correct):

Bundle b = FrameworkUtil.getBundle(LoginServiceImpl.class);
BundleContext ctx = b.getBundleContext();
ServiceReference ref = ctx.getServiceReference(PasswordEncoder.class);
PasswordEncoder encoder = ctx.getService(ref);


Tom

Am 09.12.10 17:45, schrieb Mike Moor:
> this is right, but my services come from a spring configuration and are
> published as OSGI Service through the spring-osgi-extender. so i think i
> can't define them as reference? is there no possibility to get the
> service programmatically?
Re: DI in a OSGI Service [message #644188 is a reply to message #644169] Thu, 09 December 2010 19:33 Go to previous message
Mike Moor is currently offline Mike Moor
Messages: 20
Registered: November 2010
Location: Steffisburg, Switzerland
Junior Member
i tried to put the second service as a reference into the first service.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
			http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
	<bean id="loginService"
		class="test.impl.LoginServiceImpl">
		<property name="mdEncoder" ref="mdEncoder" />
		
	</bean>
	<osgi:service ref="loginService"
		interface="test.LoginService" />

	<bean id="mdEncoder"
		class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
		<constructor-arg name="strength" value="256" />
	</bean>

	<osgi:service ref="mdEncoder"
		interface="org.springframework.security.authentication.encoding.PasswordEncoder" />

</beans>



AND IT WORKS!

Thank you for your help!!!
mike
Previous Topic:Problems between 4.0 based target platform and 4.1 based target platform.
Next Topic:XWT log and exception handling
Goto Forum:
  


Current Time: Tue Sep 30 15:56:41 GMT 2014

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

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