com.haulmont.cuba.core.jmx.ConfigStorage improvements #PL-5655

This commit is contained in:
Maxim Gorbunkov 2015-10-20 07:41:22 +00:00
parent 224339ceea
commit b205251dbe
7 changed files with 90 additions and 25 deletions

View File

@ -127,6 +127,6 @@ public class ConfigStorage implements ConfigStorageMBean {
@Override
@Authenticated
public String getConfigValue(String classFQN, String methodName) {
return configStorageCommon.getConfigValue(classFQN, methodName);
return configStorageCommon.getConfigValue(classFQN, methodName, null, null);
}
}

View File

@ -64,8 +64,8 @@ public interface ConfigStorageMBean {
@ManagedOperation(description = "Invoke a getter method of configuration interface and print the result")
@ManagedOperationParameters({
@ManagedOperationParameter(name = "className", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "value", description = "Getter method name")
@ManagedOperationParameter(name = "classFQN", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "methodName", description = "Getter method name")
})
String getConfigValue(String classFQN, String methodName);
}

View File

@ -5,19 +5,25 @@
package com.haulmont.cuba.core.config;
import com.google.common.base.Strings;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.GlobalConfig;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.security.app.LoginService;
import com.haulmont.cuba.security.global.LoginException;
import com.haulmont.cuba.security.global.UserSession;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.text.StrBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.annotation.ManagedBean;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
/**
* @author gorbunkov
@ -29,6 +35,11 @@ public class ConfigStorageCommon {
@Inject
protected Configuration configuration;
@Inject
protected LoginService loginService;
protected final Log log = LogFactory.getLog(ConfigStorageCommon.class);
public String printAppProperties(String prefix) {
List<String> list = new ArrayList<String>();
for (String name : AppContext.getPropertyNames()) {
@ -57,7 +68,16 @@ public class ConfigStorageCommon {
return "Property " + name + " set to " + value;
}
public String getConfigValue(String classFQN, String methodName) {
/**
* Method returns a result of config method invocation
* @param classFQN fully qualified configuration interface name
* @param methodName config getter method name
* @param userLogin parameter is used for authentication if there is no security context bound to the current thread
* and configuration method source is DATABASE
* @param userPassword see userLogin parameter description
* @return configuration method invocation result
*/
public String getConfigValue(String classFQN, String methodName, String userLogin, String userPassword) {
Class<?> aClass;
try {
aClass = Class.forName(classFQN);
@ -69,20 +89,55 @@ public class ConfigStorageCommon {
if (Config.class.isAssignableFrom(aClass)) {
Config config = configuration.getConfig((Class<? extends Config>)aClass);
Method method;
boolean logoutRequired = false;
try {
method = aClass.getMethod(methodName);
//if there is no security context bound to the current thread and the source of the config method is
//DATABASE, then login attempt with 'userLogin' and 'userPassword' will be made
if (AppContext.getSecurityContext() == null) {
SourceType sourceType;
Source methodSourceAnnotation = method.getAnnotation(Source.class);
if (methodSourceAnnotation != null) {
sourceType = methodSourceAnnotation.type();
} else {
Source classSourceAnnotation = aClass.getAnnotation(Source.class);
sourceType = classSourceAnnotation.type();
}
if (sourceType != null && sourceType == SourceType.DATABASE) {
if (Strings.isNullOrEmpty(userLogin)) {
return "No security context bound to the current thread. Please specify the user name.";
} else {
try {
Map<String, Locale> availableLocales = configuration.getConfig(GlobalConfig.class).getAvailableLocales();
Locale defaultLocale = availableLocales.values().iterator().next();
UserSession session = loginService.loginTrusted(userLogin, userPassword, defaultLocale);
AppContext.setSecurityContext(new SecurityContext(session));
logoutRequired = true;
} catch (LoginException e) {
log.error(ExceptionUtils.getStackTrace(e));
return "Login error: " + e.getMessage();
}
}
}
}
Object result = method.invoke(config);
return result.toString();
return result == null ? null : result.toString();
} catch (NoSuchMethodException e) {
return "Method " + methodName + "() not found in class " + classFQN;
} catch (InvocationTargetException e) {
return ExceptionUtils.getStackTrace(e);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException | IllegalAccessException e) {
return ExceptionUtils.getStackTrace(e);
} finally {
if (logoutRequired) {
loginService.logout();
AppContext.setSecurityContext(null);
}
}
} else {
return "Class " + classFQN + " is not an implementation of Config interface";
}
}
}
}

