This project has retired. For details please refer to its Attic page.
DefaultUserConfiguration xref
View Javadoc

1   package org.apache.archiva.redback.configuration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.redback.components.registry.Registry;
23  import org.apache.archiva.redback.components.registry.RegistryException;
24  import org.apache.archiva.redback.components.evaluator.DefaultExpressionEvaluator;
25  import org.apache.archiva.redback.components.evaluator.EvaluatorException;
26  import org.apache.archiva.redback.components.evaluator.ExpressionEvaluator;
27  import org.apache.archiva.redback.components.evaluator.sources.SystemPropertyExpressionSource;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.stereotype.Service;
31  
32  import javax.annotation.PostConstruct;
33  import javax.inject.Inject;
34  import javax.inject.Named;
35  import java.io.File;
36  import java.util.Collection;
37  import java.util.List;
38  
39  /**
40   * ConfigurationFactory
41   *
42   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
43   * @since 2.1
44   */
45  @Service("userConfiguration#redback")
46  public class DefaultUserConfiguration
47      implements UserConfiguration
48  {
49      private static final String DEFAULT_CONFIG_RESOURCE = "org/apache/archiva/redback/config-defaults.properties";
50  
51      protected Logger log = LoggerFactory.getLogger( getClass() );
52  
53      /**
54       * @deprecated Please configure the Redback registry instead
55       */
56      private List<String> configs;
57  
58      private Registry lookupRegistry;
59  
60      private static final String PREFIX = "org.apache.archiva.redback";
61  
62      @Inject
63      @Named(value = "commons-configuration")
64      private Registry registry;
65  
66      //TODO move this method call in the constructor
67  
68      @PostConstruct
69      public void initialize()
70          throws UserConfigurationException
71      {
72  
73          try
74          {
75              performLegacyInitialization();
76  
77              try
78              {
79                  registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX );
80              }
81              catch ( RegistryException e )
82              {
83                  // Ok, not found in context classloader; try the one in this jar.
84  
85                  ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
86                  try
87                  {
88  
89                      Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
90                      registry.addConfigurationFromResource( DEFAULT_CONFIG_RESOURCE, PREFIX );
91                  }
92                  finally
93                  {
94                      Thread.currentThread().setContextClassLoader( prevCl );
95                  }
96              }
97  
98              lookupRegistry = registry.getSubset( PREFIX );
99  
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( new File( 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 }