~ubuntu-branches/ubuntu/trusty/ivy/trusty-proposed

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/repository/vfs/VfsRepository.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
18
 
 
19
package org.apache.ivy.plugins.repository.vfs;
 
20
 
 
21
import java.io.File;
 
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;
 
27
 
 
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;
 
41
 
 
42
/**
 
43
 * Implementation of a VFS repository
 
44
 */
 
45
public class VfsRepository extends AbstractRepository {
 
46
    /**
 
47
     * Name of the resource defining the Ivy VFS Repo configuration.
 
48
     */
 
49
    private static final String IVY_VFS_CONFIG = "ivy_vfs.xml";
 
50
 
 
51
    private StandardFileSystemManager manager = null;
 
52
 
 
53
    private final CopyProgressListener progress = new RepositoryCopyProgressListener(this);
 
54
 
 
55
    /**
 
56
     * Create a new Ivy VFS Repository Instance
 
57
     */
 
58
    public VfsRepository() {
 
59
    }
 
60
 
 
61
    private FileSystemManager getVFSManager() throws IOException {
 
62
        synchronized (this) {
 
63
            if (manager == null) {
 
64
                manager = createVFSManager();
 
65
            }
 
66
        }
 
67
        return manager;
 
68
    }
 
69
 
 
70
    private StandardFileSystemManager createVFSManager() throws IOException {
 
71
        StandardFileSystemManager result = null;
 
72
        try {
 
73
            /*
 
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.
 
78
             */
 
79
            result = new StandardFileSystemManager() {
 
80
                protected void configurePlugins() throws FileSystemException {
 
81
                    // disable automatic loading potential unsupported extensions
 
82
                }
 
83
            };
 
84
            result.setConfiguration(getClass().getResource(IVY_VFS_CONFIG));
 
85
            result.init();
 
86
 
 
87
            // Generate and print a list of available schemes
 
88
            Message.verbose("Available VFS schemes...");
 
89
            String[] schemes = result.getSchemes();
 
90
            Arrays.sort(schemes);
 
91
            for (int i = 0; i < schemes.length; i++) {
 
92
                Message.verbose("VFS Supported Scheme: " + schemes[i]);
 
93
            }
 
94
        } catch (FileSystemException e) {
 
95
            /*
 
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.
 
100
             */
 
101
            Message.error("Unable to initialize VFS repository manager!");
 
102
            Message.error(e.getLocalizedMessage());
 
103
            IOException error = new IOException(e.getLocalizedMessage());
 
104
            error.initCause(e);
 
105
            throw error;
 
106
        }
 
107
 
 
108
        return result;
 
109
    }
 
110
 
 
111
    protected void finalize() {
 
112
        if (manager != null) {
 
113
            manager.close();
 
114
            manager = null;
 
115
        }
 
116
    }
 
117
 
 
118
    /**
 
119
     * Get a VfsResource
 
120
     * 
 
121
     * @param source
 
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"
 
125
     */
 
126
    public Resource getResource(String vfsURI) throws IOException {
 
127
        return new VfsResource(vfsURI, getVFSManager());
 
128
    }
 
129
 
 
130
    /**
 
131
     * Transfer a VFS Resource from the repository to the local file system.
 
132
     * 
 
133
     * @param srcVfsURI
 
134
     *            a <code>String</code> identifying the VFS resource to be fetched
 
135
     * @param destination
 
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"
 
139
     */
 
140
    public void get(String srcVfsURI, File destination) throws IOException {
 
141
        VfsResource src = new VfsResource(srcVfsURI, getVFSManager());
 
142
        fireTransferInitiated(src, TransferEvent.REQUEST_GET);
 
143
        try {
 
144
            FileContent content = src.getContent();
 
145
            if (content == null) {
 
146
                throw new IllegalArgumentException("invalid vfs uri " + srcVfsURI
 
147
                        + ": no content found");
 
148
            }
 
149
            FileUtil.copy(content.getInputStream(), destination, progress);
 
150
        } catch (IOException ex) {
 
151
            fireTransferError(ex);
 
152
            throw ex;
 
153
        } catch (RuntimeException ex) {
 
154
            fireTransferError(ex);
 
155
            throw ex;
 
156
        }
 
157
    }
 
158
 
 
159
    /**
 
160
     * Return a listing of the contents of a parent directory. Listing is a set of strings
 
161
     * representing VFS URIs.
 
162
     * 
 
163
     * @param vfsURI
 
164
     *            providing identifying a VFS provided resource
 
165
     * @throws IOException
 
166
     *             on failure.
 
167
     * @see "Supported File Systems in the jakarta-commons-vfs documentation"
 
168
     */
 
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()));
 
184
            }
 
185
        }
 
186
        return list;
 
187
    }
 
188
 
 
189
    /**
 
190
     * Transfer an Ivy resource to a VFS repository
 
191
     * 
 
192
     * @param source
 
193
     *            a <code>File</code> indentifying the local file to transfer to the repository
 
194
     * @param vfsURI
 
195
     *            a <code>String</code> identifying the destination VFS Resource.
 
196
     * @param overwrite
 
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"
 
200
     */
 
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.");
 
207
        }
 
208
        if (dest.getContent() == null) {
 
209
            throw new IllegalArgumentException("invalid vfs uri " + vfsURI
 
210
                    + " to put data to: resource has no content");
 
211
        }
 
212
 
 
213
        FileUtil.copy(new FileInputStream(source), dest.getContent().getOutputStream(), progress);
 
214
    }
 
215
 
 
216
}