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

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/resolver/IvyRepResolver.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
package org.apache.ivy.plugins.resolver;
 
19
 
 
20
import java.io.File;
 
21
import java.net.MalformedURLException;
 
22
import java.net.URL;
 
23
import java.text.ParseException;
 
24
import java.util.ArrayList;
 
25
import java.util.Collection;
 
26
import java.util.Collections;
 
27
import java.util.Date;
 
28
import java.util.HashMap;
 
29
import java.util.Iterator;
 
30
import java.util.List;
 
31
import java.util.Map;
 
32
 
 
33
import org.apache.ivy.core.IvyPatternHelper;
 
34
import org.apache.ivy.core.cache.ArtifactOrigin;
 
35
import org.apache.ivy.core.module.descriptor.Artifact;
 
36
import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
 
37
import org.apache.ivy.core.report.DownloadReport;
 
38
import org.apache.ivy.core.resolve.DownloadOptions;
 
39
import org.apache.ivy.core.resolve.ResolveData;
 
40
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
 
41
import org.apache.ivy.core.search.ModuleEntry;
 
42
import org.apache.ivy.core.search.OrganisationEntry;
 
43
import org.apache.ivy.core.search.RevisionEntry;
 
44
import org.apache.ivy.plugins.resolver.util.ResolvedResource;
 
45
import org.apache.ivy.util.Message;
 
46
import org.apache.ivy.util.XMLHelper;
 
47
import org.xml.sax.SAXException;
 
48
import org.xml.sax.helpers.DefaultHandler;
 
49
 
 
50
/**
 
51
 * IvyRepResolver is a resolver which can be used to resolve dependencies found in the ivy official
 
52
 * repository for ivy files and ibiblio maven repository for the artifacts, or similar repositories.
 
53
 * For more flexibility with url and patterns, see
 
54
 * {@link org.apache.ivy.plugins.resolver.URLResolver}.
 
55
 */
 
