| package org.eclipse.wtp.releng.tools; |
| |
| import java.net.URI; |
| import java.net.URISyntaxException; |
| |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepositoryFactory; |
| import org.eclipse.equinox.p2.core.ProvisionException; |
| import org.eclipse.equinox.p2.publisher.IPublisherAction; |
| import org.eclipse.equinox.p2.publisher.IPublisherInfo; |
| import org.eclipse.equinox.p2.publisher.Publisher; |
| import org.eclipse.equinox.p2.publisher.PublisherInfo; |
| import org.eclipse.equinox.p2.repository.IRepositoryManager; |
| import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; |
| |
| public class AddMirrorURL { |
| |
| private static final String ARTIFACT_REPO_DIRECTORY = "artifact.repo.directory"; |
| // currently, the system property to set is the same as the P2 |
| // repository property, but may not always be the case. |
| private static final String MIRROR_URL2_PROPERTY = "p2.mirrorURL"; |
| private static final String P2_MIRROR_URL = "p2.mirrorURL"; |
| private String repositoryDirectory; |
| private String mirrorURLString; |
| |
| public void rewriteMirrorURL() throws ProvisionException, URISyntaxException { |
| try { |
| IPublisherInfo info = createPublisherInfo(); |
| if (info != null) { |
| IPublisherAction[] actions = createActions(); |
| Publisher publisher = new Publisher(info); |
| publisher.publish(actions, new NullProgressMonitor()); |
| infoOutput("artifact repository mirrorURL rewritten"); |
| } |
| else { |
| infoOutput("artifact repository was not rewritten"); |
| } |
| } |
| catch (ProvisionException e) { |
| infoOutput(e.getMessage()); |
| throw e; |
| } |
| catch (URISyntaxException e) { |
| infoOutput(e.getMessage()); |
| throw e; |
| } |
| } |
| |
| private IPublisherInfo createPublisherInfo() throws ProvisionException, URISyntaxException { |
| PublisherInfo result = null; |
| |
| String pathPart = getRepositoryDirectory(); |
| if ((pathPart == null) || (pathPart.length() == 0)) { |
| infoOutput("The repository direcotry needs to be specified for this task. Try setting system property '" + ARTIFACT_REPO_DIRECTORY + "'?"); |
| } |
| else { |
| String repoURI = "file:/" + pathPart; |
| |
| |
| SimpleArtifactRepositoryFactory simpleArtifactRepositoryFactory = new SimpleArtifactRepositoryFactory(); |
| // NPE is thrown during "reload" if repository is written, and |
| // agent |
| // is not set |
| simpleArtifactRepositoryFactory.setAgent(Activator.getProvisioningAgent()); |
| IArtifactRepository artifactRepository = simpleArtifactRepositoryFactory.load(new URI(repoURI), IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, new NullProgressMonitor()); |
| if (artifactRepository != null) { |
| String existingMirrorURL = artifactRepository.getProperty(P2_MIRROR_URL); |
| if (existingMirrorURL != null) { |
| infoOutput("the repository had existing mirrorURL: " + existingMirrorURL); |
| } |
| else { |
| infoOutput("the repository had no existing mirrorURL"); |
| } |
| String newMirrorURL = null; |
| newMirrorURL = getMirrorURLString(); |
| // if property given to us is empty string, assume that is |
| // the same as "removal" which is done by passing in null. |
| if ((newMirrorURL != null) && (newMirrorURL.length() == 0)) { |
| newMirrorURL = null; |
| } |
| boolean needrewrite = false; |
| String reasonForNoWrite = null; |
| if (newMirrorURL != null) { |
| // if they are the same, don't bother |
| if (!newMirrorURL.equals(existingMirrorURL)) { |
| needrewrite = true; |
| } |
| else { |
| reasonForNoWrite = "new value and existing value of mirrorURL are the same"; |
| } |
| } |
| else { |
| // newMirror is null, means removal is desired |
| if (existingMirrorURL != null) { |
| needrewrite = true; |
| } |
| else { |
| reasonForNoWrite = "removal of mirrorURL was indicated, but repository has no existing value."; |
| } |
| } |
| if (needrewrite) { |
| result = new PublisherInfo(); |
| artifactRepository.setProperty(P2_MIRROR_URL, newMirrorURL); |
| result.setArtifactRepository(artifactRepository); |
| result.setArtifactOptions(IPublisherInfo.A_OVERWRITE); |
| } |
| else { |
| infoOutput(reasonForNoWrite); |
| } |
| } |
| else { |
| infoOutput("repository was null, not local, or otherwise not modifiable"); |
| } |
| } |
| return result; |
| } |
| |
| private IPublisherAction[] createActions() { |
| IPublisherAction[] result = new IPublisherAction[0]; |
| return result; |
| } |
| |
| public String getRepositoryDirectory() { |
| // if null, that is not explicitly 'set', then see if it was |
| // passed in as a system property. TODO: Does this contradict |
| // ant task rules? |
| if (repositoryDirectory == null) { |
| // for now, be explicit we are working with "artifact" |
| // repositories ... but could also do metadata and/or accept other |
| // input forms, such as p2.buildRepo. |
| repositoryDirectory = System.getProperty(ARTIFACT_REPO_DIRECTORY); |
| infoOutput("repository directory set from '" + ARTIFACT_REPO_DIRECTORY + "': " + repositoryDirectory); |
| } |
| return repositoryDirectory; |
| } |
| |
| private void infoOutput(String info) { |
| System.out.println("\t" + info); |
| } |
| |
| public void setRepositoryDirectory(String repositoryDirectory) { |
| this.repositoryDirectory = repositoryDirectory; |
| } |
| |
| public String getMirrorURLString() { |
| if (mirrorURLString == null) { |
| mirrorURLString = System.getProperty(MIRROR_URL2_PROPERTY); |
| infoOutput("mirror URL value set from '" + MIRROR_URL2_PROPERTY + "': " + mirrorURLString); |
| |
| } |
| return mirrorURLString; |
| } |
| |
| public void setMirrorURLString(String mirrorURLString) { |
| this.mirrorURLString = mirrorURLString; |
| } |
| |
| } |