View Javadoc

1   package fr.in2p3.jsaga.impl.namespace;
2   
3   import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
4   import fr.in2p3.jsaga.adaptor.data.ParentDoesNotExist;
5   import fr.in2p3.jsaga.adaptor.data.read.DataReaderAdaptor;
6   import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
7   import fr.in2p3.jsaga.adaptor.data.write.DataWriterAdaptor;
8   import fr.in2p3.jsaga.helpers.SAGAPattern;
9   import fr.in2p3.jsaga.impl.url.*;
10  import fr.in2p3.jsaga.sync.namespace.SyncNSDirectory;
11  import fr.in2p3.jsaga.sync.namespace.SyncNSEntry;
12  import org.ogf.saga.error.*;
13  import org.ogf.saga.namespace.Flags;
14  import org.ogf.saga.namespace.NSFactory;
15  import org.ogf.saga.session.Session;
16  import org.ogf.saga.url.URL;
17  import org.ogf.saga.url.URLFactory;
18  
19  import java.util.*;
20  import java.util.regex.Pattern;
21  import org.ogf.saga.namespace.NSEntry;
22  
23  /* ***************************************************
24   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
25   * ***             http://cc.in2p3.fr/             ***
26   * ***************************************************
27   * File:   AbstractSyncNSDirectoryImpl
28   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
29   * Date:   29 mai 2009
30   * ***************************************************
31   * Description:                                      */
32  /**
33   *
34   */
35  public abstract class AbstractSyncNSDirectoryImpl extends AbstractNSEntryDirImpl implements SyncNSDirectory {
36  
37      /** constructor for factory */
38      protected AbstractSyncNSDirectoryImpl(Session session, URL url, DataAdaptor adaptor, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
39          super(session, URLHelper.toDirectoryURL(url), adaptor, flags);
40          boolean initOK = false;
41          try {
42              this.init(flags);
43              initOK = true;
44          } finally {
45              if (!initOK) {
46                  this.close();
47              }
48          }
49      }
50  
51      /** constructor for NSDirectory.open() */
52      protected AbstractSyncNSDirectoryImpl(AbstractNSDirectoryImpl dir, URL relativeUrl, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
53          super(dir, URLHelper.toDirectoryURL(relativeUrl), flags);
54          boolean initOK = false;
55  	try {
56             this.init(flags);
57             initOK = true;
58          } finally {
59              if (!initOK) {
60                  this.close();
61              }
62          }
63      }
64  
65      /** constructor for NSEntry.openAbsolute() */
66      protected AbstractSyncNSDirectoryImpl(AbstractNSEntryImpl entry, String absolutePath, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
67          super(entry, URLHelper.toDirectoryPath(absolutePath), flags);
68          boolean initOK = false;
69  	try {
70             this.init(flags);
71             initOK = true;
72          } finally {
73              if (!initOK) {
74                  this.close();
75              }
76          }
77      }
78  
79      private void init(int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
80          if (Flags.CREATEPARENTS.isSet(flags)) {
81              flags = Flags.CREATE.or(flags);
82          }
83          new FlagsHelper(flags).allowed(JSAGAFlags.BYPASSEXIST, Flags.ALLNAMESPACEFLAGS);
84          if (Flags.CREATE.isSet(flags)) {
85              if (m_adaptor instanceof DataWriterAdaptor) {
86                  if ("/".equals(m_url.getPath())) {
87                      if (Flags.EXCL.isSet(flags)) {
88                          throw new AlreadyExistsException("Not allowed to create root directory: " + m_url.toString(), this);
89                      } else {
90                          return;
91                      }
92                  }
93                  URL parent = super._getParentDirURL();
94                  String directoryName = super._getEntryName();
95                  try {
96                      // try to make current directory
97                      ((DataWriterAdaptor) m_adaptor).makeDir(parent.getPath(), directoryName, m_url.getQuery());
98                  } catch (ParentDoesNotExist e) {
99                      // make parent directories, then retry
100                     if (Flags.CREATEPARENTS.isSet(flags)) {
101                         this._makeParentDirs();
102                         try {
103                             ((DataWriterAdaptor) m_adaptor).makeDir(parent.getPath(), directoryName, m_url.getQuery());
104                         } catch (ParentDoesNotExist e2) {
105                             throw new DoesNotExistException("Failed to create parent directory: " + parent, e.getCause());
106                         }
107                     } else {
108                         throw new DoesNotExistException("Parent directory does not exist: " + parent, e.getCause());
109                     }
110                 } catch (AlreadyExistsException e) {
111                     if (Flags.EXCL.isSet(flags)) {
112                         throw new AlreadyExistsException("Entry already exists: " + m_url, e.getCause());
113                     }
114                 }
115             } else {
116                 throw new NotImplementedException("Not supported for this protocol: " + m_url.getScheme());
117             }
118         } else if (Flags.CREATEPARENTS.isSet(flags)) {
119             this._makeParentDirs();
120         } else if (!JSAGAFlags.BYPASSEXIST.isSet(flags) && !((AbstractURLImpl) m_url).hasCache() && m_adaptor instanceof DataReaderAdaptor) {
121             boolean exists = ((DataReaderAdaptor) m_adaptor).exists(m_url.getPath(), m_url.getQuery());
122             if (!exists) {
123                 throw new DoesNotExistException("Directory does not exist: " + m_url);
124             }
125         }
126     }
127 
128     ////////////////////////////////////////// interface SyncNSDirectory //////////////////////////////////////////
129     public void changeDirSync(URL dir) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
130         if (dir.getScheme() != null || dir.getUserInfo() != null || dir.getHost() != null) {
131             throw new IncorrectURLException("Was expecting a absolute/relative path instead of: " + dir.toString());
132         }
133         m_url = _resolveRelativeUrl(m_url, dir);
134     }
135 
136     public Iterator<URL> iterator() {
137         try {
138             return this.listSync().iterator();
139         } catch (SagaException e) {
140             throw new RuntimeException(e);
141         }
142     }
143 
144     public List<URL> listSync(String pattern, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, IncorrectURLException {
145         return this._list(pattern, flags);
146     }
147 
148     public List<URL> listSync(String pattern) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, IncorrectURLException {
149         return this.listSync(pattern, Flags.NONE.getValue());
150     }
151 
152     public List<URL> listSync(int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, IncorrectURLException {
153         return this._list(null, flags);
154     }
155 
156     public List<URL> listSync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, IncorrectURLException {
157         return this.listSync(Flags.NONE.getValue());
158     }
159 
160     private List<URL> _list(String pattern, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, IncorrectURLException {
161         new FlagsHelper(flags).allowed(Flags.DEREFERENCE);
162         if (Flags.DEREFERENCE.isSet(flags)) {
163             AbstractSyncNSDirectoryImpl dir = this._dereferenceDir();
164             try {
165                 return dir._list(pattern, flags - Flags.DEREFERENCE.getValue());
166             } finally {
167                 dir.close();
168             }
169         }
170 
171         // get list
172         FileAttributes[] childs;
173         try {
174             if (m_adaptor instanceof DataReaderAdaptor) {
175                 childs = ((DataReaderAdaptor) m_adaptor).listAttributes(
176                         m_url.getPath(),
177                         m_url.getQuery());
178             } else {
179                 throw new NotImplementedException("Not supported for this protocol: " + m_url.getScheme(), this);
180             }
181         } catch (DoesNotExistException e) {
182             throw new IncorrectStateException("Directory does not exist: " + m_url, e);
183         }
184 
185         // filter
186         Pattern p = SAGAPattern.toRegexp(pattern);
187         List<URL> matchingNames = new ArrayList<URL>();
188         for (int i = 0; i < childs.length; i++) {
189             if (p == null || p.matcher(childs[i].getRelativePath()).matches()) {
190                 URL childUrl = URLFactoryImpl.createURLWithCache(childs[i]);
191                 matchingNames.add(childUrl);
192             }
193         }
194         return matchingNames;
195     }
196 
197     public List<URL> findSync(String pattern, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
198         new FlagsHelper(flags).allowed(Flags.DEREFERENCE, Flags.RECURSIVE);
199         if (Flags.DEREFERENCE.isSet(flags)) {
200             try {
201                 AbstractSyncNSDirectoryImpl dir = this._dereferenceDir();
202                 try {
203                     return dir.findSync(pattern, flags - Flags.DEREFERENCE.getValue());
204                 } finally {
205                     dir.close();
206                 }
207             } catch (IncorrectURLException e) {
208                 throw new NoSuccessException(e);
209             }
210         }
211 
212         // search
213         List<URL> matchingPath = new ArrayList<URL>();
214         if (m_adaptor instanceof DataReaderAdaptor) {
215             Pattern p = SAGAPattern.toRegexp(pattern);
216             this._doFind(p, flags, matchingPath, CURRENT_DIR_RELATIVE_PATH);
217         } else {
218             throw new NotImplementedException("Not supported for this protocol: " + m_url.getScheme(), this);
219         }
220         return matchingPath;
221     }
222 
223     public List<URL> findSync(String pattern) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
224         return this.findSync(pattern, Flags.RECURSIVE.getValue());
225     }
226 
227     private void _doFind(Pattern p, int flags, List<URL> matchingPath, URL currentRelativePath) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
228         // for each child
229         FileAttributes[] childs = this._listAttributes(m_url.getPath());
230         for (int i = 0; i < childs.length; i++) {
231             // set child relative path
232             URL childRelativePath = URLHelper.createURL(currentRelativePath, childs[i].getRelativePath());
233             // add child relative path to matching list
234             if (p == null || p.matcher(childs[i].getRelativePath()).matches()) {
235                 matchingPath.add(childRelativePath);
236             }
237             // may recurse
238             if (Flags.RECURSIVE.isSet(flags) && childs[i].getType() == FileAttributes.TYPE_DIRECTORY) {
239                 AbstractSyncNSDirectoryImpl childDir;
240                 try {
241                     URL childDirName = URLFactory.createURL(JSAGA_FACTORY, childs[i].getRelativePath());
242                     childDir = (AbstractSyncNSDirectoryImpl) this._openNSDir(childDirName);
243                     try {
244                         childDir._doFind(p, flags, matchingPath, childRelativePath);
245                     } finally {
246                         childDir.close();
247                     }
248                 } catch (IncorrectURLException e) {
249                     throw new NoSuccessException(e);
250                 }
251             }
252         }
253     }
254 
255     public boolean existsSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
256         try {
257             SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
258             ((NSEntry) entry).close();
259 	    //System.out.println("Entry exists " + name);
260             return true;
261         } catch (DoesNotExistException e) {
262             return false;
263         }
264     }
265 
266     public long getMTimeSync(URL name) throws NotImplementedException, IncorrectURLException, DoesNotExistException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException {
267         SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
268         try {
269             return entry.getMTimeSync();
270         } finally {
271             ((NSEntry) entry).close();
272         }
273     }
274 
275     public boolean isDirSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, DoesNotExistException {
276         SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
277         try {
278             return entry.isDirSync();
279         } finally {
280             ((NSEntry) entry).close();
281         }
282     }
283 
284     public boolean isEntrySync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, DoesNotExistException {
285         SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
286         try {
287             return entry.isEntrySync();
288         } finally {
289             ((NSEntry) entry).close();
290         }
291     }
292 
293     public boolean isLinkSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, DoesNotExistException {
294         SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
295         try {
296             return entry.isLinkSync();
297         } finally {
298             ((NSEntry) entry).close();
299         }
300     }
301 
302     public URL readLinkSync(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, TimeoutException, NoSuccessException, DoesNotExistException {
303         SyncNSEntry entry = this._openNSEntryWithDoesNotExist(name);
304         try {
305             return entry.readLinkSync();
306         } finally {
307             ((NSEntry) entry).close();
308         }
309     }
310     private String[] m_entriesCache = null;
311 
312     public int getNumEntriesSync() throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
313         if (m_adaptor instanceof DataReaderAdaptor) {
314             m_entriesCache = this._listNames(m_url.getPath());
315             return m_entriesCache.length;
316         } else {
317             throw new NotImplementedException("Not supported for this protocol: " + m_url.getScheme(), this);
318         }
319     }
320 
321     public URL getEntrySync(int entry) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
322         if (m_adaptor instanceof DataReaderAdaptor) {
323             if (m_entriesCache == null) {
324                 m_entriesCache = this._listNames(m_url.getPath());
325             }
326             if (entry < m_entriesCache.length) {
327                 try {
328                     return URLFactory.createURL(JSAGA_FACTORY, m_entriesCache[entry]);
329                 } catch (BadParameterException e) {
330                     throw new NoSuccessException(e);
331                 }
332             } else {
333                 throw new DoesNotExistException("Invalid index: " + entry, this);
334             }
335         } else {
336             throw new NotImplementedException("Not supported for this protocol: " + m_url.getScheme(), this);
337         }
338     }
339 
340     public void copySync(URL source, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
341         SyncNSEntry entry = this._openNSEntry(source);
342         try {
343             entry.copySync(target, flags);
344         } finally {
345             ((NSEntry) entry).close();
346         }
347     }
348 
349     public void copySync(URL source, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
350         this.copySync(source, target, Flags.NONE.getValue());
351     }
352 
353     public void copySync(String sourcePattern, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
354         if (SAGAPattern.hasWildcard(sourcePattern)) {
355             for (URL source : this.listSync(sourcePattern)) {
356                 SyncNSEntry entry = this._openNSEntry(source);
357                 try {
358                     entry.copySync(target, flags);
359                 } finally {
360                     ((NSEntry) entry).close();
361                 }
362             }
363         } else {
364             URL source = URLFactory.createURL(JSAGA_FACTORY, sourcePattern);
365             SyncNSEntry entry = this._openNSEntry(source);
366             try {
367                 entry.copySync(target, flags);
368             } finally {
369                 ((NSEntry) entry).close();
370             }
371         }
372     }
373 
374     public void copySync(String sourcePattern, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
375         this.copySync(sourcePattern, target, Flags.NONE.getValue());
376     }
377 
378     public void linkSync(URL source, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
379         SyncNSEntry entry = this._openNSEntry(source);
380         try {
381             entry.linkSync(target, flags);
382         } finally {
383             ((NSEntry) entry).close();
384         }
385     }
386 
387     public void linkSync(URL source, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
388         this.linkSync(source, target, Flags.NONE.getValue());
389     }
390 
391     public void linkSync(String sourcePattern, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
392         if (SAGAPattern.hasWildcard(sourcePattern)) {
393             for (URL source : this.listSync(sourcePattern)) {
394                 SyncNSEntry entry = this._openNSEntry(source);
395                 ((NSEntry) entry).close();
396             }
397         } else {
398             URL source = URLFactory.createURL(JSAGA_FACTORY, sourcePattern);
399             SyncNSEntry entry = this._openNSEntry(source);
400             try {
401                 entry.linkSync(target, flags);
402             } finally {
403                 ((NSEntry) entry).close();
404             }
405         }
406     }
407 
408     public void linkSync(String sourcePattern, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
409         this.linkSync(sourcePattern, target, Flags.NONE.getValue());
410     }
411 
412     public void moveSync(URL source, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
413         SyncNSEntry entry = this._openNSEntry(source);
414         try {
415             entry.moveSync(target, flags);
416         } finally {
417             ((NSEntry) entry).close();
418         }
419     }
420 
421     public void moveSync(URL source, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
422         this.moveSync(source, target, Flags.NONE.getValue());
423     }
424 
425     public void moveSync(String sourcePattern, URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
426         if (SAGAPattern.hasWildcard(sourcePattern)) {
427             for (URL source : this.listSync(sourcePattern)) {
428                 SyncNSEntry entry = this._openNSEntry(source);
429                 ((NSEntry) entry).close();
430             }
431         } else {
432             URL source = URLFactory.createURL(JSAGA_FACTORY, sourcePattern);
433             SyncNSEntry entry = this._openNSEntry(source);
434             try {
435                 entry.moveSync(target, flags);
436             } finally {
437                 ((NSEntry) entry).close();
438             }
439         }
440     }
441 
442     public void moveSync(String sourcePattern, URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
443         this.moveSync(sourcePattern, target, Flags.NONE.getValue());
444     }
445 
446     public void removeSync(URL target, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
447         SyncNSEntry entry = this._openNSEntry(target);
448         try {
449             entry.removeSync(flags);
450         } finally {
451             ((NSEntry) entry).close();
452         }
453     }
454 
455     public void removeSync(URL target) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
456         this.removeSync(target, Flags.NONE.getValue());
457     }
458 
459     public void removeSync(String targetPattern, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
460         if (SAGAPattern.hasWildcard(targetPattern)) {
461             for (URL target : this.listSync(targetPattern)) {
462                 SyncNSEntry entry = this._openNSEntry(target);
463                 try {
464                     entry.removeSync(flags);
465                 } finally {
466                     ((NSEntry) entry).close();
467                 }
468             }
469         } else {
470             URL target = URLFactory.createURL(JSAGA_FACTORY, targetPattern);
471             SyncNSEntry entry = this._openNSEntry(target);
472             try {
473                 entry.removeSync(flags);
474             } finally {
475                 ((NSEntry) entry).close();
476             }
477         }
478     }
479 
480     public void removeSync(String targetPattern) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectURLException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
481         this.removeSync(targetPattern, Flags.NONE.getValue());
482     }
483 
484     public void makeDirSync(URL target, int flags) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
485         int makeDirFlags = Flags.CREATE.or(flags);
486         NSFactory.createNSDirectory(JSAGA_FACTORY, m_session, target, makeDirFlags).close();
487     }
488 
489     public void makeDirSync(URL target) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException, TimeoutException, NoSuccessException {
490         this.makeDirSync(target, Flags.NONE.getValue());
491     }
492 
493     public void permissionsAllowSync(URL target, String id, int permissions, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, TimeoutException, NoSuccessException {
494         SyncNSEntry entry = this._openNSEntry(target, flags);
495         try {
496             entry.permissionsAllow(id, permissions);
497         } finally {
498             ((NSEntry) entry).close();
499         }
500     }
501 
502     public void permissionsAllowSync(URL target, String id, int permissions) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, TimeoutException, NoSuccessException {
503         this.permissionsAllowSync(target, id, permissions, Flags.NONE.getValue());
504     }
505 
506     public void permissionsAllowSync(String targetPattern, String id, int permissions, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, TimeoutException, NoSuccessException {
507         if (SAGAPattern.hasWildcard(targetPattern)) {
508             try {
509                 for (URL target : this.listSync(targetPattern)) {
510                     SyncNSEntry entry = this._openNSEntry(target, flags);
511                     try {
512                         entry.permissionsAllow(id, permissions);
513                     } finally {
514                         ((NSEntry) entry).close();
515                     }
516                 }
517             } catch (IncorrectURLException e) {
518                 throw new NoSuccessException(e);
519             }
520         } else {
521             URL target = URLFactory.createURL(JSAGA_FACTORY, targetPattern);
522             SyncNSEntry entry = this._openNSEntry(target, flags);
523             try {
524                 entry.permissionsAllow(id, permissions);
525             } finally {
526                 ((NSEntry) entry).close();
527             }
528         }
529     }
530 
531     public void permissionsAllowSync(String targetPattern, String id, int permissions) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, IncorrectStateException, BadParameterException, TimeoutException, NoSuccessException {
532         this.permissionsAllowSync(targetPattern, id, permissions, Flags.NONE.getValue());
533     }
534 
535     public void permissionsDenySync(URL target, String id, int permissions, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
536         SyncNSEntry entry = this._openNSEntry(target, flags);
537         try {
538             entry.permissionsDeny(id, permissions);
539         } finally {
540             ((NSEntry) entry).close();
541         }
542     }
543 
544     public void permissionsDenySync(URL target, String id, int permissions) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
545         this.permissionsDenySync(target, id, permissions, Flags.NONE.getValue());
546     }
547 
548     public void permissionsDenySync(String targetPattern, String id, int permissions, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
549         if (SAGAPattern.hasWildcard(targetPattern)) {
550             try {
551                 for (URL target : this.listSync(targetPattern)) {
552                     SyncNSEntry entry = this._openNSEntry(target, flags);
553                     try {
554                         entry.permissionsDeny(id, permissions);
555                     } finally {
556                         ((NSEntry) entry).close();
557                     }
558                 }
559             } catch (IncorrectURLException e) {
560                 throw new NoSuccessException(e);
561             } catch (IncorrectStateException e) {
562                 throw new NoSuccessException(e);
563             }
564         } else {
565             URL target = URLFactory.createURL(JSAGA_FACTORY, targetPattern);
566             SyncNSEntry entry = this._openNSEntry(target, flags);
567             try {
568                 entry.permissionsDeny(id, permissions);
569             } finally {
570                 ((NSEntry) entry).close();
571             }
572 
573         }
574     }
575 
576     public void permissionsDenySync(String targetPattern, String id, int permissions) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
577         this.permissionsDenySync(targetPattern, id, permissions, Flags.NONE.getValue());
578     }
579     ///////////////////////////////////////// protected methods /////////////////////////////////////////
580     protected static URL CURRENT_DIR_RELATIVE_PATH;
581 
582     static {
583         try {
584             CURRENT_DIR_RELATIVE_PATH = URLFactory.createURL(JSAGA_FACTORY, "./");
585         } catch (Exception e) {
586             e.printStackTrace();
587         }
588     }
589 
590     private String[] _listNames(String absolutePath) throws NotImplementedException, PermissionDeniedException, IncorrectStateException, TimeoutException, NoSuccessException {
591         FileAttributes[] files = this._listAttributes(absolutePath);
592         String[] filenames = new String[files.length];
593         for (int i = 0; i < files.length; i++) {
594             filenames[i] = files[i].getRelativePath();
595         }
596         return filenames;
597     }
598 
599     private SyncNSEntry _openNSEntry(URL name, int flags) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, TimeoutException, NoSuccessException {
600         try {
601             return (SyncNSEntry) this.open(name, flags);
602         } catch (IncorrectURLException e) {
603             throw new NoSuccessException(e);
604         } catch (IncorrectStateException e) {
605             throw new NoSuccessException(e);
606         } catch (DoesNotExistException e) {
607             throw new NoSuccessException(e);
608         } catch (AlreadyExistsException e) {
609             throw new NoSuccessException(e);
610         }
611     }
612 
613     private SyncNSEntry _openNSEntryWithDoesNotExist(URL name) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, IncorrectStateException, DoesNotExistException, TimeoutException, NoSuccessException {
614         try {
615             return (SyncNSEntry) this.open(name, Flags.NONE.getValue());
616         } catch (AlreadyExistsException e) {
617             throw new IncorrectStateException(e);
618         }
619     }
620 }