001package org.apache.archiva.redback.rest.services;
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.redback.rest.api.services.RedbackServiceException;
022import org.apache.archiva.redback.rest.api.services.UtilServices;
023import org.apache.commons.io.IOUtils;
024import org.apache.commons.lang3.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.springframework.stereotype.Service;
028
029import javax.annotation.PostConstruct;
030import java.io.IOException;
031import java.io.InputStream;
032import java.util.Map;
033import java.util.Properties;
034import java.util.concurrent.ConcurrentHashMap;
035
036/**
037 * @deprecated There is no replacement
038 * @author Olivier Lamy
039 * @since 1.4
040 */
041@Deprecated
042@Service( "utilServices#rest" )
043public class DefaultUtilServices
044    implements UtilServices
045{
046
047    private Logger log = LoggerFactory.getLogger( getClass() );
048
049    private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
050
051    @PostConstruct
052    public void init()
053        throws RedbackServiceException
054    {
055
056        // preload i18n en and fr
057        getI18nProperties( "en" );
058        getI18nProperties( "fr" );
059    }
060
061    public String getI18nResources( String locale )
062        throws RedbackServiceException
063    {
064        String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
065        if ( cachedi18n != null )
066        {
067            return cachedi18n;
068        }
069
070        Properties properties = new Properties();
071
072        // load redback user api messages
073        try
074        {
075
076            // load default first then requested locale
077            loadResource( properties, "org/apache/archiva/redback/users/messages", null );
078            loadResource( properties, "org/apache/archiva/redback/users/messages", locale );
079
080        }
081        catch ( IOException e )
082        {
083            log.warn( "skip error loading properties {}", "org/apache/archiva/redback/users/messages" );
084        }
085
086        try
087        {
088
089            // load default first then requested locale
090            loadResource( properties, "org/apache/archiva/redback/i18n/default", null );
091            loadResource( properties, "org/apache/archiva/redback/i18n/default", locale );
092
093        }
094        catch ( IOException e )
095        {
096            log.warn( "skip error loading properties {}", "org/apache/archiva/redback/i18n/default" );
097        }
098
099        StringBuilder output = new StringBuilder();
100
101        for ( Map.Entry<Object, Object> entry : properties.entrySet() )
102        {
103            output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
104            output.append( '\n' );
105        }
106
107        cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), output.toString() );
108
109        return output.toString();
110    }
111
112    public Properties getI18nProperties( String locale )
113        throws RedbackServiceException
114    {
115        try
116        {
117            Properties properties = new Properties();
118            // load default first then requested locale
119            loadResource( properties, "org/apache/archiva/redback/users/messages", null );
120            loadResource( properties, "org/apache/archiva/redback/users/messages", locale );
121
122            loadResource( properties, "org/apache/archiva/redback/i18n/default", null );
123            loadResource( properties, "org/apache/archiva/redback/i18n/default", locale );
124            return properties;
125        }
126        catch ( IOException e )
127        {
128            throw new RedbackServiceException( e.getMessage() );
129        }
130    }
131
132    private void loadResource( final Properties finalProperties, String resourceName, String locale )
133        throws IOException
134    {
135        InputStream is = null;
136        Properties properties = new Properties();
137        try
138        {
139            if ( StringUtils.isNotEmpty( locale ) )
140            {
141                resourceName = resourceName + "_" + locale;
142            }
143            resourceName = resourceName + ".properties";
144            is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName );
145            if ( is != null )
146            {
147                properties.load( is );
148                finalProperties.putAll( properties );
149            }
150            else
151            {
152                if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
153                {
154                    log.info( "cannot load resource {}", resourceName );
155                }
156            }
157        }
158        finally
159        {
160            IOUtils.closeQuietly( is );
161        }
162
163    }
164
165
166}