You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/07/09 20:29:43 UTC

cvs commit: jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/util FileSystemHelper.java JarHelper.java DirectoryHelper.java

weaver      2004/07/09 11:29:43

  Added:       portal/src/java/org/apache/jetspeed/util
                        FileSystemHelper.java JarHelper.java
                        DirectoryHelper.java
  Log:
  Our replacement for the VFS-style logic we need
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/util/FileSystemHelper.java
  
  Index: FileSystemHelper.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.util;
  
  import java.io.File;
  import java.io.IOException;
  
  /**
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   *
   * TODO To change the template for this generated type comment go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  public interface FileSystemHelper
  {
      /**
       * 
       * <p>
       * copyFrom
       * </p>
       *
       * @param directory Directory to copy content from
       * @throws {@link java.io.IlleaglArgumentException} if the <code>directory.isDirectory</code>
       * returns <code>false</code>
       */
      void copyFrom(File directory) throws IOException;
      
      /**
       * 
       * <p>
       * remove
       * </p>
       * Removes the underlying directory structure from the root directory down.
       * 
       * @return <code>true</code> if the removal war successful, otherwise returns
       * <code>false</code>.
       */
      boolean remove();
      
      /**
       * 
       * <p>
       * getRootDirectory
       * </p>
       *
       * @return the root of the directory structure
       */
      File getRootDirectory();    
      
      /**
       * 
       * <p>
       * close
       * </p>
       *
       * Cleans up resources opened up specifically by this FileSystemHelper 
       *
       */
      void close() throws IOException;
      
      /**
       * 
       * <p>
       * getSourcePath
       * </p>
       * 
       * Returns the true location of this FileSystemHelper backing object on
       * the file system.  This IS NOT always as the path of the object returned
       * from the {@link getRootDirectory} method. 
       *
       * @return the true location of this FileSystemHelper backing object. 
       */
      String getSourcePath();
      
  }
  
  
  
  1.1                  jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/util/JarHelper.java
  
  Index: JarHelper.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.util;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Enumeration;
  import java.util.jar.JarEntry;
  import java.util.jar.JarFile;
  
  /**
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
   * 
   * Creates a a temp directory to which a JarFile is expanded and can be
   * manipulated. All operations are performed by an internal instance of
   * {@link DirectoryHelper}.
   *  
   */
  public class JarHelper implements FileSystemHelper
  {
      protected JarFile jarFile;
      protected DirectoryHelper dirHelper;
      protected File file;
      private boolean deleteOnClose;
      protected File jarRoot;
  
      /**
       * 
       * @param jarFile
       * @throws IOException
       */
      public JarHelper( File file, boolean deleteOnClose ) throws IOException
      {
          this.jarFile = new JarFile(file);
          this.deleteOnClose = deleteOnClose;
          this.file = file;
          String tmpDirPath = System.getProperty("java.io.tmpdir");
          File tmpDir = new File(tmpDirPath);
          jarRoot = null;
  
          jarRoot = new File(tmpDir, "jetspeed-jar-tmp/" + file.getName());
  
          if (!jarRoot.exists())
          {
              jarRoot.mkdirs();
          }
          jarRoot.deleteOnExit();
  
          Enumeration entries = this.jarFile.entries();
          while (entries.hasMoreElements())
          {
              JarEntry jarEntry = (JarEntry) entries.nextElement();
              String name = jarEntry.getName();
              if (jarEntry.isDirectory())
              {
                  File newDir = new File(jarRoot, name);
                  newDir.mkdir();
                  newDir.deleteOnExit();
              }
              else
              {
                  copyEntryToFile(jarFile, jarRoot, jarEntry);
              }
          }
  
          dirHelper = new DirectoryHelper(jarRoot);
      }
  
      /**
       * <p>
       * copyEntryToFile
       * </p>
       * 
       * @param jarFile
       * @param jarRoot
       * @param jarEntry
       * @throws IOException
       * @throws FileNotFoundException
       */
      protected void copyEntryToFile( JarFile jarFile, File jarRoot, JarEntry jarEntry ) throws IOException,
              FileNotFoundException
      {
          String name = jarEntry.getName();
          File file = new File(jarRoot, name);
          if (!file.getParentFile().exists())
          {
              file.getParentFile().mkdirs();
              file.getParentFile().deleteOnExit();
          }
          file.createNewFile();
          file.deleteOnExit();
  
          InputStream is = null;
          OutputStream os = null;
          try
          {
              is = jarFile.getInputStream(jarEntry);
              os = new FileOutputStream(file);
  
              byte[] buf = new byte[1024];
              int len;
              while ((len = is.read(buf)) > 0)
              {
                  os.write(buf, 0, len);
              }
  
          }
          finally
          {
              if (is != null)
              {
                  is.close();
              }
  
              if (os != null)
              {
                  os.close();
              }
          }
      }
  
      /**
       * <p>
       * copyFrom
       * </p>
       * 
       * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
       * @param directory
       * @throws IOException
       */
      public void copyFrom( File directory ) throws IOException
      {
          dirHelper.copyFrom(directory);
      }
  
      /**
       * <p>
       * getRootDirectory
       * </p>
       * 
       * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
       * @return
       */
      public File getRootDirectory()
      {
          return dirHelper.getRootDirectory();
      }
  
      /**
       * <p>
       * remove
       * </p>
       * 
       * @see org.apache.jetspeed.util.FileSystemHelper#remove()
       * @return
       */
      public boolean remove()
      {
          return dirHelper.remove();
      }
  
      /**
       * <p>
       * close
       * </p>
       * 
       * @throws IOException
       * 
       * @see org.apache.jetspeed.util.FileSystemHelper#close()
       *  
       */
      public void close() throws IOException
      {
          jarFile.close();
          if (deleteOnClose)
          {
              // remove all temporary files
              dirHelper.remove();
          }
          
          dirHelper.close();
      }
  
      /**
       * <p>
       * getSourcePath
       * </p>
       * 
       * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
       * @return
       */
      public String getSourcePath()
      {
          return file.getAbsolutePath();
      }
  }
  
  
  1.1                  jakarta-jetspeed-2/portal/src/java/org/apache/jetspeed/util/DirectoryHelper.java
  
  Index: DirectoryHelper.java
  ===================================================================
  /*
   * Copyright 2000-2001,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.util;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.nio.channels.FileChannel;
  
  /**
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   *
   * TODO To change the template for this generated type comment go to
   * Window - Preferences - Java - Code Generation - Code and Comments
   */
  public class DirectoryHelper implements FileSystemHelper
  {
  
      protected File directory;
  
      /**
       * 
       */
      public DirectoryHelper(File directory)
      {
          super();
          if(!directory.exists())
          {
              directory.mkdirs();
          }
          
          if(!directory.isDirectory())
          {
              throw new IllegalArgumentException("DirectoryHelper(File) requires directory not a file.");
          }
          this.directory = directory;
      }
  
      /**
       * <p>
       * copyFrom
       * </p>
       *
       * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File)
       * @param directory
       * @throws IOException
       */
      public void copyFrom( File srcDirectory ) throws IOException
      {
          if(!directory.isDirectory())
          {
              throw new IllegalArgumentException("DirectoryHelper.copyFrom(File) requires directory not a file.");
          }
          copyFiles(srcDirectory, directory);
      }
      
      /**
       * 
       * <p>
       * copyFiles
       * </p>
       *
       * @param srcDir Source directory to copy from.
       * @param dstDir Destination directory to copy to.
       * @throws IOException
       * @throws FileNotFoundException
  
       */
      protected void copyFiles(File srcDir, File dstDir) throws IOException
      {
               
          File[] children = srcDir.listFiles();
          for(int i=0; i<children.length; i++)
          {
              File child = children[i];
              if(child.isFile())
              {
                  File toFile = new File(dstDir, child.getName());
                  toFile.createNewFile();
                  FileChannel srcChannel = new FileInputStream(child).getChannel();
                  FileChannel dstChannel = new FileOutputStream(toFile).getChannel();
                  dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
                  srcChannel.close();
                  dstChannel.close();
              }
              else
              {
                  File newSubDir = new File(dstDir, child.getName());
                  newSubDir.mkdir();
                  copyFiles(child, newSubDir);
              }
          }
      }
  
      /**
       * <p>
       * remove
       * </p>
       *
       * @see org.apache.jetspeed.util.FileSystemHelper#remove()
       * 
       */
      public boolean remove()
      {
          return doRemove(directory);
      }
      
      /**
       * 
       * <p>
       * doRemove
       * </p>
       *
       * @param file
       * @return <code>true</code> if the removal war successful, otherwise returns
       * <code>false</code>.
       */
      protected boolean doRemove(File file)
      {
          if (file.isDirectory())
  		{
  			String[] children = file.list();
  			for (int i = 0; i < children.length; i++)
  			{
  				boolean success = doRemove(new File(file, children[i]));
  				if (!success)
  				{
  					return false;
  				}
  			}
  		}
  
  		// The directory is now empty so delete it OR it is a plain file
  		return file.delete();        
      }
  
      /**
       * <p>
       * getRootDirectory
       * </p>
       *
       * @see org.apache.jetspeed.util.FileSystemHelper#getRootDirectory()
       * @return
       */
      public File getRootDirectory()
      {       
          return directory;
      }
  
      /**
       * <p>
       * close
       * </p>
       *
       * @see org.apache.jetspeed.util.FileSystemHelper#close()
       * 
       */
      public void close()
      {
          // TODO Auto-generated method stub
  
      }
      /**
       * <p>
       * getSourcePath
       * </p>
       *
       * @see org.apache.jetspeed.util.FileSystemHelper#getSourcePath()
       * @return
       */
      public String getSourcePath()
      {       
          return getRootDirectory().getAbsolutePath();
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org