Architecte J2EE je conçois et développe des solutions d'entreprise tout en essayant d'introduire les bonnes pratiques de développement issues du monde Agile.
Posted On lundi 19 mai 2008 at
à
07:47
by ehsavoie
Voici une amélioration du RepositoryFactoryBean pour Jackrabbit de spring-modules qui permet d'utiliser un fichier .properties pour remplacer des clefs dans le fichier de configuration repository.xml :
/** * Properties configuration the repository. */ private List configurationProperties; /** * Home directory for the repository. */ private Resource homeDir;
/** * Repository configuration created through Spring. */ private RepositoryConfig repositoryConfig;
if (this.configuration == null) { log.debug("no configuration resource specified, using the default one:" + DEFAULT_CONF_FILE); configuration = new ClassPathResource(DEFAULT_CONF_FILE); }
if (homeDir == null) { if (log.isDebugEnabled()) log.debug("no repository home dir specified, using the default one:" + DEFAULT_REP_DIR); homeDir = new FileSystemResource(DEFAULT_REP_DIR); } if (getConfigurationProperties() != null) { String goodConfig = replaceVariables(loadConfigurationKeys(), getConfiguration(configuration), true); repositoryConfig = RepositoryConfig.create(new InputSource( new StringReader(goodConfig)), homeDir.getFile().getAbsolutePath()); } else { repositoryConfig = RepositoryConfig.create(new InputSource(configuration .getInputStream()), homeDir.getFile().getAbsolutePath()); } }
/** * Performs variable replacement on the given string value. Each * <code>${...}</code> sequence within the given value is replaced with the * value of the named parser variable. If a variable is not found in the * properties an IllegalArgumentException is thrown unless * <code>ignoreMissing</code> is <code>true</code>. In the later case, * the missing variable is not replaced. * * @param value * the original value * @param ignoreMissing * if <code>true</code>, missing variables are not replaced. * @return value after variable replacements * @throws IllegalArgumentException * if the replacement of a referenced variable is not found */ public static String replaceVariables(Properties variables, String value, boolean ignoreMissing) throws IllegalArgumentException { StringBuffer result = new StringBuffer();
// Value: // +--+-+--------+-+-----------------+ // | |p|--> |q|--> | // +--+-+--------+-+-----------------+ int p = 0, q = value.indexOf("${"); // Find first ${ while (q != -1) { result.append(value.substring(p, q)); // Text before ${ p = q; q = value.indexOf("}", q + 2); // Find } if (q != -1) { String variable = value.substring(p + 2, q); String replacement = variables.getProperty(variable); if (replacement == null) { if (ignoreMissing) { replacement = "${" + variable + '}'; } else { throw new IllegalArgumentException("Replacement not found for ${" + variable + "}."); } } result.append(replacement); p = q + 1; q = value.indexOf("${", p); // Find next ${ } } result.append(value.substring(p, value.length())); // Trailing text return result.toString(); }
/** * Shutdown method. * */ public void destroy() throws Exception { // force cast (but use only the interface) if (repository instanceof JackrabbitRepository) ((JackrabbitRepository) repository).shutdown(); }
/** * @return Returns the defaultRepDir. */ public Resource getHomeDir() { return this.homeDir; }
/** * @param defaultRepDir * The defaultRepDir to set. */ public void setHomeDir(Resource defaultRepDir) { this.homeDir = defaultRepDir; }
/** * @return Returns the repositoryConfig. */ public RepositoryConfig getRepositoryConfig() { return this.repositoryConfig; }
/** * @param repositoryConfig * The repositoryConfig to set. */ public void setRepositoryConfig(RepositoryConfig repositoryConfig) { this.repositoryConfig = repositoryConfig; }
/** * @return Returns the configuration properties. */ public List getConfigurationProperties() { return configurationProperties; }
/** * @param configurationProperties * The configuration properties to set for the repository. */ public void setConfigurationProperties(List resources) { this.configurationProperties = resources; }
/** * Load a Resource as a String. * @param config the resource * @return the String filled with the content of the Resource * @throws IOException */ protected String getConfiguration(Resource config) throws IOException { StringWriter out = new StringWriter(); Reader reader = null; try { reader = new InputStreamReader(config.getInputStream()); char[] buffer = new char[8]; int c = 0; while ((c = reader.read(buffer)) > 0) { out.write(buffer, 0, c); } return out.toString(); } finally { if (reader != null) { reader.close(); } } } }
Posted On mercredi 30 avril 2008 at
à
00:53
by ehsavoie
Je travaille actuellement avec Jackrabbit. J'utilise spring-modules ce qui me permet de facilement instancier mon repository. Cependant comment avoir un repository pour mes tests unitaires. Grâce à ce post j'ai un respository de test tout simple. Voici donc les éléments clefs de mon fichier de configuration :