001package org.apache.archiva.admin.repository.runtime; 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.ArchivaRuntimeConfiguration; 023import org.apache.archiva.admin.model.beans.CacheConfiguration; 024import org.apache.archiva.admin.model.beans.FileLockConfiguration; 025import org.apache.archiva.admin.model.runtime.ArchivaRuntimeConfigurationAdmin; 026import org.apache.archiva.admin.repository.AbstractRepositoryAdmin; 027import org.apache.archiva.configuration.ArchivaConfiguration; 028import org.apache.archiva.configuration.Configuration; 029import org.apache.archiva.configuration.IndeterminateConfigurationException; 030import org.apache.archiva.redback.components.cache.Cache; 031import org.apache.archiva.redback.components.registry.RegistryException; 032import org.springframework.stereotype.Service; 033 034import javax.annotation.PostConstruct; 035import javax.inject.Inject; 036import javax.inject.Named; 037 038/** 039 * @author Olivier Lamy 040 * @since 1.4-M4 041 */ 042@Service( "archivaRuntimeConfigurationAdmin#default" ) 043public class DefaultArchivaRuntimeConfigurationAdmin 044 extends AbstractRepositoryAdmin 045 implements ArchivaRuntimeConfigurationAdmin 046{ 047 048 @Inject 049 private ArchivaConfiguration archivaConfiguration; 050 051 @Inject 052 @Named( value = "cache#url-failures-cache" ) 053 private Cache urlFailureCache; 054 055 @PostConstruct 056 public void initialize() 057 throws RepositoryAdminException 058 { 059 ArchivaRuntimeConfiguration archivaRuntimeConfiguration = getArchivaRuntimeConfiguration(); 060 061 boolean save = false; 062 063 // NPE free 064 if ( archivaRuntimeConfiguration.getFileLockConfiguration() == null ) 065 { 066 archivaRuntimeConfiguration.setFileLockConfiguration( new FileLockConfiguration() ); 067 } 068 069 // NPE free 070 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() == null ) 071 { 072 archivaRuntimeConfiguration.setUrlFailureCacheConfiguration( new CacheConfiguration() ); 073 } 074 075 // if -1 it means non initialized to take values from the spring bean 076 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() < 0 ) 077 { 078 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToIdleSeconds( 079 urlFailureCache.getTimeToIdleSeconds() ); 080 save = true; 081 082 } 083 urlFailureCache.setTimeToIdleSeconds( 084 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToIdleSeconds() ); 085 086 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() < 0 ) 087 { 088 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setTimeToLiveSeconds( 089 urlFailureCache.getTimeToLiveSeconds() ); 090 save = true; 091 092 } 093 urlFailureCache.setTimeToLiveSeconds( 094 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getTimeToLiveSeconds() ); 095 096 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() < 0 ) 097 { 098 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsInMemory( 099 urlFailureCache.getMaxElementsInMemory() ); 100 save = true; 101 } 102 urlFailureCache.setMaxElementsInMemory( 103 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsInMemory() ); 104 105 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() < 0 ) 106 { 107 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().setMaxElementsOnDisk( 108 urlFailureCache.getMaxElementsOnDisk() ); 109 save = true; 110 } 111 urlFailureCache.setMaxElementsOnDisk( 112 archivaRuntimeConfiguration.getUrlFailureCacheConfiguration().getMaxElementsOnDisk() ); 113 114 if ( save ) 115 { 116 updateArchivaRuntimeConfiguration( archivaRuntimeConfiguration ); 117 } 118 119 } 120 121 @Override 122 public ArchivaRuntimeConfiguration getArchivaRuntimeConfiguration() 123 throws RepositoryAdminException 124 { 125 return build( archivaConfiguration.getConfiguration().getArchivaRuntimeConfiguration() ); 126 } 127 128 @Override 129 public void updateArchivaRuntimeConfiguration( ArchivaRuntimeConfiguration archivaRuntimeConfiguration ) 130 throws RepositoryAdminException 131 { 132 Configuration configuration = archivaConfiguration.getConfiguration(); 133 configuration.setArchivaRuntimeConfiguration( build( archivaRuntimeConfiguration ) ); 134 try 135 { 136 archivaConfiguration.save( configuration ); 137 } 138 catch ( RegistryException e ) 139 { 140 throw new RepositoryAdminException( e.getMessage(), e ); 141 } 142 catch ( IndeterminateConfigurationException e ) 143 { 144 throw new RepositoryAdminException( e.getMessage(), e ); 145 } 146 } 147 148 protected ArchivaRuntimeConfiguration build( 149 org.apache.archiva.configuration.ArchivaRuntimeConfiguration archivaRuntimeConfiguration ) 150 { 151 if ( archivaRuntimeConfiguration == null ) 152 { 153 return new ArchivaRuntimeConfiguration(); 154 } 155 156 ArchivaRuntimeConfiguration res = 157 getModelMapper().map( archivaRuntimeConfiguration, ArchivaRuntimeConfiguration.class ); 158 159 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() != null ) 160 { 161 162 res.setUrlFailureCacheConfiguration( 163 getModelMapper().map( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration(), 164 CacheConfiguration.class ) ); 165 166 } 167 168 if ( archivaRuntimeConfiguration.getFileLockConfiguration() != null ) 169 { 170 res.setFileLockConfiguration( 171 getModelMapper().map( archivaRuntimeConfiguration.getFileLockConfiguration(), 172 FileLockConfiguration.class ) ); 173 } 174 175 return res; 176 } 177 178 protected org.apache.archiva.configuration.ArchivaRuntimeConfiguration build( 179 ArchivaRuntimeConfiguration archivaRuntimeConfiguration ) 180 { 181 if ( archivaRuntimeConfiguration == null ) 182 { 183 return new org.apache.archiva.configuration.ArchivaRuntimeConfiguration(); 184 } 185 186 org.apache.archiva.configuration.ArchivaRuntimeConfiguration res = 187 getModelMapper().map( archivaRuntimeConfiguration, 188 org.apache.archiva.configuration.ArchivaRuntimeConfiguration.class ); 189 190 if ( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration() != null ) 191 { 192 193 res.setUrlFailureCacheConfiguration( 194 getModelMapper().map( archivaRuntimeConfiguration.getUrlFailureCacheConfiguration(), 195 org.apache.archiva.configuration.CacheConfiguration.class ) ); 196 197 } 198 199 if ( archivaRuntimeConfiguration.getFileLockConfiguration() != null ) 200 { 201 res.setFileLockConfiguration( 202 getModelMapper().map( archivaRuntimeConfiguration.getFileLockConfiguration(), 203 org.apache.archiva.configuration.FileLockConfiguration.class ) ); 204 } 205 206 return res; 207 } 208} 209 210