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.
18
package org.apache.ivy.ant;
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;
27
import javax.xml.transform.TransformerConfigurationException;
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;
44
public class BuildBundleRepoDescriptorTask extends IvyTask {
46
private String resolverName = null;
48
private File file = null;
50
private String cacheName = null;
52
private String encoding = "UTF-8";
54
private boolean indent = true;
58
private boolean quiet;
60
public void setResolver(String resolverName) {
61
this.resolverName = resolverName;
64
public void setCache(String cacheName) {
65
this.cacheName = cacheName;
68
public void setOut(File file) {
72
public void setEncoding(String encoding) {
73
this.encoding = encoding;
76
public void setIndent(boolean indent) {
80
public void setBaseDir(File dir) {
84
public void setQuiet(boolean quiet) {
88
protected void prepareTask() {
89
if (baseDir == null) {
94
public void doExecute() throws BuildException {
96
throw new BuildException("No output file specified: use the attribute 'out'");
99
Iterator/* <ManifestAndLocation> */it;
100
if (resolverName != null) {
101
if (baseDir != null) {
102
throw new BuildException("specify only one of 'resolver' or 'baseDir'");
104
if (cacheName != null) {
105
throw new BuildException("specify only one of 'resolver' or 'cache'");
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");
113
if (!(resolver instanceof BasicResolver)) {
114
throw new BuildException("the type of resolver '"
115
+ resolver.getClass().getName() + "' is not supported.");
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'");
122
if (!baseDir.isDirectory()) {
123
throw new BuildException(baseDir + " is not a directory");
125
it = new FSManifestIterable(baseDir).iterator();
126
} else if (cacheName != null) {
127
Ivy ivy = getIvyInstance();
128
RepositoryCacheManager cacheManager = ivy.getSettings().getRepositoryCacheManager(
130
if (!(cacheManager instanceof DefaultRepositoryCacheManager)) {
131
throw new BuildException("the type of cache '"
132
+ cacheManager.getClass().getName() + "' is not supported.");
134
File basedir = ((DefaultRepositoryCacheManager) cacheManager).getBasedir();
135
it = new FSManifestIterable(basedir).iterator();
137
throw new BuildException(
138
"No resolver, cache or basedir specified: "
139
+ "please provide one of them through the attribute 'resolver', 'cache' or 'dir'");
144
out = new FileOutputStream(file);
145
} catch (FileNotFoundException e) {
146
throw new BuildException(file + " was not found", e);
151
hd = OBRXMLWriter.newHandler(out, encoding, indent);
152
} catch (TransformerConfigurationException e) {
153
throw new BuildException("Sax configuration error: " + e.getMessage(), e);
156
class AntMessageLogger2 extends AntMessageLogger {
157
AntMessageLogger2() {
158
super(BuildBundleRepoDescriptorTask.this);
161
IvyContext.getContext().getMessageLogger();
162
Message.setDefaultLogger(new AntMessageLogger2());
165
OBRXMLWriter.writeManifests(it, hd, quiet);
166
} catch (SAXException e) {
167
throw new BuildException("Sax serialisation error: " + e.getMessage(), e);
173
} catch (IOException e) {
177
Message.sumupProblems();