Refactored InitializationHelper so it can take an InputStream
Also renamed it to PrivilegeInitializationHelper
This commit is contained in:
parent
d7f10a731f
commit
c4e6c5e65d
|
@ -17,9 +17,11 @@
|
||||||
* along with Privilege. If not, see <http://www.gnu.org/licenses/>.
|
* along with Privilege. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package ch.eitchnet.privilege.xml;
|
package ch.eitchnet.privilege.helper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -30,9 +32,9 @@ import ch.eitchnet.privilege.handler.DefaultPrivilegeHandler;
|
||||||
import ch.eitchnet.privilege.handler.EncryptionHandler;
|
import ch.eitchnet.privilege.handler.EncryptionHandler;
|
||||||
import ch.eitchnet.privilege.handler.PersistenceHandler;
|
import ch.eitchnet.privilege.handler.PersistenceHandler;
|
||||||
import ch.eitchnet.privilege.handler.PrivilegeHandler;
|
import ch.eitchnet.privilege.handler.PrivilegeHandler;
|
||||||
import ch.eitchnet.privilege.helper.ClassHelper;
|
|
||||||
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
|
import ch.eitchnet.privilege.model.internal.PrivilegeContainerModel;
|
||||||
import ch.eitchnet.privilege.policy.PrivilegePolicy;
|
import ch.eitchnet.privilege.policy.PrivilegePolicy;
|
||||||
|
import ch.eitchnet.privilege.xml.PrivilegeConfigSaxReader;
|
||||||
import ch.eitchnet.utils.helper.XmlHelper;
|
import ch.eitchnet.utils.helper.XmlHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,9 +43,9 @@ import ch.eitchnet.utils.helper.XmlHelper;
|
||||||
*
|
*
|
||||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||||
*/
|
*/
|
||||||
public class InitializationHelper {
|
public class PrivilegeInitializationHelper {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(InitializationHelper.class);
|
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
|
||||||
|
@ -51,7 +53,8 @@ public class InitializationHelper {
|
||||||
* @param privilegeXmlFile
|
* @param privilegeXmlFile
|
||||||
* a {@link File} reference to the XML file containing the configuration for Privilege
|
* a {@link File} reference to the XML file containing the configuration for Privilege
|
||||||
*
|
*
|
||||||
* @return the {@link PrivilegeHandler} instance loaded from the configuration file
|
* @return the initialized {@link PrivilegeHandler} where the {@link EncryptionHandler} and
|
||||||
|
* {@link PersistenceHandler} are set and initialized as well
|
||||||
*/
|
*/
|
||||||
public static PrivilegeHandler initializeFromXml(File privilegeXmlFile) {
|
public static PrivilegeHandler initializeFromXml(File privilegeXmlFile) {
|
||||||
|
|
||||||
|
@ -60,10 +63,31 @@ public class InitializationHelper {
|
||||||
throw new PrivilegeException("Privilege file does not exist at path " + privilegeXmlFile.getAbsolutePath());
|
throw new PrivilegeException("Privilege file does not exist at path " + privilegeXmlFile.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delegate using input stream
|
||||||
|
try {
|
||||||
|
return initializeFromXml(new FileInputStream(privilegeXmlFile));
|
||||||
|
} catch (Exception e) {
|
||||||
|
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
||||||
|
throw new PrivilegeException("Failed to load configuration from " + privilegeXmlFile.getAbsolutePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the {@link PrivilegeHandler} by loading from the given input stream. This stream must be a valid XML
|
||||||
|
* source
|
||||||
|
*
|
||||||
|
* @param privilegeConfigInputStream
|
||||||
|
* the XML stream containing the privilege configuration
|
||||||
|
*
|
||||||
|
* @return the initialized {@link PrivilegeHandler} where the {@link EncryptionHandler} and
|
||||||
|
* {@link PersistenceHandler} are set and initialized as well
|
||||||
|
*/
|
||||||
|
public static PrivilegeHandler initializeFromXml(InputStream privilegeConfigInputStream) {
|
||||||
|
|
||||||
// parse configuration file
|
// parse configuration file
|
||||||
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
|
PrivilegeContainerModel containerModel = new PrivilegeContainerModel();
|
||||||
PrivilegeConfigSaxReader xmlHandler = new PrivilegeConfigSaxReader(containerModel);
|
PrivilegeConfigSaxReader xmlHandler = new PrivilegeConfigSaxReader(containerModel);
|
||||||
XmlHelper.parseDocument(privilegeXmlFile, xmlHandler);
|
XmlHelper.parseDocument(privilegeConfigInputStream, xmlHandler);
|
||||||
|
|
||||||
// initialize encryption handler
|
// initialize encryption handler
|
||||||
String encryptionHandlerClassName = containerModel.getEncryptionHandlerClassName();
|
String encryptionHandlerClassName = containerModel.getEncryptionHandlerClassName();
|
||||||
|
@ -72,7 +96,7 @@ public class InitializationHelper {
|
||||||
try {
|
try {
|
||||||
encryptionHandler.initialize(parameterMap);
|
encryptionHandler.initialize(parameterMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
InitializationHelper.logger.error(e.getMessage(), e);
|
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
||||||
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
|
throw new PrivilegeException("EncryptionHandler " + encryptionHandlerClassName
|
||||||
+ " could not be initialized");
|
+ " could not be initialized");
|
||||||
}
|
}
|
||||||
|
@ -84,7 +108,7 @@ public class InitializationHelper {
|
||||||
try {
|
try {
|
||||||
persistenceHandler.initialize(parameterMap);
|
persistenceHandler.initialize(parameterMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
InitializationHelper.logger.error(e.getMessage(), e);
|
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
||||||
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerClassName
|
throw new PrivilegeException("PersistenceHandler " + persistenceHandlerClassName
|
||||||
+ " could not be initialized");
|
+ " could not be initialized");
|
||||||
}
|
}
|
||||||
|
@ -96,7 +120,7 @@ public class InitializationHelper {
|
||||||
try {
|
try {
|
||||||
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
|
privilegeHandler.initialize(parameterMap, encryptionHandler, persistenceHandler, policyMap);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
InitializationHelper.logger.error(e.getMessage(), e);
|
PrivilegeInitializationHelper.logger.error(e.getMessage(), e);
|
||||||
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
|
throw new PrivilegeException("PrivilegeHandler " + privilegeHandler.getClass().getName()
|
||||||
+ " could not be initialized");
|
+ " could not be initialized");
|
||||||
}
|
}
|
|
@ -36,6 +36,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import ch.eitchnet.privilege.base.AccessDeniedException;
|
import ch.eitchnet.privilege.base.AccessDeniedException;
|
||||||
import ch.eitchnet.privilege.base.PrivilegeException;
|
import ch.eitchnet.privilege.base.PrivilegeException;
|
||||||
import ch.eitchnet.privilege.handler.PrivilegeHandler;
|
import ch.eitchnet.privilege.handler.PrivilegeHandler;
|
||||||
|
import ch.eitchnet.privilege.helper.PrivilegeInitializationHelper;
|
||||||
import ch.eitchnet.privilege.model.Certificate;
|
import ch.eitchnet.privilege.model.Certificate;
|
||||||
import ch.eitchnet.privilege.model.PrivilegeContext;
|
import ch.eitchnet.privilege.model.PrivilegeContext;
|
||||||
import ch.eitchnet.privilege.model.PrivilegeRep;
|
import ch.eitchnet.privilege.model.PrivilegeRep;
|
||||||
|
@ -46,7 +47,6 @@ import ch.eitchnet.privilege.model.UserState;
|
||||||
import ch.eitchnet.privilege.test.model.TestRestrictable;
|
import ch.eitchnet.privilege.test.model.TestRestrictable;
|
||||||
import ch.eitchnet.privilege.test.model.TestSystemUserAction;
|
import ch.eitchnet.privilege.test.model.TestSystemUserAction;
|
||||||
import ch.eitchnet.privilege.test.model.TestSystemUserActionDeny;
|
import ch.eitchnet.privilege.test.model.TestSystemUserActionDeny;
|
||||||
import ch.eitchnet.privilege.xml.InitializationHelper;
|
|
||||||
import ch.eitchnet.utils.helper.ArraysHelper;
|
import ch.eitchnet.utils.helper.ArraysHelper;
|
||||||
import ch.eitchnet.utils.helper.FileHelper;
|
import ch.eitchnet.utils.helper.FileHelper;
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ public class PrivilegeTest {
|
||||||
File privilegeConfigFile = new File(pwd + "/config/Privilege.xml");
|
File privilegeConfigFile = new File(pwd + "/config/Privilege.xml");
|
||||||
|
|
||||||
// initialize privilege
|
// initialize privilege
|
||||||
privilegeHandler = InitializationHelper.initializeFromXml(privilegeConfigFile);
|
privilegeHandler = PrivilegeInitializationHelper.initializeFromXml(privilegeConfigFile);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
|
|
|
@ -33,7 +33,6 @@ import java.util.Set;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -81,7 +80,7 @@ public class XmlTest {
|
||||||
tmpDir.mkdirs();
|
tmpDir.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
//@AfterClass
|
||||||
public static void destroy() throws Exception {
|
public static void destroy() throws Exception {
|
||||||
|
|
||||||
File tmpDir = new File("target/test");
|
File tmpDir = new File("target/test");
|
||||||
|
|
Loading…
Reference in New Issue