1 16 package org.apache.commons.vfs.impl; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.commons.vfs.FileSelector; 21 import org.apache.commons.vfs.FileSystemException; 22 import org.apache.commons.vfs.provider.FileReplicator; 23 import org.apache.commons.vfs.provider.VfsComponent; 24 import org.apache.commons.vfs.provider.VfsComponentContext; 25 26 import java.io.File ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedAction ; 29 import java.security.PrivilegedActionException ; 30 import java.security.PrivilegedExceptionAction ; 31 32 38 public class PrivilegedFileReplicator 39 implements FileReplicator, VfsComponent 40 { 41 private final FileReplicator replicator; 42 private final VfsComponent replicatorComponent; 43 44 public PrivilegedFileReplicator(FileReplicator replicator) 45 { 46 this.replicator = replicator; 47 if (replicator instanceof VfsComponent) 48 { 49 replicatorComponent = (VfsComponent) replicator; 50 } 51 else 52 { 53 replicatorComponent = null; 54 } 55 } 56 57 60 public void setLogger(final Log logger) 61 { 62 if (replicatorComponent != null) 63 { 64 replicatorComponent.setLogger(logger); 65 } 66 } 67 68 71 public void setContext(final VfsComponentContext context) 72 { 73 if (replicatorComponent != null) 74 { 75 replicatorComponent.setContext(context); 76 } 77 } 78 79 82 public void init() throws FileSystemException 83 { 84 if (replicatorComponent != null) 85 { 86 try 87 { 88 AccessController.doPrivileged(new InitAction()); 89 } 90 catch (final PrivilegedActionException e) 91 { 92 throw new FileSystemException("vfs.impl/init-replicator.error", null, e); 93 } 94 } 95 } 96 97 100 public void close() 101 { 102 if (replicatorComponent != null) 103 { 104 AccessController.doPrivileged(new CloseAction()); 105 } 106 } 107 108 111 public File replicateFile(FileObject srcFile, FileSelector selector) 112 throws FileSystemException 113 { 114 try 115 { 116 final ReplicateAction action = new ReplicateAction(srcFile, selector); 117 return (File ) AccessController.doPrivileged(action); 118 } 119 catch (final PrivilegedActionException e) 120 { 121 throw new FileSystemException("vfs.impl/replicate-file.error", new Object []{srcFile.getName()}, e); 122 } 123 } 124 125 128 private class InitAction implements PrivilegedExceptionAction 129 { 130 133 public Object run() throws Exception 134 { 135 replicatorComponent.init(); 136 return null; 137 } 138 } 139 140 143 private class ReplicateAction implements PrivilegedExceptionAction 144 { 145 private final FileObject srcFile; 146 private final FileSelector selector; 147 148 public ReplicateAction(final FileObject srcFile, 149 final FileSelector selector) 150 { 151 this.srcFile = srcFile; 152 this.selector = selector; 153 } 154 155 158 public Object run() throws Exception 159 { 160 return replicator.replicateFile(srcFile, selector); 163 } 164 } 165 166 169 private class CloseAction implements PrivilegedAction 170 { 171 174 public Object run() 175 { 176 replicatorComponent.close(); 177 return null; 178 } 179 } 180 } 181 | Popular Tags |