Expand my Community achievements bar.

SOLVED

Open LDAP connection once for bundle

Avatar

Level 2

Hi there

I've encountered some problems with my bundle when it connects to the LDAP server. The LDAP server reached it's connection limit due to the amount of connections. This was because there were many Java Classes that opened a LDAP connection, and didn't close it again.

Now the simplest approach would be to close every connection after the query. What I'd like to do instead is, create one LDAP connection object which can be accessed through the different classes. The connection should be opened once the bundle (to be more specific, the service) is started and only be closed when the bundle is stopped.

While I can access my LDAP connection within other components, I'm having a hard time accessing it from a class which is not a service / component. Is there anyway I can access a provided service within a normal Java class?

This is the code for the service that provides the LDAP Object:

@Component(immediate=true,metatype=true) @Service(value=LDAPConnector.class) public class LDAPConnectorImp implements LDAPConnector  { private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class); private LDAPConnection ldapConnection; protected void activate(final ComponentContext compContext) throws Exception { ldapConnection = new LDAPConnection(Config.LDAPHost, Config.LDAPPort); ldapConnection.bind(Config.LDAPUser, Config.LDAPPassword); } protected void deactivate(final ComponentContext context) { this.ldapConnection.close(); } public LDAPConnection getLdapConnection() { return ldapConnection; } }

While I can use @Reference within other services or components, I cannot use the object within a non-component class.

Any help would be appreciated!

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

technically it's possible to reference services from non-service/component classes. But then you would need to do the wiring yourself, you cannot use SCR anymore. That's something I would try to avoid...

Jörg

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

Hi,

technically it's possible to reference services from non-service/component classes. But then you would need to do the wiring yourself, you cannot use SCR anymore. That's something I would try to avoid...

Jörg