Skip to content

Commit

Permalink
DELTASPIKE-148 and DELTASPIKE-149 @MessageContextConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetracek committed Apr 6, 2012
1 parent 7c480a0 commit ca90b1c
Show file tree
Hide file tree
Showing 21 changed files with 663 additions and 116 deletions.
@@ -0,0 +1,70 @@
/*
* 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.deltaspike.core.api.literal;

import org.apache.deltaspike.core.api.message.LocaleResolver;
import org.apache.deltaspike.core.api.message.MessageContextConfig;
import org.apache.deltaspike.core.api.message.MessageInterpolator;
import org.apache.deltaspike.core.api.message.MessageResolver;

import javax.enterprise.util.AnnotationLiteral;

/**
* Literal for {@link org.apache.deltaspike.core.api.message.MessageContextConfig}
*/
public class MessageContextConfigLiteral extends AnnotationLiteral<MessageContextConfig> implements MessageContextConfig
{
private static final long serialVersionUID = -5888417869986174834L;

private final Class<? extends MessageResolver> messageResolver;
private final Class<? extends MessageInterpolator> messageInterpolator;
private final Class<? extends LocaleResolver> localeResolver;

public MessageContextConfigLiteral()
{
this(MessageResolver.class, MessageInterpolator.class, LocaleResolver.class);
}

public MessageContextConfigLiteral(Class<? extends MessageResolver> messageResolver,
Class<? extends MessageInterpolator> messageInterpolator,
Class<? extends LocaleResolver> localeResolver)
{
this.messageResolver = messageResolver;
this.messageInterpolator = messageInterpolator;
this.localeResolver = localeResolver;
}

@Override
public Class<? extends MessageResolver> messageResolver()
{
return this.messageResolver;
}

@Override
public Class<? extends MessageInterpolator> messageInterpolator()
{
return this.messageInterpolator;
}

@Override
public Class<? extends LocaleResolver> localeResolver()
{
return this.localeResolver;
}
}
@@ -0,0 +1,34 @@
/*
* 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.deltaspike.core.api.message;


import java.io.Serializable;
import java.util.Locale;

/**
* Implementations have to provide the current locale
*/
public interface LocaleResolver extends Serializable
{
/**
* @return the current locale
*/
Locale getLocale();
}
Expand Up @@ -18,13 +18,13 @@
*/
package org.apache.deltaspike.core.api.message;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target(METHOD)
@Retention(RUNTIME)
@Documented
Expand All @@ -36,26 +36,4 @@
* @return the format string
*/
String value();

/**
* The format type of this method (defaults to {@link Format#PRINTF}).
*
* @return the format type
*/
Format format() default Format.PRINTF;

/**
* The possible format types.
*/
enum Format
{
/**
* A {@link java.util.Formatter}-type format string.
*/
PRINTF,
/**
* A {@link java.text.MessageFormat}-type format string.
*/
MESSAGE_FORMAT,
}
}
Expand Up @@ -18,23 +18,19 @@
*/
package org.apache.deltaspike.core.api.message;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Qualifier
@Documented
public @interface MessageBundle
{

}
@@ -0,0 +1,38 @@
/*
* 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.deltaspike.core.api.message;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({ TYPE })
@Retention(RUNTIME)
@Documented
public @interface MessageContextConfig
{
Class<? extends MessageResolver> messageResolver() default MessageResolver.class;

Class<? extends MessageInterpolator> messageInterpolator() default MessageInterpolator.class;

Class<? extends LocaleResolver> localeResolver() default LocaleResolver.class;
}
@@ -0,0 +1,39 @@
/*
* 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.deltaspike.core.api.message;

import java.io.Serializable;

/**
* Implementations are responsible to replace placeholders in a message with the final value
*/
public interface MessageInterpolator extends Serializable
{
/**
* replaces the arguments of the given message with the given arguments
*
* instead of a MessageContextAware interface. we need it to avoid expensive operations like locking or deep cloning
* @param messageText the message text which has to be interpolated
* @param arguments a list of numbered and/or named arguments for the current message
* @return the final (interpolated) message text
* if it was possible to replace the parameters with the given arguments
* the unmodified messageText otherwise
*/
String interpolate(String messageText, Object[] arguments);
}
@@ -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.deltaspike.core.api.message;

import java.io.Serializable;

/**
* Implementations have to resolve the text stored for a given key in the message-source they are aware of
*/
public interface MessageResolver extends Serializable
{
String MISSING_RESOURCE_MARKER = "???";

/**
* @param messageDescriptor the message key (or in-lined text) of the current message
* @return the final but not interpolated message text
*/
String getMessage(String messageDescriptor);
}
@@ -0,0 +1,102 @@
/*
* 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.deltaspike.core.util;

import javax.enterprise.inject.Typed;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

@Typed()
public abstract class PropertyFileUtils
{
/**
* Constructor which prevents the instantiation of this class
*/
private PropertyFileUtils()
{
// prevent instantiation
}

public static Enumeration<URL> resolvePropertyFiles(String propertyFileName) throws IOException
{
ClassLoader cl = ClassUtils.getClassLoader(null);

Enumeration<URL> propertyFileUrls = cl.getResources(propertyFileName);

//fallback - see DELTASPIKE-98
if (!propertyFileUrls.hasMoreElements())
{
cl = PropertyFileUtils.class.getClassLoader();
propertyFileUrls = cl.getResources(propertyFileName);
}

return propertyFileUrls;
}

public static Properties loadProperties(URL url)
{
Properties props = new Properties();

InputStream inputStream = null;
try
{
inputStream = url.openStream();

if (inputStream != null)
{
props.load(inputStream);
}
}
catch (IOException e)
{
throw new IllegalStateException(e);
}
finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException e)
{
// no worries, means that the file is already closed
}
}

return props;
}

public static ResourceBundle getResourceBundle(String bundleName)
{
return getResourceBundle(bundleName, Locale.getDefault());
}

public static ResourceBundle getResourceBundle(String bundleName, Locale locale)
{
return ResourceBundle.getBundle(bundleName, locale);
}
}

0 comments on commit ca90b1c

Please sign in to comment.