This project has retired. For details please refer to its
Attic page.
DefaultUtilServices xref
1 package org.apache.archiva.redback.rest.services;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
28
29 import javax.annotation.PostConstruct;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Map;
33 import java.util.Properties;
34 import java.util.concurrent.ConcurrentHashMap;
35
36
37
38
39
40 @Service( "utilServices#rest" )
41 public class DefaultUtilServices
42 implements UtilServices
43 {
44
45 private Logger log = LoggerFactory.getLogger( getClass() );
46
47 private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
48
49 @PostConstruct
50 public void init()
51 throws RedbackServiceException
52 {
53
54
55 getI18nProperties( "en" );
56 getI18nProperties( "fr" );
57 }
58
59 public String getI18nResources( String locale )
60 throws RedbackServiceException
61 {
62 String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
63 if ( cachedi18n != null )
64 {
65 return cachedi18n;
66 }
67
68 Properties properties = new Properties();
69
70
71 try
72 {
73
74
75 loadResource( properties, "org/apache/archiva/redback/users/messages", null );
76 loadResource( properties, "org/apache/archiva/redback/users/messages", locale );
77
78 }
79 catch ( IOException e )
80 {
81 log.warn( "skip error loading properties {}", "org/apache/archiva/redback/users/messages" );
82 }
83
84 try
85 {
86
87
88 loadResource( properties, "org/apache/archiva/redback/i18n/default", null );
89 loadResource( properties, "org/apache/archiva/redback/i18n/default", locale );
90
91 }
92 catch ( IOException e )
93 {
94 log.warn( "skip error loading properties {}", "org/apache/archiva/redback/i18n/default" );
95 }
96
97 StringBuilder output = new StringBuilder();
98
99 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
100 {
101 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
102 output.append( '\n' );
103 }
104
105 cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), output.toString() );
106
107 return output.toString();
108 }
109
110 public Properties getI18nProperties( String locale )
111 throws RedbackServiceException
112 {
113 try
114 {
115 Properties properties = new Properties();
116
117 loadResource( properties, "org/apache/archiva/redback/users/messages", null );
118 loadResource( properties, "org/apache/archiva/redback/users/messages", locale );
119
120 loadResource( properties, "org/apache/archiva/redback/i18n/default", null );
121 loadResource( properties, "org/apache/archiva/redback/i18n/default", locale );
122 return properties;
123 }
124 catch ( IOException e )
125 {
126 throw new RedbackServiceException( e.getMessage() );
127 }
128 }
129
130 private void loadResource( final Properties finalProperties, String resourceName, String locale )
131 throws IOException
132 {
133 InputStream is = null;
134 Properties properties = new Properties();
135 try
136 {
137 if ( StringUtils.isNotEmpty( locale ) )
138 {
139 resourceName = resourceName + "_" + locale;
140 }
141 resourceName = resourceName + ".properties";
142 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName );
143 if ( is != null )
144 {
145 properties.load( is );
146 finalProperties.putAll( properties );
147 }
148 else
149 {
150 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
151 {
152 log.info( "cannot load resource {}", resourceName );
153 }
154 }
155 }
156 finally
157 {
158 IOUtils.closeQuietly( is );
159 }
160
161 }
162
163
164 }