56
public class IvyRepResolver extends URLResolver {
 
57
    public static final String DEFAULT_IVYPATTERN = "[organisation]/[module]/ivy-[revision].xml";
 
58
 
 
59
    public static final String DEFAULT_IVYROOT = "http://ivyrep.jayasoft.org/";
 
60
 
 
61
    private String ivyroot = null;
 
62
 
 
63
    private String ivypattern = null;
 
64
 
 
65
    private String artroot = null;
 
66
 
 
67
    private String artpattern = null;
 
68
 
 
69
    public IvyRepResolver() {
 
70
    }
 
71
 
 
72
    private void ensureArtifactConfigured(ResolverSettings settings) {
 
73
        if (settings != null && (artroot == null || artpattern == null)) {
 
74
            if (artroot == null) {
 
75
                String root = settings.getVariable("ivy.ivyrep.default.artifact.root");
 
76
                if (root != null) {
 
77
                    artroot = root;
 
78
                } else {
 
79
                    settings.configureRepositories(true);
 
80
                    artroot = settings.getVariable("ivy.ivyrep.default.artifact.root");
 
81
                }
 
82
            }
 
83
            if (artpattern == null) {
 
84
                String pattern = settings.getVariable("ivy.ivyrep.default.artifact.pattern");
 
85
                if (pattern != null) {
 
86
                    artpattern = pattern;
 
87
                } else {
 
88
                    settings.configureRepositories(false);
 
89
                    artpattern = settings.getVariable("ivy.ivyrep.default.artifact.pattern");
 
90
                }
 
91
            }
 
92
            updateWholeArtPattern();
 
93
        }
 
94
    }
 
95
 
 
96
    private void ensureIvyConfigured(ResolverSettings settings) {
 
97
        if (settings != null && (ivyroot == null || ivypattern == null)) {
 
98
            if (ivyroot == null) {
 
99
                String root = settings.getVariable("ivy.ivyrep.default.ivy.root");
 
100
                if (root != null) {
 
101
                    ivyroot = root;
 
102
                } else {
 
103
                    throw new IllegalStateException(
 
104
                        "ivyroot is mandatory on IvyRepResolver. " 
 
105
                        + "Make sure to set it in your settings, before setting ivypattern "
 
106
                        + "if you wish to set ivypattern too.");
 
107
                }
 
108
            }
 
109
            if (ivypattern == null) {
 
110
                String pattern = settings.getVariable("ivy.ivyrep.default.ivy.pattern");
 
111
                if (pattern != null) {
 
112
                    ivypattern = pattern;
 
113
                } else {
 
114
                    settings.configureRepositories(false);
 
115
                    ivypattern = settings.getVariable("ivy.ivyrep.default.ivy.pattern");
 
116
                }
 
117
            }
 
118
            updateWholeIvyPattern();
 
119
        }
 
120
    }
 
121
 
 
122
    private String getWholeIvyPattern() {
 
123
        if (ivyroot == null || ivypattern == null) {
 
124
            return null;
 
125
        }
 
126
        return ivyroot + ivypattern;
 
127
    }
 
128
 
 
129
    private String getWholeArtPattern() {
 
130
        return artroot + artpattern;
 
131
    }
 
132
 
 
133
    public String getIvypattern() {
 
134
        return ivypattern;
 
135
    }
 
136
 
 
137
    public void setIvypattern(String pattern) {
 
138
        if (pattern == null) {
 
139
            throw new NullPointerException("pattern must not be null");
 
140
        }
 
141
        ivypattern = pattern;
 
142
        ensureIvyConfigured(getSettings());
 
143
        updateWholeIvyPattern();
 
144
    }
 
145
 
 
146
    public String getIvyroot() {
 
147
        return ivyroot;
 
148
    }
 
149
 
 
150
    /**
 
151
     * Sets the root of the maven like repository. The maven like repository is necessarily an http
 
152
     * repository.
 
153
     * 
 
154
     * @param root
 
155
     *            the root of the maven like repository
 
156
     * @throws IllegalArgumentException
 
157
     *             if root does not start with "http://"
 
158
     */
 
159
    public void setIvyroot(String root) {
 
160
        if (root == null) {
 
161
            throw new NullPointerException("root must not be null");
 
162
        }
 
163
        if (!root.endsWith("/")) {
 
164
            ivyroot = root + "/";
 
165
        } else {
 
166
            ivyroot = root;
 
167
        }
 
168
        ensureIvyConfigured(getSettings());
 
169
        updateWholeIvyPattern();
 
170
    }
 
171
 
 
172
    public void setM2compatible(boolean m2compatible) {
 
173
        if (m2compatible) {
 
174
            throw new IllegalArgumentException("ivyrep does not support maven2 compatibility. " 
 
175
                + "Please use ibiblio resolver instead, or even url or filesystem resolvers for" 
 
176
                + " more specific needs.");
 
177
        }
 
178
    }
 
179
 
 
180
    private void updateWholeIvyPattern() {
 
181
        setIvyPatterns(Collections.singletonList(getWholeIvyPattern()));
 
182
    }
 
183
 
 
184
    private void updateWholeArtPattern() {
 
185
        setArtifactPatterns(Collections.singletonList(getWholeArtPattern()));
 
186
    }
 
187
 
 
188
    public void publish(Artifact artifact, File src) {
 
189
        throw new UnsupportedOperationException("publish not supported by IBiblioResolver");
 
190
    }
 
191
 
 
192
    public String getArtroot() {
 
193
        return artroot;
 
194
    }
 
195
 
 
196
    public String getArtpattern() {
 
197
        return artpattern;
 
198
    }
 
199
 
 
200
    public void setArtpattern(String pattern) {
 
201
        if (pattern == null) {
 
202
            throw new NullPointerException("pattern must not be null");
 
203
        }
 
204
        artpattern = pattern;
 
205
        ensureArtifactConfigured(getSettings());
 
206
        updateWholeArtPattern();
 
207
    }
 
208
 
 
209
    public void setArtroot(String root) {
 
210
        if (root == null) {
 
211
            throw new NullPointerException("root must not be null");
 
212
        }
 
213
        if (!root.endsWith("/")) {
 
214
            artroot = root + "/";
 
215
        } else {
 
216
            artroot = root;
 
217
        }
 
218
        ensureArtifactConfigured(getSettings());
 
219
        updateWholeArtPattern();
 
220
    }
 
221
 
 
222
    public OrganisationEntry[] listOrganisations() {
 
223
        ensureIvyConfigured(getSettings());
 
224
        try {
 
225
            URL content = new URL(ivyroot + "content.xml");
 
226
            final List ret = new ArrayList();
 
227
            XMLHelper.parse(content, null, new DefaultHandler() {
 
228
                public void startElement(String uri, String localName, String qName,
 
229
                        org.xml.sax.Attributes attributes) throws SAXException {
 
230
                    if ("organisation".equals(qName)) {
 
231
                        String org = attributes.getValue("name");
 
232
                        if (org != null) {
 
233
                            ret.add(new OrganisationEntry(IvyRepResolver.this, org));
 
234
                        }
 
235
                    }
 
236
                }
 
237
            });
 
238
            return (OrganisationEntry[]) ret.toArray(new OrganisationEntry[ret.size()]);
 
239
        } catch (MalformedURLException e) {
 
240
            //???
 
241
        } catch (Exception e) {
 
242
            Message.warn("unable to parse content.xml file on ivyrep: " + e.getMessage());
 
243
        }
 
244
        return super.listOrganisations();
 
245
    }
 
246
 
 
247
    // overwrite parent to use only ivy patterns (and not artifact ones, cause ibiblio is too slow
 
248
    // to answer)
 
249
    public ModuleEntry[] listModules(OrganisationEntry org) {
 
250
        ensureIvyConfigured(getSettings());
 
251
        Map tokenValues = new HashMap();
 
252
        tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org.getOrganisation());
 
253
        Collection names = findIvyNames(tokenValues, IvyPatternHelper.MODULE_KEY);
 
254
        ModuleEntry[] ret = new ModuleEntry[names.size()];
 
255
        int i = 0;
 
256
        for (Iterator iter = names.iterator(); iter.hasNext(); i++) {
 
257
            String name = (String) iter.next();
 
258
            ret[i] = new ModuleEntry(org, name);
 
259
        }
 
260
        return ret;
 
261
    }
 
