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.vfs.impl;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.vfs.FileObject;
21  import org.apache.commons.vfs.FileSelector;
22  import org.apache.commons.vfs.FileSystemException;
23  import org.apache.commons.vfs.provider.FileReplicator;
24  import org.apache.commons.vfs.provider.VfsComponent;
25  import org.apache.commons.vfs.provider.VfsComponentContext;
26  
27  import java.io.File;
28  import java.security.AccessController;
29  import java.security.PrivilegedAction;
30  import java.security.PrivilegedActionException;
31  import java.security.PrivilegedExceptionAction;
32  
33  /***
34   * A file replicator that wraps another file replicator, performing
35   * the replication as a privileged action.
36   *
37   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
38   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
39   */
40  public class PrivilegedFileReplicator
41      implements FileReplicator, VfsComponent
42  {
43      private final FileReplicator replicator;
44      private final VfsComponent replicatorComponent;
45  
46      public PrivilegedFileReplicator(FileReplicator replicator)
47      {
48          this.replicator = replicator;
49          if (replicator instanceof VfsComponent)
50          {
51              replicatorComponent = (VfsComponent) replicator;
52          }
53          else
54          {
55              replicatorComponent = null;
56          }
57      }
58  
59      /***
60       * Sets the Logger to use for the component.
61       */
62      public void setLogger(final Log logger)
63      {
64          if (replicatorComponent != null)
65          {
66              replicatorComponent.setLogger(logger);
67          }
68      }
69  
70      /***
71       * Sets the context for the replicator.
72       */
73      public void setContext(final VfsComponentContext context)
74      {
75          if (replicatorComponent != null)
76          {
77              replicatorComponent.setContext(context);
78          }
79      }
80  
81      /***
82       * Initialises the component.
83       */
84      public void init() throws FileSystemException
85      {
86          if (replicatorComponent != null)
87          {
88              try
89              {
90                  AccessController.doPrivileged(new InitAction());
91              }
92              catch (final PrivilegedActionException e)
93              {
94                  throw new FileSystemException("vfs.impl/init-replicator.error", null, e);
95              }
96          }
97      }
98  
99      /***
100      * Closes the replicator.
101      */
102     public void close()
103     {
104         if (replicatorComponent != null)
105         {
106             AccessController.doPrivileged(new CloseAction());
107         }
108     }
109 
110     /***
111      * Creates a local copy of the file, and all its descendents.
112      */
113     public File replicateFile(FileObject srcFile, FileSelector selector)
114         throws FileSystemException
115     {
116         try
117         {
118             final ReplicateAction action = new ReplicateAction(srcFile, selector);
119             return (File) AccessController.doPrivileged(action);
120         }
121         catch (final PrivilegedActionException e)
122         {
123             throw new FileSystemException("vfs.impl/replicate-file.error", new Object[]{srcFile.getName()}, e);
124         }
125     }
126 
127     /***
128      * An action that initialises the wrapped replicator.
129      */
130     private class InitAction implements PrivilegedExceptionAction
131     {
132         /***
133          * Performs the action.
134          */
135         public Object run() throws Exception
136         {
137             replicatorComponent.init();
138             return null;
139         }
140     }
141 
142     /***
143      * An action that replicates a file using the wrapped replicator.
144      */
145     private class ReplicateAction implements PrivilegedExceptionAction
146     {
147         private final FileObject srcFile;
148         private final FileSelector selector;
149 
150         public ReplicateAction(final FileObject srcFile,
151                                final FileSelector selector)
152         {
153             this.srcFile = srcFile;
154             this.selector = selector;
155         }
156 
157         /***
158          * Performs the action.
159          */
160         public Object run() throws Exception
161         {
162             // TODO - Do not pass the selector through.  It is untrusted
163             // TODO - Need to determine which files can be read
164             return replicator.replicateFile(srcFile, selector);
165         }
166     }
167 
168     /***
169      * An action that closes the wrapped replicator.
170      */
171     private class CloseAction implements PrivilegedAction
172     {
173         /***
174          * Performs the action.
175          */
176         public Object run()
177         {
178             replicatorComponent.close();
179             return null;
180         }
181     }
182 }