~ubuntu-branches/ubuntu/saucy/ivy/saucy-proposed

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/ant/BuildBundleRepoDescriptorTask.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-15 17:44:57 UTC
  • mfrom: (3.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130515174457-ogntd0vxluwalq11
Tags: 2.3.0-2
* Team upload.
* Upload to unstable.

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
package org.apache.ivy.ant;
 
19
 
 
20
import java.io.File;
 
21
import java.io.FileNotFoundException;
 
22
import java.io.FileOutputStream;
 
23
import java.io.IOException;
 
24
import java.io.OutputStream;
 
25
import java.util.Iterator;
 
26
 
 
27
import javax.xml.transform.TransformerConfigurationException;
 
28
 
 
29
import org.apache.ivy.Ivy;
 
30
import org.apache.ivy.core.IvyContext;
 
31
import org.apache.ivy.core.cache.DefaultRepositoryCacheManager;
 
32
import org.apache.ivy.core.cache.RepositoryCacheManager;
 
33
import org.apache.ivy.core.settings.IvySettings;
 
34
import org.apache.ivy.osgi.obr.xml.OBRXMLWriter;
 
35
import org.apache.ivy.osgi.repo.FSManifestIterable;
 
36
import org.apache.ivy.osgi.repo.ResolverManifestIterable;
 
37
import org.apache.ivy.plugins.resolver.BasicResolver;
 
38
import org.apache.ivy.plugins.resolver.DependencyResolver;
 
39
import org.apache.ivy.util.Message;
 
40
import org.apache.tools.ant.BuildException;
 
41
import org.xml.sax.ContentHandler;
 
42
import org.xml.sax.SAXException;
 
43
 
 
44
public class BuildBundleRepoDescriptorTask extends IvyTask {
 
45
 
 
46
    private String resolverName = null;
 
47
 
 
48
    private File file = null;
 
49
 
 
50
    private String cacheName = null;
 
51
 
 
52
    private String encoding = "UTF-8";
 
53
 
 
54
    private boolean indent = true;
 
55
 
 
56
    private File baseDir;
 
57
 
 
58
    private boolean quiet;
 
59
 
 
60
    public void setResolver(String resolverName) {
 
61
        this.resolverName = resolverName;
 
62
    }
 
63
 
 
64
    public void setCache(String cacheName) {
 
65
        this.cacheName = cacheName;
 
66
    }
 
67
 
 
68
    public void setOut(File file) {
 
69
        this.file = file;
 
70
    }
 
71
 
 
72
    public void setEncoding(String encoding) {
 
73
        this.encoding = encoding;
 
74
    }
 
75
 
 
76
    public void setIndent(boolean indent) {
 
77
        this.indent = indent;
 
78
    }
 
79
 
 
80
    public void setBaseDir(File dir) {
 
81
        this.baseDir = dir;
 
82
    }
 
83
 
 
84
    public void setQuiet(boolean quiet) {
 
85
        this.quiet = quiet;
 
86
    }
 
87
 
 
88
    protected void prepareTask() {
 
89
        if (baseDir == null) {
 
90
            super.prepareTask();
 
91
        }
 
92
    }
 
93
 
 
94
    public void doExecute() throws BuildException {
 
95
        if (file == null) {
 
96
            throw new BuildException("No output file specified: use the attribute 'out'");
 
97
        }
 
98
 
 
99
        Iterator/* <ManifestAndLocation> */it;
 
100
        if (resolverName != null) {
 
101
            if (baseDir != null) {
 
102
                throw new BuildException("specify only one of 'resolver' or 'baseDir'");
 
103
            }
 
104
            if (cacheName != null) {
 
105
                throw new BuildException("specify only one of 'resolver' or 'cache'");
 
106
            }
 
107
            Ivy ivy = getIvyInstance();
 
108
            IvySettings settings = ivy.getSettings();
 
109
            DependencyResolver resolver = settings.getResolver(resolverName);
 
110
            if (resolver == null) {
 
111
                throw new BuildException("the resolver '" + resolverName + "' was not found");
 
112
            }
 
113
            if (!(resolver instanceof BasicResolver)) {
 
114
                throw new BuildException("the type of resolver '"
 
115
                        + resolver.getClass().getName() + "' is not supported.");
 
116
            }
 
117
            it = new ResolverManifestIterable((BasicResolver) resolver).iterator();
 
118
        } else if (baseDir != null) {
 
119
            if (cacheName != null) {
 
120
                throw new BuildException("specify only one of 'baseDir' or 'cache'");
 
121
            }
 
122
            if (!baseDir.isDirectory()) {
 
123
                throw new BuildException(baseDir + " is not a directory");
 
124
            }
 
125
            it = new FSManifestIterable(baseDir).iterator();
 
126
        } else if (cacheName != null) {
 
127
            Ivy ivy = getIvyInstance();
 
128
            RepositoryCacheManager cacheManager = ivy.getSettings().getRepositoryCacheManager(
 
129
                cacheName);
 
130
            if (!(cacheManager instanceof DefaultRepositoryCacheManager)) {
 
131
                throw new BuildException("the type of cache '"
 
132
                        + cacheManager.getClass().getName() + "' is not supported.");
 
133
            }
 
134
            File basedir = ((DefaultRepositoryCacheManager) cacheManager).getBasedir();
 
135
            it = new FSManifestIterable(basedir).iterator();
 
136
        } else {
 
137
            throw new BuildException(
 
138
                    "No resolver, cache or basedir specified: "
 
139
                            + "please provide one of them through the attribute 'resolver', 'cache' or 'dir'");
 
140
        }
 
141
 
 
142
        OutputStream out;
 
143
        try {
 
144
            out = new FileOutputStream(file);
 
145
        } catch (FileNotFoundException e) {
 
146
            throw new BuildException(file + " was not found", e);
 
147
        }
 
148
 
 
149
        ContentHandler hd;
 
150
        try {
 
151
            hd = OBRXMLWriter.newHandler(out, encoding, indent);
 
152
        } catch (TransformerConfigurationException e) {
 
153
            throw new BuildException("Sax configuration error: " + e.getMessage(), e);
 
154
        }
 
155
 
 
156
        class AntMessageLogger2 extends AntMessageLogger {
 
157
            AntMessageLogger2() {
 
158
                super(BuildBundleRepoDescriptorTask.this);
 
159
            }
 
160
        }
 
161
        IvyContext.getContext().getMessageLogger();
 
162
        Message.setDefaultLogger(new AntMessageLogger2());
 
163
 
 
164
        try {
 
165
            OBRXMLWriter.writeManifests(it, hd, quiet);
 
166
        } catch (SAXException e) {
 
167
            throw new BuildException("Sax serialisation error: " + e.getMessage(), e);
 
168
        }
 
169
 
 
170
        try {
 
171
            out.flush();
 
172
            out.close();
 
173
        } catch (IOException e) {
 
174
            // don't care
 
175
        }
 
176
 
 
177
        Message.sumupProblems();
 
178
    }
 
179
 
 
180
}