Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a23f78834932a7eb82bd1c3a03f2e671a4be7570 (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
/*
 * Copyright (c) 2014, 2015, 2017 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.p2.core;

import org.eclipse.oomph.p2.RepositoryType;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.repository.IRepository;
import org.eclipse.equinox.p2.repository.IRepositoryManager;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public abstract class RepositoryProviderMap<M extends IRepositoryManager<T>, R extends IRepository<T>, T, P extends RepositoryProvider<M, R, T>>
{
  private final Map<URI, P> providers = new HashMap<URI, P>();

  private final M repositoryManager;

  public RepositoryProviderMap(M repositoryManager)
  {
    this.repositoryManager = repositoryManager;
  }

  public final M getRepositoryManager()
  {
    return repositoryManager;
  }

  public final P getRepositoryProvider(URI location)
  {
    P provider = providers.get(location);
    if (provider == null)
    {
      provider = createProvider(location);
    }

    return provider;
  }

  public final synchronized R getRepository(URI location)
  {
    return getRepository(location, new NullProgressMonitor());
  }

  public final synchronized R getRepository(URI location, IProgressMonitor monitor)
  {
    P provider = getRepositoryProvider(location);
    return provider.getRepository(monitor);
  }

  public final synchronized R createRepository(URI location, String name, String type, Map<String, String> properties)
  {
    P provider = createProvider(location);
    return provider.createRepository(name, type, properties);
  }

  public final P removeAllContent(URI location, IProgressMonitor monitor)
  {
    P provider = getRepositoryProvider(location);
    provider.removeAllContent(monitor);
    return provider;
  }

  public final P dispose(URI location)
  {
    P provider = providers.remove(location);
    if (provider != null)
    {
      provider.dispose();
    }

    return provider;
  }

  public final void dispose()
  {
    for (P provider : providers.values())
    {
      provider.dispose();
    }

    providers.clear();
  }

  public abstract RepositoryType getRepositoryType();

  protected abstract P createProvider(M repositoryManager, URI location);

  private P createProvider(URI location)
  {
    dispose(location);

    P provider = createProvider(repositoryManager, location);
    providers.put(location, provider);
    return provider;
  }

  /**
   * @author Eike Stepper
   */
  public static final class Metadata
      extends RepositoryProviderMap<IMetadataRepositoryManager, IMetadataRepository, IInstallableUnit, RepositoryProvider.Metadata>
  {
    public Metadata(IMetadataRepositoryManager repositoryManager)
    {
      super(repositoryManager);
    }

    @Override
    public RepositoryType getRepositoryType()
    {
      return RepositoryType.METADATA;
    }

    @Override
    protected RepositoryProvider.Metadata createProvider(IMetadataRepositoryManager repositoryManager, URI location)
    {
      return new RepositoryProvider.Metadata(repositoryManager, location);
    }
  }

  /**
   * @author Eike Stepper
   */
  public static final class Artifact extends RepositoryProviderMap<IArtifactRepositoryManager, IArtifactRepository, IArtifactKey, RepositoryProvider.Artifact>
  {
    public Artifact(IArtifactRepositoryManager repositoryManager)
    {
      super(repositoryManager);
    }

    @Override
    public RepositoryType getRepositoryType()
    {
      return RepositoryType.ARTIFACT;
    }

    @Override
    protected RepositoryProvider.Artifact createProvider(IArtifactRepositoryManager repositoryManager, URI location)
    {
      return new RepositoryProvider.Artifact(repositoryManager, location);
    }
  }
}

Back to the top