Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DELTASPIKE-148 and DELTASPIKE-149 @MessageContextConfig
- Loading branch information
Showing
21 changed files
with
663 additions
and
116 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...api/src/main/java/org/apache/deltaspike/core/api/literal/MessageContextConfigLiteral.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/LocaleResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...e/core/api/src/main/java/org/apache/deltaspike/core/api/message/MessageContextConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
39 changes: 39 additions & 0 deletions
39
...ke/core/api/src/main/java/org/apache/deltaspike/core/api/message/MessageInterpolator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
35 changes: 35 additions & 0 deletions
35
...aspike/core/api/src/main/java/org/apache/deltaspike/core/api/message/MessageResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
102 changes: 102 additions & 0 deletions
102
deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/PropertyFileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.