r1156 - in trunk/simexplorer-is: simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util
Author: tchemit Date: 2008-02-20 23:09:00 +0000 (Wed, 20 Feb 2008) New Revision: 1156 Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/Config.java Log: refactoring config to allowed speficif config file in local service Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-20 23:06:46 UTC (rev 1155) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-20 23:09:00 UTC (rev 1156) @@ -17,24 +17,39 @@ * ##% */ package fr.cemagref.simexplorer.is.service; -import static org.codelutin.i18n.I18n._; import fr.cemagref.simexplorer.is.entities.metadata.Version; import fr.cemagref.simexplorer.is.exceptions.SimExplorerBusinessException; import fr.cemagref.simexplorer.is.exceptions.SimExplorerException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; import fr.cemagref.simexplorer.is.storage.engine.StorageEngineImpl; +import static org.codelutin.i18n.I18n._; -/** - * The Class StorageServiceClient. - */ +/** The Class StorageServiceClient. */ public class StorageServiceClient extends StorageServiceCommon { /** The storage engine. */ private StorageEngine storageEngine; + /** + * la localisation du fichier de configuration à lire, si null utilisera le fichier + * par défaut (properties/config.properties) + */ + private String configFile; + /** le prefixe à supprimera chaque clef de la configuration. (voir Config) * */ + private String prefixKey; + + public StorageServiceClient() { + this(null, null); + } + + public StorageServiceClient(String configFile, String prefixKey) { + this.configFile = configFile; + this.prefixKey = prefixKey; + } + /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#loginUser(java.lang.String, java.lang.String) - */ + * @see fr.cemagref.simexplorer.is.service.StorageService#loginUser(java.lang.String, java.lang.String) + */ public String loginUser(String login, String password) throws SimExplorerException { throw new SimExplorerBusinessException( @@ -48,7 +63,7 @@ protected StorageEngine getStorageEngine() throws SimExplorerException { if (storageEngine == null) { - storageEngine = new StorageEngineImpl(); + storageEngine = new StorageEngineImpl(configFile, prefixKey); storageEngine.open(); } return storageEngine; Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-20 23:06:46 UTC (rev 1155) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-20 23:09:00 UTC (rev 1156) @@ -50,24 +50,16 @@ * maitriser la configuration. */ public FileSystemAttachmentHandler() { - baseFolder = Config.getProperties().getProperty("simexplorer.data"); + baseFolder = Config.getProperties().getProperty(Config.DATA_PROPERTY); } /** - * Constructeur utilisant une configuration spécifique - * - * @param baseFolder le répertoire de base - */ - public FileSystemAttachmentHandler(String baseFolder) { - this.baseFolder = baseFolder; - } - - /** * Retrieve file associated to content Create directories associated to file. * * @param entity DataEntity related to content * @param attachment the attachment * + * @param createDir flag to create directory if not exists * @return Instance of file */ private File getFile(MetaData entity, Attachment attachment, boolean createDir) { Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-20 23:06:46 UTC (rev 1155) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-20 23:09:00 UTC (rev 1156) @@ -23,11 +23,10 @@ import java.util.ArrayList; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.StringTokenizer; +import java.util.Properties; import java.util.Map.Entry; import org.apache.commons.logging.Log; @@ -77,11 +76,10 @@ private static final Log log = LogFactory.getLog(LuceneDatabase.class); /** DB folder. */ - private static String dbFolder = Config.getProperties().getProperty("simexplorer.db"); + private String dbFolder; /** Optimize period. */ - private static Integer optimizePeriod = new Integer(Config.getProperties() - .getProperty("simexplorer.optimizeperiod")); + private Integer optimizePeriod; /** Lock factory for Lucene writer. */ private static LockFactory lockFactory = new NoLockFactory(); @@ -107,6 +105,18 @@ /** The Constant writerSynchronizer. */ private final static Object writerSynchronizer = new Object(); + public LuceneDatabase() { + Properties config = Config.getProperties(); + this.dbFolder = String.valueOf(config.getProperty(Config.DB_FOLDER_PROPERTY)); + this.optimizePeriod = Integer.valueOf(config.getProperty(Config.OPTIMIZE_PERIOD_PROPERTY)); + log.info(this); + } + + @Override + public String toString() { + return super.toString()+"<dbFolder:"+dbFolder+", optimizePeriod:"+optimizePeriod+">"; + } + /** * Gets the searcher pool. * @@ -342,7 +352,8 @@ properties.put(KEY_VERSION, version.toString()); // Search elements - Hits hits = findHits(properties, searcher, SORT_DATE_NONE); + Hits hits; + hits = findHits(properties, searcher, SORT_DATE_NONE); return hits; } @@ -510,7 +521,7 @@ * @return null if no sort */ private Sort getSortDate(int dateOrder) { - Sort sort = null; + Sort sort; if (dateOrder != SORT_DATE_NONE) { SortField[] fields = new SortField[3]; fields[1] = SortField.FIELD_SCORE; @@ -633,8 +644,8 @@ query.add(new TermQuery(new Term(kv.getKey(), kv.getValue())), BooleanClause.Occur.MUST); } - Hits hits = searcher.search(query, getSortDate(dateOrder)); - + Hits hits; + hits = searcher.search(query, getSortDate(dateOrder)); return hits; } @@ -889,7 +900,7 @@ * @param toField the to field * * @return the associated elements - * @throws SimExplorerException + * @throws SimExplorerException if any pb */ protected List<MetaData> getAssociatedElements(MetaData mde, String fromField, String toField) throws SimExplorerException { Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-20 23:06:46 UTC (rev 1155) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-20 23:09:00 UTC (rev 1156) @@ -17,6 +17,16 @@ * ##% */ package fr.cemagref.simexplorer.is.storage.engine; +import fr.cemagref.simexplorer.is.entities.attachment.Attachment; +import fr.cemagref.simexplorer.is.entities.metadata.MetaData; +import fr.cemagref.simexplorer.is.entities.metadata.Version; +import fr.cemagref.simexplorer.is.exceptions.SimExplorerException; +import fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler; +import fr.cemagref.simexplorer.is.storage.attachment.FileSystemAttachmentHandler; +import fr.cemagref.simexplorer.is.storage.database.Database; +import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; +import fr.cemagref.simexplorer.is.storage.util.Config; + import java.io.InputStream; import java.io.Reader; import java.io.StringReader; @@ -27,18 +37,9 @@ import java.util.Set; import java.util.UUID; -import fr.cemagref.simexplorer.is.entities.attachment.Attachment; -import fr.cemagref.simexplorer.is.entities.metadata.MetaData; -import fr.cemagref.simexplorer.is.entities.metadata.Version; -import fr.cemagref.simexplorer.is.exceptions.SimExplorerException; -import fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler; -import fr.cemagref.simexplorer.is.storage.attachment.FileSystemAttachmentHandler; -import fr.cemagref.simexplorer.is.storage.database.Database; -import fr.cemagref.simexplorer.is.storage.database.lucene.LuceneDatabase; - /** * Handle data storage and indexing. - * + * * @author landais */ public class StorageEngineImpl implements StorageEngine { @@ -55,11 +56,24 @@ /** The opened. */ private boolean opened = false; + /** Default constructor with default implementations of storage and indexing. */ + public StorageEngineImpl() { + this(null, null); + } + /** - * Default constructor with default implementations of storage and indexing. + * constructor with specified configuration + * <p/> + * default implementations of storage and indexing. + * + * @param configFile location of config file + * @param prefixKey prefix key to suppress on properties key */ - public StorageEngineImpl() { + public StorageEngineImpl(String configFile, String prefixKey) { super(); + if (configFile != null) { + Config.init(configFile, prefixKey); + } // Indexing database = new LuceneDatabase(); // Storage @@ -99,6 +113,7 @@ /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#saveElement(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.util.Map) */ + public void saveElement(String token, MetaData element, Map<Attachment, InputStream> attachments) throws SimExplorerException { @@ -158,6 +173,7 @@ /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#getMetadata(java.lang.String) */ + public MetaData getMetadata(String token, String uuid) throws SimExplorerException { MetaData mde = null; List<MetaData> set = database.findElementsById(uuid, true); @@ -178,7 +194,8 @@ * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#getMetadata(java.lang.String, fr.cemagref.simexplorer.is.entities.metadata.Version) */ public MetaData getMetadata(String token, String uuid, Version version) throws SimExplorerException { - MetaData mde = database.getElement(uuid, version); + MetaData mde; + mde = database.getElement(uuid, version); return mde; } @@ -239,7 +256,8 @@ * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#retrieveData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) */ public InputStream retrieveData(String token, MetaData entity, Attachment attachment) throws SimExplorerException { - InputStream result = attachmentHandler.retrieveData(entity, attachment); + InputStream result; + result = attachmentHandler.retrieveData(entity, attachment); return result; } @@ -254,9 +272,10 @@ * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#findFullText(java.lang.String, boolean, int, int, int) */ public MetaData[] findFullText(String token, String query, boolean onlyLatest, int indexStart, int count, - int dateOrder) throws SimExplorerException { + int dateOrder) throws SimExplorerException { List<MetaData> elements = database.findElementsByContentSearch(query, onlyLatest, indexStart, count, dateOrder); - MetaData[] result = elements.toArray(new MetaData[elements.size()]); + MetaData[] result; + result = elements.toArray(new MetaData[elements.size()]); return result; } @@ -264,9 +283,10 @@ * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#findElementsByType(java.lang.String, boolean, int, int, int) */ public MetaData[] findElementsByType(String token, String type, boolean onlyLatest, int start, int count, - int dateOrder) throws SimExplorerException { + int dateOrder) throws SimExplorerException { List<MetaData> elements = database.findElementsByType(type, onlyLatest, start, count, dateOrder); - MetaData[] result = elements.toArray(new MetaData[elements.size()]); + MetaData[] result; + result = elements.toArray(new MetaData[elements.size()]); return result; } @@ -305,9 +325,8 @@ /** * Gets the tmp attachment. - * + * * @param id the id - * * @return the tmp attachment */ private Attachment getTmpAttachment(String id) { @@ -338,7 +357,8 @@ * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#retrieveTempData(java.lang.String) */ public InputStream retrieveTempData(String id) throws SimExplorerException { - InputStream is = attachmentHandler.retrieveData(mdTmp, getTmpAttachment(id)); + InputStream is; + is = attachmentHandler.retrieveData(mdTmp, getTmpAttachment(id)); return is; } @@ -347,7 +367,8 @@ */ @Override public String retrieveMD5TempData(String id) throws SimExplorerException { - String md5 = attachmentHandler.retrieveMD5Data(mdTmp, getTmpAttachment(id)); + String md5; + md5 = attachmentHandler.retrieveMD5Data(mdTmp, getTmpAttachment(id)); return md5; } Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/Config.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/Config.java 2008-02-20 23:06:46 UTC (rev 1155) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/Config.java 2008-02-20 23:09:00 UTC (rev 1156) @@ -17,18 +17,28 @@ * ##% */ package fr.cemagref.simexplorer.is.storage.util; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Properties; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * The Class Config. - */ +/** The Class Config. */ public class Config { + public static final String DB_FOLDER_PROPERTY = "simexplorer.db"; + public static final String DATA_PROPERTY = "simexplorer.data"; + public static final String OPTIMIZE_PERIOD_PROPERTY = "simexplorer.optimizeperiod"; + public static final String ADMIN_MAIL_PROPERTY = "simexplorer.adminmail"; + /** The Constant log. */ private static final Log log = LogFactory.getLog(Config.class); @@ -38,36 +48,125 @@ /** The properties location. */ private static String propertiesLocation = "/properties/config.properties"; + private static String prefixKey = null; + /** * Inits the properties. - * - * @throws IOException - * Signals that an I/O exception has occurred. + * + * @param location config file to load + * @throws IOException Signals that an I/O exception has occurred. */ - private static void initProperties() throws IOException { + private static void initProperties(String location) throws IOException { log.info("Loading properties " + propertiesLocation); properties = new Properties(); + String locationStr = null; URL inClasspath = ClassLoader.getSystemClassLoader().getResource( propertiesLocation); if (inClasspath == null) { - inClasspath = Config.class.getResource(propertiesLocation); + inClasspath = Config.class.getResource(location); } - if (inClasspath != null) { - properties.load(inClasspath.openStream()); + InputStream stream = null; + if (inClasspath == null) { + // try a simple file + File f = new File(location); + if (f.exists()) { + locationStr = f.toString(); + stream = new FileInputStream(f); + } + } else { + locationStr = inClasspath.toString(); + stream = inClasspath.openStream(); } + + if (stream == null) { + throw new IllegalStateException("could not find properties file for " + propertiesLocation); + } + + log.info("Loading properties " + locationStr); + try { + properties.load(stream); + } finally { + stream.close(); + } + if (prefixKey != null) { + // suppress prefix fromour keys + translateProperties(prefixKey, DB_FOLDER_PROPERTY, DATA_PROPERTY, OPTIMIZE_PERIOD_PROPERTY, ADMIN_MAIL_PROPERTY); + } + } + private static void translateProperties(String prefixKey, String... keys) { + + Properties myProps = new Properties(); + // lowcase keys + for (Map.Entry<Object, Object> entry : properties.entrySet()) { + String k = entry.getKey() + ""; + myProps.setProperty(k.toLowerCase(), entry.getValue().toString().toLowerCase()); + } + int p = prefixKey.length(); + String newPrefix = "simexplorer."; + int p2 = newPrefix.length(); + for (String key : keys) { + String suffix = key.substring(p2); + String k = (prefixKey + suffix).toLowerCase(); + if (myProps.containsKey(k)) { + myProps.setProperty((newPrefix + k.substring(p)), myProps.getProperty(k)); + myProps.remove(k); + } + } + List<String> authorizedKeys = Arrays.asList(keys); + for (Iterator<Map.Entry<Object, Object>> it = myProps.entrySet().iterator(); it.hasNext();) + { + Map.Entry<Object, Object> entry = it.next(); + if (!authorizedKeys.contains(entry.getKey() + "")) { + it.remove(); + } + } + setProperties(myProps); + } + /** * Gets the properties. - * + * * @return the properties */ public static Properties getProperties() { + return getProperties(propertiesLocation); + } + + /** + * Sets the properties location. + * + * @param propertiesLocation the new properties location + * @param prefixKey prefixKey to suppress in properties + */ + public static void init(String propertiesLocation, String prefixKey) { + Config.propertiesLocation = propertiesLocation; + Config.prefixKey = prefixKey; + setProperties(null); + } + + /** + * Sets the properties location. + * + * @param propertiesLocation the new properties location + */ + public static void setPropertiesLocation(String propertiesLocation) { + init(propertiesLocation, null); + } + + /** + * Gets the properties. + * + * @param location config file to load + * @return the properties + */ + protected static Properties getProperties(String location) { if (properties == null) { try { - initProperties(); + initProperties(location); } catch (Exception e) { log.error(e); // this is a fatal error ? @@ -79,22 +178,10 @@ /** * Sets the properties. - * - * @param properties - * the new properties + * + * @param properties the new properties */ - public static void setProperties(Properties properties) { + protected static void setProperties(Properties properties) { Config.properties = properties; } - - /** - * Sets the properties location. - * - * @param propertiesLocation - * the new properties location - */ - public static void setPropertiesLocation(String propertiesLocation) { - Config.propertiesLocation = propertiesLocation; - setProperties(null); - } }
participants (1)
-
tchemit@users.labs.libre-entreprise.org