View File

@ -7,6 +7,7 @@ package com.haulmont.cuba.portal.jmx;
import com.haulmont.cuba.core.config.ConfigStorageCommon;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.portal.config.PortalConfig;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;
@ -27,6 +28,9 @@ public class ConfigStorage implements ConfigStorageMBean {
@Inject
protected ConfigStorageCommon configStorageCommon;
@Inject
protected PortalConfig portalConfig;
@Override
public String printAppProperties() {
return printAppProperties(null);
@ -48,7 +52,8 @@ public class ConfigStorage implements ConfigStorageMBean {
}
@Override
public String getConfigValue(String classFQN, String methodName) {
return configStorageCommon.getConfigValue(classFQN, methodName);
public String getConfigValue(String classFQN, String methodName, String userLogin) {
String trustedPassword = portalConfig.getTrustedClientPassword();
return configStorageCommon.getConfigValue(classFQN, methodName, userLogin, trustedPassword);
}
}

View File

@ -37,8 +37,9 @@ public interface ConfigStorageMBean {
@ManagedOperation(description = "Invoke a getter method of configuration interface and print the result")
@ManagedOperationParameters({
@ManagedOperationParameter(name = "className", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "value", description = "Getter method name")
@ManagedOperationParameter(name = "classFQN", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "methodName", description = "Getter method name"),
@ManagedOperationParameter(name = "userLogin", description = "User login that will be used for creating a user session")
})
String getConfigValue(String classFQN, String methodName);
String getConfigValue(String classFQN, String methodName, String userLogin);
}

View File

@ -6,9 +6,7 @@
package com.haulmont.cuba.web.jmx;
import com.haulmont.cuba.core.config.ConfigStorageCommon;
import com.haulmont.cuba.core.sys.AppContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;
import com.haulmont.cuba.web.auth.WebAuthConfig;
import org.springframework.stereotype.Component;
@ -27,6 +25,9 @@ public class ConfigStorage implements ConfigStorageMBean {
@Inject
protected ConfigStorageCommon configStorageCommon;
@Inject
protected WebAuthConfig webAuthConfig;
@Override
public String printAppProperties() {
return printAppProperties(null);
@ -48,8 +49,9 @@ public class ConfigStorage implements ConfigStorageMBean {
}
@Override
public String getConfigValue(String classFQN, String methodName) {
return configStorageCommon.getConfigValue(classFQN, methodName);
public String getConfigValue(String classFQN, String methodName, String userLogin) {
String trustedPassword = webAuthConfig.getTrustedClientPassword();
return configStorageCommon.getConfigValue(classFQN, methodName, userLogin, trustedPassword);
}
}

View File

@ -37,8 +37,10 @@ public interface ConfigStorageMBean {
@ManagedOperation(description = "Invoke a getter method of configuration interface and print the result")
@ManagedOperationParameters({
@ManagedOperationParameter(name = "className", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "value", description = "Getter method name")
@ManagedOperationParameter(name = "classFQN", description = "Fully qualified name of a configuration interface"),
@ManagedOperationParameter(name = "methodName", description = "Getter method name"),
@ManagedOperationParameter(name = "userLogin", description = "User login that will be used for creating a user session. " +
"You can leave this field blank when using JMX console from CUBA application.")
})
String getConfigValue(String classFQN, String methodName);
String getConfigValue(String classFQN, String methodName, String userLogin);
}