View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.context.filesystem;
22  
23  import goldengate.common.command.exception.CommandAbstractException;
24  import goldengate.common.command.exception.Reply550Exception;
25  import goldengate.common.command.exception.Reply553Exception;
26  import goldengate.common.file.filesystembased.FilesystemBasedDirImpl;
27  import goldengate.common.file.filesystembased.FilesystemBasedOptsMLSxImpl;
28  import goldengate.common.file.filesystembased.specific.FilesystemBasedCommonsIo;
29  import goldengate.common.file.filesystembased.specific.FilesystemBasedDirJdkAbstract;
30  
31  import java.io.File;
32  import java.io.FileFilter;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import openr66.context.R66Session;
38  import openr66.context.authentication.R66Auth;
39  import openr66.protocol.configuration.Configuration;
40  
41  /**
42   * Directory representation
43   *
44   * @author frederic bregier
45   *
46   */
47  public class R66Dir extends FilesystemBasedDirImpl {
48  
49      /**
50       * @param session
51       */
52      public R66Dir(R66Session session) {
53          super(session, new FilesystemBasedOptsMLSxImpl());
54      }
55  
56      /*
57       * (non-Javadoc)
58       *
59       * @see goldengate.common.file.DirInterface#newFile(java.lang.String,
60       * boolean)
61       */
62      @Override
63      public R66File newFile(String path, boolean append)
64              throws CommandAbstractException {
65          return new R66File((R66Session) getSession(), this, path, append);
66      }
67  
68      /**
69       * Same as setUnique() except that File will be prefixed by id and postfixed by filename
70       *
71       * @param prefix
72       * @param filename
73       * @return the R66File with a unique filename and a temporary extension
74       * @throws CommandAbstractException
75       */
76      public synchronized R66File setUniqueFile(long prefix, String filename)
77              throws CommandAbstractException {
78          checkIdentify();
79          File file = null;
80          String prename = prefix + "_";
81          if (prename.length() < 3) {
82              prename = "xx_"+prename;
83          }
84          String basename = R66File.getBasename(filename);
85          try {
86              file = File.createTempFile(prename, "_" + basename +
87                      Configuration.EXT_R66, getFileFromPath(currentDir));
88          } catch (IOException e) {
89              throw new Reply550Exception("Cannot create unique file from " +
90                      basename);
91          }
92          String currentFile = getRelativePath(file);
93          return newFile(normalizePath(currentFile), false);
94      }
95  
96      /**
97       *
98       * @param file
99       * @return the final unique basename without the temporary extension
100      */
101     public static String getFinalUniqueFilename(R66File file) {
102         String finalpath = file.getBasename();
103         int pos = finalpath.lastIndexOf(Configuration.EXT_R66);
104         if (pos > 0) {
105             finalpath = finalpath.substring(0, pos);
106         }
107         return finalpath;
108     }
109 
110     /**
111      * Finds all files matching a wildcard expression (based on '?', '~' or
112      * '*') but without checking BusinessPath, thus returning absolute path.
113      *
114      * @param pathWithWildcard
115      *            The wildcard expression with a business path.
116      * @return List of String as relative paths matching the wildcard
117      *         expression. Those files are tested as valid from business point
118      *         of view. If Wildcard support is not active, if the path contains
119      *         any wildcards, it will throw an error.
120      * @throws CommandAbstractException
121      */
122     protected List<String> wildcardFilesNoCheck(String pathWithWildcard)
123             throws CommandAbstractException {
124         List<String> resultPaths = new ArrayList<String>();
125         // First check if pathWithWildcard contains wildcards
126         if (!(pathWithWildcard.contains("*") || pathWithWildcard.contains("?") || pathWithWildcard
127                 .contains("~"))) {
128             // No so simply return the list containing this path
129             resultPaths.add(pathWithWildcard);
130             return resultPaths;
131         }
132         // Do we support Wildcard path
133         if (!FilesystemBasedDirJdkAbstract.ueApacheCommonsIo) {
134             throw new Reply553Exception("Wildcards in pathname is not allowed");
135         }
136         File wildcardFile = new File(pathWithWildcard);
137         File rootFile;
138         initWindowsSupport();
139         if (ISUNIX) {
140             rootFile = new File("/");
141         } else {
142             rootFile = getCorrespondingRoot(wildcardFile);
143         }
144         // Split wildcard path into subdirectories.
145         List<String> subdirs = new ArrayList<String>();
146         while (wildcardFile != null) {
147             File parent = wildcardFile.getParentFile();
148             if (parent == null) {
149                 subdirs.add(0, wildcardFile.getPath());
150                 break;
151             }
152             subdirs.add(0, wildcardFile.getName());
153             if (parent.equals(rootFile)) {
154                 // End of wildcard path
155                 subdirs.add(0, parent.getPath());
156                 break;
157             }
158             wildcardFile = parent;
159         }
160         List<File> basedPaths = new ArrayList<File>();
161         // First set root
162         basedPaths.add(new File(subdirs.get(0)));
163         int i = 1;
164         // For each wilcard subdirectory
165         while (i < subdirs.size()) {
166             // Set current filter
167             FileFilter fileFilter = FilesystemBasedCommonsIo
168                     .getWildcardFileFilter(subdirs.get(i));
169             List<File> newBasedPaths = new ArrayList<File>();
170             // Look for matches in all the current search paths
171             for (File dir: basedPaths) {
172                 if (dir.isDirectory()) {
173                     for (File match: dir.listFiles(fileFilter)) {
174                         newBasedPaths.add(match);
175                     }
176                 }
177             }
178             // base Search Path changes now
179             basedPaths = newBasedPaths;
180             i ++;
181         }
182         // Valid each file first
183         for (File file: basedPaths) {
184             resultPaths.add(file.getAbsolutePath());
185         }
186         return resultPaths;
187     }
188 
189     /**
190      * Create a new file according to the path without checking BusinessPath,
191      * so as external File.
192      * @param path
193      * @return the File created
194      * @throws CommandAbstractException
195      */
196     public R66File setFileNoCheck(String path)
197         throws CommandAbstractException {
198         checkIdentify();
199         String newpath = consolidatePath(path);
200         List<String> paths = wildcardFilesNoCheck(newpath);
201         if (paths.size() != 1) {
202             throw new Reply550Exception("FileInterface not found: " +
203                     paths.size() + " founds");
204         }
205         String extDir = paths.get(0);
206         return new R66File((R66Session) getSession(), this, extDir);
207     }
208 
209     /**
210      * This method returns the Full path for the current directory
211      *
212      * @return the full path associated with the current Dir
213      */
214     public String getFullPath() {
215         if (session.getAuth() == null) {
216             return currentDir;
217         }
218         return ((R66Auth) session.getAuth()).getAbsolutePath(currentDir);
219     }
220 
221     @Override
222     public String toString() {
223         return "Dir: " + currentDir;
224     }
225 }