http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/messages/loggers/test/TestMessageRecord.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/messages/loggers/test/TestMessageRecord.java b/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/messages/loggers/test/TestMessageRecord.java deleted file mode 100644 index 46edf98..0000000 --- a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/messages/loggers/test/TestMessageRecord.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.messages.loggers.test; - -import org.apache.hadoop.gateway.i18n.messages.MessageLevel; - -/** - * - */ -public class TestMessageRecord { - - private final TestMessageLogger logger; - private final StackTraceElement caller; - private final MessageLevel level; - private final String id; - private final String message; - private final Throwable throwable; - - public TestMessageRecord( TestMessageLogger logger, StackTraceElement caller, MessageLevel level, String id, String message, Throwable throwable ) { - this.logger = logger; - this.caller = caller; - this.level = level; - this.id = id; - this.message = message; - this.throwable = throwable; - } - - public TestMessageLogger getLogger() { - return logger; - } - - public StackTraceElement getCaller() { - return caller; - } - - public MessageLevel getLevel() { - return level; - } - - public String getId() { - return id; - } - - public String getMessage() { - return message; - } - - public Throwable getThrowable() { - return throwable; - } - -}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resource.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resource.java b/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resource.java deleted file mode 100644 index 727b385..0000000 --- a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resource.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * - */ -@Retention( RetentionPolicy.RUNTIME ) -@Target( ElementType.METHOD ) -public @interface Resource { - static final String DEFAULT_TEXT = "{}"; - String text() default DEFAULT_TEXT; -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resources.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resources.java b/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resources.java deleted file mode 100644 index 2762dd5..0000000 --- a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/Resources.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * - */ -@Retention( RetentionPolicy.RUNTIME ) -@Target( ElementType.TYPE ) -public @interface Resources { - static final String DEFAULT_BUNDLE = ""; - String bundle() default DEFAULT_BUNDLE; -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesFactory.java b/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesFactory.java deleted file mode 100644 index b98adc5..0000000 --- a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesFactory.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -import java.lang.reflect.Proxy; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * - */ -public class ResourcesFactory { - - private static Map<Class<?>, Object> proxies = new ConcurrentHashMap<Class<?>, Object>(); - - @SuppressWarnings( "unchecked" ) - public static <T> T get( Class<T> clazz ) { - Object proxy = proxies.get( clazz ); - if( proxy == null ) { - Resources anno = clazz.getAnnotation( Resources.class ); - if( anno == null ) { - throw new IllegalArgumentException( clazz.getName() + " missing @" + Resources.class.getCanonicalName() ); - } - ResourcesInvoker invoker = new ResourcesInvoker( clazz ); - proxy = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{ clazz }, invoker ); - proxies.put( clazz, proxy ); - } - return (T)proxy; - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesInvoker.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesInvoker.java b/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesInvoker.java deleted file mode 100644 index 865f8db..0000000 --- a/gateway-i18n/src/main/java/org/apache/hadoop/gateway/i18n/resources/ResourcesInvoker.java +++ /dev/null @@ -1,162 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.text.MessageFormat; -import java.util.ListResourceBundle; -import java.util.Locale; -import java.util.MissingResourceException; -import java.util.ResourceBundle; -import java.util.concurrent.ConcurrentHashMap; - -/** - * - */ -public class ResourcesInvoker implements InvocationHandler { - - private static ResourceBundle MISSING_BUNDLE = new ListResourceBundle() { - @Override - protected Object[][] getContents() { - return null; - } - }; - - private Class bundleClass; - private String bundleName; - private ConcurrentHashMap<Locale, ResourceBundle> bundles; - - public ResourcesInvoker( Class<?> bundleClass ) { - this.bundleClass = bundleClass; - this.bundleName = calcBundleName( bundleClass ); - this.bundles = new ConcurrentHashMap<Locale, ResourceBundle>(); - } - - @Override - public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { - return getText( method, args ); - } - - protected final String getText( final Method method, final Object[] args ) { - String pattern = getPattern( method ); - String text = MessageFormat.format( pattern, args ); - return text; - } - - protected final String getPattern( final Method method ) { - String pattern = getBundlePattern( method ); - if( pattern == null ) { - pattern = getAnnotationPattern( method ); - if( pattern == null || Resource.DEFAULT_TEXT.equals( pattern ) ) { - pattern = getDefaultPattern( method ); - } - } - return pattern; - } - - protected String getAnnotationPattern( final Method method ) { - String pattern = null; - final Resource anno = method.getAnnotation( Resource.class ); - if( anno != null ) { - pattern = anno.text(); - } - return pattern; - } - - protected final String getBundlePattern( final Method method ) { - String pattern = null; - final ResourceBundle bundle = findBundle(); - if( bundle != null && bundle.containsKey( method.getName() ) ) { - pattern = bundle.getString( method.getName() ); - } - return pattern; - } - - protected static final String getDefaultPattern( final Method method ) { - final String prefix = method.getName(); - String suffix; - final int params = method.getParameterTypes().length; - switch( params ) { - case( 0 ) : suffix = ""; break; - case( 1 ) : suffix = "(\"{0}\")"; break; - case( 2 ) : suffix = "(\"{0}\",\"{1}\")"; break; - case( 3 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\")"; break; - case( 4 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\")"; break; - case( 5 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\")"; break; - case( 6 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\")"; break; - case( 7 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\")"; break; - case( 8 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\")"; break; - case( 9 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\")"; break; - case( 10 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\")"; break; - default : suffix = createDefaultPatternSuffix( params ); - } - return prefix + suffix; - } - - private static final String createDefaultPatternSuffix( final int size ) { - StringBuilder builder = new StringBuilder( 1 + size*7 ); - builder.append( "(" ); - for( int i=0; i<size; i++ ) { - if( i>0 ) { - builder.append( "," ); - } - builder.append( "\"{" ).append( i ).append( "}\"" ); - } - builder.append( ")" ); - return builder.toString(); - - } - - private static final String calcBundleName( final Class<?> clazz ) { - String bundle = null; - final Resources anno = clazz.getAnnotation( Resources.class ); - if( anno != null ) { - bundle = anno.bundle(); - if( Resources.DEFAULT_BUNDLE.equals( bundle ) ) { - bundle = null; - } - } - if( bundle == null ) { - bundle = clazz.getCanonicalName().replace( '.', '/' ); - } - return bundle; - } - - - protected String getBundleName() { - return bundleName; - } - - protected final ResourceBundle findBundle() { - final Locale locale = Locale.getDefault(); - ResourceBundle bundle = bundles.get( locale ); - if( bundle == MISSING_BUNDLE ) { - bundle = null; - } else if ( bundle == null ) { - try { - bundle = ResourceBundle.getBundle( getBundleName(), locale, bundleClass.getClassLoader() ); - bundles.put( locale, bundle ); - } catch( MissingResourceException e ) { - bundles.put( locale, MISSING_BUNDLE ); - } - } - return bundle; - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Message.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Message.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Message.java new file mode 100644 index 0000000..68761ac --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Message.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.METHOD ) +public @interface Message { + static final String DEFAULT_TEXT = "{}"; + static final int DEFAULT_CODE = -1; + MessageLevel level() default MessageLevel.INFO; + int code() default DEFAULT_CODE; + String text() default DEFAULT_TEXT; +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLevel.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLevel.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLevel.java new file mode 100644 index 0000000..38058de --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLevel.java @@ -0,0 +1,25 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +/** + * + */ +public enum MessageLevel { + FATAL, ERROR, WARN, INFO, DEBUG, TRACE +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLogger.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLogger.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLogger.java new file mode 100644 index 0000000..851ec80 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLogger.java @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +/** + * + */ +public interface MessageLogger { + + boolean isLoggable( MessageLevel level ); + + void log( StackTraceElement caller, MessageLevel messageLevel, String messageId, String messageText, Throwable thrown ); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLoggerFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLoggerFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLoggerFactory.java new file mode 100644 index 0000000..3d87360 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessageLoggerFactory.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +/** + * + */ +public interface MessageLoggerFactory { + + MessageLogger getLogger( String name ); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Messages.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Messages.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Messages.java new file mode 100644 index 0000000..041da6d --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/Messages.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.TYPE ) +public @interface Messages { + static final String DEFAULT_BUNDLE = ""; + static final String DEFAULT_LOGGER = ""; + static final String DEFAULT_CODES = "{0}"; + String bundle() default DEFAULT_BUNDLE; + String logger() default DEFAULT_LOGGER; + String codes() default DEFAULT_CODES; +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesFactory.java new file mode 100644 index 0000000..26a48e9 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesFactory.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import org.apache.knox.gateway.i18n.messages.loggers.jdk.JdkMessageLoggerFactory; + +import java.lang.reflect.Proxy; +import java.util.Iterator; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + */ +public class MessagesFactory { + + private static MessageLoggerFactory loggers = getMessageLoggerFactory(); + private static Map<Class<?>, Object> proxies = new ConcurrentHashMap<Class<?>, Object>(); + + @SuppressWarnings( "unchecked" ) + public static <T> T get( Class<T> clazz ) { + Object proxy = proxies.get( clazz ); + if( proxy == null ) { + Messages anno = clazz.getAnnotation( Messages.class ); + if( anno == null ) { + throw new IllegalArgumentException( clazz.getName() + " missing @" + Messages.class.getCanonicalName() ); + } + MessagesInvoker invoker = new MessagesInvoker( clazz, loggers ); + proxy = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{ clazz }, invoker ); + proxies.put( clazz, proxy ); + } + return (T)proxy; + } + + private static MessageLoggerFactory getMessageLoggerFactory() { + MessageLoggerFactory factory; + ServiceLoader<MessageLoggerFactory> loader = ServiceLoader.load( MessageLoggerFactory.class ); + Iterator<MessageLoggerFactory> factories = loader.iterator(); + if( factories != null && factories.hasNext() ) { + factory = loader.iterator().next(); + } else { + factory = new JdkMessageLoggerFactory(); + } + return factory; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesInvoker.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesInvoker.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesInvoker.java new file mode 100644 index 0000000..87e3845 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/MessagesInvoker.java @@ -0,0 +1,173 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import org.apache.knox.gateway.i18n.resources.ResourcesInvoker; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.text.MessageFormat; + +/** + * + */ +public class MessagesInvoker extends ResourcesInvoker implements InvocationHandler { + + private String codes; + private MessageLogger logger; + private String bundle; + + public MessagesInvoker( Class<?> clazz, MessageLoggerFactory loggers ) { + super( clazz ); + Messages anno = clazz.getAnnotation( Messages.class ); + codes = calcCodePattern( clazz, anno ); + bundle = calcBundleName( clazz, anno ); + logger = getLogger( clazz, anno, loggers ); + } + + @Override + public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { + String message = null; + MessageLevel level = getLevel( method ); + if( logger.isLoggable( level ) ) { + message = getText( method, args ); + String code = getCode( method ); + Throwable throwable = findLoggableThrowable( logger, method, args ); + // Supposedly this Throwable way is faster than the Thread way. + // From looking at the JRE code it looks this is probably the case. + // The second version ends up calling the first version after getting the current thread + // and then checking that it is being called from the current thread. + //TODO: Should probably only do this if the logger is at debug or finer to make sure there isn't a performance impact during production usage. + StackTraceElement caller = new Throwable().getStackTrace()[2]; + // StackTraceElement caller = Thread.currentThread().getStackTrace()[3]; + logger.log( caller, level, code, message, throwable ); + } + return message; + } + + private final String getCode( final Method method ) { + String code = null; + Message anno = method.getAnnotation( Message.class ); + if( anno != null ) { + int num = anno.code(); + if( Message.DEFAULT_CODE != num ) { + code = MessageFormat.format( codes, num ); + } + } + return code; + } + + private static final StackTrace getStackTraceAnno( final Method method, final int param ) { + final Annotation[] annos = method.getParameterAnnotations()[ param ]; + for( Annotation anno: annos ) { + if( anno instanceof StackTrace ) { + return (StackTrace)anno; + } + } + return null; + } + + private static final Throwable findLoggableThrowable( final MessageLogger logger, final Method method, final Object[] args ) { + Throwable throwable = null; + if( args != null ) { + for( int i=0; i<args.length; i++ ) { + Object arg = args[i]; + if( arg instanceof Throwable ) { + StackTrace anno = getStackTraceAnno( method, i ); + if( anno != null ) { + if( logger.isLoggable( anno.level() ) ) { + throwable = (Throwable)arg; + break; + } + } + } + } + } + return throwable; + } + + protected String getAnnotationPattern( final Method method ) { + String pattern = null; + Message anno = method.getAnnotation( Message.class ); + if( anno != null ) { + pattern = anno.text(); + } + return pattern; + } + + private static final MessageLevel getLevel( final Method method ) { + MessageLevel level; + Message anno = method.getAnnotation( Message.class ); + if( anno == null ) { + level = MessageLevel.INFO; + } else { + level = anno.level(); + } + return level; + } + + private static String calcCodePattern( final Class<?> clazz, final Messages anno ) { + String pattern = anno.codes(); + if( Messages.DEFAULT_CODES.equals( pattern ) ) { + pattern = clazz.getCanonicalName().replace( '.', '/' ); + } + return pattern; + } + + private static String calcBundleName( Class<?> clazz, Messages anno ) { + String bundle = null; + if( anno != null ) { + bundle = anno.bundle(); + if( Messages.DEFAULT_BUNDLE.equals( bundle ) ) { + bundle = null; + } + } + if( bundle == null ) { + bundle = clazz.getCanonicalName().replace( '.', '/' ); + } + return bundle; + } + + private static final String calcLoggerName( final Class<?> clazz, final Messages anno ) { + String logger = null; + if( anno != null ) { + logger = anno.logger(); + if( Messages.DEFAULT_LOGGER.equals( logger ) ) { + logger = null; + } + } + if( logger == null ) { + logger = clazz.getCanonicalName(); + } + return logger; + } + + protected String getBundleName() { + return bundle; + } + + private static final MessageLogger getLogger( final Class<?> clazz, final Messages anno, final MessageLoggerFactory loggers ) { + return loggers.getLogger( calcLoggerName( clazz, anno ) ); + } + + public String toString() { + return "MessageInvoker["+bundle+"]"; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/StackTrace.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/StackTrace.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/StackTrace.java new file mode 100644 index 0000000..e4aec45 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/StackTrace.java @@ -0,0 +1,32 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.PARAMETER ) +public @interface StackTrace { + MessageLevel level() default MessageLevel.DEBUG; +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLogger.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLogger.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLogger.java new file mode 100644 index 0000000..ead570a --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLogger.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.jdk; + +import org.apache.knox.gateway.i18n.messages.MessageLevel; +import org.apache.knox.gateway.i18n.messages.MessageLogger; + +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * + */ +final class JdkMessageLogger implements MessageLogger { + + private final Logger logger; + + JdkMessageLogger( final Logger logger ) { + this.logger = logger; + } + + @Override + public final boolean isLoggable( final MessageLevel level ) { + return logger.isLoggable( toLevel( level ) ); + } + + //TODO: Handle message ID. + @Override + public final void log( final StackTraceElement caller, final MessageLevel level, final String id, final String message, final Throwable thrown ) { + LogRecord record = new LogRecord( toLevel( level ), message ); + record.setSourceClassName( caller.getClassName() ); + record.setSourceMethodName( caller.getMethodName() ); + if( thrown != null ) { + record.setThrown( thrown ); + } + logger.log( record ); + } + + private static final Level toLevel( final MessageLevel level ) { + switch( level ) { + case FATAL: return Level.SEVERE; + case ERROR: return Level.SEVERE; + case WARN: return Level.WARNING; + case INFO: return Level.INFO; + case DEBUG: return Level.FINE; + case TRACE: return Level.FINEST; + default: return Level.OFF; + } + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLoggerFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLoggerFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLoggerFactory.java new file mode 100644 index 0000000..d1127bd --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/jdk/JdkMessageLoggerFactory.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.jdk; + +import org.apache.knox.gateway.i18n.messages.MessageLogger; +import org.apache.knox.gateway.i18n.messages.MessageLoggerFactory; + +import java.util.logging.Logger; + +/** + * + */ +public class JdkMessageLoggerFactory implements MessageLoggerFactory { + + @Override + public MessageLogger getLogger( String name ) { + return new JdkMessageLogger( Logger.getLogger( name ) ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLogger.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLogger.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLogger.java new file mode 100644 index 0000000..b444faae --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLogger.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.sout; + +import org.apache.knox.gateway.i18n.messages.MessageLevel; +import org.apache.knox.gateway.i18n.messages.MessageLogger; + +import java.text.MessageFormat; + +/** + * + */ +public class SoutMessageLogger implements MessageLogger { + + private static final String FORMAT_WITHOUT_ID = "{0}: {2}"; + private static final String FORMAT_WITH_ID = "{0}: {2} [{1}]"; + + private final String name; + + SoutMessageLogger( String name ) { + this.name = name; + } + + @Override + public boolean isLoggable( MessageLevel level ) { + return true; + } + + private static final String getFormat( final String id ) { + return( id == null ) ? FORMAT_WITHOUT_ID : FORMAT_WITH_ID; + } + + @Override + public void log( final StackTraceElement caller, final MessageLevel level, final String id, final String message, final Throwable thrown ) { + System.out.println( MessageFormat.format( getFormat( id ), level, id, message ) ); // I18N not required. + if( thrown != null ) { + thrown.printStackTrace(); + } + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLoggerFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLoggerFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLoggerFactory.java new file mode 100644 index 0000000..567d5b8 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/sout/SoutMessageLoggerFactory.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.sout; + +import org.apache.knox.gateway.i18n.messages.MessageLogger; +import org.apache.knox.gateway.i18n.messages.MessageLoggerFactory; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + */ +public class SoutMessageLoggerFactory implements MessageLoggerFactory { + + private static SoutMessageLoggerFactory INSTANCE; + private static final Map<String,MessageLogger> LOGGERS = new ConcurrentHashMap<String,MessageLogger>(); + + public static SoutMessageLoggerFactory getFactory() { + if( INSTANCE == null ) { + INSTANCE = new SoutMessageLoggerFactory(); + } + return INSTANCE; + } + + public SoutMessageLoggerFactory() { + INSTANCE = this; + } + + @Override + public MessageLogger getLogger( String name ) { + MessageLogger logger = LOGGERS.get( name ); + if( logger == null ) { + logger = new SoutMessageLogger( name ); + LOGGERS.put( name, logger ); + } + return logger; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLogger.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLogger.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLogger.java new file mode 100644 index 0000000..4641b22 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLogger.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.test; + +import org.apache.knox.gateway.i18n.messages.MessageLevel; +import org.apache.knox.gateway.i18n.messages.MessageLogger; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * + */ +public class TestMessageLogger implements MessageLogger { + + public final List<TestMessageRecord> records = Collections.synchronizedList( new ArrayList<TestMessageRecord>() ); + + public final String name; + + TestMessageLogger( final String name ) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public boolean isLoggable( MessageLevel level ) { + return true; + } + + @Override + public void log( final StackTraceElement caller, final MessageLevel level, final String id, final String text, final Throwable thrown ) { + TestMessageRecord record = new TestMessageRecord( this, caller, level, id, text, thrown ); + records.add( record ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLoggerFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLoggerFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLoggerFactory.java new file mode 100644 index 0000000..3d093a9 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageLoggerFactory.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.test; + +import org.apache.knox.gateway.i18n.messages.MessageLogger; +import org.apache.knox.gateway.i18n.messages.MessageLoggerFactory; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + */ +public class TestMessageLoggerFactory implements MessageLoggerFactory { + + private static TestMessageLoggerFactory INSTANCE; + private static final Map<String,MessageLogger> LOGGERS = new ConcurrentHashMap<String,MessageLogger>(); + + public static TestMessageLoggerFactory getFactory() { + if( INSTANCE == null ) { + INSTANCE = new TestMessageLoggerFactory(); + } + return INSTANCE; + } + + public TestMessageLoggerFactory() { + INSTANCE = this; + } + + @Override + public MessageLogger getLogger( String name ) { + MessageLogger logger = LOGGERS.get( name ); + if( logger == null ) { + logger = new TestMessageLogger( name ); + LOGGERS.put( name, logger ); + } + return logger; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageRecord.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageRecord.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageRecord.java new file mode 100644 index 0000000..e51b0a6 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/messages/loggers/test/TestMessageRecord.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages.loggers.test; + +import org.apache.knox.gateway.i18n.messages.MessageLevel; + +/** + * + */ +public class TestMessageRecord { + + private final TestMessageLogger logger; + private final StackTraceElement caller; + private final MessageLevel level; + private final String id; + private final String message; + private final Throwable throwable; + + public TestMessageRecord( TestMessageLogger logger, StackTraceElement caller, MessageLevel level, String id, String message, Throwable throwable ) { + this.logger = logger; + this.caller = caller; + this.level = level; + this.id = id; + this.message = message; + this.throwable = throwable; + } + + public TestMessageLogger getLogger() { + return logger; + } + + public StackTraceElement getCaller() { + return caller; + } + + public MessageLevel getLevel() { + return level; + } + + public String getId() { + return id; + } + + public String getMessage() { + return message; + } + + public Throwable getThrowable() { + return throwable; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resource.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resource.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resource.java new file mode 100644 index 0000000..5e37c64 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resource.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.resources; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.METHOD ) +public @interface Resource { + static final String DEFAULT_TEXT = "{}"; + String text() default DEFAULT_TEXT; +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resources.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resources.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resources.java new file mode 100644 index 0000000..4d0c053 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/Resources.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.resources; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.TYPE ) +public @interface Resources { + static final String DEFAULT_BUNDLE = ""; + String bundle() default DEFAULT_BUNDLE; +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesFactory.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesFactory.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesFactory.java new file mode 100644 index 0000000..cf7fa23 --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesFactory.java @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.resources; + +import java.lang.reflect.Proxy; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + */ +public class ResourcesFactory { + + private static Map<Class<?>, Object> proxies = new ConcurrentHashMap<Class<?>, Object>(); + + @SuppressWarnings( "unchecked" ) + public static <T> T get( Class<T> clazz ) { + Object proxy = proxies.get( clazz ); + if( proxy == null ) { + Resources anno = clazz.getAnnotation( Resources.class ); + if( anno == null ) { + throw new IllegalArgumentException( clazz.getName() + " missing @" + Resources.class.getCanonicalName() ); + } + ResourcesInvoker invoker = new ResourcesInvoker( clazz ); + proxy = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{ clazz }, invoker ); + proxies.put( clazz, proxy ); + } + return (T)proxy; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesInvoker.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesInvoker.java b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesInvoker.java new file mode 100644 index 0000000..065da0c --- /dev/null +++ b/gateway-i18n/src/main/java/org/apache/knox/gateway/i18n/resources/ResourcesInvoker.java @@ -0,0 +1,162 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.resources; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.text.MessageFormat; +import java.util.ListResourceBundle; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + */ +public class ResourcesInvoker implements InvocationHandler { + + private static ResourceBundle MISSING_BUNDLE = new ListResourceBundle() { + @Override + protected Object[][] getContents() { + return null; + } + }; + + private Class bundleClass; + private String bundleName; + private ConcurrentHashMap<Locale, ResourceBundle> bundles; + + public ResourcesInvoker( Class<?> bundleClass ) { + this.bundleClass = bundleClass; + this.bundleName = calcBundleName( bundleClass ); + this.bundles = new ConcurrentHashMap<Locale, ResourceBundle>(); + } + + @Override + public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { + return getText( method, args ); + } + + protected final String getText( final Method method, final Object[] args ) { + String pattern = getPattern( method ); + String text = MessageFormat.format( pattern, args ); + return text; + } + + protected final String getPattern( final Method method ) { + String pattern = getBundlePattern( method ); + if( pattern == null ) { + pattern = getAnnotationPattern( method ); + if( pattern == null || Resource.DEFAULT_TEXT.equals( pattern ) ) { + pattern = getDefaultPattern( method ); + } + } + return pattern; + } + + protected String getAnnotationPattern( final Method method ) { + String pattern = null; + final Resource anno = method.getAnnotation( Resource.class ); + if( anno != null ) { + pattern = anno.text(); + } + return pattern; + } + + protected final String getBundlePattern( final Method method ) { + String pattern = null; + final ResourceBundle bundle = findBundle(); + if( bundle != null && bundle.containsKey( method.getName() ) ) { + pattern = bundle.getString( method.getName() ); + } + return pattern; + } + + protected static final String getDefaultPattern( final Method method ) { + final String prefix = method.getName(); + String suffix; + final int params = method.getParameterTypes().length; + switch( params ) { + case( 0 ) : suffix = ""; break; + case( 1 ) : suffix = "(\"{0}\")"; break; + case( 2 ) : suffix = "(\"{0}\",\"{1}\")"; break; + case( 3 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\")"; break; + case( 4 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\")"; break; + case( 5 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\")"; break; + case( 6 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\")"; break; + case( 7 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\")"; break; + case( 8 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\")"; break; + case( 9 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\")"; break; + case( 10 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\")"; break; + default : suffix = createDefaultPatternSuffix( params ); + } + return prefix + suffix; + } + + private static final String createDefaultPatternSuffix( final int size ) { + StringBuilder builder = new StringBuilder( 1 + size*7 ); + builder.append( "(" ); + for( int i=0; i<size; i++ ) { + if( i>0 ) { + builder.append( "," ); + } + builder.append( "\"{" ).append( i ).append( "}\"" ); + } + builder.append( ")" ); + return builder.toString(); + + } + + private static final String calcBundleName( final Class<?> clazz ) { + String bundle = null; + final Resources anno = clazz.getAnnotation( Resources.class ); + if( anno != null ) { + bundle = anno.bundle(); + if( Resources.DEFAULT_BUNDLE.equals( bundle ) ) { + bundle = null; + } + } + if( bundle == null ) { + bundle = clazz.getCanonicalName().replace( '.', '/' ); + } + return bundle; + } + + + protected String getBundleName() { + return bundleName; + } + + protected final ResourceBundle findBundle() { + final Locale locale = Locale.getDefault(); + ResourceBundle bundle = bundles.get( locale ); + if( bundle == MISSING_BUNDLE ) { + bundle = null; + } else if ( bundle == null ) { + try { + bundle = ResourceBundle.getBundle( getBundleName(), locale, bundleClass.getClassLoader() ); + bundles.put( locale, bundle ); + } catch( MissingResourceException e ) { + bundles.put( locale, MISSING_BUNDLE ); + } + } + return bundle; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTest.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTest.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTest.java deleted file mode 100644 index 56f0b69..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTest.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.messages; - -import org.apache.hadoop.gateway.i18n.messages.loggers.test.TestMessageLogger; -import org.apache.hadoop.gateway.i18n.messages.loggers.test.TestMessageLoggerFactory; -import org.apache.hadoop.gateway.i18n.messages.loggers.test.TestMessageRecord; -import org.apache.hadoop.test.category.FastTests; -import org.apache.hadoop.test.category.UnitTests; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -/** - * - */ -@Category( { UnitTests.class, FastTests.class } ) -public class MessagesTest { - - @Test - public void testFirst() { - MessagesTestSubject log = MessagesFactory.get( MessagesTestSubject.class ); - - log.withFullAnnotationAndParameter( 7 ); - - TestMessageLogger logger = (TestMessageLogger)TestMessageLoggerFactory.getFactory().getLogger( "some.logger.name" ); - assertThat( logger.records.size(), equalTo( 1 ) ); - - TestMessageRecord record = logger.records.get( 0 ); - - assertThat( record.getCaller().getClassName(), is( this.getClass().getName() ) ); - assertThat( record.getCaller().getMethodName(), is( "testFirst" ) ); - - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTestSubject.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTestSubject.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTestSubject.java deleted file mode 100644 index 8129aa5..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/messages/MessagesTestSubject.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.messages; - -import static org.apache.hadoop.gateway.i18n.messages.MessageLevel.ERROR; -import static org.apache.hadoop.gateway.i18n.messages.MessageLevel.INFO; - -/** - * - */ -@Messages( bundle="some.bundle.name", logger="some.logger.name", codes="ID:{0}" ) -public interface MessagesTestSubject { - - @Message( level=ERROR, code=3, text="p0={0}" ) - void withFullAnnotationAndParameter( int x ); - - @Message( level=INFO, code=42, text="str={0}, t={1}" ) - void withEverything( String str, @StackTrace(level=INFO) Throwable t ); - - @Message - void withoutParams(); - - void withoutAnnotations( int x ); - - @Message - void withoutStackTrace( Throwable t ); - - @Message - void withMismatchedText(); - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTest.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTest.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTest.java deleted file mode 100644 index 28313d9..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTest.java +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -import org.apache.hadoop.test.category.FastTests; -import org.apache.hadoop.test.category.UnitTests; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import java.util.Locale; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; - -/** - * - */ -@Category( { UnitTests.class, FastTests.class } ) -public class ResourcesTest { - - private Locale locale; - - @Before - public void setup() { - locale = Locale.getDefault(); - } - - @After - public void cleanup() { - Locale.setDefault( locale ); - } - - @Test - public void testResourceFormatting() { - - ResourcesTestFormattingSubject res = ResourcesFactory.get( ResourcesTestFormattingSubject.class ); - - assertThat( - res.withoutAnnotationsOrParameters(), - equalTo( "withoutAnnotationsOrParameters" ) ); - - assertThat( - res.withAnnotationWithoutPatternOneParam( 42 ), - equalTo( "withAnnotationWithoutPatternOneParam(\"42\")" ) ); - - assertThat( - res.withAnnotationWithPatternOneParam( 72 ), - equalTo( "before72after" ) ); - - assertThat( - res.withAnnotationWithSimplePatternOneParam( 33 ), - equalTo( "33" ) ); - - assertThat( - res.withoutAnnotationsWithElevenParams( "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11" ), - equalTo( "withoutAnnotationsWithElevenParams(\"v1\",\"v2\",\"v3\",\"v4\",\"v5\",\"v6\",\"v7\",\"v8\",\"v9\",\"v10\",\"v11\")" ) ); - - assertThat( - res.withoutAnnotationsWithOneParam( 17 ), - equalTo( "withoutAnnotationsWithOneParam(\"17\")" ) ); - - assertThat( - res.withMoreFormatParamsThanMethodParams( 7 ), - equalTo( "7,{1}" ) ); - - assertThat( - res.withLessFormatParamsThanMethodParams( 7, 11 ), - equalTo( "7" ) ); - } - - @Test - public void testResourceLocales() { - - ResourcesTestLocaleSubject res = ResourcesFactory.get( ResourcesTestLocaleSubject.class ); - - Locale.setDefault( Locale.CHINESE ); // Change to something that we won't have test bundles for. - assertThat( res.testResource( "queryParam" ), equalTo( "default=[queryParam]" ) ); - - Locale.setDefault( Locale.ENGLISH ); - assertThat( res.testResource( "queryParam" ), equalTo( "en=[queryParam]" ) ); - - Locale.setDefault( Locale.US ); - assertThat( res.testResource( "queryParam" ), equalTo( "us=[queryParam]" ) ); - - Locale.setDefault( Locale.UK ); - assertThat( res.testResource( "queryParam" ), equalTo( "uk=[queryParam]" ) ); - - } - - @Test - public void testNamedBundle() { - - ResourcesTestNamedSubject res = ResourcesFactory.get( ResourcesTestNamedSubject.class ); - - Locale.setDefault( Locale.CHINESE ); // Change to something that we won't have test bundles for. - assertThat( res.testResource( "queryParam" ), equalTo( "default=[queryParam]" ) ); - - Locale.setDefault( Locale.CANADA ); - assertThat( res.testResource( "queryParam" ), equalTo( "ca=[queryParam]" ) ); - - } - - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestFormattingSubject.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestFormattingSubject.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestFormattingSubject.java deleted file mode 100644 index 0ff337f..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestFormattingSubject.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -/** - * - */ -@Resources( bundle="some.bundle.name" ) -public interface ResourcesTestFormattingSubject { - - @Resource(text="{0}") - String withAnnotationWithSimplePatternOneParam( int x ); - - @Resource(text="before{0}after") - String withAnnotationWithPatternOneParam( int x ); - - @Resource - String withAnnotationWithoutPatternOneParam( int x ); - - String withoutAnnotationsOrParameters(); - - String withoutAnnotationsWithOneParam( int x ); - - String withoutAnnotationsWithElevenParams( String p1, String p2, String p3, String p4, String p5, String p6, String p7, String p8, String p9, String p10, String p11 ); - - @Resource(text="{0},{1}") - String withMoreFormatParamsThanMethodParams( int x ); - - @Resource(text="{0}") - String withLessFormatParamsThanMethodParams( int x, int y ); - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestLocaleSubject.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestLocaleSubject.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestLocaleSubject.java deleted file mode 100644 index 1f4dc6a..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestLocaleSubject.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -/** - * - */ -@Resources -public interface ResourcesTestLocaleSubject { - - @Resource(text="default=[{0}]") - String testResource( String param ); - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestNamedSubject.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestNamedSubject.java b/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestNamedSubject.java deleted file mode 100644 index 198786f..0000000 --- a/gateway-i18n/src/test/java/org/apache/hadoop/gateway/i18n/resources/ResourcesTestNamedSubject.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.hadoop.gateway.i18n.resources; - -/** - * - */ -@Resources( bundle="org.apache.hadoop.gateway.i18n.resources.NamedBundle" ) -public interface ResourcesTestNamedSubject { - - @Resource(text="default=[{0}]") - String testResource( String param ); - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTest.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTest.java b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTest.java new file mode 100644 index 0000000..d53e99d --- /dev/null +++ b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTest.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +import org.apache.knox.gateway.i18n.messages.loggers.test.TestMessageLogger; +import org.apache.knox.gateway.i18n.messages.loggers.test.TestMessageLoggerFactory; +import org.apache.knox.gateway.i18n.messages.loggers.test.TestMessageRecord; +import org.apache.hadoop.test.category.FastTests; +import org.apache.hadoop.test.category.UnitTests; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * + */ +@Category( { UnitTests.class, FastTests.class } ) +public class MessagesTest { + + @Test + public void testFirst() { + MessagesTestSubject log = MessagesFactory.get( MessagesTestSubject.class ); + + log.withFullAnnotationAndParameter( 7 ); + + TestMessageLogger logger = (TestMessageLogger)TestMessageLoggerFactory.getFactory().getLogger( "some.logger.name" ); + assertThat( logger.records.size(), equalTo( 1 ) ); + + TestMessageRecord record = logger.records.get( 0 ); + + assertThat( record.getCaller().getClassName(), is( this.getClass().getName() ) ); + assertThat( record.getCaller().getMethodName(), is( "testFirst" ) ); + + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTestSubject.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTestSubject.java b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTestSubject.java new file mode 100644 index 0000000..fdecab5 --- /dev/null +++ b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/messages/MessagesTestSubject.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.messages; + +/** + * + */ +@Messages( bundle="some.bundle.name", logger="some.logger.name", codes="ID:{0}" ) +public interface MessagesTestSubject { + + @Message(level= MessageLevel.ERROR, code=3, text="p0={0}" ) + void withFullAnnotationAndParameter( int x ); + + @Message(level= MessageLevel.INFO, code=42, text="str={0}, t={1}" ) + void withEverything( String str, @StackTrace(level= MessageLevel.INFO) Throwable t ); + + @Message + void withoutParams(); + + void withoutAnnotations( int x ); + + @Message + void withoutStackTrace( Throwable t ); + + @Message + void withMismatchedText(); + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/resources/ResourcesTest.java ---------------------------------------------------------------------- diff --git a/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/resources/ResourcesTest.java b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/resources/ResourcesTest.java new file mode 100644 index 0000000..a876b89 --- /dev/null +++ b/gateway-i18n/src/test/java/org/apache/knox/gateway/i18n/resources/ResourcesTest.java @@ -0,0 +1,121 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.i18n.resources; + +import org.apache.hadoop.test.category.FastTests; +import org.apache.hadoop.test.category.UnitTests; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.util.Locale; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * + */ +@Category( { UnitTests.class, FastTests.class } ) +public class ResourcesTest { + + private Locale locale; + + @Before + public void setup() { + locale = Locale.getDefault(); + } + + @After + public void cleanup() { + Locale.setDefault( locale ); + } + + @Test + public void testResourceFormatting() { + + ResourcesTestFormattingSubject res = ResourcesFactory.get( ResourcesTestFormattingSubject.class ); + + assertThat( + res.withoutAnnotationsOrParameters(), + equalTo( "withoutAnnotationsOrParameters" ) ); + + assertThat( + res.withAnnotationWithoutPatternOneParam( 42 ), + equalTo( "withAnnotationWithoutPatternOneParam(\"42\")" ) ); + + assertThat( + res.withAnnotationWithPatternOneParam( 72 ), + equalTo( "before72after" ) ); + + assertThat( + res.withAnnotationWithSimplePatternOneParam( 33 ), + equalTo( "33" ) ); + + assertThat( + res.withoutAnnotationsWithElevenParams( "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11" ), + equalTo( "withoutAnnotationsWithElevenParams(\"v1\",\"v2\",\"v3\",\"v4\",\"v5\",\"v6\",\"v7\",\"v8\",\"v9\",\"v10\",\"v11\")" ) ); + + assertThat( + res.withoutAnnotationsWithOneParam( 17 ), + equalTo( "withoutAnnotationsWithOneParam(\"17\")" ) ); + + assertThat( + res.withMoreFormatParamsThanMethodParams( 7 ), + equalTo( "7,{1}" ) ); + + assertThat( + res.withLessFormatParamsThanMethodParams( 7, 11 ), + equalTo( "7" ) ); + } + + @Test + public void testResourceLocales() { + + ResourcesTestLocaleSubject res = ResourcesFactory.get( ResourcesTestLocaleSubject.class ); + + Locale.setDefault( Locale.CHINESE ); // Change to something that we won't have test bundles for. + assertThat( res.testResource( "queryParam" ), equalTo( "default=[queryParam]" ) ); + + Locale.setDefault( Locale.ENGLISH ); + assertThat( res.testResource( "queryParam" ), equalTo( "en=[queryParam]" ) ); + + Locale.setDefault( Locale.US ); + assertThat( res.testResource( "queryParam" ), equalTo( "us=[queryParam]" ) ); + + Locale.setDefault( Locale.UK ); + assertThat( res.testResource( "queryParam" ), equalTo( "uk=[queryParam]" ) ); + + } + + @Test + public void testNamedBundle() { + + ResourcesTestNamedSubject res = ResourcesFactory.get( ResourcesTestNamedSubject.class ); + + Locale.setDefault( Locale.CHINESE ); // Change to something that we won't have test bundles for. + assertThat( res.testResource( "queryParam" ), equalTo( "default=[queryParam]" ) ); + + Locale.setDefault( Locale.CANADA ); + assertThat( res.testResource( "queryParam" ), equalTo( "ca=[queryParam]" ) ); + + } + + +}