FileSystemUtilities.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.resources;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.OutputStream;
  29. import java.nio.charset.Charset;
  30. import java.nio.file.Files;
  31. import java.nio.file.Path;

  32. import org.openspcoop2.utils.Utilities;
  33. import org.openspcoop2.utils.UtilsException;


  34. /**
  35.  * Classe utilizzabile per raccogliere utility su operazioni effettuate su file system
  36.  *
  37.  * @author Andrea Poli (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class FileSystemUtilities {
  42.    
  43.     private FileSystemUtilities() {}

  44.     public static File createTempFile(String prefix, String suffix) throws IOException{
  45.         Path p = createTempPath(prefix, suffix);
  46.         if(p==null) {
  47.             throw new IOException("Creation failed");
  48.         }
  49.         return p.toFile();
  50.     }
  51.     public static Path createTempPath(String prefix, String suffix) throws IOException{
  52.         return Utilities.createTempPath(prefix, suffix);
  53.     }
  54.    
  55.     public static File createTempFile(File dir, String prefix, String suffix) throws IOException{
  56.         Path p = createTempPath(dir.toPath(), prefix, suffix);
  57.         if(p==null) {
  58.             throw new IOException("Creation failed");
  59.         }
  60.         return p.toFile();
  61.     }
  62.     public static Path createTempPath(Path dir, String prefix, String suffix) throws IOException{
  63.         return Utilities.createTempPath(dir, prefix, suffix);
  64.     }
  65.    
  66.     public static void copy(File in, File out) throws IOException {
  67.         FileSystemUtilities.copy(in.getAbsolutePath(),out.getAbsolutePath());
  68.     }
  69.     public static void copy(String in, String out)
  70.     throws IOException {
  71.         try(FileInputStream fin = new FileInputStream(in);
  72.             FileOutputStream fout = new FileOutputStream(out);){
  73.             FileSystemUtilities.copy(fin,fout);
  74.             fout.flush();
  75.         }
  76.     }
  77.     public static void copy(InputStream in, OutputStream out)
  78.     throws IOException {

  79.         byte[] buffer = new byte[256];
  80.         while (true) {
  81.             int bytesRead = in.read(buffer);
  82.             if (bytesRead == -1) break;
  83.             out.write(buffer, 0, bytesRead);
  84.         }

  85.     }

  86.     public static void copyDirectory(String srcPath, String dstPath)
  87.     throws IOException{
  88.         FileSystemUtilities.copyDirectory(new File(srcPath), new File(dstPath));
  89.     }
  90.     public static void copyDirectory(File srcPath, File dstPath)
  91.     throws IOException{

  92.         if (srcPath.isDirectory()){
  93.             if (!dstPath.exists()){
  94.                 dstPath.mkdir();
  95.             }

  96.             String[] files = srcPath.list();
  97.             if(files!=null) {
  98.                 for(int i = 0; i < files.length; i++){
  99.                         FileSystemUtilities.copyDirectory(new File(srcPath, files[i]),
  100.                                 new File(dstPath, files[i]));
  101.                 }
  102.             }
  103.         }
  104.         else{
  105.             if(!srcPath.exists()){
  106.                 /** System.out.println("File or directory does not exist."); */
  107.             }
  108.             else {
  109.                 try (InputStream in = new FileInputStream(srcPath);
  110.                     OutputStream out = new FileOutputStream(dstPath);){
  111.                     // Transfer bytes from in to out
  112.                     byte[] buf = new byte[1024];
  113.                     int len;
  114.                     while ((len = in.read(buf)) > 0) {
  115.                         out.write(buf, 0, len);
  116.                     }
  117.                     out.flush();
  118.                 }
  119.             }
  120.         }
  121.         /** System.out.println("Directory copied."); */
  122.     }
  123.    
  124.    
  125.     public static String readFile(String f) throws UtilsException, FileNotFoundException{
  126.         return FileSystemUtilities.readFile(new File(f));
  127.     }
  128.     public static String readFile(File f) throws UtilsException, FileNotFoundException{
  129.         return readFileEngine(f, null, null);
  130.     }
  131.     public static String readFile(String f, String charsetName) throws UtilsException, FileNotFoundException{
  132.         return FileSystemUtilities.readFile(new File(f),charsetName);
  133.     }
  134.     public static String readFile(File f, String charsetName) throws UtilsException, FileNotFoundException{
  135.         return readFileEngine(f, charsetName, null);
  136.     }
  137.     public static String readFile(String f, Charset charset) throws UtilsException, FileNotFoundException{
  138.         return FileSystemUtilities.readFile(new File(f), charset);
  139.     }
  140.     public static String readFile(File f, Charset charset) throws UtilsException, FileNotFoundException{
  141.         return readFileEngine(f, null, charset);
  142.     }
  143.     private static String readFileEngine(File f, String charsetName, Charset charset) throws UtilsException, FileNotFoundException{
  144.         try (FileInputStream fis =new FileInputStream(f);
  145.             ByteArrayOutputStream byteInputBuffer = new ByteArrayOutputStream();){
  146.             byte [] readB = new byte[8192];
  147.             int readByte = 0;
  148.             while((readByte = fis.read(readB))!= -1){
  149.                 byteInputBuffer.write(readB,0,readByte);
  150.             }
  151.             byteInputBuffer.flush();
  152.            
  153.             if(charsetName!=null) {
  154.                 return  byteInputBuffer.toString(charsetName);
  155.             }
  156.             else if(charset!=null) {
  157.                 return  byteInputBuffer.toString(charset);
  158.             }
  159.             else {
  160.                 return  byteInputBuffer.toString();
  161.             }
  162.         }
  163.         catch(FileNotFoundException notFound) {
  164.             throw notFound;
  165.         }
  166.         catch(Exception e) {
  167.             throw new UtilsException(e.getMessage(),e);
  168.         }
  169.     }
  170.    
  171.     public static byte[] readBytesFromFile(String f) throws UtilsException, FileNotFoundException{
  172.         return FileSystemUtilities.readBytesFromFile(new File(f));
  173.     }
  174.     public static byte[] readBytesFromFile(File f) throws UtilsException, FileNotFoundException{    
  175.         try (FileInputStream fis =new FileInputStream(f);
  176.             ByteArrayOutputStream byteInputBuffer = new ByteArrayOutputStream();){
  177.             byte [] readB = new byte[8192];
  178.             int readByte = 0;
  179.             while((readByte = fis.read(readB))!= -1){
  180.                 byteInputBuffer.write(readB,0,readByte);
  181.             }
  182.             byteInputBuffer.flush();
  183.            
  184.             return  byteInputBuffer.toByteArray();
  185.         }
  186.         catch(FileNotFoundException notFound) {
  187.             throw notFound;
  188.         }
  189.         catch(Exception e) {
  190.             throw new UtilsException(e.getMessage(),e);
  191.         }
  192.     }
  193.    
  194.    
  195.     public static void writeFile(String f,byte[] contenuto)throws UtilsException{
  196.         FileSystemUtilities.writeFile(new File(f), contenuto);
  197.     }
  198.     public static void writeFile(File f,byte[] contenuto)throws UtilsException{
  199.         try (FileOutputStream fos = new FileOutputStream(f);){
  200.             fos.write(contenuto);
  201.             fos.flush();
  202.         }
  203.         catch(Exception e) {
  204.             throw new UtilsException(e.getMessage(),e);
  205.         }
  206.     }
  207.     public static void writeFile(String f,byte[] ... args)throws UtilsException{
  208.         FileSystemUtilities.writeFile(new File(f), args);
  209.     }
  210.     public static void writeFile(File f,byte[] ... args)throws UtilsException{
  211.         if(args!=null){
  212.             try (FileOutputStream fos = new FileOutputStream(f);){
  213.                 for(int i=0; i<args.length; i++){
  214.                     fos.write(args[i]);
  215.                 }
  216.                 fos.flush();
  217.             }
  218.             catch(Exception e) {
  219.                 throw new UtilsException(e.getMessage(),e);
  220.             }
  221.         }
  222.     }
  223.    
  224.     public static void copyFileAndReplaceAllKeywords(String read,String write,String keyword,String values) throws UtilsException, FileNotFoundException{
  225.         FileSystemUtilities.copyFileAndReplaceAllKeywords(new File(read),new File(write),keyword,values);
  226.     }
  227.     public static void copyFileAndReplaceAllKeywords(File read,File write,String keyword,String values) throws UtilsException, FileNotFoundException{
  228.         String[]k = new String[1];
  229.         k[0] = keyword;
  230.         String[]v = new String[1];
  231.         v[0] = values;
  232.         FileSystemUtilities.copyFileAndReplaceAllKeywords(read, write, k, v);
  233.     }
  234.     public static void copyFileAndReplaceAllKeywords(String read,String write,String[] keyword,String[] values) throws UtilsException, FileNotFoundException{
  235.         FileSystemUtilities.copyFileAndReplaceAllKeywords(new File(read),new File(write),keyword,values);
  236.     }
  237.     public static void copyFileAndReplaceAllKeywords(File read,File write,String[] keyword,String[] values) throws UtilsException, FileNotFoundException{
  238.         String file = FileSystemUtilities.readFile(read);
  239.         for(int i=0; i<keyword.length; i++){
  240.            
  241.             /** System.out.println("FILE ["+file+"] contains ["+keyword[i]+"] (value:"+file.indexOf(keyword[i])+")"); */
  242.             int indexOf = file.indexOf(keyword[i]);
  243.             while(indexOf>=0){
  244.                 /** System.out.println("REPLACE ["+keyword[i]+"] ["+values[i]+"]"); */
  245.                 file = file.replace(keyword[i], values[i]);
  246.                 indexOf = file.indexOf(keyword[i],indexOf+values[i].length());
  247.                 /** System.out.println("INDEX OF ["+indexOf+"]"); */
  248.             }
  249.         }
  250.         FileSystemUtilities.writeFile(write, file.getBytes());
  251.     }
  252.    
  253.     public static void copyFileAndReplaceKeywords(String read,String write,String keyword,String values) throws UtilsException, FileNotFoundException{
  254.         FileSystemUtilities.copyFileAndReplaceKeywords(new File(read),new File(write),keyword,values);
  255.     }
  256.     public static void copyFileAndReplaceKeywords(File read,File write,String keyword,String values) throws UtilsException, FileNotFoundException{
  257.         String[]k = new String[1];
  258.         k[0] = keyword;
  259.         String[]v = new String[1];
  260.         v[0] = values;
  261.         FileSystemUtilities.copyFileAndReplaceKeywords(read, write, k, v);
  262.     }
  263.    
  264.     public static void copyFileAndReplaceKeywords(String read,String write,String[] keyword,String[] values) throws UtilsException, FileNotFoundException{
  265.         FileSystemUtilities.copyFileAndReplaceKeywords(new File(read),new File(write),keyword,values);
  266.     }
  267.     public static void copyFileAndReplaceKeywords(File read,File write,String[] keyword,String[] values) throws UtilsException, FileNotFoundException{
  268.         String file = FileSystemUtilities.readFile(read);
  269.         for(int i=0; i<keyword.length; i++){
  270.             file = file.replace(keyword[i], values[i]);
  271.         }
  272.         FileSystemUtilities.writeFile(write, file.getBytes());
  273.     }


  274.     public static boolean emptyDir(String dir) {
  275.         File d = new File(dir);
  276.         if(!d.exists()){
  277.             return true;
  278.         }
  279.         return FileSystemUtilities.emptyDir(d);
  280.     }
  281.    
  282.     public static boolean emptyDir(File dir) {

  283.         if (dir.isDirectory()) {
  284.             String[] children = dir.list();
  285.             if(children!=null && children.length>0) {
  286.                 for (int i=0; i<children.length; i++) {
  287.                     boolean success = FileSystemUtilities.deleteDir(new File(dir, children[i]));
  288.                     if (!success) {
  289.                         return false;
  290.                     }
  291.                 }
  292.             }
  293.         }
  294.        
  295.         return true;
  296.     }
  297.    
  298.     public static boolean deleteDirNotEmpty(String dir, int maxChilds) {
  299.         return deleteDirNotEmpty(new File(dir), maxChilds);
  300.     }
  301.     public static boolean deleteDirNotEmpty(File dir, int maxChilds) {
  302.         return deleteDirNotEmpty(dir, maxChilds , 0);
  303.     }
  304.     private static boolean deleteDirNotEmpty(File dir, int maxChilds, int level) {
  305.         if (emptyDir(dir)) {
  306.             deleteDir(dir);
  307.             return true;
  308.         }
  309.         else {
  310.             if(maxChilds==level) {
  311.                 return false;
  312.             }
  313.             File [] childs = dir.listFiles();
  314.             if(childs!=null && childs.length>0) {
  315.                 return deleteDirNotEmpty(childs, maxChilds, level);
  316.             }
  317.             return true;
  318.         }
  319.     }
  320.     private static boolean deleteDirNotEmpty(File [] childs, int maxChilds, int level) {
  321.         boolean ok = true;
  322.         for (File file : childs) {
  323.             if(file.isDirectory()) {
  324.                 boolean v = deleteDirNotEmpty(file, maxChilds , level+1);
  325.                 if(!v) {
  326.                     ok = false;
  327.                 }
  328.             }
  329.             else {
  330.                 try {
  331.                     Files.delete(file.toPath());
  332.                 }catch(Exception e) {
  333.                     ok = false;
  334.                 }
  335.             }
  336.         }
  337.         return ok;
  338.     }
  339.    
  340.     public static boolean deleteDir(String dir) {
  341.         File d = new File(dir);
  342.         if(!d.exists()){
  343.             return true;
  344.         }
  345.         return FileSystemUtilities.deleteDir(d);
  346.     }
  347.    
  348.     public static boolean deleteDir(File dir) {

  349.         if (!emptyDir(dir)) {
  350.             return false;
  351.         }

  352.         // The directory is now empty so now it can be smoked
  353.         try {
  354.             Files.delete(dir.toPath());
  355.             return true;
  356.         }catch(Exception e) {
  357.             return false;
  358.         }
  359.     }
  360.    
  361.     public static void deleteFile(File f) {
  362.         try {
  363.             if(f!=null) {
  364.                 java.nio.file.Files.delete(f.toPath());
  365.             }
  366.         }catch(Exception e) {
  367.             // ignore
  368.         }
  369.     }
  370.    
  371.     public static void clearFile(File f) {
  372.         try {
  373.             if(f!=null) {
  374.                 try(FileOutputStream fos = new FileOutputStream(f);){
  375.                     byte[] emptyBytes = new byte[0];
  376.                     fos.write(emptyBytes);
  377.                     fos.flush();
  378.                 }
  379.             }
  380.         }catch(Exception e) {
  381.             // ignore
  382.         }
  383.     }
  384.    
  385.     public static boolean moveToDir(String src,String destDir){
  386.          // File (or directory) to be moved
  387.         File file = new File(src);
  388.        
  389.         // Destination directory
  390.         File dir = new File(destDir);
  391.        
  392.         return FileSystemUtilities.moveToDir(file,dir);
  393.     }
  394.     public static boolean moveToDir(File src,File destDir){

  395.         // Move file to new directory
  396.         return src.renameTo(new File(destDir, src.getName()));

  397.     }
  398.    
  399.     public static boolean moveToFile(String src,String destFile){
  400.      // File (or directory) to be moved
  401.        File file = new File(src);
  402.        
  403.        // Destination directory
  404.        File dir = new File(destFile);
  405.        
  406.        return FileSystemUtilities.moveToDir(file,dir);
  407.    }
  408.    public static boolean moveToFile(File src,File destFile){

  409.        // Move file to new directory
  410.        return src.renameTo(destFile);
  411.    }
  412.    
  413.    public static void mkdirParentDirectory(File file) throws UtilsException {
  414.        FileSystemUtilities.mkdirParentDirectory(file.getAbsolutePath());
  415.    }
  416.    public static void mkdirParentDirectory(File file,
  417.            Boolean readable, Boolean readableOwnerOnly,
  418.            Boolean writable, Boolean writableOwnerOnly,
  419.            Boolean executable, Boolean executableOwnerOnly) throws UtilsException {
  420.        FileSystemUtilities.mkdirParentDirectory(file.getAbsolutePath(),
  421.                readable, readableOwnerOnly,
  422.                writable, writableOwnerOnly,
  423.                executable, executableOwnerOnly);
  424.    }
  425.    public static void mkdirParentDirectory(String file) throws UtilsException {
  426.        mkdirParentDirectory(file,
  427.                null, null,
  428.                null, null,
  429.                null, null);
  430.    }
  431.    public static void mkdirParentDirectory(String file,
  432.            Boolean readable, Boolean readableOwnerOnly,
  433.            Boolean writable, Boolean writableOwnerOnly,
  434.            Boolean executable, Boolean executableOwnerOnly) throws UtilsException {
  435.         try{
  436.             File p = new File(file);
  437.             if(p.getParentFile()==null){
  438.                 return;
  439.             }
  440.             if(p.getParentFile().exists()){
  441.                 return;
  442.             }
  443.             FileSystemUtilities.mkdirParentDirectory(p.getParentFile().getAbsolutePath());
  444.             if(!p.getParentFile().mkdir()){
  445.                 throw new UtilsException(DIRECTORY_PREFIX_MSG+p.getParentFile().getAbsolutePath()+"] non esistente e creazione non riuscita");
  446.             }
  447.             else {
  448.                 setRWX(p,
  449.                         readable, readableOwnerOnly,
  450.                         writable, writableOwnerOnly,
  451.                         executable, executableOwnerOnly);
  452.             }
  453.         }catch(Exception e){
  454.             throw new UtilsException("mkdirParentDirectory non riuscito: "+e.getMessage(),e);
  455.         }
  456.    }
  457.    private static void setRWX(File p,
  458.            Boolean readable, Boolean readableOwnerOnly,
  459.            Boolean writable, Boolean writableOwnerOnly,
  460.            Boolean executable, Boolean executableOwnerOnly) {
  461.        setParentR(p, readable, readableOwnerOnly);
  462.        setParentW(p, writable, writableOwnerOnly);
  463.        setParentX(p, executable, executableOwnerOnly);
  464.    }
  465.    private static void setParentR(File p, Boolean readable, Boolean readableOwnerOnly) {
  466.        if(readable!=null) {
  467.             if(readableOwnerOnly!=null) {
  468.                 if(!p.getParentFile().setReadable(readable, readableOwnerOnly)) {
  469.                     // ignore
  470.                 }
  471.             }
  472.             else {
  473.                 if(!p.getParentFile().setReadable(readable)) {
  474.                     // ignore
  475.                 }
  476.             }
  477.        }
  478.    }
  479.    private static void setParentW(File p, Boolean writable, Boolean writableOwnerOnly) {
  480.        if(writable!=null) {
  481.             if(writableOwnerOnly!=null) {
  482.                 if(!p.getParentFile().setWritable(writable, writableOwnerOnly)) {
  483.                     // ignore
  484.                 }
  485.             }
  486.             else {
  487.                 if(!p.getParentFile().setWritable(writable)) {
  488.                     // ignore
  489.                 }
  490.             }
  491.         }
  492.    }
  493.    private static void setParentX(File p, Boolean executable, Boolean executableOwnerOnly) {
  494.         if(executable!=null) {
  495.             if(executableOwnerOnly!=null) {
  496.                 if(!p.getParentFile().setExecutable(executable, executableOwnerOnly)) {
  497.                     // ignore
  498.                 }
  499.             }
  500.             else {
  501.                 if(!p.getParentFile().setExecutable(executable)) {
  502.                     // ignore
  503.                 }
  504.             }
  505.         }
  506.    }
  507.    
  508.    public static void mkdir(String f) throws UtilsException{
  509.        mkdir(f, new FileSystemMkdirConfig());
  510.    }
  511.    public static void mkdir(String f,
  512.            Boolean readable, Boolean readableOwnerOnly,
  513.            Boolean writable, Boolean writableOwnerOnly,
  514.            Boolean executable, Boolean executableOwnerOnly) throws UtilsException{
  515.        mkdir(f, new FileSystemMkdirConfig(),
  516.                new FileRWXConfig(readable, readableOwnerOnly,
  517.                        writable, writableOwnerOnly,
  518.                        executable, executableOwnerOnly));
  519.    }
  520.    public static void mkdir(String f,
  521.            FileRWXConfig rwxConfig) throws UtilsException{
  522.        mkdir(f, new FileSystemMkdirConfig(), rwxConfig);
  523.    }
  524.    public static void mkdir(String f, FileSystemMkdirConfig config) throws UtilsException{
  525.        File dir = new File(f);
  526.        mkdir(dir, config);
  527.    }
  528.    public static void mkdir(String f, FileSystemMkdirConfig config,
  529.            FileRWXConfig rwxConfig) throws UtilsException{
  530.        File dir = new File(f);
  531.        mkdir(dir, config, rwxConfig);
  532.    }
  533.    
  534.    public static void mkdir(File dir) throws UtilsException{
  535.        mkdir(dir, new FileSystemMkdirConfig());
  536.    }
  537.    public static void mkdir(File dir,
  538.            Boolean readable, Boolean readableOwnerOnly,
  539.            Boolean writable, Boolean writableOwnerOnly,
  540.            Boolean executable, Boolean executableOwnerOnly) throws UtilsException{
  541.        mkdir(dir, new FileSystemMkdirConfig(),
  542.                new FileRWXConfig(readable, readableOwnerOnly,
  543.                        writable, writableOwnerOnly,
  544.                        executable, executableOwnerOnly));
  545.    }
  546.    public static void mkdir(File dir, FileRWXConfig rwxConfig) throws UtilsException{
  547.        mkdir(dir, new FileSystemMkdirConfig(), rwxConfig);
  548.    }
  549.    public static void mkdir(File dir, FileSystemMkdirConfig config) throws UtilsException{
  550.        mkdir(dir, config, null);
  551.    }
  552.    public static void mkdir(File dir, FileSystemMkdirConfig config,
  553.            FileRWXConfig rwxConfig) throws UtilsException{
  554.        
  555.        Boolean readable = null;
  556.        Boolean readableOwnerOnly = null;
  557.        Boolean writable = null;
  558.        Boolean writableOwnerOnly = null;
  559.        Boolean executable = null;
  560.        Boolean executableOwnerOnly = null;
  561.        if(rwxConfig!=null) {
  562.            readable = rwxConfig.getReadable();
  563.            readableOwnerOnly = rwxConfig.getReadableOwnerOnly();
  564.            writable = rwxConfig.getWritable();
  565.            writableOwnerOnly = rwxConfig.getWritableOwnerOnly();
  566.            executable = rwxConfig.getExecutable();
  567.            executableOwnerOnly = rwxConfig.getExecutableOwnerOnly();
  568.        }
  569.        
  570.        if(dir.exists()){
  571.            checkDir(dir, config);
  572.         }
  573.         else{
  574.             if(dir.getParentFile()!=null) {
  575.                 File parent = dir.getParentFile();
  576.                 if(!parent.exists() &&
  577.                     config.crateParentIfNotExists) {
  578.                     mkdirParentDirectory(dir,
  579.                             readable, readableOwnerOnly,
  580.                             writable, writableOwnerOnly,
  581.                             executable, executableOwnerOnly);
  582.                 }
  583.             }
  584.             if(!dir.mkdir()){
  585.                 throw new UtilsException("Creazione directory ["+dir.getAbsolutePath()+"] non riuscita");
  586.             }
  587.             else {
  588.                 setR(dir, readable, readableOwnerOnly);
  589.                 setW(dir, writable, writableOwnerOnly);
  590.                 setX(dir, executable, executableOwnerOnly);
  591.             }
  592.         }
  593.    }
  594.    private static void checkDir(File dir, FileSystemMkdirConfig config) throws UtilsException {
  595.        if(!dir.isDirectory()){
  596.             throw new UtilsException("File ["+dir.getAbsolutePath()+"] is not directory");
  597.         }
  598.         if(config.isCheckCanRead() &&
  599.             !dir.canRead()){
  600.             throw new UtilsException(DIRECTORY_PREFIX_MSG+dir.getAbsolutePath()+"] cannot read");
  601.         }
  602.         if(config.isCheckCanWrite() &&
  603.             !dir.canWrite()){
  604.             throw new UtilsException(DIRECTORY_PREFIX_MSG+dir.getAbsolutePath()+"] cannot write");
  605.         }
  606.         if(config.isCheckCanExecute() &&
  607.             !dir.canExecute()){
  608.             throw new UtilsException(DIRECTORY_PREFIX_MSG+dir.getAbsolutePath()+"] cannot execute");
  609.         }
  610.    }
  611.    private static void setR(File dir, Boolean readable, Boolean readableOwnerOnly) {
  612.        if(readable!=null) {
  613.             if(readableOwnerOnly!=null) {
  614.                 if(!dir.setReadable(readable, readableOwnerOnly)) {
  615.                     // ignore
  616.                 }
  617.             }
  618.             else {
  619.                 if(!dir.setReadable(readable)) {
  620.                     // ignore
  621.                 }
  622.             }
  623.         }
  624.    }
  625.    private static void setW(File dir, Boolean writable, Boolean writableOwnerOnly) {
  626.        if(writable!=null) {
  627.             if(writableOwnerOnly!=null) {
  628.                 if(!dir.setWritable(writable, writableOwnerOnly)) {
  629.                     // ignore
  630.                 }
  631.             }
  632.             else {
  633.                 if(!dir.setWritable(writable)) {
  634.                     // ignore
  635.                 }
  636.             }
  637.         }
  638.    }
  639.    private static void setX(File dir, Boolean executable, Boolean executableOwnerOnly) {
  640.        if(executable!=null) {
  641.             if(executableOwnerOnly!=null) {
  642.                 if(!dir.setExecutable(executable, executableOwnerOnly)) {
  643.                     // ignore
  644.                 }
  645.             }
  646.             else {
  647.                 if(!dir.setExecutable(executable)) {
  648.                     // ignore
  649.                 }
  650.             }
  651.         }
  652.    }
  653.    
  654.    private static final String DIRECTORY_PREFIX_MSG = "Directory [";
  655. }