Author: mfortun Date: 2011-04-26 12:25:41 +0200 (Tue, 26 Apr 2011) New Revision: 833 Url: http://nuiton.org/repositories/revision/wikitty/833 Log: * Documentation * Correct method * remove static from method that not have to * add support for directory name filter eg: .svn, .git, etc Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java 2011-04-22 16:37:22 UTC (rev 832) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/MimeTypePubHelper.java 2011-04-26 10:25:41 UTC (rev 833) @@ -15,8 +15,19 @@ */ public class MimeTypePubHelper { + /** + * Mapping between extention and mime type, key: extension, value: mimeType + */ protected Map<String, String> mapExtensionMime; + + /** + * White list of mime type that have to be transform as a wikittyPubText + */ protected List<String> mimePubText; + + /** + * The default mime type + */ public static String DEFAULT_MIME_TYPE = "application/octet-stream"; public MimeTypePubHelper() { @@ -48,6 +59,15 @@ this.mimePubText = mimePubText; } + /** + * Return the corresponding mime Type for an extension, default if not + * recognized + * + * @param ext + * the extension + * @return the corresponding mimeTypeForExt DEFAULT_MIME_TYPE if ext not + * recognized + */ public String getMimeForExtension(String ext) { String result = mapExtensionMime.get(ext); @@ -58,14 +78,33 @@ return result; } + /** + * Used to check if a file have to be converted as a wikittyPubText + * + * @param mimeType + * of the file + * @return if the mimetype of a file correspond to a wikittyPubText + */ public boolean isPubTextMime(String mimeType) { return mimePubText.contains(mimeType); } + /** + * Add a mimeType corresponding to a WikittyPubText + * + * @param mime + */ public void addMimeForPubText(String mime) { mimePubText.add(mime); } + /** + * Add an entry in the map that store mapping between extension and + * mimeType. + * + * @param extension + * @param mime + */ public void addExtensionMime(String extension, String mime) { mapExtensionMime.put(extension, mime); } Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java 2011-04-22 16:37:22 UTC (rev 832) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/PropertiesExtended.java 2011-04-26 10:25:41 UTC (rev 833) @@ -1,13 +1,16 @@ package org.nuiton.wikitty.publication.synchro; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; +import java.io.IOException; import java.util.Properties; /** - * Class usefull when load properties file, update, delete, then save again - * File used to load properties is store. + * Class usefull when load properties file, update, delete, then save again File + * used to load properties is store, and this allow to save again the properties + * inside the file used to load them. * * @author mfortun * @@ -19,20 +22,69 @@ */ private static final long serialVersionUID = -264337198024996529L; + /** + * The original file used to load and create properties + */ protected File origin; - public PropertiesExtended(File origin) throws Exception { + + public File getOrigin() { + return origin; + } + + public void setOrigin(File origin) { + this.origin = origin; + } + + + + /** + * Default constructor, need a file from whom load the properties. This is + * equivalent of a basic creation of properties and load after property from + * a file. + * + * @param origin + * the file from whom load the property, it is save for store. + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public PropertiesExtended(File origin) throws FileNotFoundException, + IOException { super(); this.load(origin); } - public void load(File file) throws Exception { + + /** + * Load the property from the file, and store the file, for storage. + * + * @param file + * the file from whom to load the property, it replace the file + * use to create this class + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public void load(File file) throws FileNotFoundException, IOException { this.origin = file; this.load(new FileReader(origin)); } - public void store() throws Exception { + /** + * Store the properties inside the last file used to load the property (or + * by default file used to create the class), equivalent of store(new + * FileWriter('File'),""); + * + * @throws IOException + * if error while reading the file + * @throws FileNotFoundException + * if file not found + */ + public void store() throws IOException { this.store(new FileWriter(origin), ""); } Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java 2011-04-22 16:37:22 UTC (rev 832) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublication.java 2011-04-26 10:25:41 UTC (rev 833) @@ -25,6 +25,7 @@ package org.nuiton.wikitty.publication.synchro; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -126,17 +127,12 @@ } - static public void synchronisation(String origin, String target) - throws Exception { + static public void synchronisation(String origin, String target) throws URISyntaxException + { boolean isRecur = applicationConfig .getOptionAsBoolean(IS_RECURSION_OPTION); - - - - /* - * TODO mfortun-2011-04-14 handle exception if uri format does not match - */ + URI uriOrigin = new URI(origin); URI uriTarget = new URI(target); @@ -164,7 +160,6 @@ WikittyProxy proxyTarget = new WikittyProxy( WikittyServiceFactory.buildWikittyService(temp2)); - String labelOrigin = uriOrigin.getFragment(); String labelTarget = uriOrigin.getFragment(); @@ -252,11 +247,9 @@ * if option delete remove those who are on the target but not on origin */ if (isDelete) { - + for (String id : existOnlyOnTarget) { - - - + Wikitty w = proxyTarget.restore(id); WikittyLabelHelper.removeLabels(w, labelTarget); @@ -317,10 +310,11 @@ } /** - * Used to construct criteria on wikittypubdata an pubtext on the + * Used to construct criteria on wikittypubdata and pubtext on the * wikittylabel extension * * @param label + * the label criteria * @param isRecur * is recusion * @return the constructed criteria @@ -365,13 +359,15 @@ */ static protected ApplicationConfig setUpApplicationConfigServerConnector( URI uri) { - + // prepare new application config ApplicationConfig result = new ApplicationConfig(); // transfert main properties to new application config result.setOptions(applicationConfig.getFlatOptions()); String url = uri.toASCIIString(); + if (uri.getScheme().equals("file")) { + result.setOption(WIKITTY_OPTION_COMPONENT, "org.nuiton.wikitty.publication.synchro.WikittyPublicationFileSystem"); } else if (uri.getScheme().equals("cajo")) { @@ -380,6 +376,7 @@ // remove fragment from the uri. url = url.replaceAll("\\#.*", ""); + } else if (uri.getScheme().equals("hessian")) { result.setOption(WIKITTY_OPTION_COMPONENT, "org.nuiton.wikitty.services.WikittyServiceHessianClient"); Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-04-22 16:37:22 UTC (rev 832) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/synchro/WikittyPublicationFileSystem.java 2011-04-26 10:25:41 UTC (rev 833) @@ -27,8 +27,11 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -96,10 +99,26 @@ static public String WIKITTYLABEL_SEPARATOR = "."; + /** + * The working directory of the wikitty service + */ protected File homeFile; + + /** + * if recursion is chose + */ protected boolean recursion; + + /** + * The initial label of the wikitties, and the path to the first wikitties + * of the working directory + */ protected String label; + /** + * Property key to store the current label in a wikittyProperty meta file. + * Usefull to retrieve easily the wikitties and convert file as wikitty. + */ static public String META_CURRENT_LABEL = "current.label"; /* @@ -108,70 +127,96 @@ * solution un case of delete we can easily read the id and make operation * on the wikitty */ + /** + * property key used in the metaPropertyFile to store the wikitty's version + */ static public String META_PREFIX_KEY_VERSION = "version."; + /** + * property key used in the metaPropertyFile to store the wikitty's content + * checksum + */ static public String META_PREFIX_KEY_CHECKSUM = "checksum."; + /** + * property key used in the metaPropertyFile to store the wikitty's id + */ static public String META_PREFIX_KEY_ID = "id."; + /** + * The file name of the meta property file + */ static public String WIKITTY_FILE_META_PROPERTIES_FILE = "meta.properties"; - static public String WIKITTY_FILE_SERVICE = "ws.properties"; - /* * Need a different file for id and meta information about wikittiesFiles * because with this solution we can simply read the ids with props.keySet() */ + /** + * The file Name of the id property file + */ static public String WIKITTY_ID_PROPERTIES_FILE = "ids.properties"; + /** + * The file name of the wikitty service property + */ + static public String WIKITTY_FILE_SERVICE = "ws.properties"; + + /** + * the file name of the property directory + */ static public String PROPERTY_DIRECTORY = ".wp"; + /** + * the mimeType Handler used to determine mimeType for file extension and if + * file have to be converted as wikittyPubText or WikittyPubData + */ protected MimeTypePubHelper mimeHelper; /** - * Constructor with the working directory + * List of directory name that does'nt have to be harvest exampe : .svn, + * .git + */ + protected List<String> directoryNameBlackList; + + /** + * Default constructor, call by a factory * - * @param home - * the home directory + * @param app + * application config needed to correctly initialize this. Need + * wikitty url to be set (with URI format) and option for + * recursion, @see WikittyPublication for constant + * @throws URISyntaxException + * if serveur url is not in the proper format (URI format) */ + public WikittyPublicationFileSystem(ApplicationConfig app) + throws URISyntaxException { - public WikittyPublicationFileSystem(ApplicationConfig app) { - /* - * load informations from the application config to correctly initialize - * this - */ - try { + this.mimeHelper = new MimeTypePubHelper(); - this.mimeHelper = new MimeTypePubHelper(); + String url = app.getOption(WikittyPublication.WIKITTY_OPTION_URL); + URI uri = new URI(url); - - - - String url = app.getOption(WikittyPublication.WIKITTY_OPTION_URL); - URI uri = new URI(url); + this.homeFile = new File(uri.getPath()); - this.homeFile = new File(uri.getPath()); + if (homeFile == null || !homeFile.exists()) { + File cur = FileUtil.getCurrentDirectory(); + homeFile = new File(cur.getAbsolutePath()); + } - if (homeFile == null || !homeFile.exists()) { - File cur = FileUtil.getCurrentDirectory(); - homeFile = new File(cur.getAbsolutePath()); - } + this.label = uri.getFragment(); - this.label = uri.getFragment(); + if (label == null) { + label = homeFile.getName(); + } - if (label == null) { - label = homeFile.getName(); - } + this.recursion = app + .getOptionAsBoolean(WikittyPublication.IS_RECURSION_OPTION); - this.recursion = app - .getOptionAsBoolean(WikittyPublication.IS_RECURSION_OPTION); + this.directoryNameBlackList = new ArrayList<String>(); + directoryNameBlackList.add(".svn"); + directoryNameBlackList.add(".git"); - - - - } catch (Exception e) { - e.printStackTrace(); - } } public File getHomeFile() { @@ -290,6 +335,9 @@ } @Override + /** + * Store wikitty as file + */ public WikittyEvent store(String securityToken, Collection<Wikitty> wikitties, boolean force) { WikittyEvent result = new WikittyEvent(this); @@ -300,7 +348,8 @@ Set<String> set = WikittyLabelHelper.getLabels(w); String ourDir = ""; - // search for our label + // search for our label recursion determine how search the label + // if have to match exactly or startswith for (String name : set) { if (!recursion && name.equalsIgnoreCase(label)) { ourDir = name; @@ -309,10 +358,11 @@ } } + // if label is empty it means that the wikitty have to be remove + // from the FileSystem if ("".equals(ourDir)) { // remove file if the wikitty already exist - BidiMap location = harvestLocalWikitties(homeFile, true); FileSystemWIkittyId idSystem = (FileSystemWIkittyId) location @@ -326,8 +376,8 @@ wikittyFile.delete(); } } - - return result; + // jump to the next wikitty to store + continue; } // create the directories from the label @@ -338,7 +388,7 @@ String path = homeFile.getCanonicalFile() + File.separator + ourDir.replace(".", File.separator); - // correct the pb with directory name begin by . + // correct the problem with directory name begin by . path = path.replace(File.separator + File.separator, File.separator + "."); @@ -346,6 +396,7 @@ // create the propertie directory if necessary File propertieDirectory = new File(path + File.separator + WikittyPublicationFileSystem.PROPERTY_DIRECTORY); + if (!propertieDirectory.exists() || !propertieDirectory.isDirectory()) { propertieDirectory.mkdir(); @@ -355,6 +406,8 @@ String extension = ""; File wikittyFile = null; + // construct the file differently if wikittyPubData or + // wikittyPubText if (w.hasExtension(WikittyPubData.EXT_WIKITTYPUBDATA)) { name = WikittyPubDataHelper.getName(w); // String mime = WikittyPubDataHelper.getMimeType(w); @@ -385,6 +438,7 @@ FileUtil.writeString(wikittyFile, content); } + // if file properly write, write wikittyProperties if (wikittyFile != null) { // write current label @@ -467,27 +521,22 @@ } @Override + /** + * restore file as wikitty + */ public List<Wikitty> restore(String securityToken, List<String> id) { - /* - * FIXME mfortun-2011-04-14 s'assurer que quand on restore on restore - * bien la version correctement, potentiellement si ya eut des - * modifications faites entre la version précédent et actuel on fasse - * gaffe à incrémenter la version "mineur" - * - * - * - * version d'un wikitty sous forme x.y, où x incrémenté par serveur y - * incrémenté à chaque set sur content. - */ - List<Wikitty> result = new ArrayList<Wikitty>(); try { + // construct the starts file from the label and the working + // directory String labelToDir = this.labelToPath(label); File starts = new File(homeFile.getAbsolutePath() + File.separator + labelToDir); + // harvest all wikitty from the file system BidiMap locations = harvestLocalWikitties(starts, recursion); + // register wikitty in the result for (String wikid : id) { Object value = locations.get(wikid); @@ -510,17 +559,13 @@ public WikittyEvent delete(String securityToken, Collection<String> ids) { try { + // load localisation of all the wikitties BidiMap location = harvestLocalWikitties(homeFile, true); for (String id : ids) { Object value = location.get(id); - if (value == null) { - location = location.inverseBidiMap(); - value = location.get(id); - } - if (value != null) { FileSystemWIkittyId localisation = (FileSystemWIkittyId) value; @@ -533,13 +578,10 @@ new File(path), WikittyPublicationFileSystem.WIKITTY_FILE_META_PROPERTIES_FILE); - // update - propsProperties - .remove(WikittyPublicationFileSystem.META_PREFIX_KEY_CHECKSUM - + fileName); - propsProperties - .remove(WikittyPublicationFileSystem.META_PREFIX_KEY_VERSION - + fileName); + // update remove properties + propsProperties.remove(META_PREFIX_KEY_CHECKSUM + fileName); + propsProperties.remove(META_PREFIX_KEY_VERSION + fileName); + propsProperties.remove(META_PREFIX_KEY_ID + fileName); // resave propsProperties.store(); @@ -951,25 +993,25 @@ List<Criteria> criteria) { Map<String, Wikitty> wikitties = new HashMap<String, Wikitty>(); try { + // construct properly with the working directory and the label + // the starts directory String labelToDir = this.labelToPath(label); File starts = new File(homeFile.getAbsolutePath() + File.separator + labelToDir); if (starts.exists()) { + // check for new file, modifications and deleted wikitty harvestNewCheckModificationsAndDeleted(starts, label); } + // load all locals wikitty BidiMap map = harvestLocalWikitties(starts, true); - - - + // put all local wikitties in a map for (Object o : map.keySet()) { String id = (String) o; FileSystemWIkittyId location = (FileSystemWIkittyId) map .get(id); - - - + wikitties.put(id, restore(id, location)); } @@ -1021,23 +1063,28 @@ Map<String, Wikitty> wikitties = new HashMap<String, Wikitty>(); try { + // construct properly with the working directory and the label + // the starts directory String labelToDir = this.labelToPath(label); File starts = new File(homeFile.getAbsolutePath() + File.separator + labelToDir); if (starts.exists()) { + // check for new file, modifications and deleted wikitty harvestNewCheckModificationsAndDeleted(starts, label); } + // load all locals wikitty BidiMap map = harvestLocalWikitties(starts, true); + // put all local wikitties in a map for (Object o : map.keySet()) { String id = (String) o; FileSystemWIkittyId location = (FileSystemWIkittyId) map .get(id); + wikitties.put(id, restore(id, location)); } - } catch (Exception e) { // TODO mfortun-2011-04-21 really handle exception e.printStackTrace(); @@ -1115,17 +1162,17 @@ * @param recursivly * @return list of harvested file */ - static public List<File> harvestPropertyDirectory(File starts, - boolean recursivly) { + public List<File> harvestPropertyDirectory(File starts, boolean recursivly) { List<File> result = new ArrayList<File>(); for (File child : starts.listFiles()) { - + // search for all property directory, usefull to retrieve wikitty if (child.isDirectory() && child.getName().equals(PROPERTY_DIRECTORY)) { result.add(child); - } else if (child.isDirectory() && recursivly) { + } else if (child.isDirectory() && recursivly + && !this.directoryNameBlackList.contains(child.getName())) { result.addAll(harvestPropertyDirectory(child, recursivly)); } } @@ -1140,13 +1187,14 @@ * @return * @throws Exception */ - static public BidiMap harvestLocalWikitties(File starts, boolean recursivly) + public BidiMap harvestLocalWikitties(File starts, boolean recursivly) throws Exception { BidiMap result = new DualHashBidiMap(); - List<File> propertiesDirectory = WikittyPublicationFileSystem - .harvestPropertyDirectory(starts, recursivly); + List<File> propertiesDirectory = harvestPropertyDirectory(starts, + recursivly); + // use the list of property to retrieve wikitty for (File propsDir : propertiesDirectory) { Properties idProps = new Properties(); @@ -1157,7 +1205,9 @@ } Set<Object> ids = idProps.keySet(); - + // simply load the id property file to retrieve wikitty id + // with the path to the propertyfile and the value of the file + // construct FileSytemWikittyId for (Object id : ids) { String name = idProps.getProperty((String) id); String path = propsDir.getParent(); @@ -1179,8 +1229,22 @@ return result; } + /** + * Simply return the propertyExtended corresponding that can be found in + * starts/.wp/ directory. It create the corresponding property file if + * does'nt exist + * + * @param starts + * the directory that containt a .wp directory containing the + * property + * @param name + * the property file name, use constants + * @return the properties extended requested + * @throws IOException + * if error while reading the file + */ static public PropertiesExtended getWikittyPublicationProperties( - File starts, String name) throws Exception { + File starts, String name) throws IOException { File propertieDirectory = new File(starts.getCanonicalPath() + File.separator @@ -1198,8 +1262,21 @@ return result; } + /** + * Restore the corresponding wikitty identify by the id and is location on + * the filesytem + * + * @param id + * the wikitty id + * @param fileId + * the wikitty file system id (his localisation on the + * wikittyFileSystem) + * @return the restored wikitty + * @throws IOException + * if errors while reading the file + */ protected Wikitty restore(String id, FileSystemWIkittyId fileId) - throws Exception { + throws IOException { Wikitty result = new WikittyImpl(id); @@ -1244,7 +1321,8 @@ WikittyPubDataHelper.setFileExtension(result, extension); } - // re set the version + // re set the version (re set the version now allow the version to be + // not altered by operation on the wikitty to restore it result.setVersion(props .getProperty(WikittyPublicationFileSystem.META_PREFIX_KEY_VERSION + completeName)); @@ -1252,8 +1330,14 @@ return result; } + /** + * + * @param starts + * @param label + * @throws Exception + */ protected void harvestNewCheckModificationsAndDeleted(File starts, - String label) throws Exception { + String label) throws Exception { File propertyFile = new File(starts + File.separator + PROPERTY_DIRECTORY); @@ -1325,7 +1409,7 @@ } - protected void checkModifications(File child) throws Exception { + protected void checkModifications(File child) throws IOException { // will check if there was modification with checksum BufferedInputStream input = new BufferedInputStream( @@ -1421,9 +1505,8 @@ */ protected boolean createFilesFromLabelPath(String label) throws Exception { - label = label.replace(".", File.separator); - label = label.replace(File.separator + File.separator, File.separator - + "."); + label = labelToPath(label); + String[] pathElements = StringUtil.split(label, File.separator); boolean result = false; @@ -1443,6 +1526,11 @@ return result; } + /** + * Construct correctly the path from a label + * @param label the label + * @return the correct path + */ public String labelToPath(String label) { String result = label;
participants (1)
-
mfortun@users.nuiton.org