001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.impl;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSelector;
022: import org.apache.commons.vfs.FileSystemException;
023: import org.apache.commons.vfs.provider.FileReplicator;
024: import org.apache.commons.vfs.provider.VfsComponent;
025: import org.apache.commons.vfs.provider.VfsComponentContext;
026:
027: import java.io.File;
028: import java.security.AccessController;
029: import java.security.PrivilegedAction;
030: import java.security.PrivilegedActionException;
031: import java.security.PrivilegedExceptionAction;
032:
033: /**
034: * A file replicator that wraps another file replicator, performing
035: * the replication as a privileged action.
036: *
037: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
038: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
039: */
040: public class PrivilegedFileReplicator implements FileReplicator,
041: VfsComponent {
042: private final FileReplicator replicator;
043: private final VfsComponent replicatorComponent;
044:
045: public PrivilegedFileReplicator(FileReplicator replicator) {
046: this .replicator = replicator;
047: if (replicator instanceof VfsComponent) {
048: replicatorComponent = (VfsComponent) replicator;
049: } else {
050: replicatorComponent = null;
051: }
052: }
053:
054: /**
055: * Sets the Logger to use for the component.
056: */
057: public void setLogger(final Log logger) {
058: if (replicatorComponent != null) {
059: replicatorComponent.setLogger(logger);
060: }
061: }
062:
063: /**
064: * Sets the context for the replicator.
065: */
066: public void setContext(final VfsComponentContext context) {
067: if (replicatorComponent != null) {
068: replicatorComponent.setContext(context);
069: }
070: }
071:
072: /**
073: * Initialises the component.
074: */
075: public void init() throws FileSystemException {
076: if (replicatorComponent != null) {
077: try {
078: AccessController.doPrivileged(new InitAction());
079: } catch (final PrivilegedActionException e) {
080: throw new FileSystemException(
081: "vfs.impl/init-replicator.error", null, e);
082: }
083: }
084: }
085:
086: /**
087: * Closes the replicator.
088: */
089: public void close() {
090: if (replicatorComponent != null) {
091: AccessController.doPrivileged(new CloseAction());
092: }
093: }
094:
095: /**
096: * Creates a local copy of the file, and all its descendents.
097: */
098: public File replicateFile(FileObject srcFile, FileSelector selector)
099: throws FileSystemException {
100: try {
101: final ReplicateAction action = new ReplicateAction(srcFile,
102: selector);
103: return (File) AccessController.doPrivileged(action);
104: } catch (final PrivilegedActionException e) {
105: throw new FileSystemException(
106: "vfs.impl/replicate-file.error",
107: new Object[] { srcFile.getName() }, e);
108: }
109: }
110:
111: /**
112: * An action that initialises the wrapped replicator.
113: */
114: private class InitAction implements PrivilegedExceptionAction {
115: /**
116: * Performs the action.
117: */
118: public Object run() throws Exception {
119: replicatorComponent.init();
120: return null;
121: }
122: }
123:
124: /**
125: * An action that replicates a file using the wrapped replicator.
126: */
127: private class ReplicateAction implements PrivilegedExceptionAction {
128: private final FileObject srcFile;
129: private final FileSelector selector;
130:
131: public ReplicateAction(final FileObject srcFile,
132: final FileSelector selector) {
133: this .srcFile = srcFile;
134: this .selector = selector;
135: }
136:
137: /**
138: * Performs the action.
139: */
140: public Object run() throws Exception {
141: // TODO - Do not pass the selector through. It is untrusted
142: // TODO - Need to determine which files can be read
143: return replicator.replicateFile(srcFile, selector);
144: }
145: }
146:
147: /**
148: * An action that closes the wrapped replicator.
149: */
150: private class CloseAction implements PrivilegedAction {
151: /**
152: * Performs the action.
153: */
154: public Object run() {
155: replicatorComponent.close();
156: return null;
157: }
158: }
159: }
|