This project has retired. For details please refer to its Attic page.
ArchivaLdapConnectionFactory xref
View Javadoc
1   package org.apache.archiva.web.runtime.ldap;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.admin.model.RepositoryAdminException;
22  import org.apache.archiva.admin.model.beans.LdapConfiguration;
23  import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
24  import org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory;
25  import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.stereotype.Service;
29  
30  import javax.annotation.PostConstruct;
31  import javax.inject.Inject;
32  import javax.naming.InvalidNameException;
33  import java.util.Map;
34  import java.util.Properties;
35  
36  /**
37   * @author Olivier Lamy
38   * @since 1.4-M4
39   */
40  @Service( "ldapConnectionFactory#archiva" )
41  public class ArchivaLdapConnectionFactory
42      extends ConfigurableLdapConnectionFactory
43  {
44  
45      private final Logger log = LoggerFactory.getLogger(ArchivaLdapConnectionFactory.class);
46  
47      private boolean valid = false;
48  
49      @Inject
50      private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
51  
52      private LdapConnectionConfiguration ldapConnectionConfiguration;
53  
54      @PostConstruct
55      @Override
56      public void initialize()
57      {
58          try
59          {
60              LdapConfiguration ldapConfiguration =
61                  redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getLdapConfiguration();
62              ldapConnectionConfiguration = new LdapConnectionConfiguration();
63              ldapConnectionConfiguration.setHostname( ldapConfiguration.getHostName() );
64              ldapConnectionConfiguration.setPort( ldapConfiguration.getPort() );
65              ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() );
66              ldapConnectionConfiguration.setBaseDn( ldapConfiguration.getBaseDn() );
67              ldapConnectionConfiguration.setContextFactory( ldapConfiguration.getContextFactory() );
68              ldapConnectionConfiguration.setBindDn( ldapConfiguration.getBindDn() );
69              ldapConnectionConfiguration.setPassword( ldapConfiguration.getPassword() );
70              ldapConnectionConfiguration.setAuthenticationMethod( ldapConfiguration.getAuthenticationMethod() );
71              ldapConnectionConfiguration.setExtraProperties( toProperties( ldapConfiguration.getExtraProperties() ) );
72              valid=true;
73          }
74          catch ( InvalidNameException e )
75          {
76              log.error("Error during initialization of LdapConnectionFactory "+e.getMessage(),e);
77              // throw new RuntimeException( "Error while initializing connection factory.", e );
78          }
79          catch ( RepositoryAdminException e )
80          {
81              throw new RuntimeException( "Error while initializing ldapConnectionConfiguration: " + e.getMessage(), e );
82          }
83      }
84  
85      private Properties toProperties( Map<String, String> map )
86      {
87          Properties properties = new Properties();
88          if ( map == null )
89          {
90              return properties;
91          }
92          for ( Map.Entry<String, String> entry : map.entrySet() )
93          {
94              properties.put( entry.getKey(), entry.getValue() );
95          }
96          return properties;
97      }
98  
99      @Override
100     public LdapConnectionConfiguration getLdapConnectionConfiguration()
101     {
102         return this.ldapConnectionConfiguration;
103     }
104 
105     @Override
106     public void setLdapConnectionConfiguration( LdapConnectionConfiguration ldapConnectionConfiguration )
107     {
108         this.ldapConnectionConfiguration = ldapConnectionConfiguration;
109     }
110 
111     @Override
112     public boolean isValid() {
113         return valid;
114     }
115 }