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