package org.apache.archiva.admin.repository.runtime;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import net.sf.beanlib.provider.replicator.BeanReplicator;
import org.apache.archiva.admin.model.RepositoryAdminException;
import org.apache.archiva.admin.model.beans.ArchivaLdapConfiguration;
import org.apache.archiva.admin.model.beans.ArchivaRuntimeConfiguration;
import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin;
import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
import org.apache.archiva.configuration.Configuration;
import org.apache.archiva.configuration.IndeterminateConfigurationException;
import org.apache.archiva.configuration.RedbackRuntimeConfiguration;
import org.apache.archiva.redback.components.registry.RegistryException;
import org.apache.archiva.redback.configuration.UserConfiguration;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * @author Olivier Lamy
 * @since 1.4-M4
 */
@Service( "archivaRuntimeConfigurationAdmin#default" )
public class DefaultArchivaRuntimeConfigurationAdmin
    extends AbstractRepositoryAdmin
    implements ArchivaRuntimeConfigurationAdmin
{

    @Inject
    @Named( value = "userConfiguration" )
    UserConfiguration userConfiguration;

    @PostConstruct
    public void initialize()
        throws RepositoryAdminException
    {
        ArchivaRuntimeConfiguration archivaRuntimeConfiguration = getArchivaRuntimeConfiguration();
        // migrate or not data from redback
        if ( !archivaRuntimeConfiguration.isMigratedFromRedbackConfiguration() )
        {
            // so migrate if available
            String userManagerImpl = userConfiguration.getString( "user.manager.impl" );
            if ( StringUtils.isNotEmpty( userManagerImpl ) )
            {
                archivaRuntimeConfiguration.setUserManagerImpl( userManagerImpl );
            }

            // now ldap

            ArchivaLdapConfiguration archivaLdapConfiguration =
                archivaRuntimeConfiguration.getArchivaLdapConfiguration();
            if ( archivaLdapConfiguration == null )
            {
                archivaLdapConfiguration = new ArchivaLdapConfiguration();
                archivaRuntimeConfiguration.setArchivaLdapConfiguration( archivaLdapConfiguration );
            }

            archivaLdapConfiguration.setHostName( userConfiguration.getString( "ldap.config.hostname", null ) );
            archivaLdapConfiguration.setPort( userConfiguration.getInt( "ldap.config.port", -1 ) );
            archivaLdapConfiguration.setSsl( userConfiguration.getBoolean( "ldap.config.ssl", false ) );
            archivaLdapConfiguration.setBaseDn( userConfiguration.getConcatenatedList( "ldap.config.base.dn", null ) );
            archivaLdapConfiguration.setContextFactory(
                userConfiguration.getString( "ldap.config.context.factory", null ) );
            archivaLdapConfiguration.setBindDn( userConfiguration.getConcatenatedList( "ldap.config.bind.dn", null ) );
            archivaLdapConfiguration.setPassword( userConfiguration.getString( "ldap.config.password", null ) );
            archivaLdapConfiguration.setAuthenticationMethod(
                userConfiguration.getString( "ldap.config.authentication.method", null ) );

            archivaRuntimeConfiguration.setMigratedFromRedbackConfiguration( true );

            updateArchivaRuntimeConfiguration( archivaRuntimeConfiguration );
        }

    }

    public ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration()
        throws RepositoryAdminException
    {
        return build( getArchivaConfiguration().getConfiguration().getRedbackRuntimeConfiguration() );
    }

    public void updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
        throws RepositoryAdminException
    {
        RedbackRuntimeConfiguration runtimeConfiguration = build( archivaRuntimeConfiguration );
        Configuration configuration = getArchivaConfiguration().getConfiguration();
        configuration.setRedbackRuntimeConfiguration( runtimeConfiguration );
        try
        {
            getArchivaConfiguration().save( configuration );
        }
        catch ( RegistryException e )
        {
            throw new RepositoryAdminException( e.getMessage(), e );
        }
        catch ( IndeterminateConfigurationException e )
        {
            throw new RepositoryAdminException( e.getMessage(), e );
        }
    }

    private ArchivaRuntimeConfiguration build( RedbackRuntimeConfiguration runtimeConfiguration )
    {
        return new BeanReplicator().replicateBean( runtimeConfiguration, ArchivaRuntimeConfiguration.class );
    }

    private RedbackRuntimeConfiguration build( ArchivaRuntimeConfiguration archivaRuntimeConfiguration )
    {
        return new BeanReplicator().replicateBean( archivaRuntimeConfiguration, RedbackRuntimeConfiguration.class );
    }
}

@@ -1601,32 +1601,103 @@
 rConfiguration;%0A
+import org.apache.archiva.redback.configuration.UserConfigurationKeys;%0A
 import org.apach
@@ -1925,17 +1925,16 @@
 Service(
- 
 %22archiva
@@ -1967,17 +1967,16 @@
 default%22
- 
 )%0Apublic
@@ -2132,17 +2132,16 @@
  @Named(
- 
 value = 
@@ -2159,17 +2159,16 @@
 uration%22
- 
 )%0A    Us
@@ -2289,32 +2289,33 @@
 Exception%0A    %7B%0A
+%0A
         ArchivaR
@@ -2642,27 +2642,47 @@
 ng( 
-%22user.manager.impl%22
+UserConfigurationKeys.USER_MANAGER_IMPL
  );%0A
@@ -3324,16 +3324,32 @@
 ostName(
+%0A               
  userCon
@@ -3374,30 +3374,43 @@
 ng( 
-%22ldap.config.hostname%22
+UserConfigurationKeys.LDAP_HOSTNAME
 , nu
@@ -3493,26 +3493,39 @@
 nt( 
-%22ldap.config.port%22
+UserConfigurationKeys.LDAP_PORT
 , -1
@@ -3609,25 +3609,38 @@
 an( 
-%22ldap.config.ssl%22
+UserConfigurationKeys.LDAP_SSL
 , fa
@@ -3873,37 +3873,49 @@
 ng( 
-%22ldap.config.context.factory%22
+UserConfigurationKeys.LDAP_CONTEX_FACTORY
 , nu
@@ -4091,16 +4091,32 @@
 assword(
+%0A               
  userCon
@@ -4141,30 +4141,43 @@
 ng( 
-%22ldap.config.password%22
+UserConfigurationKeys.LDAP_PASSWORD
 , nu
@@ -4295,43 +4295,56 @@
 ng( 
-%22ldap.config.authentication.method%22
+UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD
 , nu

use constants keys from redback