Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 00e75e64d20ea756d3560ae90c57c4d76f109f28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.internal.repository.tools.tasks;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.types.FileSet;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.core.Version;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.InstallableUnitQuery;
import org.eclipse.equinox.internal.provisional.p2.metadata.query.LatestIUVersionQuery;
import org.eclipse.equinox.internal.provisional.p2.query.*;
import org.eclipse.equinox.p2.internal.repository.tools.Repo2Runnable;

/**
 * Ant task which calls the "repo to runnable" application. This application takes an
 * existing p2 repository (local or remote), iterates over its list of IUs, and fetches 
 * all of the corresponding artifacts to a user-specified location. Once fetched, the
 * artifacts will be in "runnable" form... that is directory-based bundles will be
 * extracted into folders and packed JAR files will be un-packed.
 * 
 * @since 1.0
 */
public class Repo2RunnableTask extends Task {

	private final Repo2Runnable application;
	private List iuTasks = new ArrayList();
	private List sourceRepos = new ArrayList();

	/*
	 * Constructor for the class. Create a new instance of the application
	 * so we can populate it with attributes.
	 */
	public Repo2RunnableTask() {
		super();
		this.application = new Repo2Runnable();
	}

	public Repo2RunnableTask(Repo2Runnable application) {
		super();
		this.application = application;
	}

	/* (non-Javadoc)
	 * @see org.apache.tools.ant.Task#execute()
	 */
	public void execute() throws BuildException {
		try {
			prepareSourceRepos();
			List ius = prepareIUs();
			if (ius == null || ius.size() == 0)
				throw new BuildException("Need to specify either a non-empty source metadata repository or a valid list of IUs.");
			application.setSourceIUs(ius);
			IStatus result = application.run(null);
			if (result.matches(IStatus.ERROR))
				throw new ProvisionException(result);
		} catch (ProvisionException e) {
			throw new BuildException("Error occurred while transforming repository.", e);
		} catch (URISyntaxException e) {
			throw new BuildException("Error occurred while transforming repository.", e);
		}
	}

	/*
	 * If the user specified some source repositories via sub-elements
	 * then add them to the transformer for consideration.
	 */
	private void prepareSourceRepos() {
		if (sourceRepos == null || sourceRepos.isEmpty())
			return;
		for (Iterator iter = sourceRepos.iterator(); iter.hasNext();) {
			Object next = iter.next();
			if (next instanceof MyFileSet) {
				MyFileSet fileset = (MyFileSet) next;
				// determine if the user set a "location" attribute or used a fileset
				if (fileset.location == null) {
					DirectoryScanner scanner = fileset.getDirectoryScanner(getProject());
					String[][] elements = new String[][] {scanner.getIncludedDirectories(), scanner.getIncludedFiles()};
					for (int i = 0; i < 2; i++) {
						for (int j = 0; j < elements[i].length; j++) {
							URI uri = new File(fileset.getDir(), elements[i][j]).toURI();
							application.addSourceArtifactRepository(uri);
							application.addSourceMetadataRepository(uri);
						}
					}
				} else {
					application.addSourceArtifactRepository(fileset.location);
					application.addSourceMetadataRepository(fileset.location);
				}
			}
		}
	}

	protected void addMetadataSourceRepository(URI repoLocation) {
		application.addSourceMetadataRepository(repoLocation);
	}

	protected void addArtifactSourceRepository(URI repoLocation) {
		application.addSourceArtifactRepository(repoLocation);
	}

	protected List prepareIUs() throws URISyntaxException {
		if (iuTasks == null || iuTasks.isEmpty())
			return null;

		CompositeMetadataRepository repository = new CompositeMetadataRepository(new URI("memory:/composite"), "parent metadata repo", null); //$NON-NLS-1$ //$NON-NLS-2$
		for (Iterator iter = application.getSourceMetadataRepositories().iterator(); iter.hasNext();) {
			repository.addChild((URI) iter.next());
		}
		List result = new ArrayList();
		for (Iterator iter = iuTasks.iterator(); iter.hasNext();) {
			IUTask iu = (IUTask) iter.next();
			String id = iu.getId();
			Version version = null;
			Collector collector = new Collector();

			if (iu.getVersion() == null || iu.getVersion().length() == 0 || iu.getVersion().startsWith("${")) {//$NON-NLS-1$
				// Get the latest version of the iu
				Query query = new CompositeQuery(new Query[] {new InstallableUnitQuery(id), new LatestIUVersionQuery()});
				repository.query(query, collector, null);
			} else {
				version = new Version(iu.getVersion());
				repository.query(new InstallableUnitQuery(id, version), collector, null);
			}

			if (collector.isEmpty())
				System.err.println("Unable to find " + id + version != null ? " " + version : "");
			else
				result.add(collector.iterator().next());
		}
		return result;
	}

	/*
	 * If the repositories are co-located then the user just has to set one
	 * argument to specify both the artifact and metadata repositories.
	 */
	public void setSource(String location) {
		application.addSourceArtifactRepository(location);
		application.addSourceMetadataRepository(location);
	}

	/*
	 * If the repositories are co-located then the user just has to set one
	 * argument to specify both the artifact and metadata repositories.
	 */
	public void setDestination(String location) {
		application.setDestinationArtifactRepository(location);
		application.setDestinationMetadataRepository(location);
	}

	/*
	 * Create an object to hold IU information since the user specified an "iu" sub-element.
	 */
	public Object createIu() {
		IUTask iu = new IUTask();
		iuTasks.add(iu);
		return iu;
	}

	/*
	 * Create a special file set since the user specified a "source" sub-element.
	 */
	public FileSet createSource() {
		MyFileSet set = new MyFileSet();
		sourceRepos.add(set);
		return set;
	}

	/*
	 * New FileSet subclass which adds an optional "location" attribute.
	 */
	public class MyFileSet extends FileSet {
		String location;

		public MyFileSet() {
			super();
		}

		public void setLocation(String value) {
			this.location = value;
		}
	}
}

Back to the top