[Minor] Changed exceptions to contain cause and code clean up
This commit is contained in:
parent
01884029b8
commit
a8b0f37834
|
@ -20,6 +20,7 @@
|
||||||
package ch.eitchnet.privilege.handler;
|
package ch.eitchnet.privilege.handler;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.text.MessageFormat;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
@ -56,7 +57,7 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
|
|
||||||
private Map<String, String> parameterMap;
|
private Map<String, String> parameterMap;
|
||||||
|
|
||||||
private String modelPath;
|
private File modelPath;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<User> getAllUsers() {
|
public List<User> getAllUsers() {
|
||||||
|
@ -125,22 +126,35 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
String basePath = this.parameterMap.get(XmlConstants.XML_PARAM_BASE_PATH);
|
String basePath = this.parameterMap.get(XmlConstants.XML_PARAM_BASE_PATH);
|
||||||
File basePathF = new File(basePath);
|
File basePathF = new File(basePath);
|
||||||
if (!basePathF.exists() && !basePathF.isDirectory()) {
|
if (!basePathF.exists() && !basePathF.isDirectory()) {
|
||||||
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
|
String msg = "[{0}] Defined parameter {1} does not point to a valid path at {2}"; //$NON-NLS-1$
|
||||||
+ XmlConstants.XML_PARAM_BASE_PATH + " is invalid");
|
msg = MessageFormat.format(msg, PersistenceHandler.class.getName(), XmlConstants.XML_PARAM_BASE_PATH,
|
||||||
|
basePathF.getAbsolutePath());
|
||||||
|
throw new PrivilegeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get model file name
|
// get model file name
|
||||||
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
|
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
|
||||||
if (modelFileName == null || modelFileName.isEmpty()) {
|
if (modelFileName == null || modelFileName.isEmpty()) {
|
||||||
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
|
String msg = "[{0}] Defined parameter {1} is not valid as it is empty!"; //$NON-NLS-1$
|
||||||
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid");
|
msg = MessageFormat.format(msg, PersistenceHandler.class.getName(), XmlConstants.XML_PARAM_MODEL_FILE);
|
||||||
|
throw new PrivilegeException(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate file exists
|
||||||
|
String modelPathS = basePath + "/" + modelFileName; //$NON-NLS-1$
|
||||||
|
File modelPath = new File(modelPathS);
|
||||||
|
if (!modelPath.exists()) {
|
||||||
|
String msg = "[{0}] Defined parameter {1} is invalid as model file does not exist at path {2}"; //$NON-NLS-1$
|
||||||
|
msg = MessageFormat.format(msg, PersistenceHandler.class.getName(), XmlConstants.XML_PARAM_MODEL_FILE,
|
||||||
|
modelPath.getAbsolutePath());
|
||||||
|
throw new PrivilegeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// save path to model
|
// save path to model
|
||||||
this.modelPath = basePath + "/" + modelFileName;
|
this.modelPath = modelPath;
|
||||||
|
|
||||||
if (reload())
|
if (reload())
|
||||||
XmlPersistenceHandler.logger.info("Privilege Data loaded.");
|
logger.info("Privilege Data loaded."); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -152,22 +166,14 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
@Override
|
@Override
|
||||||
public boolean reload() {
|
public boolean reload() {
|
||||||
|
|
||||||
// validate file exists
|
|
||||||
File modelsFile = new File(this.modelPath);
|
|
||||||
if (!modelsFile.exists()) {
|
|
||||||
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
|
|
||||||
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid as models file does not exist at path "
|
|
||||||
+ modelsFile.getAbsolutePath());
|
|
||||||
}
|
|
||||||
|
|
||||||
this.roleMap = Collections.synchronizedMap(new HashMap<String, Role>());
|
this.roleMap = Collections.synchronizedMap(new HashMap<String, Role>());
|
||||||
this.userMap = Collections.synchronizedMap(new HashMap<String, User>());
|
this.userMap = Collections.synchronizedMap(new HashMap<String, User>());
|
||||||
|
|
||||||
// parse models xml file to XML document
|
// parse models xml file to XML document
|
||||||
PrivilegeModelSaxReader xmlHandler = new PrivilegeModelSaxReader();
|
PrivilegeModelSaxReader xmlHandler = new PrivilegeModelSaxReader();
|
||||||
XmlHelper.parseDocument(modelsFile, xmlHandler);
|
XmlHelper.parseDocument(this.modelPath, xmlHandler);
|
||||||
|
|
||||||
this.modelsFileDate = modelsFile.lastModified();
|
this.modelsFileDate = this.modelPath.lastModified();
|
||||||
|
|
||||||
// ROLES
|
// ROLES
|
||||||
List<Role> roles = xmlHandler.getRoles();
|
List<Role> roles = xmlHandler.getRoles();
|
||||||
|
@ -184,8 +190,8 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
this.userMapDirty = false;
|
this.userMapDirty = false;
|
||||||
this.roleMapDirty = false;
|
this.roleMapDirty = false;
|
||||||
|
|
||||||
XmlPersistenceHandler.logger.info("Read " + this.userMap.size() + " Users");
|
logger.info(MessageFormat.format("Read {0} Users", this.userMap.size())); //$NON-NLS-1$
|
||||||
XmlPersistenceHandler.logger.info("Read " + this.roleMap.size() + " Roles");
|
logger.info(MessageFormat.format("Read {0} Roles", this.roleMap.size())); //$NON-NLS-1$
|
||||||
|
|
||||||
// validate we have a user with PrivilegeAdmin access
|
// validate we have a user with PrivilegeAdmin access
|
||||||
boolean privilegeAdminExists = false;
|
boolean privilegeAdminExists = false;
|
||||||
|
@ -198,8 +204,9 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!privilegeAdminExists) {
|
if (!privilegeAdminExists) {
|
||||||
XmlPersistenceHandler.logger.warn("No User with role '" + PrivilegeHandler.PRIVILEGE_ADMIN_ROLE
|
String msg = "No User with role ''{0}'' exists. Privilege modifications will not be possible!"; //$NON-NLS-1$
|
||||||
+ "' exists. Privilege modifications will not be possible!");
|
msg = MessageFormat.format(msg, PrivilegeHandler.PRIVILEGE_ADMIN_ROLE);
|
||||||
|
logger.warn(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -214,21 +221,20 @@ public class XmlPersistenceHandler implements PersistenceHandler {
|
||||||
// get models file name
|
// get models file name
|
||||||
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
|
String modelFileName = this.parameterMap.get(XmlConstants.XML_PARAM_MODEL_FILE);
|
||||||
if (modelFileName == null || modelFileName.isEmpty()) {
|
if (modelFileName == null || modelFileName.isEmpty()) {
|
||||||
throw new PrivilegeException("[" + PersistenceHandler.class.getName() + "] Defined parameter "
|
String msg = "[{0}] Defined parameter {1} is invalid"; //$NON-NLS-1$
|
||||||
+ XmlConstants.XML_PARAM_MODEL_FILE + " is invalid");
|
msg = MessageFormat.format(msg, PersistenceHandler.class.getName(), XmlConstants.XML_PARAM_MODEL_FILE);
|
||||||
|
throw new PrivilegeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// get model file
|
// get model file
|
||||||
File modelFile = new File(this.modelPath);
|
boolean modelFileUnchanged = this.modelPath.exists() && this.modelPath.lastModified() == this.modelsFileDate;
|
||||||
boolean modelFileUnchanged = modelFile.exists() && modelFile.lastModified() == this.modelsFileDate;
|
|
||||||
if (modelFileUnchanged && !this.roleMapDirty && !this.userMapDirty) {
|
if (modelFileUnchanged && !this.roleMapDirty && !this.userMapDirty) {
|
||||||
XmlPersistenceHandler.logger
|
logger.warn("Not persisting as current file is unchanged and model data is not dirty"); //$NON-NLS-1$
|
||||||
.warn("Not persisting as current file is unchanged and model data is not dirty");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// delegate writing
|
// delegate writing
|
||||||
PrivilegeModelDomWriter modelWriter = new PrivilegeModelDomWriter(getAllUsers(), getAllRoles(), modelFile);
|
PrivilegeModelDomWriter modelWriter = new PrivilegeModelDomWriter(getAllUsers(), getAllRoles(), this.modelPath);
|
||||||
modelWriter.write();
|
modelWriter.write();
|
||||||
|
|
||||||
// reset dirty states
|
// reset dirty states
|
||||||
|
|
|
@ -22,11 +22,9 @@ package ch.eitchnet.privilege.helper;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.text.MessageFormat;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import ch.eitchnet.privilege.base.PrivilegeException;
|
import ch.eitchnet.privilege.base.PrivilegeException;
|
||||||
import ch.eitchnet.privilege.handler.DefaultPrivilegeHandler;
|
import ch.eitchnet.privilege.handler.DefaultPrivilegeHandler;
|
||||||
import ch.eitchnet.privilege.handler.EncryptionHandler;
|
import ch.eitchnet.privilege.handler.EncryptionHandler;
|
||||||
|
@ -45,8 +43,6 @@ import ch.eitchnet.utils.helper.XmlHelper;
|
||||||
*/
|
*/
|
||||||
public class PrivilegeInitializationHelper {
|
public class PrivilegeInitializationHelper {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PrivilegeInitializationHelper.class);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the {@link DefaultPrivilegeHandler} from the configuration file
|
* Initializes the {@link DefaultPrivilegeHandler} from the configuration file
|
||||||
*
|
*
|
||||||
|
@ -60,15 +56,18 @@ public class PrivilegeInitializationHelper {
|
||||||
|
|
||||||
// make sure file exists
|
// make sure file exists
|
||||||
if (!privilegeXmlFile.exists()) {
|
if (!privilegeXmlFile.exists()) {
|
||||||
throw new PrivilegeException("Privilege file does not exist at path " + privilegeXmlFile.getAbsolutePath());
|
String msg = "Privilege file does not exist at path {0}"; //$NON-NLS-1$
|
||||||
|
msg = MessageFormat.format(msg, privilegeXmlFile.getAbsolutePath());
|
||||||
|
throw new PrivilegeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// delegate using input stream
|
// delegate using input stream
|
||||||
try {
|
try {
|
||||||
return initializeFromXml(new FileInputStream(privilegeXmlFile));
|
return initializeFromXml(new FileInputStream(privilegeXmlFile));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
String msg = "Failed to load configuration from {0}"; //$NON-NLS-1$
|
||||||
throw new PrivilegeException("Failed to load configuration from " + privilegeXmlFile.getAbsolutePath());
|
msg = MessageFormat.format(msg, privilegeXmlFile.getAbsolutePath());
|
||||||
|
throw new PrivilegeException(msg, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,9 +95,9 @@ public class PrivilegeInitializationHelper {
|
||||||
try {
|
try {
|
||||||
encryptionHandler.initialize(parameterMap);
|
encryptionHandler.initialize(parameterMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
String msg = "EncryptionHandler {0} could not be initialized"; //$NON-NLS-1$
|
||||||
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
|
msg = MessageFormat.format(msg, encryptionHandlerClassName);
|
||||||
+ " could not be initialized");
|
throw new PrivilegeException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize persistence handler
|
// initialize persistence handler
|
||||||
|
@ -108,9 +107,9 @@ public class PrivilegeInitializationHelper {
|
||||||
try {
|
try {
|
||||||
persistenceHandler.initialize(parameterMap);
|
persistenceHandler.initialize(parameterMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
String msg = "PersistenceHandler {0} could not be initialized"; //$NON-NLS-1$
|
||||||
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerClassName
|
msg = MessageFormat.format(msg, persistenceHandlerClassName);
|
||||||
+ " could not be initialized");
|
throw new PrivilegeException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize privilege handler
|
// initialize privilege handler
|
||||||
|
@ -120,9 +119,9 @@ public class PrivilegeInitializationHelper {
|
||||||
try {
|
try {
|
||||||
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
|
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
String msg = "PrivilegeHandler {0} could not be initialized"; //$NON-NLS-1$
|
||||||
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
|
msg = MessageFormat.format(msg, privilegeHandler.getClass().getName());
|
||||||
+ " could not be initialized");
|
throw new PrivilegeException(msg, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return privilegeHandler;
|
return privilegeHandler;
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class PrivilegeContext {
|
||||||
*/
|
*/
|
||||||
public void validateAction(Restrictable restrictable) throws AccessDeniedException, PrivilegeException {
|
public void validateAction(Restrictable restrictable) throws AccessDeniedException, PrivilegeException {
|
||||||
|
|
||||||
// the the privilege for the restrictable
|
// the privilege for the restrictable
|
||||||
String privilegeName = restrictable.getPrivilegeName();
|
String privilegeName = restrictable.getPrivilegeName();
|
||||||
IPrivilege privilege = this.privileges.get(privilegeName);
|
IPrivilege privilege = this.privileges.get(privilegeName);
|
||||||
if (privilege == null) {
|
if (privilege == null) {
|
||||||
|
|
Loading…
Reference in New Issue