Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jellyfish/implementation/ConnectionManagerDefault.java


1   package org.jellyfish.implementation;
2   
3   import java.util.Properties;
4   
5   import javax.naming.Context;
6   import javax.naming.NamingException;
7   import javax.naming.directory.DirContext;
8   import javax.naming.directory.InitialDirContext;
9   
10  import org.apache.avalon.framework.logger.LogEnabled;
11  import org.apache.avalon.framework.logger.Logger;
12  import org.apache.avalon.framework.service.ServiceException;
13  import org.apache.avalon.framework.service.ServiceManager;
14  import org.apache.avalon.framework.service.Serviceable;
15  import org.jellyfish.ConfigurationManager;
16  import org.jellyfish.ConnectionManager;
17  
18  public class ConnectionManagerDefault
19    implements ConnectionManager, Serviceable, LogEnabled {
20    private static ConnectionManagerDefault instance;
21    private DirContext initialContext;
22    private ServiceManager serviceManager;
23    private Logger logger;
24    private ConfigurationManager configurationManager;
25  
26    public ConnectionManagerDefault() {
27    }
28  
29    public synchronized DirContext getContext() {
30      if (initialContext == null) {
31        try {
32          initialContext =
33            getContext(
34              configurationManager
35                .getConfiguration()
36                .getConnectionDn(),
37              configurationManager
38                .getConfiguration()
39                .getConnectionPassword());
40          logger.debug(
41            "Connected to "
42              + configurationManager
43                .getConfiguration()
44                .getConnectionUrl());
45        } catch (NamingException e) {
46          logger.fatalError(e.getMessage());
47          throw new RuntimeException("Could not connect.");
48        }
49      }
50      return initialContext;
51    }
52  
53    public synchronized DirContext getContext(String userName, String password)
54      throws NamingException {
55      Properties properties = new Properties();
56      properties.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
57      properties.setProperty(
58        Context.PROVIDER_URL,
59        configurationManager.getConfiguration().getConnectionUrl());
60      properties.setProperty(
61        Context.INITIAL_CONTEXT_FACTORY,
62        configurationManager.getConfiguration().getContextFactory());
63      properties.setProperty(Context.SECURITY_PRINCIPAL, userName);
64      properties.setProperty(Context.SECURITY_CREDENTIALS, password);
65      return createInitialContext(properties);
66    }
67  
68    protected DirContext createInitialContext(Properties properties)
69      throws NamingException {
70      return new InitialDirContext(properties);
71    }
72  
73    public void service(ServiceManager serviceManager) {
74      this.serviceManager = serviceManager;
75      try {
76        configurationManager =
77          (ConfigurationManager) serviceManager.lookup(
78            ConfigurationManager.ROLE);
79      } catch (ServiceException e) {
80        logger.fatalError(e.getMessage(), e);
81        throw new RuntimeException(e);
82      }
83    }
84  
85    public void enableLogging(Logger logger) {
86      this.logger = logger;
87    }
88  
89  }