1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.impl;
18
19 import java.io.File;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22 import java.security.PrivilegedActionException;
23 import java.security.PrivilegedExceptionAction;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.vfs2.FileObject;
27 import org.apache.commons.vfs2.FileSelector;
28 import org.apache.commons.vfs2.FileSystemException;
29 import org.apache.commons.vfs2.provider.FileReplicator;
30 import org.apache.commons.vfs2.provider.VfsComponent;
31 import org.apache.commons.vfs2.provider.VfsComponentContext;
32
33
34
35
36 public class PrivilegedFileReplicator implements FileReplicator, VfsComponent {
37
38 private final FileReplicator replicator;
39 private final VfsComponent replicatorComponent;
40
41 public PrivilegedFileReplicator(final FileReplicator replicator) {
42 this.replicator = replicator;
43 if (replicator instanceof VfsComponent) {
44 replicatorComponent = (VfsComponent) replicator;
45 } else {
46 replicatorComponent = null;
47 }
48 }
49
50
51
52
53
54
55 @Override
56 public void setLogger(final Log logger) {
57 if (replicatorComponent != null) {
58 replicatorComponent.setLogger(logger);
59 }
60 }
61
62
63
64
65
66
67 @Override
68 public void setContext(final VfsComponentContext context) {
69 if (replicatorComponent != null) {
70 replicatorComponent.setContext(context);
71 }
72 }
73
74
75
76
77
78
79 @Override
80 public void init() throws FileSystemException {
81 if (replicatorComponent != null) {
82 try {
83 AccessController.doPrivileged(new InitAction());
84 } catch (final PrivilegedActionException e) {
85 throw new FileSystemException("vfs.impl/init-replicator.error", e);
86 }
87 }
88 }
89
90
91
92
93 @Override
94 public void close() {
95 if (replicatorComponent != null) {
96 AccessController.doPrivileged(new CloseAction());
97 }
98 }
99
100
101
102
103
104
105
106
107
108 @Override
109 public File replicateFile(final FileObject srcFile, final FileSelector selector) throws FileSystemException {
110 try {
111 final ReplicateAction action = new ReplicateAction(srcFile, selector);
112 return AccessController.doPrivileged(action);
113 } catch (final PrivilegedActionException e) {
114 throw new FileSystemException("vfs.impl/replicate-file.error", e, srcFile.getName());
115 }
116 }
117
118
119
120
121 private class InitAction implements PrivilegedExceptionAction<Object> {
122
123
124
125 @Override
126 public Object run() throws Exception {
127 replicatorComponent.init();
128 return null;
129 }
130 }
131
132
133
134
135 private class ReplicateAction implements PrivilegedExceptionAction<File> {
136 private final FileObject srcFile;
137 private final FileSelector selector;
138
139 public ReplicateAction(final FileObject srcFile, final FileSelector selector) {
140 this.srcFile = srcFile;
141 this.selector = selector;
142 }
143
144
145
146
147
148
149 @Override
150 public File run() throws Exception {
151
152
153 return replicator.replicateFile(srcFile, selector);
154 }
155 }
156
157
158
159
160 private class CloseAction implements PrivilegedAction<Object> {
161
162
163
164 @Override
165 public Object run() {
166 replicatorComponent.close();
167 return null;
168 }
169 }
170 }