View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A file replicator that wraps another file replicator, performing the replication as a privileged action.
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       * Sets the Logger to use for the component.
52       *
53       * @param logger The logger.
54       */
55      @Override
56      public void setLogger(final Log logger) {
57          if (replicatorComponent != null) {
58              replicatorComponent.setLogger(logger);
59          }
60      }
61  
62      /**
63       * Sets the context for the replicator.
64       *
65       * @param context The component context.
66       */
67      @Override
68      public void setContext(final VfsComponentContext context) {
69          if (replicatorComponent != null) {
70              replicatorComponent.setContext(context);
71          }
72      }
73  
74      /**
75       * Initializes the component.
76       *
77       * @throws FileSystemException if an error occurs.
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       * Closes the replicator.
92       */
93      @Override
94      public void close() {
95          if (replicatorComponent != null) {
96              AccessController.doPrivileged(new CloseAction());
97          }
98      }
99  
100     /**
101      * Creates a local copy of the file, and all its descendants.
102      *
103      * @param srcFile The source FileObject.
104      * @param selector The file selector.
105      * @return The replicated file.
106      * @throws FileSystemException if an error occurs.
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      * An action that initialises the wrapped replicator.
120      */
121     private class InitAction implements PrivilegedExceptionAction<Object> {
122         /**
123          * Performs the action.
124          */
125         @Override
126         public Object run() throws Exception {
127             replicatorComponent.init();
128             return null;
129         }
130     }
131 
132     /**
133      * An action that replicates a file using the wrapped replicator.
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          * Performs the action.
146          *
147          * @throws Exception if an error occurs.
148          */
149         @Override
150         public File run() throws Exception {
151             // TODO - Do not pass the selector through. It is untrusted
152             // TODO - Need to determine which files can be read
153             return replicator.replicateFile(srcFile, selector);
154         }
155     }
156 
157     /**
158      * An action that closes the wrapped replicator.
159      */
160     private class CloseAction implements PrivilegedAction<Object> {
161         /**
162          * Performs the action.
163          */
164         @Override
165         public Object run() {
166             replicatorComponent.close();
167             return null;
168         }
169     }
170 }