@Test @Transactional public void testCreateDomainStorage() throws Exception { Domain domain = new Domain(); domain.setName("TestCreation"); File tmpFile = null; try { tmpFile = File.createTempFile("domain", "TestCreation"); populateFile(tmpFile); // Mock FileRepositoryManager mockStatic(FileRepositoryManager.class); when(FileRepositoryManager.getDomainPropertiesPath("TestCreation")) .thenReturn(tmpFile.getAbsolutePath()); // Create domain storage dao.createDomainStorage(domain); // Looks for domain created tables testTablesExistence(true); } catch (Exception e) { tmpFile.delete(); throw e; } }
/** * Remove domain authentication and settings properties file * * @param domainToRemove domain to remove * @throws DomainDeletionException */ private void removeDomainPropertiesFile(Domain domainToRemove) throws DomainDeletionException { SilverTrace.info( "admin", "SQLDomainService.removeDomainAuthenticationPropertiesFile()", "root.MSG_GEN_ENTER_METHOD"); String domainName = domainToRemove.getName(); String domainPropertiesPath = FileRepositoryManager.getDomainPropertiesPath(domainName); String authenticationPropertiesPath = FileRepositoryManager.getDomainAuthenticationPropertiesPath(domainName); File domainPropertiesFile = new File(domainPropertiesPath); File authenticationPropertiesFile = new File(authenticationPropertiesPath); boolean domainPropertiesFileDeleted = domainPropertiesFile.delete(); boolean authenticationPropertiesFileDeleted = authenticationPropertiesFile.delete(); if ((!domainPropertiesFileDeleted) || (!authenticationPropertiesFileDeleted)) { SilverTrace.warn( "admin", "SQLDomainService.removeDomainAuthenticationPropertiesFile()", "admin.EX_DELETE_DOMAIN_PROPERTIES", "domainPropertiesFileDeleted:" + domainPropertiesFileDeleted + ", authenticationPropertiesFileDeleted:" + authenticationPropertiesFileDeleted); } }
/** * Delete phisycally domain and authentication properties files * * @param domainName domain name concerned */ private void removePropertiesFiles(String domainName) { String authenticationPropertiesPath = FileRepositoryManager.getDomainAuthenticationPropertiesPath(domainName); String domainPropertiesPath = FileRepositoryManager.getDomainPropertiesPath(domainName); new File(authenticationPropertiesPath).delete(); new File(domainPropertiesPath).delete(); }
private String generateUserTableCreateStatement(String domainName) throws IOException { Properties props = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream(FileRepositoryManager.getDomainPropertiesPath(domainName)); props.load(fis); } finally { IOUtils.closeQuietly(fis); } int numberOfColumns = Integer.parseInt(props.getProperty("property.Number")); StringBuilder createStatement = new StringBuilder(); createStatement.append("CREATE TABLE Domain").append(domainName).append("_User "); createStatement.append("("); // Common columns createStatement.append("id int NOT NULL , firstName varchar(100) NULL , "); createStatement.append("lastName varchar(100) NULL ," + "email varchar(200) NULL , "); createStatement.append("login varchar(50) NOT NULL ," + "password varchar(123) NULL , "); createStatement.append("passwordValid char(1) NULL , "); // Domain specific columns String specificColumnName; String specificColumnType; int specificColumnMaxLength; for (int i = 1; i <= numberOfColumns; i++) { specificColumnType = props.getProperty("property_" + String.valueOf(i) + ".Type"); specificColumnName = props.getProperty("property_" + String.valueOf(i) + ".MapParameter"); String maxLengthPropertyValue = props.getProperty("property_" + String.valueOf(i) + ".MaxLength"); if (StringUtil.isInteger(maxLengthPropertyValue)) { specificColumnMaxLength = Integer.parseInt(maxLengthPropertyValue); } else { specificColumnMaxLength = DomainProperty.DEFAULT_MAX_LENGTH; } createStatement.append(specificColumnName); if ("BOOLEAN".equals(specificColumnType)) { createStatement.append(" int NOT NULL DEFAULT (0) "); } else { createStatement.append(" varchar(").append(specificColumnMaxLength).append(") NULL "); } if (i != numberOfColumns) { createStatement.append(", "); } } createStatement.append(")"); return createStatement.toString(); }
/** * Generates domain authentication properties file * * @param domainToCreate domain to create * @throws DomainCreationException */ private void generateDomainAuthenticationPropertiesFile(Domain domainToCreate) throws DomainCreationException { SilverTrace.info( "admin", "SQLDomainService.generateDomainAuthenticationPropertiesFile()", "root.MSG_GEN_ENTER_METHOD"); String domainName = domainToCreate.getName(); String domainPropertiesPath = FileRepositoryManager.getDomainPropertiesPath(domainName); String authenticationPropertiesPath = FileRepositoryManager.getDomainAuthenticationPropertiesPath(domainName); boolean allowPasswordChange = templateSettings.getBoolean("allowPasswordChange", true); String cryptMethod = templateSettings.getString("database.SQLPasswordEncryption", Authentication.ENC_TYPE_MD5); SilverpeasTemplate template = getNewTemplate(); template.setAttribute("SQLPasswordEncryption", cryptMethod); template.setAttribute("allowPasswordChange", allowPasswordChange); template.setAttribute("SQLDriverClass", adminSettings.getString("AdminDBDriver")); template.setAttribute("SQLJDBCUrl", adminSettings.getString("WaProductionDb")); template.setAttribute("SQLAccessLogin", adminSettings.getString("WaProductionUser")); template.setAttribute("SQLAccessPasswd", adminSettings.getString("WaProductionPswd")); template.setAttribute("SQLUserTableName", "Domain" + domainName + "_User"); File domainPropertiesFile = new File(domainPropertiesPath); File authenticationPropertiesFile = new File(authenticationPropertiesPath); PrintWriter out = null; try { out = new PrintWriter(new FileWriter(authenticationPropertiesFile)); out.print(template.applyFileTemplate("templateDomainAuthenticationSQL")); } catch (IOException e) { domainPropertiesFile.delete(); authenticationPropertiesFile.delete(); throw new DomainCreationException( "SQLDomainService.generateDomainAuthenticationPropertiesFile()", domainToCreate.toString(), e); } finally { out.close(); } }
@Override protected void checkDomainName(String domainName) throws DomainConflictException, AdminException { // Commons checks super.checkDomainName(domainName); // Check properties files availability // com.stratelia.silverpeas.domains.domain<domainName>.properties // com.stratelia.silverpeas.authentication.autDomain<domainName>.properties String authenticationPropertiesPath = FileRepositoryManager.getDomainAuthenticationPropertiesPath(domainName); String domainPropertiesPath = FileRepositoryManager.getDomainPropertiesPath(domainName); if (new File(authenticationPropertiesPath).exists()) { throw new DomainAuthenticationPropertiesAlreadyExistsException(domainName); } if (new File(domainPropertiesPath).exists()) { throw new DomainPropertiesAlreadyExistsException(domainName); } }