|
Added
Link Here
|
| 1 |
package org.eclipse.equinox.internal.p2.publisher.ant.repository; |
| 2 |
|
| 3 |
import java.net.*; |
| 4 |
import java.util.*; |
| 5 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 6 |
import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; |
| 7 |
import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; |
| 8 |
import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository; |
| 9 |
import org.eclipse.equinox.internal.p2.publisher.Activator; |
| 10 |
import org.eclipse.equinox.internal.p2.publisher.ant.TaskMessages; |
| 11 |
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; |
| 12 |
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager; |
| 13 |
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; |
| 14 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; |
| 15 |
import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; |
| 16 |
import org.eclipse.equinox.internal.provisional.p2.repository.*; |
| 17 |
import org.eclipse.osgi.util.NLS; |
| 18 |
|
| 19 |
/* |
| 20 |
* Methods copied from org.eclipse.equinox.p2.internal.repository.tools.tasks.PublisherApplication |
| 21 |
*/ |
| 22 |
public class PublisherApplication { |
| 23 |
|
| 24 |
private IArtifactRepository destinationArtifactRepository = null; |
| 25 |
private IMetadataRepository destinationMetadataRepository = null; |
| 26 |
|
| 27 |
private CompositeMetadataRepository compositeMetadataRepository = null; |
| 28 |
private CompositeArtifactRepository compositeArtifactRepository = null; |
| 29 |
|
| 30 |
private List sourceRepositories = new ArrayList(); |
| 31 |
private List destinationRepos = new ArrayList(); |
| 32 |
private List artifactReposToRemove = new ArrayList(); |
| 33 |
private List metadataReposToRemove = new ArrayList(); |
| 34 |
|
| 35 |
public void initializeRepos(IProgressMonitor progress) throws ProvisionException { |
| 36 |
IArtifactRepositoryManager artifactRepositoryManager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.getContext(), IArtifactRepositoryManager.class.getName()); |
| 37 |
IMetadataRepositoryManager metadataRepositoryManager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName()); |
| 38 |
URI curLocation = null; |
| 39 |
try { |
| 40 |
for (Iterator iter = sourceRepositories.iterator(); iter.hasNext();) { |
| 41 |
RepositoryDescriptor repo = (RepositoryDescriptor) iter.next(); |
| 42 |
curLocation = repo.getRepoLocation(); |
| 43 |
if (repo.isBoth()) { |
| 44 |
addRepository(artifactRepositoryManager, curLocation, 0, progress); |
| 45 |
addRepository(metadataRepositoryManager, curLocation, 0, progress); |
| 46 |
} else if (repo.isArtifact()) |
| 47 |
addRepository(artifactRepositoryManager, curLocation, 0, progress); |
| 48 |
else if (repo.isMetadata()) |
| 49 |
addRepository(metadataRepositoryManager, curLocation, 0, progress); |
| 50 |
else |
| 51 |
throw new ProvisionException(NLS.bind(TaskMessages.unknown_repository_type, repo.getRepoLocation())); |
| 52 |
} |
| 53 |
} catch (ProvisionException e) { |
| 54 |
if (e.getCause() instanceof MalformedURLException) { |
| 55 |
throw new ProvisionException(NLS.bind(TaskMessages.exception_invalidSource, curLocation), e); |
| 56 |
} |
| 57 |
throw e; |
| 58 |
} |
| 59 |
processDestinationRepos(artifactRepositoryManager, metadataRepositoryManager); |
| 60 |
} |
| 61 |
|
| 62 |
private void processDestinationRepos(IArtifactRepositoryManager artifactRepositoryManager, IMetadataRepositoryManager metadataRepositoryManager) throws ProvisionException { |
| 63 |
RepositoryDescriptor artifactRepoDescriptor = null; |
| 64 |
RepositoryDescriptor metadataRepoDescriptor = null; |
| 65 |
|
| 66 |
Iterator iter = destinationRepos.iterator(); |
| 67 |
while (iter.hasNext() && (artifactRepoDescriptor == null || metadataRepoDescriptor == null)) { |
| 68 |
RepositoryDescriptor repo = (RepositoryDescriptor) iter.next(); |
| 69 |
if (repo.isArtifact() && artifactRepoDescriptor == null) |
| 70 |
artifactRepoDescriptor = repo; |
| 71 |
if (repo.isMetadata() && metadataRepoDescriptor == null) |
| 72 |
metadataRepoDescriptor = repo; |
| 73 |
} |
| 74 |
|
| 75 |
if (artifactRepoDescriptor != null) |
| 76 |
destinationArtifactRepository = initializeDestination(artifactRepoDescriptor, artifactRepositoryManager); |
| 77 |
if (metadataRepoDescriptor != null) |
| 78 |
destinationMetadataRepository = initializeDestination(metadataRepoDescriptor, metadataRepositoryManager); |
| 79 |
} |
| 80 |
|
| 81 |
protected IMetadataRepository initializeDestination(RepositoryDescriptor toInit, IMetadataRepositoryManager mgr) throws ProvisionException { |
| 82 |
try { |
| 83 |
IMetadataRepository repository = addRepository(mgr, toInit.getRepoLocation(), IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, null); |
| 84 |
if (initDestinationRepository(repository, toInit)) |
| 85 |
return repository; |
| 86 |
} catch (ProvisionException e) { |
| 87 |
//fall through and create a new repository below |
| 88 |
} |
| 89 |
|
| 90 |
IMetadataRepository source = null; |
| 91 |
try { |
| 92 |
if (toInit.getFormat() != null) |
| 93 |
source = mgr.loadRepository(toInit.getFormat(), 0, null); |
| 94 |
} catch (ProvisionException e) { |
| 95 |
//Ignore. |
| 96 |
} |
| 97 |
//This code assumes source has been successfully loaded before this point |
| 98 |
//No existing repository; create a new repository at destinationLocation but with source's attributes. |
| 99 |
try { |
| 100 |
IMetadataRepository result = mgr.createRepository(toInit.getRepoLocation(), toInit.getName() != null ? toInit.getName() : (source != null ? source.getName() : toInit.getRepoLocation().toString()), IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, source != null ? source.getProperties() : null); |
| 101 |
if (toInit.isCompressed() && !result.getProperties().containsKey(IRepository.PROP_COMPRESSED)) |
| 102 |
result.setProperty(IRepository.PROP_COMPRESSED, "true"); //$NON-NLS-1$ |
| 103 |
return (IMetadataRepository) RepositoryHelper.validDestinationRepository(result); |
| 104 |
} catch (UnsupportedOperationException e) { |
| 105 |
throw new ProvisionException(NLS.bind(TaskMessages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
protected IArtifactRepository initializeDestination(RepositoryDescriptor toInit, IArtifactRepositoryManager mgr) throws ProvisionException { |
| 110 |
try { |
| 111 |
IArtifactRepository repository = addRepository(mgr, toInit.getRepoLocation(), IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, null); |
| 112 |
if (initDestinationRepository(repository, toInit)) |
| 113 |
return repository; |
| 114 |
} catch (ProvisionException e) { |
| 115 |
//fall through and create a new repository below |
| 116 |
} |
| 117 |
IArtifactRepository source = null; |
| 118 |
try { |
| 119 |
if (toInit.getFormat() != null) |
| 120 |
source = mgr.loadRepository(toInit.getFormat(), 0, null); |
| 121 |
} catch (ProvisionException e) { |
| 122 |
//Ignore. |
| 123 |
} |
| 124 |
//This code assumes source has been successfully loaded before this point |
| 125 |
//No existing repository; create a new repository at destinationLocation but with source's attributes. |
| 126 |
// TODO for now create a Simple repo by default. |
| 127 |
try { |
| 128 |
IArtifactRepository result = mgr.createRepository(toInit.getRepoLocation(), toInit.getName() != null ? toInit.getName() : (source != null ? source.getName() : toInit.getRepoLocation().toString()), IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, source != null ? source.getProperties() : null); |
| 129 |
if (toInit.isCompressed() && !result.getProperties().containsKey(IRepository.PROP_COMPRESSED)) |
| 130 |
result.setProperty(IRepository.PROP_COMPRESSED, "true"); //$NON-NLS-1$ |
| 131 |
return (IArtifactRepository) RepositoryHelper.validDestinationRepository(result); |
| 132 |
} catch (UnsupportedOperationException e) { |
| 133 |
throw new ProvisionException(NLS.bind(TaskMessages.exception_invalidDestination, toInit.getRepoLocation()), e.getCause()); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
protected boolean initDestinationRepository(IRepository repository, RepositoryDescriptor descriptor) { |
| 138 |
if (repository != null && repository.isModifiable()) { |
| 139 |
if (descriptor.getName() != null) |
| 140 |
repository.setName(descriptor.getName()); |
| 141 |
if (repository instanceof ICompositeRepository && !descriptor.isAppend()) |
| 142 |
((ICompositeRepository) repository).removeAllChildren(); |
| 143 |
else if (repository instanceof IMetadataRepository && !descriptor.isAppend()) |
| 144 |
((IMetadataRepository) repository).removeAll(); |
| 145 |
else if (repository instanceof IArtifactRepository && !descriptor.isAppend()) |
| 146 |
((IArtifactRepository) repository).removeAll(); |
| 147 |
return true; |
| 148 |
} |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
public IArtifactRepository getArtifactRepository() { |
| 153 |
return destinationArtifactRepository; |
| 154 |
} |
| 155 |
|
| 156 |
public IMetadataRepository getMetadataRepository() { |
| 157 |
return destinationMetadataRepository; |
| 158 |
} |
| 159 |
|
| 160 |
public List getContextRepositories() { |
| 161 |
return sourceRepositories; |
| 162 |
} |
| 163 |
|
| 164 |
public void addSource(RepositoryDescriptor descriptor) { |
| 165 |
sourceRepositories.add(descriptor); |
| 166 |
} |
| 167 |
|
| 168 |
public IMetadataRepository getCompositeMetadataRepository() { |
| 169 |
if (compositeMetadataRepository == null) { |
| 170 |
try { |
| 171 |
compositeMetadataRepository = new CompositeMetadataRepository(new URI("memory:/composite"), "parent metadata repo", null);//$NON-NLS-1$ //$NON-NLS-2$ |
| 172 |
} catch (URISyntaxException e) { |
| 173 |
//Can't happen |
| 174 |
} |
| 175 |
for (Iterator iter = sourceRepositories.iterator(); iter.hasNext();) { |
| 176 |
RepositoryDescriptor repo = (RepositoryDescriptor) iter.next(); |
| 177 |
if (repo.isMetadata()) |
| 178 |
compositeMetadataRepository.addChild(repo.getRepoLocation()); |
| 179 |
} |
| 180 |
} |
| 181 |
return compositeMetadataRepository; |
| 182 |
} |
| 183 |
|
| 184 |
public IArtifactRepository getCompositeArtifactRepository() { |
| 185 |
if (compositeArtifactRepository == null) { |
| 186 |
try { |
| 187 |
compositeArtifactRepository = new CompositeArtifactRepository(new URI("memory:/composite"), "parent artifact repo", null);//$NON-NLS-1$ //$NON-NLS-2$ |
| 188 |
} catch (URISyntaxException e) { |
| 189 |
//Can't happen |
| 190 |
} |
| 191 |
for (Iterator iter = sourceRepositories.iterator(); iter.hasNext();) { |
| 192 |
RepositoryDescriptor repo = (RepositoryDescriptor) iter.next(); |
| 193 |
if (repo.isArtifact()) |
| 194 |
compositeArtifactRepository.addChild(repo.getRepoLocation()); |
| 195 |
} |
| 196 |
} |
| 197 |
return compositeArtifactRepository; |
| 198 |
} |
| 199 |
|
| 200 |
//Helper to add a repository. It takes care of adding the repos to the deletion list and loading it |
| 201 |
protected IArtifactRepository addRepository(IArtifactRepositoryManager manager, URI location, int flags, IProgressMonitor monitor) throws ProvisionException { |
| 202 |
if (!manager.contains(location)) |
| 203 |
artifactReposToRemove.add(location); |
| 204 |
return manager.loadRepository(location, flags, monitor); |
| 205 |
} |
| 206 |
|
| 207 |
//Helper to add a repository. It takes care of adding the repos to the deletion list and loading it |
| 208 |
protected IMetadataRepository addRepository(IMetadataRepositoryManager manager, URI location, int flags, IProgressMonitor monitor) throws ProvisionException { |
| 209 |
if (!manager.contains(location)) |
| 210 |
metadataReposToRemove.add(location); |
| 211 |
return manager.loadRepository(location, flags, monitor); |
| 212 |
} |
| 213 |
} |