001package org.apache.archiva.redback.configuration; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.archiva.components.evaluator.DefaultExpressionEvaluator; 023import org.apache.archiva.components.evaluator.EvaluatorException; 024import org.apache.archiva.components.evaluator.ExpressionEvaluator; 025import org.apache.archiva.components.evaluator.sources.SystemPropertyExpressionSource; 026import org.apache.archiva.components.registry.Registry; 027import org.apache.archiva.components.registry.RegistryException; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030import org.springframework.stereotype.Service; 031 032import javax.annotation.PostConstruct; 033import javax.inject.Inject; 034import javax.inject.Named; 035import java.nio.file.Paths; 036import java.util.Collection; 037import java.util.List; 038 039/** 040 * ConfigurationFactory 041 * 042 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> 043 * @since 2.1 044 */ 045@Service("userConfiguration#redback") 046public class DefaultUserConfiguration 047 implements UserConfiguration 048{ 049 private static final String DEFAULT_CONFIG_RESOURCE = "org/apache/archiva/redback/config-defaults.properties"; 050 051 protected Logger log = LoggerFactory.getLogger( getClass() ); 052 053 /** 054 * @deprecated Please configure the Redback registry instead 055 */ 056 private List<String> configs; 057 058 private Registry lookupRegistry; 059 060 public static final String PREFIX = "org.apache.archiva.redback"; 061 062 @Inject 063 @Named(value = "commons-configuration") 064 private Registry registry; 065 066 //TODO move this method call in the constructor 067 068 @PostConstruct 069 public void initialize() 070 throws UserConfigurationException 071 { 072 073 try 074 { 075 performLegacyInitialization(); 076 077 try 078 { 079 registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX ); 080 } 081 catch ( RegistryException e ) 082 { 083 // Ok, not found in context classloader; try the one in this jar. 084 085 ClassLoader prevCl = Thread.currentThread().getContextClassLoader(); 086 try 087 { 088 089 Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ); 090 registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX ); 091 } 092 finally 093 { 094 Thread.currentThread().setContextClassLoader( prevCl ); 095 } 096 } 097 098 lookupRegistry = registry.getSubset( PREFIX ); 099 100 log.debug( "User configuration {}", lookupRegistry.dump() ); 101 } 102 catch ( RegistryException e ) 103 { 104 throw new UserConfigurationException( e.getMessage(), e ); 105 } 106 107 } 108 109 private void performLegacyInitialization() 110 throws RegistryException 111 { 112 ExpressionEvaluator evaluator = new DefaultExpressionEvaluator(); 113 evaluator.addExpressionSource( new SystemPropertyExpressionSource() ); 114 115 if ( configs != null ) 116 { 117 if ( !configs.isEmpty() ) 118 { 119 // TODO: plexus should be able to do this on it's own. 120 log.warn( 121 "DEPRECATED: the <configs> elements is deprecated. Please configure the Redback registry instead" ); 122 } 123 124 for ( String configName : configs ) 125 { 126 try 127 { 128 configName = evaluator.expand( configName ); 129 } 130 catch ( EvaluatorException e ) 131 { 132 log.warn( "Unable to resolve configuration name: {}", e.getMessage(), e ); 133 } 134 log.info( "Attempting to find configuration [{}] (resolved to [{}])", configName, configName ); 135 136 registry.addConfigurationFromFile(Paths.get(configName), configName, PREFIX ); 137 } 138 } 139 } 140 141 public String getString( String key ) 142 { 143 return lookupRegistry.getString( key ); 144 } 145 146 public String getString( String key, String defaultValue ) 147 { 148 String value = lookupRegistry.getString( key, defaultValue ); 149 return value; 150 } 151 152 public int getInt( String key ) 153 { 154 return lookupRegistry.getInt( key ); 155 } 156 157 public int getInt( String key, int defaultValue ) 158 { 159 return lookupRegistry.getInt( key, defaultValue ); 160 } 161 162 public boolean getBoolean( String key ) 163 { 164 return lookupRegistry.getBoolean( key ); 165 } 166 167 public boolean getBoolean( String key, boolean defaultValue ) 168 { 169 return lookupRegistry.getBoolean( key, defaultValue ); 170 } 171 172 @SuppressWarnings("unchecked") 173 public List<String> getList( String key ) 174 { 175 return lookupRegistry.getList( key ); 176 } 177 178 public String getConcatenatedList( String key, String defaultValue ) 179 { 180 List<String> list = getList( key ); 181 if ( !list.isEmpty() ) 182 { 183 StringBuilder s = new StringBuilder(); 184 for ( String value : list ) 185 { 186 if ( s.length() > 0 ) 187 { 188 s.append( "," ); 189 } 190 s.append( value ); 191 } 192 log.debug( "getList for key {} return {}", key, s.toString() ); 193 return s.toString(); 194 } 195 log.debug( "getList for key {} return {}", key, defaultValue ); 196 return defaultValue; 197 } 198 199 /** 200 * @return 201 * @deprecated 202 */ 203 public List<String> getConfigs() 204 { 205 return configs; 206 } 207 208 /** 209 * @param configs 210 * @deprecated 211 */ 212 public void setConfigs( List<String> configs ) 213 { 214 this.configs = configs; 215 } 216 217 public Registry getRegistry() 218 { 219 return registry; 220 } 221 222 public void setRegistry( Registry registry ) 223 { 224 this.registry = registry; 225 } 226 227 public Collection<String> getKeys() 228 { 229 return this.registry.getSubset( PREFIX ).getFullKeys(); 230 } 231}