You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2004/02/10 16:32:19 UTC

cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/rc RevisionControlException.java RevisionController.java RCML.java

andreas     2004/02/10 07:32:19

  Modified:    src/java/org/apache/lenya/cms/rc RevisionController.java
                        RCML.java
  Added:       src/java/org/apache/lenya/cms/rc
                        RevisionControlException.java
  Log:
  refactored revision controller
  
  Revision  Changes    Path
  1.26      +67 -36    cocoon-lenya/src/java/org/apache/lenya/cms/rc/RevisionController.java
  
  Index: RevisionController.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/rc/RevisionController.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- RevisionController.java	18 Dec 2003 17:01:18 -0000	1.25
  +++ RevisionController.java	10 Feb 2004 15:32:19 -0000	1.26
  @@ -65,7 +65,6 @@
   import org.apache.lenya.util.XPSFileOutputStream;
   import org.apache.log4j.Category;
   
  -
   /**
    * Controller for the reserved check-in, check-out, the backup versions and the rollback 
    *
  @@ -83,7 +82,7 @@
       //   username as identity parameter to reservedCheckIn()
       //
       public static final String systemUsername = "System";
  -    
  +
       private String rcmlDir = null;
       private String rootDir = null;
       private String backupDir = null;
  @@ -95,9 +94,11 @@
       public RevisionController() {
           Configuration conf = new Configuration();
           rcmlDir = conf.getRcmlDirectory();
  -        if (!new File(rcmlDir).exists()) log.error("No such directory: " + rcmlDir);
  +        if (!new File(rcmlDir).exists())
  +            log.error("No such directory: " + rcmlDir);
           backupDir = conf.getBackupDirectory();
  -        if (!new File(backupDir).exists()) log.error("No such directory: " + backupDir);
  +        if (!new File(backupDir).exists())
  +            log.error("No such directory: " + backupDir);
           rootDir = "conf.rootDirectory";
       }
   
  @@ -154,19 +155,16 @@
        * @param source The filename of the file to check out
        * @param identity The identity of the user
        * @return File File to check out
  -	 * @throws FileReservedCheckOutException if the document is already checked out by another
  -     *            user
  -     * @throws FileNotFoundException if an error occurs
  -	 * @throws IOException if an error occurs
  -	 * @throws Exception if an error occurs
  -	 */
  -    public File reservedCheckOut(String source, String identity)
  -        throws FileNotFoundException, FileReservedCheckOutException, IOException, Exception {
  +     * @throws Exception if an error occurs
  +     */
  +    public File reservedCheckOut(String source, String identity) throws Exception {
  +        
           File file = new File(rootDir + source);
  -
  +        /*
           if (!file.isFile()) {
               throw new FileNotFoundException(file.getAbsolutePath());
           }
  +        */
   
           RCML rcml = new RCML(rcmlDir, source, rootDir);
   
  @@ -175,13 +173,15 @@
           // The same user is allowed to check out repeatedly without
           // having to check back in first.
           //
  -		if (entry != null) {
  -		  log.debug("entry: " + entry);
  -          log.debug("entry.type:" + entry.getType());
  -          log.debug("entry.identity" + entry.getIdentity());
  -		}  
  +        if (entry != null) {
  +            log.debug("entry: " + entry);
  +            log.debug("entry.type:" + entry.getType());
  +            log.debug("entry.identity" + entry.getIdentity());
  +        }
   
  -        if ((entry != null) && (entry.getType() != RCML.ci) && !entry.getIdentity().equals(identity)) {
  +        if ((entry != null)
  +            && (entry.getType() != RCML.ci)
  +            && !entry.getIdentity().equals(identity)) {
               throw new FileReservedCheckOutException(rootDir + source, rcml);
           }
   
  @@ -191,6 +191,33 @@
       }
   
       /**
  +     * Checks if a source can be checked out.
  +     * @param source The source.
  +     * @param identity The identity who requests checking out.
  +     * @return A boolean value.
  +     * @throws Exception when something went wrong.
  +     */
  +    public boolean canCheckOut(String source, String identity) throws Exception {
  +        RCML rcml = new RCML(rcmlDir, source, rootDir);
  +
  +        RCMLEntry entry = rcml.getLatestEntry();
  +
  +        // The same user is allowed to check out repeatedly without
  +        // having to check back in first.
  +        //
  +        if (entry != null) {
  +            log.debug("entry: " + entry);
  +            log.debug("entry.type:" + entry.getType());
  +            log.debug("entry.identity" + entry.getIdentity());
  +        }
  +
  +        boolean checkedOutByOther =
  +            entry != null && entry.getType() != RCML.ci && !entry.getIdentity().equals(identity);
  +
  +        return !checkedOutByOther;
  +    }
  +
  +    /**
        * Try to make a reserved check in of the file destination for a user with identity. A backup
        * copy can be made.
        *
  @@ -256,19 +283,22 @@
               }
           }
   
  -        File originalFile = new File(rootDir , destination);
  +        File originalFile = new File(rootDir, destination);
           long time = new Date().getTime();
   
           if (backup && originalFile.isFile()) {
  -            File backupFile = new File(backupDir , destination + ".bak." + time);
  +            File backupFile = new File(backupDir, destination + ".bak." + time);
               File parent = new File(backupFile.getParent());
   
               if (!parent.isDirectory()) {
                   parent.mkdirs();
               }
   
  -            log.info("Backup: copy " + originalFile.getAbsolutePath() + " to " +
  -                backupFile.getAbsolutePath());
  +            log.info(
  +                "Backup: copy "
  +                    + originalFile.getAbsolutePath()
  +                    + " to "
  +                    + backupFile.getAbsolutePath());
   
               InputStream in = new FileInputStream(originalFile.getAbsolutePath());
   
  @@ -304,7 +334,7 @@
        * @return String The absolute path of the backup version
        */
       public String getBackupFilename(long time, String filename) {
  -        File backup = new File(backupDir , filename + ".bak." + time);
  +        File backup = new File(backupDir, filename + ".bak." + time);
   
           return backup.getAbsolutePath();
       }
  @@ -325,12 +355,15 @@
        * @exception Exception if another problem occurs
        */
       public long rollback(String destination, String identity, boolean backupFlag, long time)
  -        throws FileReservedCheckInException, FileReservedCheckOutException, FileNotFoundException, 
  +        throws
  +            FileReservedCheckInException,
  +            FileReservedCheckOutException,
  +            FileNotFoundException,
               Exception {
           // Make sure the old version exists
           //
  -        File backup = new File(backupDir , destination + ".bak." + time);
  -        File current = new File(rootDir , destination);
  +        File backup = new File(backupDir, destination + ".bak." + time);
  +        File current = new File(rootDir, destination);
   
           if (!backup.isFile()) {
               throw new FileNotFoundException(backup.getAbsolutePath());
  @@ -358,12 +391,11 @@
   
           out.close();
   
  -		// Try to check back in, this might cause
  -		// a backup of the current version to be created if
  -		// desired by the user.
  -		//
  -		long newtime = reservedCheckIn(destination, identity, backupFlag);
  -
  +        // Try to check back in, this might cause
  +        // a backup of the current version to be created if
  +        // desired by the user.
  +        //
  +        long newtime = reservedCheckIn(destination, identity, backupFlag);
   
           return newtime;
       }
  @@ -377,8 +409,7 @@
        * @exception Exception FileNotFoundException if the back  version or the current version
        *            couldn't be found
        */
  -    public void undoCheckIn(long time, String destination)
  -        throws Exception {
  +    public void undoCheckIn(long time, String destination) throws Exception {
           File backup = new File(backupDir + "/" + destination + ".bak." + time);
           File current = new File(rootDir + destination);
   
  
  
  
  1.24      +59 -44    cocoon-lenya/src/java/org/apache/lenya/cms/rc/RCML.java
  
  Index: RCML.java
  ===================================================================
  RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/rc/RCML.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- RCML.java	10 Feb 2004 11:37:36 -0000	1.23
  +++ RCML.java	10 Feb 2004 15:32:19 -0000	1.24
  @@ -56,6 +56,8 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.util.HashMap;
  +import java.util.Map;
   import java.util.Vector;
   
   import javax.xml.parsers.ParserConfigurationException;
  @@ -68,7 +70,6 @@
   import org.w3c.dom.Element;
   import org.w3c.dom.Node;
   
  -
   /**
    * Handle with the RCML file
    *
  @@ -79,15 +80,25 @@
    */
   public class RCML {
       private static Category log = Category.getInstance(RCML.class);
  -    
  +
       public static final short co = 0;
       public static final short ci = 1;
  -    
  +
       private File rcmlFile;
       private Document document = null;
       private boolean dirty = false;
       private int maximalNumberOfEntries = 5;
   
  +    private static Map ELEMENTS = new HashMap();
  +    protected static final String ELEMENT_CHECKIN = "CheckIn";
  +    protected static final String ELEMENT_CHECKOUT = "CheckOut";
  +    protected static final String ELEMENT_BACKUP = "Backup";
  +
  +    {
  +        ELEMENTS.put(new Short(ci), ELEMENT_CHECKIN);
  +        ELEMENTS.put(new Short(co), ELEMENT_CHECKOUT);
  +    }
  +
       /**
        * Creates a new RCML object.
        */
  @@ -110,15 +121,14 @@
        *
        * @throws Exception if an error occurs
        */
  -    public RCML(String rcmlDirectory, String filename, String rootDirectory)
  -        throws Exception {
  +    public RCML(String rcmlDirectory, String filename, String rootDirectory) throws Exception {
           this();
  -        rcmlFile = new File(rcmlDirectory , filename + ".rcml");
  -     
  +        rcmlFile = new File(rcmlDirectory, filename + ".rcml");
  +
           if (!rcmlFile.isFile()) {
               // The rcml file does not yet exist, so we create it now...
               //
  -            File dataFile = new File(rootDirectory , filename);
  +            File dataFile = new File(rootDirectory, filename);
               long lastModified = 0;
   
               if (dataFile.isFile()) {
  @@ -127,10 +137,10 @@
   
               initDocument();
   
  -			// Create a "fake" checkin entry so it looks like the
  -			// system checked the document in. We use the filesystem
  -			// modification date as checkin time.
  -			//
  +            // Create a "fake" checkin entry so it looks like the
  +            // system checked the document in. We use the filesystem
  +            // modification date as checkin time.
  +            //
               checkOutIn(RCML.ci, RevisionController.systemUsername, lastModified, false);
   
               File parent = new File(rcmlFile.getParent());
  @@ -146,7 +156,7 @@
        * initialise the RCML-document. Delete all entries
        */
       public void initDocument() throws ParserConfigurationException {
  -        document = DocumentHelper.createDocument(null, "XPSRevisionControl", null); 
  +        document = DocumentHelper.createDocument(null, "XPSRevisionControl", null);
       }
   
       /**
  @@ -185,31 +195,27 @@
        */
       public void checkOutIn(short type, String identity, long time, boolean backup)
           throws IOException, Exception {
  -            
  +
  +        if (type != co && type != ci) {
  +            throw new IllegalArgumentException(
  +                "ERROR: " + this.getClass().getName() + ".checkOutIn(): No such type");
  +        }
  +
           NamespaceHelper helper = new NamespaceHelper(null, "", document);
  -            
  +
           Element identityElement = helper.createElement("Identity", identity);
           Element timeElement = helper.createElement("Time", "" + time);
   
  -        Element checkOutElement = null;
  -
  -        if (type == co) {
  -            checkOutElement = helper.createElement("CheckOut");
  -        } else if (type == ci) {
  -            checkOutElement = helper.createElement("CheckIn");
  -        } else {
  -            log.error("ERROR: " + this.getClass().getName() + ".checkOutIn(): No such type");
  -
  -            return;
  -        }
  +        String elementName = (String) ELEMENTS.get(new Short(type));
  +        Element checkOutElement = helper.createElement(elementName);
   
           checkOutElement.appendChild(identityElement);
           checkOutElement.appendChild(timeElement);
   
  -		if (backup) {
  -			Element backupElement = helper.createElement("Backup");
  -			checkOutElement.appendChild(backupElement);
  -		}
  +        if (backup) {
  +            Element backupElement = helper.createElement(ELEMENT_BACKUP);
  +            checkOutElement.appendChild(backupElement);
  +        }
   
           Element root = document.getDocumentElement();
           root.insertBefore(checkOutElement, root.getFirstChild());
  @@ -246,7 +252,9 @@
       public CheckOutEntry getLatestCheckOutEntry() throws Exception {
           XPointerFactory xpf = new XPointerFactory();
   
  -        Vector firstCheckOut = xpf.select(document.getDocumentElement(),
  +        Vector firstCheckOut =
  +            xpf.select(
  +                document.getDocumentElement(),
                   "xpointer(/XPSRevisionControl/CheckOut[1]/Identity)xpointer(/XPSRevisionControl/CheckOut[1]/Time)");
   
           if (firstCheckOut.size() == 0) {
  @@ -271,7 +279,9 @@
       public CheckInEntry getLatestCheckInEntry() throws Exception {
           XPointerFactory xpf = new XPointerFactory();
   
  -        Vector firstCheckIn = xpf.select(document.getDocumentElement(),
  +        Vector firstCheckIn =
  +            xpf.select(
  +                document.getDocumentElement(),
                   "xpointer(/XPSRevisionControl/CheckIn[1]/Identity)xpointer(/XPSRevisionControl/CheckIn[1]/Time)");
   
           if (firstCheckIn.size() == 0) {
  @@ -322,15 +332,17 @@
       public Vector getEntries() throws Exception {
           XPointerFactory xpf = new XPointerFactory();
   
  -        Vector entries = xpf.select(document.getDocumentElement(),
  +        Vector entries =
  +            xpf.select(
  +                document.getDocumentElement(),
                   "xpointer(/XPSRevisionControl/CheckOut|/XPSRevisionControl/CheckIn)");
           Vector RCMLEntries = new Vector();
   
           for (int i = 0; i < entries.size(); i++) {
               Element elem = (Element) entries.get(i);
               String time = elem.getElementsByTagName("Time").item(0).getFirstChild().getNodeValue();
  -            String identity = elem.getElementsByTagName("Identity").item(0).getFirstChild()
  -                                  .getNodeValue();
  +            String identity =
  +                elem.getElementsByTagName("Identity").item(0).getFirstChild().getNodeValue();
   
               if (elem.getTagName().equals("CheckOut")) {
                   RCMLEntries.add(new CheckOutEntry(identity, new Long(time).longValue()));
  @@ -353,14 +365,17 @@
       public void pruneEntries(String backupDir) throws Exception {
           XPointerFactory xpf = new XPointerFactory();
   
  -        Vector entries = xpf.select(document.getDocumentElement(),
  +        Vector entries =
  +            xpf.select(
  +                document.getDocumentElement(),
                   "xpointer(/XPSRevisionControl/CheckOut|/XPSRevisionControl/CheckIn)");
   
           for (int i = maximalNumberOfEntries; i < entries.size(); i++) {
               Element current = (Element) entries.get(i);
   
               // remove the backup file associated with this entry
  -            String time = current.getElementsByTagName("Time").item(0).getFirstChild().getNodeValue();
  +            String time =
  +                current.getElementsByTagName("Time").item(0).getFirstChild().getNodeValue();
               File backupFile = new File(backupDir + "/" + time + ".bak");
               backupFile.delete();
               // remove the entry from the list
  @@ -392,16 +407,16 @@
           return dirty;
       }
   
  -	/**
  -	 * Set the value dirty to true
  -	 */
  +    /**
  +     * Set the value dirty to true
  +     */
       protected void setDirty() {
           dirty = true;
       }
   
  -	/**
  -	 * Set the value dirty to false
  -	 */
  +    /**
  +     * Set the value dirty to false
  +     */
       protected void clearDirty() {
           dirty = false;
       }
  
  
  
  1.1                  cocoon-lenya/src/java/org/apache/lenya/cms/rc/RevisionControlException.java
  
  Index: RevisionControlException.java
  ===================================================================
  /*
  <License>
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Lenya" and  "Apache Software Foundation"  must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Michael Wechner <mi...@apache.org>. For more information on the Apache Soft-
   ware Foundation, please see <http://www.apache.org/>.
  
   Lenya includes software developed by the Apache Software Foundation, W3C,
   DOM4J Project, BitfluxEditor, Xopus, and WebSHPINX.
  </License>
  */package org.apache.lenya.cms.rc;
  
  /**
   * A revision control exception.
   * 
   * @author <a href="mailto:andreas@apache.org">Andreas Hartmann</a>
   * @version $Id: RevisionControlException.java,v 1.1 2004/02/10 15:32:19 andreas Exp $
   */
  public class RevisionControlException extends Exception {
  
      /**
       * Ctor.
       */
      public RevisionControlException() {
          super();
      }
  
      /**
       * Ctor.
       * @param message The message.
       */
      public RevisionControlException(String message) {
          super(message);
      }
  
      /**
       * Ctor.
       * @param cause The cause.
       */
      public RevisionControlException(Throwable cause) {
          super(cause);
      }
  
      /**
       * Ctor.
       * @param message The message.
       * @param cause The cause.
       */
      public RevisionControlException(String message, Throwable cause) {
          super(message, cause);
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org