Debian Codegraph @

src/java/org/astrogrid/samp/web/AuthResourceBundle.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.lang.reflect.Proxy;
7 import java.util.Collection;
8 import java.util.Enumeration;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.MissingResourceException;
12 import java.util.ResourceBundle;
13 import java.util.logging.Logger;
14
15 /**
16 * ResourceBundle for internationalising the Web Profile authorization
17 * dialogue.
18 *
19 * @author Mark Taylor
20 * @since 15 Jul 2011
21 */
22 public class AuthResourceBundle extends ResourceBundle {
23
24 private final Hashtable map_;
25 private static final Logger logger_ =
26 Logger.getLogger( AuthResourceBundle.class.getName() );
27
28 /**
29 * Constructs default (English) instance.
30 */
31 public AuthResourceBundle() {
32 this( getDefaultContent() );
33 }
34
35 /**
36 * Constructs a bundle based on a Content implementation.
37 *
38 * @param content contains information required for bundle
39 */
40 protected AuthResourceBundle( Content content ) {
41 map_ = new Hashtable();
42 Method[] methods = getContentMethods();
43 Object[] noArgs = new Object[ 0 ];
44 for ( int im = 0; im < methods.length; im++ ) {
45 Method method = methods[ im ];
46 String mname = method.getName();
47 try {
48 map_.put( method.getName(), method.invoke( content, noArgs ) );
49 }
50 catch ( IllegalAccessException e ) {
51 throw (RuntimeException)
52 new RuntimeException( "Failed to call method "
53 + method.getName() )
54 .initCause( e );
55 }
56 catch ( InvocationTargetException e ) {
57 throw (RuntimeException)
58 new RuntimeException( "Failed to call method "
59 + method.getName() )
60 .initCause( e );
61 }
62 }
63 checkHasAllKeys( this );
64 }
65
66 protected final Object handleGetObject( String key ) {
67 return map_.get( key );
68 }
69
70 public final Enumeration getKeys() {
71 return map_.keys();
72 }
73
74 /**
75 * Returns a Content object based on a bundle which has the keys
76 * that AuthResourceBundle is supposed to have.
77 * If any of the required keys are missing, the result falls back
78 * to a default bundle.
79 *
80 * @param bundle resource bundle
81 * @return content object guaranteed to have non-null contents for
82 * all its attributes
83 */
84 public static Content getAuthContent( final ResourceBundle bundle ) {
85 try {
86 checkHasAllKeys( bundle );
87 }
88 catch ( MissingResourceException e ) {
89 logger_.warning( "Some keys missing from localised auth resource "
90 + "bundle; using English" );
91 return getDefaultContent();
92 }
93 InvocationHandler ihandler = new InvocationHandler() {
94 public Object invoke( Object proxy, Method method,
95 Object[] args ) throws Throwable {
96 String key = method.getName();
97 Class rclazz = method.getReturnType();
98 if ( String.class.equals( rclazz ) ) {
99 return bundle.getString( key );
100 }
101 else {
102 throw new RuntimeException( "Unsuitable return type "
103 + rclazz.getName()
104 + " (shouldn't happen)" );
105 }
106 }
107 };
108 return (Content)
109 Proxy
110 .newProxyInstance( AuthResourceBundle.class.getClassLoader(),
111 new Class[] { Content.class }, ihandler );
112 }
113
114 /**
115 * Returns all the methods of the Content interface which correspond
116 * to AuthResourceBundle entries.
117 *
118 * @return resource bundle methods, all have no arguments and return String
119 */
120 static Method[] getContentMethods() {
121 return Content.class.getMethods();
122 }
123
124 /**
125 * Determines if a bundle has all the required keys for this class.
126 *
127 * @param bundle bundle to test
128 * @return true iff bundle has all required keys
129 */
130 static void checkHasAllKeys( ResourceBundle bundle ) {
131 Collection bkeys = new HashSet();
132 for ( Enumeration en = bundle.getKeys(); en.hasMoreElements(); ) {
133 bkeys.add( en.nextElement() );
134 }
135 Collection mnames = new HashSet();
136 Method[] methods = getContentMethods();
137 for ( int im = 0; im < methods.length; im++ ) {
138 mnames.add( methods[ im ].getName() );
139 }
140 mnames.removeAll( bkeys );
141 if ( ! mnames.isEmpty() ) {
142 throw new MissingResourceException(
143 "Missing resources " + mnames,
144 AuthResourceBundle.class.getName(),
145 mnames.iterator().next().toString() );
146 }
147 }
148
149 /**
150 * Returns a default Content implementation.
151 *
152 * @return english content
153 */
154 private static Content getDefaultContent() {
155 return new AuthResourceBundle_en.EnglishContent();
156 }
157
158 /**
159 * Returns a string suitable for entry into a .properties file
160 * for a given Method of a given Content object.
161 *
162 * @param content auth resource content
163 * @param method Content method (public String x())
164 */
165 private static String toPropertyString( Content content, Method method ) {
166 try {
167 String value = (String) method.invoke( content, new Object[ 0 ] );
168 value = value.replaceAll( "\n", "\\\\n" );
169 StringBuffer sbuf = new StringBuffer();
170 sbuf.append( method.getName() )
171 .append( '=' );
172 for ( int i = 0; i < value.length(); i++ ) {
173 char c = value.charAt( i );
174 if ( c == '\n' ) {
175 sbuf.append( "\\n" );
176 }
177 else if ( c >= 32 && c < 128 ) {
178 sbuf.append( c );
179 }
180 else {
181 String xs = Integer.toHexString( (int) c );
182 sbuf.append( "\\u" );
183 for ( int j = xs.length(); j < 4; j++ ) {
184 sbuf.append( '0' );
185 }
186 sbuf.append( xs );
187 }
188 }
189 return sbuf.toString();
190 }
191 catch ( IllegalAccessException e ) {
192 throw (RuntimeException)
193 new RuntimeException( "Failed to call method "
194 + method.getName() )
195 .initCause( e );
196 }
197 catch ( InvocationTargetException e ) {
198 throw (RuntimeException)
199 new RuntimeException( "Failed to call method "
200 + method.getName() )
201 .initCause( e );
202 }
203 }
204
205 /**
206 * Writes a template .properties file. Sensitive to the locale.
207 */
208 public static void main( String[] args ) {
209 ResourceBundle lBundle =
210 ResourceBundle.getBundle( AuthResourceBundle.class.getName() );
211 Content lContent = getAuthContent( lBundle );
212 Content dContent = getDefaultContent();
213 Method[] methods = getContentMethods();
214 System.out.println( "# Template for "
215 + AuthResourceBundle.class.getName()
216 + "_xx.properties file," );
217 System.out.println( "# giving localised text "
218 + "for Web Profile client authorization dialogue." );
219 System.out.println( "# Please fill in language-specific values for "
220 + "each key, as in the example." );
221 System.out.println( "# Follow the capitalization and punctuation "
222 + "of the English version." );
223 System.out.println( "# Long lines should be broken up with return "
224 + "characters (\\n)." );
225 System.out.println( "# Encoding is ISO 8859-1; "
226 + "see java.util.Properties docs for detailed "
227 + "syntax." );
228 System.out.println( "#" );
229 System.out.println( "# Alternatively, implement "
230 + AuthResourceBundle.class.getName() + "_xx" );
231 System.out.println( "# using " + AuthResourceBundle_en.class.getName()
232 + " as an example." );
233 System.out.println();
234 for ( int im = 0; im < methods.length; im++ ) {
235 Method method = methods[ im ];
236 System.out.println( "# " + toPropertyString( dContent, method ) );
237 System.out.println( toPropertyString( lContent, method ) );
238 }
239 }
240
241 /**
242 * Defines the keys and value types required for a bundle of this class.
243 * See the English language implementation in
244 * {@link AuthResourceBundle_en} for example text.
245 *
246 * <p>All methods have no arguments and return a String.
247 * The methods with names
248 * that end "<code>Lines</code>" should return text which contains
249 * line breaks (<code>\n</code> characters). Each such line will
250 * be displayed as it stands in the GUI, so it shouldn't be too long.
251 *
252 * <p>The method names define the keys which can be used if a
253 * property resource file is used to supply the content.
254 */
255 public static interface Content {
256
257 /**
258 * Returns the title for the confirmation window.
259 */
260 String windowTitle();
261
262 /**
263 * Returns lines introducing the registration request.
264 */
265 String appIntroductionLines();
266
267 /**
268 * Returns the word meaning "Name" (initial capitalised).
269 */
270 String nameWord();
271
272 /**
273 * Returns the word meaning "Origin" (initial capitalised).
274 */
275 String originWord();
276
277 /**
278 * Returns the word meaning "undeclared" (not capitalised).
279 */
280 String undeclaredWord();
281
282 /**
283 * Returns lines suitable explaining the privileges that a
284 * registered client will have.
285 */
286 String privilegeWarningLines();
287
288 /**
289 * Returns lines with advice on whether you should accept or decline.
290 */
291 String adviceLines();
292
293 /**
294 * Returns a line asking whether to authorize (yes/no).
295 */
296 String questionLine();
297
298 /**
299 * Returns the word meaning "Yes" (initial capitalised).
300 */
301 String yesWord();
302
303 /**
304 * Returns the word meaning "No" (initial capitalised).
305 */
306 String noWord();
307 }
308 }