*/
package org.collectionspace;
+import java.io.PrintStream;
+
import net.sf.ehcache.CacheException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
final private static String OPTIONS_HELP = "help";
final private static String MSG_SEPARATOR = "--";
+ final private static String LOGGING_SEPARATOR_HEAD = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>";
+ final private static String LOGGING_SEPARATOR_TAIL = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
+ final private static String LOGGING_INFO_PREFIX = "[INFO] ";
+ final private static String LOGGING_ERROR_PREFIX = "[ERROR] ";
final private static boolean generateOnly(String param) {
boolean result = false;
}
return result;
}
+
+ //
+ // Private logging methods. We should try to get this code to use a logging utility like Log4j, Slf4j, etc.
+ // I'm not sure why we are not using a logging util? But at least we're consolidating all calls to System.out and Sytem.err.
+ //
+ private static void logError(String errMessage) {
+ System.out.println(LOGGING_ERROR_PREFIX + errMessage);
+ }
+
+ private static void logInfo(PrintStream outStream, String infoMessage) {
+ outStream.println(LOGGING_INFO_PREFIX + infoMessage);
+ }
+
+ private static void logInfo(String infoMessage) {
+ logInfo(System.out, infoMessage);
+ }
+
+ private static void logConfiguration(String user,
+ String password,
+ String tenantBinding,
+ String exportDir) {
+ logInfo(LOGGING_SEPARATOR_HEAD);
+ logInfo("Creating CollectionSpace authorization metadata using the following settings:");
+ logInfo("\tuser=" + user);
+ logInfo("\tpassword=" + password);
+ logInfo("\ttenantBinding=" + tenantBinding);
+ logInfo("\texportDir=" + exportDir);
+ logInfo(LOGGING_SEPARATOR_TAIL);
+ }
+
+ private static void printUsage(PrintStream outStream) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("\nUsage : java -cp <classpath> " + ImportAuthz.class.getName() + " <options>");
+ sb.append("\nOptions :");
+ sb.append("\n -g <" + OPTIONS_GENERATE_ONLY + "> generate only, do not seed AuthZ values in the security tables");
+ sb.append("\n -u <" + OPTIONS_USERNAME + "> cspace username");
+ sb.append("\n -p <" + OPTIONS_PASSWORD + "> password");
+ sb.append("\n -b <" + OPTIONS_TENANT_BINDING + "> tenant binding file (fully qualified path)");
+ sb.append("\n -edir <" + OPTIONS_EXPORT_DIR + "> directory to export authz data into");
+ logInfo(sb.toString());
+ }
+
+ private static void printUsage() {
+ printUsage(System.out);
+ }
+
+ private static void logInitialErrorCauseMsg(Throwable t) {
+ if (t != null) {
+ if (t.getCause() != null) {
+ logInitialErrorCauseMsg(t.getCause());
+ } else {
+ logError(t.getMessage());
+ }
+ }
+ }
+
+ private static Options createOptions() {
+ Options options = new Options();
+ options.addOption("g", true, OPTIONS_GENERATE_ONLY);
+ options.addOption("u", true, OPTIONS_USERNAME);
+ options.addOption("p", true, OPTIONS_PASSWORD);
+ options.addOption("b", true, OPTIONS_TENANT_BINDING);
+ options.addOption("edir", true, OPTIONS_EXPORT_DIR);
+ options.addOption("h", true, OPTIONS_HELP);
+ return options;
+ }
+ //
+ // End of logging methods.
+ //
+
+ //
+ // Create our AuthZ metadata
+ //
public static void main(String[] args) {
Options options = createOptions();
String password = line.getOptionValue("p");
String tenantBinding = line.getOptionValue("b");
String exportDir = line.getOptionValue("edir");
- System.out.println("user=" + user
- + " password=" + password
- + " tenantBinding=" + tenantBinding
- + " exportDir=" + exportDir);
+ logConfiguration(user, password, tenantBinding, exportDir);
+ //
+ // Instantiate an AuthZ seed driver and ask it to generate our AuthZ metadata
+ //
AuthorizationSeedDriver driver = new AuthorizationSeedDriver(
user, password, tenantBinding, exportDir);
driver.generate();
//
if (generateOnly(generate_only) == false) {
driver.seed();
- } {
- System.out.println("WARNING: '-g' was set to 'true' so AuthZ tables were not seeded.");
+ } else {
+ logError("WARNING: '-g' was set to 'true' so AuthZ tables were ***NOT*** seeded.");
}
} catch (ParseException exp) {
- // oops, something went wrong
- System.err.println("Parsing failed. Reason: " + exp.getMessage());
+ logError("Parsing failed. Reason: " + exp.getMessage());
} catch (Exception e) {
- System.out.println("Error : " + e.getMessage());
- System.out.println(MSG_SEPARATOR);
- printUsage();
- System.out.println(MSG_SEPARATOR);
- System.out.println("Import failed: ");
- printInitialErrorCauseMsg(e);
+ logError("Error : " + e.getMessage());
+ logError(MSG_SEPARATOR);
+ printUsage(System.err);
+ logError(MSG_SEPARATOR);
+ logError("Import failed: ");
+ logInitialErrorCauseMsg(e);
System.exit(1);
}
-
- }
-
- private static void printInitialErrorCauseMsg(Throwable t) {
- if (t != null) {
- if (t.getCause() != null) {
- printInitialErrorCauseMsg(t.getCause());
- } else {
- System.out.println(t.getMessage());
- }
- }
- }
-
- private static Options createOptions() {
- Options options = new Options();
- options.addOption("g", true, OPTIONS_GENERATE_ONLY);
- options.addOption("u", true, OPTIONS_USERNAME);
- options.addOption("p", true, OPTIONS_PASSWORD);
- options.addOption("b", true, OPTIONS_TENANT_BINDING);
- options.addOption("edir", true, OPTIONS_EXPORT_DIR);
- options.addOption("h", true, OPTIONS_HELP);
- return options;
- }
-
- private static void printUsage() {
- StringBuilder sb = new StringBuilder();
- sb.append("\nUsage : java -cp <classpath> " + ImportAuthz.class.getName() + " <options>");
- sb.append("\nOptions :");
- sb.append("\n -g <" + OPTIONS_GENERATE_ONLY + "> generate only, do not seed AuthZ values in the security tables");
- sb.append("\n -u <" + OPTIONS_USERNAME + "> cspace username");
- sb.append("\n -p <" + OPTIONS_PASSWORD + "> password");
- sb.append("\n -b <" + OPTIONS_TENANT_BINDING + "> tenant binding file (fully qualified path)");
- sb.append("\n -edir <" + OPTIONS_EXPORT_DIR + "> directory to export authz data into");
- System.out.println(sb.toString());
}
}
authzGen.exportDefaultPermissions(exportDir + File.separator + PERMISSION_FILE);
authzGen.exportDefaultPermissionRoles(exportDir + File.separator + PERMISSION_ROLE_FILE);
if (logger.isDebugEnabled()) {
- logger.debug("authorization generation completed ");
+ logger.debug("Authorization generation completed but not yet persisted.");
}
} catch (Exception ex) {
- if (logger.isDebugEnabled()) {
- ex.printStackTrace();
- }
+ logger.error("AuthorizationSeedDriver caught an exception: ", ex);
throw new RuntimeException(ex);
}
}
new String[]{SPRING_SECURITY_METADATA});
login();
System.setProperty("spring-beans-config", SPRING_SECURITY_METADATA);
+ // authZ local not used but call to AuthZ.get() has side-effect of initializing our Spring Security context
AuthZ authZ = AuthZ.get();
txManager = (org.springframework.jdbc.datasource.DataSourceTransactionManager) appContext.getBean("transactionManager");
if (logger.isDebugEnabled()) {
- logger.debug("spring setup complete");
+ logger.debug("Spring Security setup complete.");
}
}
Authentication authRequest = new UsernamePasswordAuthenticationToken(user, password, gauths);
SecurityContextHolder.getContext().setAuthentication(authRequest);
if (logger.isDebugEnabled()) {
- logger.debug("login successful for user=" + user);
+ logger.debug("Spring Security login successful for user=" + user);
}
}
private void logout() {
SecurityContextHolder.getContext().setAuthentication(null);
if (logger.isDebugEnabled()) {
- logger.debug("logged out user=" + user);
+ logger.debug("Spring Security logged out user=" + user);
}
}
authzStore.store(permRoleRel);
}
- if (logger.isDebugEnabled()) {
- logger.debug("authroization storage completed ");
+ if (logger.isInfoEnabled()) {
+ logger.info("Authroization metata persisted.");
}
-
}
private TransactionStatus beginTransaction(String name) {
import org.collectionspace.services.common.tenant.TenantBindingConfig;
import org.collectionspace.services.common.types.PropertyItemType;
-import ch.elca.el4j.util.codingsupport.Reject;
-import ch.elca.el4j.services.xmlmerge.AbstractXmlMergeException;
-import ch.elca.el4j.services.xmlmerge.ConfigurationException;
import ch.elca.el4j.services.xmlmerge.Configurer;
-import ch.elca.el4j.services.xmlmerge.XmlMerge;
import ch.elca.el4j.services.xmlmerge.config.AttributeMergeConfigurer;
import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
-import ch.elca.el4j.services.xmlmerge.config.PropertyXPathConfigurer;
-import ch.elca.el4j.services.xmlmerge.merge.DefaultXmlMerge;
-
-import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import ch.elca.el4j.services.xmlmerge.Configurer;
-import ch.elca.el4j.services.xmlmerge.config.AttributeMergeConfigurer;
-import ch.elca.el4j.services.xmlmerge.config.ConfigurableXmlMerge;
-
/**
* ServicesConfigReader reads service layer specific configuration
*
protected File getTenantsRootDir() {
File result = null;
- String tenantsRootPath = getConfigRootDir() + File.separator + TENANT_BINDINGS_ROOTDIRNAME;
- File tenantsRootDir = new File(tenantsRootPath);
- if (tenantsRootDir.exists() == true) {
- result = tenantsRootDir;
- logger.debug("Tenants home directory is: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isDebug() == true) check
- } else {
- logger.error("Tenants home directory is missing. Can't find: " + tenantsRootDir.getAbsolutePath()); //FIXME: REM - Add proper if (logger.isError() == true) check
+ String errMessage = null;
+ try {
+ String tenantsRootPath = getConfigRootDir() + File.separator + TENANT_BINDINGS_ROOTDIRNAME;
+ File tenantsRootDir = new File(tenantsRootPath);
+ if (tenantsRootDir.exists() == true) {
+ result = tenantsRootDir;
+ if (logger.isDebugEnabled() == true) {
+ logger.debug("The home directory for all tenants is at: " + result.getCanonicalPath());
+ }
+ } else {
+ errMessage = "The home directory for all tenants is missing or inaccesible: ";
+ try {
+ errMessage = errMessage + tenantsRootDir.getCanonicalPath();
+ } catch (IOException ioException) {
+ errMessage = errMessage + tenantsRootDir.getAbsolutePath();
+ }
+ }
+ } catch (IOException e) {
+ // Log this exception, but continue anyway. Caller should handle the null result gracefully.
+ logger.equals(e);
}
+
+ if (errMessage != null) {
+ logger.error(errMessage);
+ }
+
return result;
}
+
/*
* Take the directory of the prototype bindings and the directory of the delta bindings. Merge the two and create (replace) a file
* named "tenant-bindings.xml"
result = new ConfigurableXmlMerge(configurer).merge(inputStreamArray);
} catch (Exception e) {
logger.error("Could not merge tenant configuration delta file: " +
- deltaFile.getAbsolutePath(), e);
+ deltaFile.getCanonicalPath(), e);
}
//
// Try to save the merge output to a file that is suffixed with ".merged.xml" in the same directory
if (result != null) {
File outputDir = deltaFile.getParentFile();
String mergedFileName = outputDir.getAbsolutePath() + File.separator +
- this.TENANT_BINDINGS_FILENAME_PREFIX + MERGED_SUFFIX;
+ TenantBindingConfigReaderImpl.TENANT_BINDINGS_FILENAME_PREFIX + MERGED_SUFFIX;
File mergedOutFile = new File(mergedFileName);
try {
FileUtils.copyInputStreamToFile(result, mergedOutFile);
docTypes.put(docTypeKey, serviceBinding);
}
}
- if (logger.isDebugEnabled()) {
- logger.debug("readServiceBindings() added service "
+ if (logger.isTraceEnabled()) {
+ logger.trace("readServiceBindings() added service "
+ " name=" + key
+ " workspace=" + serviceBinding.getName());
}