Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 brianR 1
/*
2
 *   Copyright 2010 Brian Rosenberger (Brutex Network)
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
 
17
package net.brutex.xservices.types;
18
 
19
import java.io.File;
20
import javax.xml.bind.annotation.XmlElement;
21
import javax.xml.bind.annotation.XmlEnum;
22
import javax.xml.bind.annotation.XmlType;
10 brianR 23
import net.brutex.xservices.util.BrutexNamespaces;
6 brianR 24
import org.apache.tools.ant.Project;
25
import org.apache.tools.ant.types.Resource;
26
import org.apache.tools.ant.types.resources.TarResource;
27
import org.apache.tools.ant.types.resources.ZipResource;
28
import org.apache.tools.tar.TarEntry;
29
import org.apache.tools.zip.ZipEntry;
30
 
31
/**
10 brianR 32
 * Resource from archive declaration.
33
 *
34
 * Defines a resource within an archive.
6 brianR 35
 * @author Brian Rosenberger, bru@brutex.de
36
 */
10 brianR 37
@XmlType(namespace = BrutexNamespaces.WS_XSERVICES, name="ArchiveResourceType")
6 brianR 38
public class ArchiveResource
39
        implements ResourceInterface {
40
 
10 brianR 41
    /**
42
     * Archive Type.
43
     */
6 brianR 44
    @XmlElement(defaultValue = "ZIP", nillable = false, required = true)
45
    public ArchiveType type = ArchiveType.ZIP;
10 brianR 46
 
47
    /**
48
     * Archive file.
49
     *
50
     * Path and filename of the archive to use.
51
     */
6 brianR 52
    @XmlElement(nillable = false, required = true)
53
    public String archive;
10 brianR 54
 
55
    /**
56
     * URI within the archive.
57
     *
58
     * This is usually a filename or a path/filename combination. Relative paths
59
     * are based from the archive root. It depends on how the archive has been
50 brianR 60
     * created whether or not it is possible to use relative paths, absolute
10 brianR 61
     * paths are required otherwise. Uses "/" as separator.
62
     */
6 brianR 63
    @XmlElement(nillable = false, required = true)
64
    public String uri;
65
 
10 brianR 66
    /**
67
     * Supported archive types.
68
     */
69
    @XmlEnum(value=String.class)
6 brianR 70
    public enum ArchiveType {
71
 
10 brianR 72
        /**
73
         * Zip archive type.
74
         */
75
        ZIP,
76
 
77
        /**
78
         * Tar archive type, without compression
79
         */
80
        TAR,
81
 
82
        /**
83
         * Tar archive type, with GZIP compression
84
         */
85
        GZTAR
6 brianR 86
    }
87
 
10 brianR 88
    /**
89
     * Get Apache Ant Resource Type.
90
     *
91
     * @param p     Ant project
92
     * @return      this ArchiveResource as Ant Resource
93
     */
6 brianR 94
    public Resource getAntResource(Project p) {
95
        Resource res = null;
96
        switch (type) {
97
            case TAR:
98
                TarEntry tarentry = new TarEntry(uri);
99
                TarResource tres = new TarResource(new File(archive), tarentry);
100
                res = tres;
101
                break;
102
 
103
            case GZTAR:
104
                TarResource gres = new TarResource(new org.apache.tools.ant.types.resources.GZipResource(
105
                        new org.apache.tools.ant.types.resources.FileResource(
106
                        new File(archive))), new TarEntry(uri));
107
                res = gres;
108
                break;
109
 
110
            default: //This is type ZIP also
111
                ZipEntry zipentry = new ZipEntry(uri);
112
                if (zipentry.getTime() == -1) {
113
                    zipentry.setTime(System.currentTimeMillis());
114
                }
115
                res = new ZipResource(new File(archive), null, zipentry);
116
                break;
117
        }
118
 
119
        res.setProject(p);
120
        return res;
121
    }
122
}