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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
package org.apache.ivy.plugins.repository.vfs;
22
import java.io.FileInputStream;
23
import java.io.IOException;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.List;
28
import org.apache.commons.vfs.FileContent;
29
import org.apache.commons.vfs.FileObject;
30
import org.apache.commons.vfs.FileSystemException;
31
import org.apache.commons.vfs.FileSystemManager;
32
import org.apache.commons.vfs.FileType;
33
import org.apache.commons.vfs.impl.StandardFileSystemManager;
34
import org.apache.ivy.plugins.repository.AbstractRepository;
35
import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
36
import org.apache.ivy.plugins.repository.Resource;
37
import org.apache.ivy.plugins.repository.TransferEvent;
38
import org.apache.ivy.util.CopyProgressListener;
39
import org.apache.ivy.util.FileUtil;
40
import org.apache.ivy.util.Message;
43
* Implementation of a VFS repository
45
public class VfsRepository extends AbstractRepository {
47
* Name of the resource defining the Ivy VFS Repo configuration.
49
private static final String IVY_VFS_CONFIG = "ivy_vfs.xml";
51
private StandardFileSystemManager manager = null;
53
private final CopyProgressListener progress = new RepositoryCopyProgressListener(this);
56
* Create a new Ivy VFS Repository Instance
58
public VfsRepository() {
61
private FileSystemManager getVFSManager() throws IOException {
63
if (manager == null) {
64
manager = createVFSManager();
70
private StandardFileSystemManager createVFSManager() throws IOException {
71
StandardFileSystemManager result = null;
74
* The DefaultFileSystemManager gets its configuration from the jakarta-vfs-common
75
* implementation which includes the res and tmp schemes which are of no use to use
76
* here. Using StandardFileSystemManager lets us specify which schemes to support as
77
* well as providing a mechanism to change this support without recompilation.
79
result = new StandardFileSystemManager() {
80
protected void configurePlugins() throws FileSystemException {
81
// disable automatic loading potential unsupported extensions
84
result.setConfiguration(getClass().getResource(IVY_VFS_CONFIG));
87
// Generate and print a list of available schemes
88
Message.verbose("Available VFS schemes...");
89
String[] schemes = result.getSchemes();
91
for (int i = 0; i < schemes.length; i++) {
92
Message.verbose("VFS Supported Scheme: " + schemes[i]);
94
} catch (FileSystemException e) {
96
* If our attempt to initialize a VFS Repository fails we log the failure but continue
97
* on. Given that an Ivy instance may involve numerous different repository types, it
98
* seems overly cautious to throw a runtime exception on the initialization failure of
99
* just one repository type.
101
Message.error("Unable to initialize VFS repository manager!");
102
Message.error(e.getLocalizedMessage());
103
IOException error = new IOException(e.getLocalizedMessage());
111
protected void finalize() {
112
if (manager != null) {
122
* a <code>String</code> identifying a VFS Resource
123
* @throws <code>IOException</code> on failure
124
* @see "Supported File Systems in the jakarta-commons-vfs documentation"
126
public Resource getResource(String vfsURI) throws IOException {
127
return new VfsResource(vfsURI, getVFSManager());
131
* Transfer a VFS Resource from the repository to the local file system.
134
* a <code>String</code> identifying the VFS resource to be fetched
136
* a <code>File</code> identifying the destination file
137
* @throws <code>IOException</code> on failure
138
* @see "Supported File Systems in the jakarta-commons-vfs documentation"
140
public void get(String srcVfsURI, File destination) throws IOException {
141
VfsResource src = new VfsResource(srcVfsURI, getVFSManager());
142
fireTransferInitiated(src, TransferEvent.REQUEST_GET);
144
FileContent content = src.getContent();
145
if (content == null) {
146
throw new IllegalArgumentException("invalid vfs uri " + srcVfsURI
147
+ ": no content found");
149
FileUtil.copy(content.getInputStream(), destination, progress);
150
} catch (IOException ex) {
151
fireTransferError(ex);
153
} catch (RuntimeException ex) {
154
fireTransferError(ex);
160
* Return a listing of the contents of a parent directory. Listing is a set of strings
161
* representing VFS URIs.
164
* providing identifying a VFS provided resource
165
* @throws IOException
167
* @see "Supported File Systems in the jakarta-commons-vfs documentation"
169
public List list(String vfsURI) throws IOException {
170
ArrayList list = new ArrayList();
171
Message.debug("list called for URI" + vfsURI);
172
FileObject resourceImpl = getVFSManager().resolveFile(vfsURI);
173
Message.debug("resourceImpl=" + resourceImpl.toString());
174
Message.debug("resourceImpl.exists()" + resourceImpl.exists());
175
Message.debug("resourceImpl.getType()" + resourceImpl.getType());
176
Message.debug("FileType.FOLDER" + FileType.FOLDER);
177
if ((resourceImpl != null) && resourceImpl.exists()
178
&& (resourceImpl.getType() == FileType.FOLDER)) {
179
FileObject[] children = resourceImpl.getChildren();
180
for (int i = 0; i < children.length; i++) {
181
FileObject child = children[i];
182
Message.debug("child " + i + child.getName().getURI());
183
list.add(VfsResource.normalize(child.getName().getURI()));
190
* Transfer an Ivy resource to a VFS repository
193
* a <code>File</code> indentifying the local file to transfer to the repository
195
* a <code>String</code> identifying the destination VFS Resource.
197
* whether to overwrite an existing resource.
198
* @throws <code>IOException</code> on failure.
199
* @see "Supported File Systems in the jakarta-commons-vfs documentation"
201
public void put(File source, String vfsURI, boolean overwrite) throws IOException {
202
VfsResource dest = new VfsResource(vfsURI, getVFSManager());
203
fireTransferInitiated(dest, TransferEvent.REQUEST_PUT);
204
if (dest.physicallyExists() && !overwrite) {
205
throw new IOException("Cannot copy. Destination file: " + dest.getName()
206
+ " exists and overwrite not set.");
208
if (dest.getContent() == null) {
209
throw new IllegalArgumentException("invalid vfs uri " + vfsURI
210
+ " to put data to: resource has no content");
213
FileUtil.copy(new FileInputStream(source), dest.getContent().getOutputStream(), progress);