001package org.apache.archiva.web.runtime.ldap; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.RepositoryAdminException; 022import org.apache.archiva.admin.model.beans.LdapConfiguration; 023import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin; 024import org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory; 025import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028import org.springframework.stereotype.Service; 029 030import javax.annotation.PostConstruct; 031import javax.inject.Inject; 032import javax.naming.InvalidNameException; 033import java.util.Map; 034import java.util.Properties; 035 036/** 037 * @author Olivier Lamy 038 * @since 1.4-M4 039 */ 040@Service( "ldapConnectionFactory#archiva" ) 041public class ArchivaLdapConnectionFactory 042 extends ConfigurableLdapConnectionFactory 043{ 044 045 private final Logger log = LoggerFactory.getLogger(ArchivaLdapConnectionFactory.class); 046 047 private boolean valid = false; 048 049 @Inject 050 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin; 051 052 private LdapConnectionConfiguration ldapConnectionConfiguration; 053 054 @PostConstruct 055 @Override 056 public void initialize() 057 { 058 try 059 { 060 LdapConfiguration ldapConfiguration = 061 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getLdapConfiguration(); 062 ldapConnectionConfiguration = new LdapConnectionConfiguration(); 063 ldapConnectionConfiguration.setHostname( ldapConfiguration.getHostName() ); 064 ldapConnectionConfiguration.setPort( ldapConfiguration.getPort() ); 065 ldapConnectionConfiguration.setSsl( ldapConfiguration.isSsl() ); 066 ldapConnectionConfiguration.setBaseDn( ldapConfiguration.getBaseDn() ); 067 ldapConnectionConfiguration.setContextFactory( ldapConfiguration.getContextFactory() ); 068 ldapConnectionConfiguration.setBindDn( ldapConfiguration.getBindDn() ); 069 ldapConnectionConfiguration.setPassword( ldapConfiguration.getPassword() ); 070 ldapConnectionConfiguration.setAuthenticationMethod( ldapConfiguration.getAuthenticationMethod() ); 071 ldapConnectionConfiguration.setExtraProperties( toProperties( ldapConfiguration.getExtraProperties() ) ); 072 valid=true; 073 } 074 catch ( InvalidNameException e ) 075 { 076 log.error("Error during initialization of LdapConnectionFactory "+e.getMessage(),e); 077 // throw new RuntimeException( "Error while initializing connection factory.", e ); 078 } 079 catch ( RepositoryAdminException e ) 080 { 081 throw new RuntimeException( "Error while initializing ldapConnectionConfiguration: " + e.getMessage(), e ); 082 } 083 } 084 085 private Properties toProperties( Map<String, String> map ) 086 { 087 Properties properties = new Properties(); 088 if ( map == null ) 089 { 090 return properties; 091 } 092 for ( Map.Entry<String, String> entry : map.entrySet() ) 093 { 094 properties.put( entry.getKey(), entry.getValue() ); 095 } 096 return properties; 097 } 098 099 @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}