mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-03 03:38:33 +08:00
MessageProvider added
This commit is contained in:
parent
30f384cfc3
commit
9c0f810490
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 11:33:01
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.global;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public abstract class MessageProvider
|
||||
{
|
||||
public static final String IMPL_PROP = "cuba.MetadataProvider.impl";
|
||||
|
||||
private static final String DEFAULT_IMPL = "com.haulmont.cuba.core.sys.ResourceBundleMessageProvider";
|
||||
|
||||
private static MessageProvider instance;
|
||||
|
||||
private static MessageProvider getInstance() {
|
||||
if (instance == null) {
|
||||
String implClassName = System.getProperty(IMPL_PROP);
|
||||
if (implClassName == null)
|
||||
implClassName = DEFAULT_IMPL;
|
||||
try {
|
||||
Class implClass = Thread.currentThread().getContextClassLoader().loadClass(implClassName);
|
||||
instance = (MessageProvider) implClass.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static String getMessage(Class caller, String key) {
|
||||
return getInstance().__getMessage(caller, key);
|
||||
}
|
||||
|
||||
public static String getMessage(Class caller, String key, Locale locale) {
|
||||
return getInstance().__getMessage(caller, key, locale);
|
||||
}
|
||||
|
||||
public static String getMessage(Enum caller) {
|
||||
return getInstance().__getMessage(caller);
|
||||
}
|
||||
|
||||
public static String getMessage(Enum caller, Locale locale) {
|
||||
return getInstance().__getMessage(caller, locale);
|
||||
}
|
||||
|
||||
public static String getMessage(String pack, String key) {
|
||||
return getInstance().__getMessage(pack, key);
|
||||
}
|
||||
|
||||
public static String getMessage(String pack, String key, Locale locale) {
|
||||
return getInstance().__getMessage(pack, key, locale);
|
||||
}
|
||||
|
||||
protected abstract String __getMessage(Class caller, String key);
|
||||
|
||||
protected abstract String __getMessage(Class caller, String key, Locale locale);
|
||||
|
||||
protected abstract String __getMessage(Enum caller);
|
||||
|
||||
protected abstract String __getMessage(Enum caller, Locale locale);
|
||||
|
||||
protected abstract String __getMessage(String pack, String key);
|
||||
|
||||
protected abstract String __getMessage(String pack, String key, Locale locale);
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 11:40:42
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.sys;
|
||||
|
||||
import com.haulmont.cuba.core.SecurityProvider;
|
||||
import com.haulmont.cuba.core.global.MessageProvider;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class ResourceBundleMessageProvider extends MessageProvider
|
||||
{
|
||||
public static final String BUNDLE_NAME = "messages";
|
||||
|
||||
private Log log = LogFactory.getLog(ResourceBundleMessageProvider.class);
|
||||
|
||||
protected String __getMessage(Class caller, String key) {
|
||||
return __getMessage(caller, key, SecurityProvider.currentUserSession().getLocale());
|
||||
}
|
||||
|
||||
protected String __getMessage(Class caller, String key, Locale locale) {
|
||||
return __getMessage(getPackName(caller), key, locale);
|
||||
}
|
||||
|
||||
protected String __getMessage(Enum caller) {
|
||||
return __getMessage(caller, SecurityProvider.currentUserSession().getLocale());
|
||||
}
|
||||
|
||||
protected String __getMessage(Enum caller, Locale locale) {
|
||||
return __getMessage(
|
||||
getPackName(caller.getClass()),
|
||||
caller.getClass().getName() + "." + caller.name(),
|
||||
locale
|
||||
);
|
||||
}
|
||||
|
||||
protected String __getMessage(String pack, String key) {
|
||||
return __getMessage(pack, key, SecurityProvider.currentUserSession().getLocale());
|
||||
}
|
||||
|
||||
protected String __getMessage(String pack, String key, Locale locale) {
|
||||
ResourceBundle bundle = null;
|
||||
String s = pack;
|
||||
while (s != null) {
|
||||
try {
|
||||
bundle = ResourceBundle.getBundle(s + "." + BUNDLE_NAME, locale);
|
||||
break;
|
||||
} catch (MissingResourceException e) {
|
||||
// not found, keep searching
|
||||
}
|
||||
int pos = s.lastIndexOf(".");
|
||||
if (pos < 0)
|
||||
s = null;
|
||||
else
|
||||
s = s.substring(0, pos);
|
||||
}
|
||||
if (bundle == null) {
|
||||
log.warn("Resource bundle for '" + pack + "' not found");
|
||||
return key;
|
||||
}
|
||||
try {
|
||||
return bundle.getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
log.warn("Resource key '" + key + "' not found in bundle " + s + "." + BUNDLE_NAME);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
private String getPackName(Class c) {
|
||||
String className = c.getName();
|
||||
int pos = className.lastIndexOf(".");
|
||||
if (pos > 0)
|
||||
return className.substring(0, pos);
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
@ -14,12 +14,12 @@ import com.haulmont.cuba.security.global.UserSession;
|
||||
import com.haulmont.cuba.security.global.LoginException;
|
||||
import com.haulmont.cuba.security.global.NoUserSessionException;
|
||||
import com.haulmont.cuba.security.entity.User;
|
||||
import com.haulmont.cuba.security.resource.Messages;
|
||||
import com.haulmont.cuba.security.sys.UserSessionManager;
|
||||
import com.haulmont.cuba.core.PersistenceProvider;
|
||||
import com.haulmont.cuba.core.EntityManager;
|
||||
import com.haulmont.cuba.core.Query;
|
||||
import com.haulmont.cuba.core.SecurityProvider;
|
||||
import com.haulmont.cuba.core.global.MessageProvider;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import java.util.List;
|
||||
@ -46,7 +46,8 @@ public class LoginWorkerBean implements LoginWorker
|
||||
if (list.isEmpty()) {
|
||||
log.warn("Failed to authenticate: " + login);
|
||||
throw new LoginException(
|
||||
String.format(Messages.getString("LoginException.InvalidLoginOrPassword", locale), login));
|
||||
String.format(MessageProvider.getMessage(getClass(), "LoginException.InvalidLoginOrPassword", locale),
|
||||
login));
|
||||
}
|
||||
else {
|
||||
User user = (User) list.get(0);
|
||||
@ -66,7 +67,8 @@ public class LoginWorkerBean implements LoginWorker
|
||||
if (list.isEmpty()) {
|
||||
log.warn("Failed to authenticate: " + activeDirectoryUser);
|
||||
throw new LoginException(
|
||||
String.format(Messages.getString("LoginException.InvalidActiveDirectoryUser", locale), activeDirectoryUser));
|
||||
String.format(MessageProvider.getMessage(getClass(), "LoginException.InvalidActiveDirectoryUser", locale),
|
||||
activeDirectoryUser));
|
||||
}
|
||||
else {
|
||||
User user = (User) list.get(0);
|
||||
|
@ -10,14 +10,17 @@
|
||||
*/
|
||||
package com.haulmont.cuba.security.global;
|
||||
|
||||
import com.haulmont.cuba.security.resource.Messages;
|
||||
import com.haulmont.cuba.core.global.MessageProvider;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class NoUserSessionException extends RuntimeException
|
||||
{
|
||||
private static final long serialVersionUID = 4820628023682230319L;
|
||||
|
||||
public NoUserSessionException(UUID sessionId) {
|
||||
super(String.format(
|
||||
Messages.getString("NoUserSessionException"), sessionId.toString()));
|
||||
MessageProvider.getMessage(NoUserSessionException.class, "NoUserSessionException"),
|
||||
sessionId.toString()));
|
||||
}
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 01.12.2008 17:58:59
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.security.resource;
|
||||
|
||||
import com.haulmont.cuba.core.SecurityProvider;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class Messages
|
||||
{
|
||||
private static final String BUNDLE_NAME = "com.haulmont.cuba.security.resource.messages";
|
||||
|
||||
public static ResourceBundle getResourceBundle(Locale locale) {
|
||||
return ResourceBundle.getBundle(BUNDLE_NAME, locale);
|
||||
}
|
||||
|
||||
public static ResourceBundle getResourceBundle() {
|
||||
Locale locale = SecurityProvider.currentUserSession().getLocale();
|
||||
return getResourceBundle(locale);
|
||||
}
|
||||
|
||||
|
||||
public static String getString(String key) {
|
||||
try {
|
||||
return getResourceBundle().getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getString(String key, Locale locale) {
|
||||
try {
|
||||
return getResourceBundle(locale).getString(key);
|
||||
} catch (MissingResourceException e) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 12:30:48
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core;
|
||||
|
||||
import com.haulmont.cuba.core.global.MessageProvider;
|
||||
import com.haulmont.cuba.core.mp_test.MpTestObj;
|
||||
import com.haulmont.cuba.core.mp_test.nested.MpTestNestedObj;
|
||||
import com.haulmont.cuba.core.mp_test.nested.MpTestNestedEnum;
|
||||
|
||||
public class MessageProviderTest extends CubaTestCase
|
||||
{
|
||||
public void test() {
|
||||
String msg = MessageProvider.getMessage(MpTestObj.class, "key1");
|
||||
assertEquals("Message1", msg);
|
||||
|
||||
msg = MessageProvider.getMessage(MpTestNestedObj.class, "key2");
|
||||
assertEquals("Message2", msg);
|
||||
|
||||
msg = MessageProvider.getMessage("com.haulmont.cuba.core.mp_test.nested", "key1");
|
||||
assertEquals("Message1", msg);
|
||||
|
||||
msg = MessageProvider.getMessage("test", "key1");
|
||||
assertEquals("key1", msg);
|
||||
|
||||
msg = MessageProvider.getMessage(MpTestNestedEnum.ONE);
|
||||
assertEquals("One", msg);
|
||||
|
||||
msg = MessageProvider.getMessage(MpTestNestedObj.InternalEnum.FIRST);
|
||||
assertEquals("First", msg);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 12:35:12
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.mp_test;
|
||||
|
||||
public class MpTestObj
|
||||
{
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
key1=Message1
|
||||
key2=Message2
|
||||
com.haulmont.cuba.core.mp_test.nested.MpTestNestedEnum.ONE=One
|
||||
com.haulmont.cuba.core.mp_test.nested.MpTestNestedObj$InternalEnum.FIRST=First
|
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 14:46:56
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.mp_test.nested;
|
||||
|
||||
public enum MpTestNestedEnum
|
||||
{
|
||||
ONE, TWO
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
|
||||
* Haulmont Technology proprietary and confidential.
|
||||
* Use is subject to license terms.
|
||||
|
||||
* Author: Konstantin Krivopustov
|
||||
* Created: 06.03.2009 12:34:33
|
||||
*
|
||||
* $Id$
|
||||
*/
|
||||
package com.haulmont.cuba.core.mp_test.nested;
|
||||
|
||||
public class MpTestNestedObj
|
||||
{
|
||||
public enum InternalEnum
|
||||
{
|
||||
FIRST, SECOND
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user