262
 
 
263
    public RevisionEntry[] listRevisions(ModuleEntry mod) {
 
264
        ensureIvyConfigured(getSettings());
 
265
        ensureArtifactConfigured(getSettings());
 
266
        return super.listRevisions(mod);
 
267
    }
 
268
 
 
269
    public String getTypeName() {
 
270
        return "ivyrep";
 
271
    }
 
272
 
 
273
    // override some methods to ensure configuration
 
274
    public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data)
 
275
            throws ParseException {
 
276
        ensureIvyConfigured(data.getSettings());
 
277
        return super.getDependency(dd, data);
 
278
    }
 
279
 
 
280
    protected ResolvedResource findArtifactRef(Artifact artifact, Date date) {
 
281
        ensureArtifactConfigured(getSettings());
 
282
        return super.findArtifactRef(artifact, date);
 
283
    }
 
284
 
 
285
    public DownloadReport download(Artifact[] artifacts, DownloadOptions options) {
 
286
        ensureArtifactConfigured(getSettings());
 
287
        return super.download(artifacts, options);
 
288
    }
 
289
 
 
290
    public boolean exists(Artifact artifact) {
 
291
        ensureArtifactConfigured(getSettings());
 
292
        return super.exists(artifact);
 
293
    }
 
294
    
 
295
    public ArtifactOrigin locate(Artifact artifact) {
 
296
        ensureArtifactConfigured(getSettings());
 
297
        return super.locate(artifact);
 
298
    }
 
299
 
 
300
    public List getIvyPatterns() {
 
301
        ensureIvyConfigured(getSettings());
 
302
        return super.getIvyPatterns();
 
303
    }
 
304
 
 
305
    public List getArtifactPatterns() {
 
306
        ensureArtifactConfigured(getSettings());
 
307
        return super.getArtifactPatterns();
 
308
    }
 
309
}