KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > impl > PrivilegedFileReplicator


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import java.security.PrivilegedActionException JavaDoc;
30 import java.security.PrivilegedExceptionAction JavaDoc;
31
32 /**
33  * A file replicator that wraps another file replicator, performing
34  * the replication as a privileged action.
35  *
36  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
37  */

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     /**
58      * Sets the Logger to use for the component.
59      */

60     public void setLogger(final Log logger)
61     {
62         if (replicatorComponent != null)
63         {
64             replicatorComponent.setLogger(logger);
65         }
66     }
67
68     /**
69      * Sets the context for the replicator.
70      */

71     public void setContext(final VfsComponentContext context)
72     {
73         if (replicatorComponent != null)
74         {
75             replicatorComponent.setContext(context);
76         }
77     }
78
79     /**
80      * Initialises the component.
81      */

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 JavaDoc e)
91             {
92                 throw new FileSystemException("vfs.impl/init-replicator.error", null, e);
93             }
94         }
95     }
96
97     /**
98      * Closes the replicator.
99      */

100     public void close()
101     {
102         if (replicatorComponent != null)
103         {
104             AccessController.doPrivileged(new CloseAction());
105         }
106     }
107
108     /**
109      * Creates a local copy of the file, and all its descendents.
110      */

111     public File JavaDoc replicateFile(FileObject srcFile, FileSelector selector)
112         throws FileSystemException
113     {
114         try
115         {
116             final ReplicateAction action = new ReplicateAction(srcFile, selector);
117             return (File JavaDoc) AccessController.doPrivileged(action);
118         }
119         catch (final PrivilegedActionException JavaDoc e)
120         {
121             throw new FileSystemException("vfs.impl/replicate-file.error", new Object JavaDoc[]{srcFile.getName()}, e);
122         }
123     }
124
125     /**
126      * An action that initialises the wrapped replicator.
127      */

128     private class InitAction implements PrivilegedExceptionAction JavaDoc
129     {
130         /**
131          * Performs the action.
132          */

133         public Object JavaDoc run() throws Exception JavaDoc
134         {
135             replicatorComponent.init();
136             return null;
137         }
138     }
139
140     /**
141      * An action that replicates a file using the wrapped replicator.
142      */

143     private class ReplicateAction implements PrivilegedExceptionAction JavaDoc
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         /**
156          * Performs the action.
157          */

158         public Object JavaDoc run() throws Exception JavaDoc
159         {
160             // TODO - Do not pass the selector through. It is untrusted
161
// TODO - Need to determine which files can be read
162
return replicator.replicateFile(srcFile, selector);
163         }
164     }
165
166     /**
167      * An action that closes the wrapped replicator.
168      */

169     private class CloseAction implements PrivilegedAction JavaDoc
170     {
171         /**
172          * Performs the action.
173          */

174         public Object JavaDoc run()
175         {
176             replicatorComponent.close();
177             return null;
178         }
179     }
180 }
181
Popular Tags