|
Added
Link Here
|
| 1 |
package org.eclipse.equinox.internal.p2.engine.repo; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.net.MalformedURLException; |
| 5 |
import java.net.URL; |
| 6 |
import java.util.*; |
| 7 |
import org.eclipse.core.runtime.*; |
| 8 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 9 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 10 |
import org.eclipse.equinox.internal.p2.engine.EngineActivator; |
| 11 |
import org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry; |
| 12 |
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; |
| 13 |
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus; |
| 14 |
import org.eclipse.equinox.internal.provisional.p2.core.repository.IRepository; |
| 15 |
import org.eclipse.equinox.internal.provisional.p2.core.repository.RepositoryEvent; |
| 16 |
import org.eclipse.equinox.internal.provisional.p2.engine.IProfile; |
| 17 |
import org.eclipse.equinox.internal.provisional.p2.query.Collector; |
| 18 |
import org.eclipse.equinox.internal.provisional.p2.query.Query; |
| 19 |
import org.eclipse.equinox.internal.provisional.spi.p2.metadata.repository.AbstractMetadataRepository; |
| 20 |
|
| 21 |
public class ProfileMetadataRepository extends AbstractMetadataRepository { |
| 22 |
|
| 23 |
public static final String TYPE = "org.eclipse.equinox.p2.engine.repo.metadataRepository"; //$NON-NLS-1$ |
| 24 |
public static final Integer VERSION = new Integer(1); |
| 25 |
private IProfile profile; |
| 26 |
|
| 27 |
public ProfileMetadataRepository(URL location, IProgressMonitor monitor) throws ProvisionException { |
| 28 |
super(location.toExternalForm(), TYPE, VERSION.toString(), location, null, null, null); |
| 29 |
|
| 30 |
try { |
| 31 |
profile = getProfile(location); |
| 32 |
} catch (RuntimeException e) { |
| 33 |
throw new ProvisionException(new Status(IStatus.ERROR, EngineActivator.ID, ProvisionException.REPOSITORY_FAILED_READ, e.getMessage(), e)); |
| 34 |
} |
| 35 |
|
| 36 |
publishArtifactRepos(); |
| 37 |
} |
| 38 |
|
| 39 |
private void publishArtifactRepos() { |
| 40 |
List artifactRepos = new ArrayList(); |
| 41 |
String bundlePool = profile.getProperty(IProfile.PROP_CACHE); |
| 42 |
if (bundlePool != null) { |
| 43 |
try { |
| 44 |
artifactRepos.add(new File(bundlePool).toURL().toExternalForm()); |
| 45 |
} catch (MalformedURLException e) { |
| 46 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "invalid repo reference with location: " + bundlePool, e)); //$NON-NLS-1$ |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
String sharedBundlePool = profile.getProperty(IProfile.PROP_SHARED_CACHE); |
| 51 |
if (sharedBundlePool != null) { |
| 52 |
try { |
| 53 |
artifactRepos.add(new File(sharedBundlePool).toURL().toExternalForm()); |
| 54 |
} catch (MalformedURLException e) { |
| 55 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "invalid repo reference with location: " + sharedBundlePool, e)); //$NON-NLS-1$ |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
String dropinRepositories = profile.getProperty("org.eclipse.equinox.p2.cache.extensions"); |
| 60 |
if (dropinRepositories != null) { |
| 61 |
StringTokenizer tokenizer = new StringTokenizer(dropinRepositories, "|"); |
| 62 |
while (tokenizer.hasMoreTokens()) { |
| 63 |
artifactRepos.add(tokenizer.nextToken()); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
IProvisioningEventBus bus = (IProvisioningEventBus) ServiceHelper.getService(EngineActivator.getContext(), IProvisioningEventBus.SERVICE_NAME); |
| 68 |
if (bus == null) |
| 69 |
return; |
| 70 |
for (Iterator it = artifactRepos.iterator(); it.hasNext();) { |
| 71 |
String repo = (String) it.next(); |
| 72 |
try { |
| 73 |
URL artifactRepo = new URL(repo); |
| 74 |
bus.publishEvent(new RepositoryEvent(artifactRepo, IRepository.TYPE_ARTIFACT, RepositoryEvent.DISCOVERED, true)); |
| 75 |
} catch (MalformedURLException e) { |
| 76 |
LogHelper.log(new Status(IStatus.WARNING, EngineActivator.ID, "invalid repo reference with location: " + repo, e)); //$NON-NLS-1$ |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public void initialize(RepositoryState state) { |
| 82 |
// nothing to do |
| 83 |
} |
| 84 |
|
| 85 |
public Collector query(Query query, Collector collector, IProgressMonitor monitor) { |
| 86 |
return profile.query(query, collector, monitor); |
| 87 |
} |
| 88 |
|
| 89 |
public static void validate(URL location, IProgressMonitor monitor) throws ProvisionException { |
| 90 |
try { |
| 91 |
getProfile(location); |
| 92 |
} catch (RuntimeException e) { |
| 93 |
throw new ProvisionException(new Status(IStatus.ERROR, "org.eclipse.equinox.p2.engine", ProvisionException.REPOSITORY_FAILED_READ, e.getMessage(), e)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private static IProfile getProfile(URL location) { |
| 98 |
if (!"file".equalsIgnoreCase(location.getProtocol())) |
| 99 |
throw new IllegalArgumentException("Profile Registry must use 'file' protocol."); |
| 100 |
|
| 101 |
File registryDirectory = new File(location.getPath()); |
| 102 |
if (!registryDirectory.isDirectory()) |
| 103 |
throw new IllegalArgumentException("Profile Registry not found. " + registryDirectory + " is not a directory."); |
| 104 |
|
| 105 |
String query = location.getQuery(); |
| 106 |
//expect profileid=xyz |
| 107 |
if (query == null || query.indexOf("profileid=") == -1) |
| 108 |
throw new IllegalArgumentException("'profileid' query parameter not specified."); |
| 109 |
|
| 110 |
String profileid = query.substring(query.indexOf("profileid=") + "profileid=".length()); |
| 111 |
if (profileid == null || profileid.length() == 0 || profileid.indexOf('&') != -1) |
| 112 |
throw new IllegalArgumentException("bad 'profileid' query parameter: " + profileid); |
| 113 |
|
| 114 |
SimpleProfileRegistry profileRegistry = new SimpleProfileRegistry(registryDirectory, null, false); |
| 115 |
IProfile profile = profileRegistry.getProfile(profileid); |
| 116 |
if (profile == null) |
| 117 |
throw new IllegalArgumentException("No profile found for : " + profileid); |
| 118 |
|
| 119 |
return profile; |
| 120 |
} |
| 121 |
} |