From: <jbo...@li...> - 2006-01-25 23:27:27
|
Author: szimano Date: 2006-01-25 18:27:05 -0500 (Wed, 25 Jan 2006) New Revision: 2204 Added: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileHistory.java trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileTools.java Modified: trunk/forge/portal-extensions/shotoku/shotoku-files/project.xml trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileDirectory.java trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileNode.java trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FilesContentManager.java Log: further implemenattion to filesystem for shotoku http://jira.jboss.com/jira/browse/JBSHOTOKU-53 Modified: trunk/forge/portal-extensions/shotoku/shotoku-files/project.xml =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/project.xml 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/project.xml 2006-01-25 23:27:05 UTC (rev 2204) @@ -85,8 +85,17 @@ <version>1.0</version> <jar>activation.jar</jar> </dependency> - </dependencies> + + <dependency> + <groupId>jboss</groupId> + <artifactId>jboss-common</artifactId> + <version>1.0</version> + <jar>jboss-common.jar</jar> + </dependency> + + </dependencies> + <build> <sourceDirectory>src/java</sourceDirectory> <resources> Modified: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileDirectory.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileDirectory.java 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileDirectory.java 2006-01-25 23:27:05 UTC (rev 2204) @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; +import org.jboss.shotoku.ContentManager; import org.jboss.shotoku.Directory; import org.jboss.shotoku.Node; import org.jboss.shotoku.NodeList; @@ -42,13 +43,17 @@ private File directory; + private FilesContentManager manager; + /** * */ private static final long serialVersionUID = 1L; - public FileDirectory(String directory) throws RepositoryException { + public FileDirectory(String directory, FilesContentManager manager) throws RepositoryException { File dir = new File(directory); + + this.manager = manager; if (!dir.isDirectory()) { throw new RepositoryException(directory + " is not a directory"); @@ -74,7 +79,15 @@ // get all children for (File file : children) { if (file.isFile()) { - list.add(new FileNode(file)); + try { + list.add(new FileNode(this, file.getName(), false, manager)); + } catch (ResourceAlreadyExists e) { + //shouldn't happen + new RepositoryException(e); + } catch (ResourceDoesNotExist e) { + //shouldn't happen + new RepositoryException(e); + } } } @@ -101,10 +114,15 @@ File file = new File(this.directory.getAbsolutePath() + name); if (!file.exists()) { - throw new RepositoryException("File " + file.getAbsolutePath() + throw new ResourceDoesNotExist("File " + file.getAbsolutePath() + " doesn't exists"); } - return new FileNode(name); + try { + return new FileNode(this, name, false, manager); + } catch (ResourceAlreadyExists e) { + // it's not going to happen + return null; + } } public Directory getDirectory(String name) throws RepositoryException, @@ -137,7 +155,12 @@ } } - return new FileNode(file); + try { + return new FileNode(this, name, true, manager); + } catch (ResourceDoesNotExist e) { + // not going to happen + return null; + } } public Directory newDirectory(String name) throws ResourceAlreadyExists, @@ -226,5 +249,9 @@ // TODO Auto-generated method stub return null; } + + public String toString() { + return directory.getAbsolutePath(); + } } Added: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileHistory.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileHistory.java 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileHistory.java 2006-01-25 23:27:05 UTC (rev 2204) @@ -0,0 +1,107 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.shotoku.files; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.jboss.shotoku.History; +import org.jboss.shotoku.Node; +import org.jboss.shotoku.NodeList; +import org.jboss.shotoku.exceptions.RepositoryException; +import org.jboss.shotoku.exceptions.ResourceDoesNotExist; + +public class FileHistory implements History { + + private FileNode node; + + private FilesContentManager manager; + + public FileHistory(FileNode node, FilesContentManager manager) { + this.node = node; + this.manager = manager; + } + + public int getRevisionsCount() throws RepositoryException { + Properties history = FileTools.getHistoryForNode(node, manager); + + int i = 1; + + while (history.getProperty("ver." + i) != null) { + i++; + } + + return --i; + } + + public Node getNodeAtRevision(int revision) throws ResourceDoesNotExist, + RepositoryException { + + // get copy of this node + Node newNode = node.getDirectory().getNode(node.getName()); + + File revFile = FileTools.getFileAtRevision(node, revision, manager); + + if (!revFile.exists()) { + throw new ResourceDoesNotExist("There is no revision " + revision + + " of node " + node.getFullName()); + } + + //change node's content + try { + InputStream is = new FileInputStream(revFile); + + newNode.setContent(is); + + is.close(); + } catch (FileNotFoundException e) { + throw new RepositoryException(e); + } catch (IOException e) { + throw new RepositoryException(e); + } + + return newNode; + } + + public NodeList getAllRevisions() throws RepositoryException { + List<Node> list = new ArrayList<Node>(); + + int max = getRevisionsCount(); + + for (int i = 0; i < max; i++) { + try { + list.add(getNodeAtRevision(i)); + } catch (ResourceDoesNotExist e) { + throw new RepositoryException(e); + } + } + + return new NodeList(list); + } + +} Modified: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileNode.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileNode.java 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileNode.java 2006-01-25 23:27:05 UTC (rev 2204) @@ -29,7 +29,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.Date; +import java.util.HashMap; import java.util.Map; +import java.util.Properties; import org.jboss.shotoku.Directory; import org.jboss.shotoku.History; @@ -38,110 +40,159 @@ import org.jboss.shotoku.exceptions.DeleteException; import org.jboss.shotoku.exceptions.MoveException; import org.jboss.shotoku.exceptions.RepositoryException; +import org.jboss.shotoku.exceptions.ResourceAlreadyExists; +import org.jboss.shotoku.exceptions.ResourceDoesNotExist; import org.jboss.shotoku.exceptions.SaveException; public class FileNode implements Node { private File file; - + + private File tmpFile; + + private FilesContentManager manager; + + private Directory directory; + + private Properties props; + + private boolean propsChanged = false; + + private boolean contentChanged = false; + + private int revision; + + private boolean lastVersion = true; + /** * */ private static final long serialVersionUID = 1L; - public FileNode(String file) throws RepositoryException { - File fil = new File(file); - + public FileNode(Directory directory, String node, boolean newFile, + FilesContentManager manager) throws RepositoryException, + ResourceAlreadyExists, ResourceDoesNotExist { + File fil = new File(directory + File.pathSeparator + node); + + this.manager = manager; + this.directory = directory; + if (!fil.isFile()) { - throw new RepositoryException(file + " is not a file"); + throw new RepositoryException(directory + File.pathSeparator + node + + " is not a file"); } - + + if (!newFile) { + refreshProps(); + } + + if (newFile && fil.exists()) { + throw new ResourceAlreadyExists(directory + File.pathSeparator + + node + " already exists"); + } else if (!newFile && !fil.exists()) { + throw new ResourceDoesNotExist(directory + File.pathSeparator + + node + " doesn't exist"); + } + this.file = fil; - } - - public FileNode(File file) throws RepositoryException { - if (file.isFile()) { - this.file = file; + + try { + tmpFile = File.createTempFile("shotoku-file", node); + } catch (IOException e) { + throw new RepositoryException(e); } - else { - throw new RepositoryException(file.getAbsolutePath()+" is not a file."); - } + + revision = getHistory().getRevisionsCount(); } + /* + * public FileNode(File file) throws RepositoryException { if + * (file.isFile()) { this.file = file; } else { throw new + * RepositoryException(file.getAbsolutePath() + " is not a file."); } } + */ + public String getContent() throws RepositoryException { StringBuffer buffer = new StringBuffer(); - + try { - InputStream is = new FileInputStream(file); - + InputStream is = new FileInputStream( + (contentChanged || lastVersion) ? tmpFile : file); + char chr; - - while ((chr = (char)is.read()) != -1) { + + while ((chr = (char) is.read()) != -1) { buffer.append(chr); } - + is.close(); } catch (FileNotFoundException e) { new RepositoryException(e); } catch (IOException e) { new RepositoryException(e); } - - + return buffer.toString(); } public void setContent(String content) { try { - OutputStream os = new FileOutputStream(file); - + OutputStream os = new FileOutputStream(tmpFile); + for (int i = 0; i < content.length(); i++) { os.write(content.charAt(i)); } - + os.close(); } catch (FileNotFoundException e) { new RepositoryException(e); } catch (IOException e) { new RepositoryException(e); } + + contentChanged = true; } public void setContent(InputStream is) { try { - OutputStream os = new FileOutputStream(file); - + OutputStream os = new FileOutputStream(tmpFile); + int i; - + while ((i = is.read()) != -1) { os.write(i); } - + os.close(); } catch (FileNotFoundException e) { new RepositoryException(e); } catch (IOException e) { new RepositoryException(e); } + + contentChanged = true; } public void setContent(byte[] b) { try { - OutputStream os = new FileOutputStream(file); - + OutputStream os = new FileOutputStream(tmpFile); + os.write(b); - + os.close(); } catch (FileNotFoundException e) { new RepositoryException(e); } catch (IOException e) { new RepositoryException(e); } + + contentChanged = true; } public OutputStream getOutputStream() { try { - return new FileOutputStream(file); + contentChanged = true; + + return new FileOutputStream(tmpFile); } catch (FileNotFoundException e) { e.printStackTrace(); return null; @@ -149,26 +200,33 @@ } public History getHistory() throws RepositoryException { - // TODO Auto-generated method stub - return null; + return new FileHistory(this, manager); } public int getRevisionNumber() throws RepositoryException { - // TODO Auto-generated method stub - return 0; + if (lastVersion) + return getHistory().getRevisionsCount(); + else + return revision; } + public void setRevision(int revision) { + lastVersion = true; + this.revision = revision; + } + public void copyToFile(String fileName) throws RepositoryException { try { OutputStream os = new FileOutputStream(new File(fileName)); InputStream is = getContentInputStream(); - + int b; while ((b = is.read()) != -1) os.write(b); - - os.close(); is.close(); - + + os.close(); + is.close(); + } catch (FileNotFoundException e) { new RepositoryException(e); } catch (IOException e) { @@ -179,7 +237,8 @@ public InputStream getContentInputStream() throws RepositoryException { try { - return new FileInputStream(file); + return new FileInputStream( + (contentChanged || lastVersion) ? tmpFile : file); } catch (FileNotFoundException e) { throw new RepositoryException(e); } @@ -187,14 +246,14 @@ public byte[] getContentByteArray() throws RepositoryException { InputStream is = getContentInputStream(); - - byte[] b = new byte[(int)getLength()]; - + + byte[] b = new byte[(int) getLength()]; + try { is.read(b); - + is.close(); - + return b; } catch (IOException e) { throw new RepositoryException(e); @@ -214,55 +273,109 @@ } public String getMimeType() { - // TODO Auto-generated method stub - return null; + return FileTools.getMimeType(file); } /** - * Copies this resource to the given directory. This can't be called if - * this resource is new and not yet saved. + * Copies this resource to the given directory. This can't be called if this + * resource is new and not yet saved. * * @param dir * Directory to copy this resource to. This can't be a new and * not saved directory. * @param newName - * New name of this resource (in directory dir after copying). + * New name of this resource (in directory dir after copying). * @param logMessage - * Log message associated with this resource copy. + * Log message associated with this resource copy. * @throws CopyException */ - public void copyTo(Directory dir, String newName, - String logMessage) throws CopyException{ - //TODO + public void copyTo(Directory dir, String newName, String logMessage) + throws CopyException { + try { + Node newNode = dir.newNode(newName); + + newNode.setContent(getContentByteArray()); + + newNode.save(logMessage); + } catch (RepositoryException e) { + throw new CopyException(e); + } catch (ResourceAlreadyExists e) { + throw new CopyException(e); + } catch (SaveException e) { + throw new CopyException(e); + } } - + /** - * Moves this resource to the given directory. This can't be called if - * this resource is new and not yet saved. This resource should not be - * used after performing this operation. + * Moves this resource to the given directory. This can't be called if this + * resource is new and not yet saved. This resource should not be used after + * performing this operation. * * @param dir * Directory to move this resource to. This can't be a new and * not saved directory. * @param logMessage - * Log message associated with this resource move. + * Log message associated with this resource move. * @throws MoveException */ - public void moveTo(Directory dir, String logMessage) throws MoveException{ - //TODO + public void moveTo(Directory dir, String logMessage) throws MoveException { + + File toFile = new File(dir + File.pathSeparator + file.getName()); + + try { + // get new node + Node newNode = dir.newNode(file.getName()); + + if (toFile.exists()) { + throw new MoveException("There is " + file.getName() + + " in directory " + dir); + } + + // get history dirs + File oldHisDir = FileTools.getHistoryDir(this, manager); + File newHisDir = FileTools.getHistoryDir((FileNode) newNode, + manager); + + if (newHisDir.exists()) { + throw new MoveException("History for " + newNode.getFullName() + + " already exists"); + } + + // move files + file.renameTo(toFile); + oldHisDir.renameTo(newHisDir); + + file = toFile; + } catch (RepositoryException e) { + new MoveException(e); + } catch (ResourceAlreadyExists e) { + new MoveException(e); + } + } - + /** * Gets a map of all properties associated with this resource. * * @return A map of properties associated with this resource. * @throws RepositoryException */ - public Map<String, String> getProperties() throws RepositoryException{ - //TODO - return null; + public Map<String, String> getProperties() throws RepositoryException { + refreshProps(); + Map<String, String> map = new HashMap<String, String>(); + + for (Object key : props.keySet()) { + map.put((String) key, props.getProperty((String) key)); + } + + return map; } - + + private void refreshProps() { + if (!propsChanged) + props = FileTools.getPropertiesForNode(this, manager); + } + /** * Gets the value of the given property. * @@ -271,11 +384,11 @@ * @return Value of the given property. * @throws RepositoryException */ - public String getProperty(String propertyName) throws RepositoryException{ - //TODO - return null; + public String getProperty(String propertyName) throws RepositoryException { + refreshProps(); + return props.getProperty(propertyName); } - + /** * Deletes the the given property. * @@ -283,23 +396,26 @@ * Name of the property to delete. * @throws RepositoryException */ - public void deleteProperty(String propertyName) throws RepositoryException{ - //TODO + public void deleteProperty(String propertyName) throws RepositoryException { + refreshProps(); + props.remove(propertyName); } /** - * Sets the value of the given property. Only after saving this change will be - * persisted. + * Sets the value of the given property. Only after saving this change will + * be persisted. * * @param propertyName * Name of the property to set. It must begin with a character, - * and cannot contain any special characters (so the regexp to which - * a property name must match would be [a-z][a-z0-9]*). + * and cannot contain any special characters (so the regexp to + * which a property name must match would be [a-z][a-z0-9]*). * @param propertyValue * Value of the property to set. */ - public void setProperty(String propertyName, String propertyValue){ - //TODO + public void setProperty(String propertyName, String propertyValue) { + refreshProps(); + props.setProperty(propertyName, propertyValue); + propsChanged = true; } /** @@ -308,7 +424,7 @@ * @return A directory to which this node/ directory belongs. Null if this * directory is already the root directory. */ - public Directory getParent() throws RepositoryException{ + public Directory getParent() throws RepositoryException { return new FileDirectory(file.getParentFile()); } @@ -317,12 +433,95 @@ * * @param logMessage * Log message for saving this node/ directory. - * @throws SaveException + * @throws SaveException * @throws RepositoryException */ - public void save(String logMessage) throws SaveException, RepositoryException{ - //TODO - + public void save(String logMessage) throws SaveException, + RepositoryException { + Properties history = FileTools.getHistoryForNode(this, manager); + + // get last props version + int i = 1; + + while (history.getProperty("ver." + i) != null) { + i++; + } + + history.setProperty("ver." + i, logMessage); + + // get next history file + File nextVersion = FileTools.getNextHistoryFile(this, manager, i); + + if (nextVersion.exists()) { + throw new SaveException("Conflict in saving: history file exists"); + } + + try { + nextVersion.createNewFile(); + } catch (IOException e) { + throw new RepositoryException(e); + } + + // save prevoius version + copyToFile(file.getAbsolutePath()); + + // save new version + FileOutputStream newFileOS = null; + try { + newFileOS = new FileOutputStream(file); + } catch (FileNotFoundException e2) { + throw new SaveException(e2); + } + + FileInputStream tmpFileIS = null; + try { + tmpFileIS = new FileInputStream(tmpFile); + } catch (FileNotFoundException e2) { + throw new SaveException(e2); + } + + int b; + + try { + while ((b = tmpFileIS.read()) != -1) { + newFileOS.write(b); + } + } catch (IOException e2) { + throw new SaveException(e2); + } + + try { + tmpFileIS.close(); + tmpFileIS.close(); + } catch (IOException e2) { + throw new SaveException(e2); + } + + // save props and history + FileOutputStream fos = null; + FileOutputStream fosPr = null; + try { + fos = new FileOutputStream(FileTools.getHistoryFile(this, manager)); + fosPr = new FileOutputStream(FileTools.getPropsFile(this, manager)); + } catch (FileNotFoundException e) { + throw new SaveException(e); + } + + try { + history.store(fos, FileTools.STORE_TEXT); + props.store(fosPr, FileTools.STORE_TEXT); + } catch (IOException e) { + try { + fos.close(); + fosPr.close(); + } catch (IOException e1) { + throw new RepositoryException(e); + } + throw new RepositoryException(e); + } + + // set bools to false + lastVersion = contentChanged = propsChanged = false; } /** @@ -332,24 +531,34 @@ * the resource is not yet saved or contains modifications. * @throws RepositoryException */ - public String getLogMessage() throws RepositoryException{ - //TODO - return null; - + public String getLogMessage() throws RepositoryException { + + Properties history = FileTools.getHistoryForNode(this, manager); + + int i = 1; + + while (history.getProperty("ver." + i) != null) { + i++; + } + i--; + + return history.getProperty("ver." + i); + } /** * Deletes this node or directory (immediately, no <code>save()</code> is - * needed). This resource should not be used after performing this + * needed). This resource should not be used after performing this * operation. * * @throws DeleteException * @throws RepositoryException */ - public void delete() throws DeleteException, RepositoryException{ + public void delete() throws DeleteException, RepositoryException { try { if (!file.delete()) { - throw new DeleteException("Couldn't delete file "+file.getAbsolutePath()); + throw new DeleteException("Couldn't delete file " + + file.getAbsolutePath()); } } catch (SecurityException e) { throw new RepositoryException(e); @@ -363,19 +572,26 @@ * is the root directory. */ public String getName() { - //TODO - return null; + return file.getName(); } - + /** * Gets the full name of this resource, that is, path to this resource * relative to the content manager this node was read from. * * @return Full name of this resource. */ - public String getFullName(){ - //TODO - return null; + public String getFullName() { + return file.getAbsolutePath().replace( + manager.getPathToRepoCurrent() + File.pathSeparator + + manager.getPrefix(), ""); } + public Directory getDirectory() { + return directory; + } + + public String getNodeName() { + return file.getName(); + } } Added: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileTools.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileTools.java 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FileTools.java 2006-01-25 23:27:05 UTC (rev 2204) @@ -0,0 +1,187 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2005, JBoss Inc., and individual contributors as indicated + * by the @authors tag. See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.shotoku.files; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; + +import javax.activation.MimetypesFileTypeMap; + +import org.jboss.shotoku.exceptions.RepositoryException; + +public class FileTools { + + private static MimetypesFileTypeMap mimeTypes; + + private final static String HIST_PROPS = "history.properties"; + + private final static String PROPS = "properties.properties"; + + public static final String STORE_TEXT = "Properties saved by Shotoku file-based"; + + public static final String REVISION_SUFFIX = ".version"; + + static { + mimeTypes = new MimetypesFileTypeMap(FileTools.class + .getResourceAsStream("/mime.types.txt")); + } + + public static String getMimeType(File file) { + return mimeTypes.getContentType(file); + } + + public static File getFileAtRevision(FileNode node, int revison, + FilesContentManager manager) { + // delete beggining part of path to get only dir inside repo + String directory = node.getDirectory().toString().replace( + manager.getPathToRepoCurrent(), ""); + + String pathToHistory = manager.getPathToRepoHistory() + + File.pathSeparator + directory + File.pathSeparator + + node.getNodeName() + "-his" + File.pathSeparator + revison + + REVISION_SUFFIX; + + File hisFile = new File(pathToHistory); + + return hisFile; + } + + public static File getHistoryDir(FileNode node, FilesContentManager manager) { + // delete beggining part of path to get only dir inside repo + String directory = node.getDirectory().toString().replace( + manager.getPathToRepoCurrent(), ""); + + String pathToHistory = manager.getPathToRepoHistory() + + File.pathSeparator + directory + File.pathSeparator + + node.getNodeName() + "-his"; + + File hisFile = new File(pathToHistory); + + return hisFile; + } + + public static File getHistoryFile(FileNode node, FilesContentManager manager) { + // delete beggining part of path to get only dir inside repo + String directory = node.getDirectory().toString().replace( + manager.getPathToRepoCurrent(), ""); + + String pathToHistory = manager.getPathToRepoHistory() + + File.pathSeparator + directory + File.pathSeparator + + node.getNodeName() + "-his"; + + createHistoryStructure(pathToHistory); + + File hisFile = new File(pathToHistory + File.pathSeparator + HIST_PROPS); + + return hisFile; + } + + public static File getPropsFile(FileNode node, FilesContentManager manager) { + // delete beggining part of path to get only dir inside repo + String directory = node.getDirectory().toString().replace( + manager.getPathToRepoCurrent(), ""); + + String pathToProps = manager.getPathToRepoHistory() + + File.pathSeparator + directory + File.pathSeparator + + node.getNodeName() + "-his"; + + createHistoryStructure(pathToProps); + + File propsFile = new File(pathToProps + File.pathSeparator + PROPS); + + return propsFile; + } + + public static Properties getHistoryForNode(FileNode node, + FilesContentManager manager) throws RepositoryException { + + File hisFile = getHistoryFile(node, manager); + + Properties hisProps = new Properties(); + + try { + hisProps.load(new FileInputStream(hisFile)); + } catch (FileNotFoundException e) { + throw new RepositoryException(e); + } catch (IOException e) { + throw new RepositoryException(e); + } + + return hisProps; + } + + public static Properties getPropertiesForNode(FileNode node, + FilesContentManager manager) throws RepositoryException { + + File propsFile = getPropsFile(node, manager); + + Properties props = new Properties(); + + try { + props.load(new FileInputStream(propsFile)); + } catch (FileNotFoundException e) { + throw new RepositoryException(e); + } catch (IOException e) { + throw new RepositoryException(e); + } + + return props; + } + + private static void createHistoryStructure(String hisDirStr) + throws RepositoryException { + File hisDir = new File(hisDirStr); + if (!hisDir.exists()) { + // create structure + hisDir.mkdirs(); + try { + (new File(hisDirStr + File.pathSeparator + HIST_PROPS)) + .createNewFile(); + (new File(hisDirStr + File.pathSeparator + PROPS)) + .createNewFile(); + } catch (IOException e) { + throw new RepositoryException(e); + } + } + } + + public static File getNextHistoryFile(FileNode node, + FilesContentManager manager, int version) { + // delete beggining part of path to get only dir inside repo + String directory = node.getDirectory().toString().replace( + manager.getPathToRepoCurrent(), ""); + + String pathToHistory = manager.getPathToRepoHistory() + + File.pathSeparator + directory + File.pathSeparator + + node.getNodeName() + "-his"; + + createHistoryStructure(pathToHistory); + + File hisFile = new File(pathToHistory + File.pathSeparator + version + + REVISION_SUFFIX); + + return hisFile; + } +} Modified: trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FilesContentManager.java =================================================================== --- trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FilesContentManager.java 2006-01-25 23:26:53 UTC (rev 2203) +++ trunk/forge/portal-extensions/shotoku/shotoku-files/src/java/org/jboss/shotoku/files/FilesContentManager.java 2006-01-25 23:27:05 UTC (rev 2204) @@ -21,8 +21,10 @@ */ package org.jboss.shotoku.files; +import java.io.File; import java.util.Collection; +import org.jboss.logging.Logger; import org.jboss.shotoku.ContentManager; import org.jboss.shotoku.Directory; import org.jboss.shotoku.Node; @@ -34,43 +36,108 @@ public class FilesContentManager extends ContentManager { - public FilesContentManager(String arg0, String arg1) throws RepositoryException { - super(arg0, arg1); - // TODO Auto-generated constructor stub + private String pathToRepo; + + private String pathToRepoCurrent; + + private String pathToRepoHistory; + + private final static String CURRENT = "CURRENT"; + + private final static String HISTORY = "HISTORY"; + + private Logger log = Logger.getLogger(FilesContentManager.class); + + public FilesContentManager(String id, String prefix) throws RepositoryException { + super(id, prefix); + + // add separator at the end and at the begginig + if (!prefix.startsWith("/")) { + prefix = "/" + prefix; + } + + if (!prefix.endsWith("/")) { + prefix += "/"; + } + + pathToRepo = getProperty("shotoku.default.localpath"); + + if (pathToRepo == null) { + throw new RepositoryException("shotoku.default.localpath is not set in properties"); + } + else if (!(new File(pathToRepo)).exists() || !(new File(pathToRepo)).isDirectory()){ + throw new RepositoryException(pathToRepo+" doesn't exist or isn't a directory"); + } + + // create paths + + pathToRepoCurrent = pathToRepo + File.pathSeparator + CURRENT; + pathToRepoHistory = pathToRepo + File.pathSeparator + HISTORY; + + // check for current and history existance + File fileCur = new File(pathToRepoCurrent); + File fileHis = new File(pathToRepoHistory); + + // create CURRENT folder if it doesn't exist + if (!fileCur.exists()) { + fileCur.mkdirs(); + log.info("Added CURRENT folder"); + } + + // create HISTORY folder if it doesn't exist + if (!fileHis.exists()) { + fileHis.mkdirs(); + log.info("Added HISTORY folder"); + } } @Override public Directory getRootDirectory() throws RepositoryException { - // TODO Auto-generated method stub - return null; + return new FileDirectory(getPathToRepoCurrent(), this); } @Override - public Node getNode(String arg0) throws ResourceDoesNotExist, + public Node getNode(String nodeName) throws ResourceDoesNotExist, RepositoryException { - // TODO Auto-generated method stub - return null; + return getRootDirectory().getNode(nodeName); } @Override - public Directory getDirectory(String arg0) throws ResourceDoesNotExist, + public Directory getDirectory(String dirName) throws ResourceDoesNotExist, RepositoryException { - // TODO Auto-generated method stub - return null; + return getRootDirectory().getDirectory(dirName); } @Override - public void save(Collection<Resource> arg0, String arg1) + public void save(Collection<Resource> resources, String message) throws SaveException, RepositoryException { - // TODO Auto-generated method stub - + for (Resource resource : resources) { + resource.save(message); + } } @Override - public void delete(Collection<Resource> arg0) throws DeleteException, + public void delete(Collection<Resource> resources) throws DeleteException, RepositoryException { - // TODO Auto-generated method stub + for (Resource resource : resources) { + resource.delete(); + } + } + public String getPathToRepo() { + return pathToRepo; } + public String getPathToRepoCurrent() { + return pathToRepoCurrent + prefix; + } + + public String getPathToRepoHistory() { + return pathToRepoHistory + prefix; + } + + public String getPrefix() { + return prefix; + } + } |