1 package org.codehaus.redback.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
24 import org.apache.archiva.redback.rest.api.services.UtilServices;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service;
29 import javax.annotation.PostConstruct;
30 import java.io.IOException;
31 import java.io.InputStream;
33 import java.util.Properties;
34 import java.util.concurrent.ConcurrentHashMap;
37 * @author Olivier Lamy
40 @Service( "utilServices#rest" )
41 public class DefaultUtilServices
42 implements UtilServices
45 private Logger log = LoggerFactory.getLogger( getClass() );
47 private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
51 throws RedbackServiceException
54 // preload i18n en and fr
55 getI18nProperties( "en" );
56 getI18nProperties( "fr" );
59 public String getI18nResources( String locale )
60 throws RedbackServiceException
62 String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
63 if ( cachedi18n != null )
68 Properties properties = new Properties();
70 // load redback user api messages
74 // load default first then requested locale
75 loadResource( properties, "org/codehaus/plexus/redback/users/messages", null );
76 loadResource( properties, "org/codehaus/plexus/redback/users/messages", locale );
79 catch ( IOException e )
81 log.warn( "skip error loading properties {}", "org/codehaus/plexus/redback/users/messages" );
87 // load default first then requested locale
88 loadResource( properties, "org/codehaus/redback/i18n/default", null );
89 loadResource( properties, "org/codehaus/redback/i18n/default", locale );
92 catch ( IOException e )
94 log.warn( "skip error loading properties {}", "org/codehaus/redback/i18n/default" );
97 StringBuilder output = new StringBuilder();
99 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
101 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
102 output.append( '\n' );
105 cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), output.toString() );
107 return output.toString();
110 public Properties getI18nProperties( String locale )
111 throws RedbackServiceException
115 Properties properties = new Properties();
116 // load default first then requested locale
117 loadResource( properties, "org/codehaus/plexus/redback/users/messages", null );
118 loadResource( properties, "org/codehaus/plexus/redback/users/messages", locale );
120 loadResource( properties, "org/codehaus/redback/i18n/default", null );
121 loadResource( properties, "org/codehaus/redback/i18n/default", locale );
124 catch ( IOException e )
126 throw new RedbackServiceException( e.getMessage() );
130 private void loadResource( final Properties finalProperties, String resourceName, String locale )
133 InputStream is = null;
134 Properties properties = new Properties();
137 if ( StringUtils.isNotEmpty( locale ) )
139 resourceName = resourceName + "_" + locale;
141 resourceName = resourceName + ".properties";
142 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
145 properties.load( is );
146 finalProperties.putAll( properties );
150 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
152 log.info( "cannot load resource {}", resourceName );
158 IOUtils.closeQuietly( is );