Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 412c606a9ab002eb711a6a40204a81c50dcd0c06 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (c) 2014-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 v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.p2.internal.core;

import org.eclipse.oomph.p2.P2Exception;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.equinox.internal.p2.engine.ISurrogateProfileHandler;
import org.eclipse.equinox.internal.p2.engine.Profile;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.index.IIndex;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
@SuppressWarnings("restriction")
public final class LazyProfile extends org.eclipse.equinox.internal.p2.engine.Profile
{
  private final LazyProfileRegistry registry;

  private final File profileDirectory;

  private SoftReference<org.eclipse.equinox.internal.p2.engine.Profile> delegate;

  private org.eclipse.equinox.internal.p2.engine.Profile parent;

  /**
   * The value for this is never actually used, but it ensures there is a strong reference so that the delegate is never garbage collection.
   */
  @SuppressWarnings("unused")
  private org.eclipse.equinox.internal.p2.engine.Profile self;

  public LazyProfile(LazyProfileRegistry registry, String profileId, File profileDirectory)
  {
    super(registry.getProvisioningAgent(), profileId, null, null);
    this.registry = registry;
    this.profileDirectory = profileDirectory;
  }

  public synchronized org.eclipse.equinox.internal.p2.engine.Profile getDelegate(boolean loadOnDemand)
  {
    if (delegate != null)
    {
      org.eclipse.equinox.internal.p2.engine.Profile referent = delegate.get();
      if (referent != null)
      {
        return referent;
      }

      delegate = null;
    }

    if (!loadOnDemand)
    {
      return null;
    }

    String profileId = getProfileId();
    org.eclipse.equinox.internal.p2.engine.Profile referent = registry.loadProfile(profileId, profileDirectory);
    if (referent == null)
    {
      throw new P2Exception("Profile '" + profileId + "' could not be loaded from " + profileDirectory);
    }

    referent.setParent(parent);
    delegate = new SoftReference<org.eclipse.equinox.internal.p2.engine.Profile>(referent);

    // Keep a strong reference to the referenced profile if this is the self profile.
    if (getProfileId().equals(registry.self))
    {
      self = referent;
    }

    return referent;
  }

  private org.eclipse.equinox.internal.p2.engine.Profile getDelegate()
  {
    return getDelegate(true);
  }

  @Override
  public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor)
  {
    return getDelegate().query(query, monitor);
  }

  @Override
  public IProfile getParentProfile()
  {
    return getDelegate().getParentProfile();
  }

  @Override
  public synchronized void setParent(org.eclipse.equinox.internal.p2.engine.Profile parent)
  {
    this.parent = parent;

    org.eclipse.equinox.internal.p2.engine.Profile delegate = getDelegate(false);
    if (delegate != null)
    {
      delegate.setParent(parent);
    }
  }

  @Override
  public boolean isRootProfile()
  {
    return getDelegate().isRootProfile();
  }

  @Override
  public void addSubProfile(String subProfileId) throws IllegalArgumentException
  {
    getDelegate().addSubProfile(subProfileId);
  }

  @Override
  public void removeSubProfile(String subProfileId) throws IllegalArgumentException
  {
    getDelegate().removeSubProfile(subProfileId);
  }

  @Override
  public boolean hasSubProfiles()
  {
    return getDelegate().hasSubProfiles();
  }

  @Override
  public List<String> getSubProfileIds()
  {
    return getDelegate().getSubProfileIds();
  }

  @Override
  public String getProperty(String key)
  {
    Profile delegate = getDelegate();

    // If we're getting the cache property and this is for an installation with a shared bundle pool...
    if (IProfile.PROP_CACHE.equals(key) && "true".equals(delegate.getProperty(org.eclipse.oomph.p2.core.Profile.PROP_PROFILE_SHARED_POOL)))
    {
      // If we're being called from org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry.updateRoamingProfile(Profile)
      StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
      if (stackTrace.length > 3 && "updateRoamingProfile".equals(stackTrace[2].getMethodName()))
      {
        // Return the value of the install folder instead.
        // This will prevent that method from trying to change the value of the cache property.
        return delegate.getProperty(IProfile.PROP_INSTALL_FOLDER);
      }
    }

    return delegate.getProperty(key);
  }

  @Override
  public String getLocalProperty(String key)
  {
    return getDelegate().getLocalProperty(key);
  }

  @Override
  public void setProperty(String key, String value)
  {
    Profile delegate = getDelegate();
    if (IProfile.PROP_CACHE.equals(key))
    {
      // If we're setting the cache property,
      // and this is a profile for an Oomph installation with a shared bundle pool for which the cache property has already been set,
      // we don't want org.eclipse.equinox.internal.p2.engine.SimpleProfileRegistry.updateRoamingProfile(Profile)
      // to change the cache location to point to the installation.
      if ("true".equals(delegate.getProperty(org.eclipse.oomph.p2.core.Profile.PROP_PROFILE_SHARED_POOL)) && delegate.getProperty(IProfile.PROP_CACHE) != null)
      {
        return;
      }
    }

    delegate.setProperty(key, value);
  }

  @Override
  public void removeProperty(String key)
  {
    getDelegate().removeProperty(key);
  }

  @Override
  public synchronized IIndex<IInstallableUnit> getIndex(String memberName)
  {
    return getDelegate().getIndex(memberName);
  }

  @Override
  public Iterator<IInstallableUnit> everything()
  {
    return getDelegate().everything();
  }

  @Override
  public Object getManagedProperty(Object client, String memberName, Object key)
  {
    return getDelegate().getManagedProperty(client, memberName, key);
  }

  @Override
  public IQueryResult<IInstallableUnit> available(IQuery<IInstallableUnit> query, IProgressMonitor monitor)
  {
    return getDelegate().available(query, monitor);
  }

  @Override
  public String getInstallableUnitProperty(IInstallableUnit iu, String key)
  {
    return getDelegate().getInstallableUnitProperty(iu, key);
  }

  @Override
  public String setInstallableUnitProperty(IInstallableUnit iu, String key, String value)
  {
    return getDelegate().setInstallableUnitProperty(iu, key, value);
  }

  @Override
  public String removeInstallableUnitProperty(IInstallableUnit iu, String key)
  {
    return getDelegate().removeInstallableUnitProperty(iu, key);
  }

  @Override
  public Map<String, String> getLocalProperties()
  {
    return getDelegate().getLocalProperties();
  }

  @Override
  public Map<String, String> getProperties()
  {
    return getDelegate().getProperties();
  }

  @Override
  public IProvisioningAgent getProvisioningAgent()
  {
    return getDelegate().getProvisioningAgent();
  }

  @Override
  public void addProperties(Map<String, String> properties)
  {
    getDelegate().addProperties(properties);
  }

  @Override
  public void addInstallableUnit(IInstallableUnit iu)
  {
    getDelegate().addInstallableUnit(iu);
  }

  @Override
  public void removeInstallableUnit(IInstallableUnit iu)
  {
    getDelegate().removeInstallableUnit(iu);
  }

  @Override
  public Map<String, String> getInstallableUnitProperties(IInstallableUnit iu)
  {
    return getDelegate().getInstallableUnitProperties(iu);
  }

  @Override
  public void clearLocalProperties()
  {
    getDelegate().clearLocalProperties();
  }

  @Override
  public boolean isChanged()
  {
    return getDelegate().isChanged();
  }

  @Override
  public void setChanged(boolean isChanged)
  {
    getDelegate().setChanged(isChanged);
  }

  @Override
  public void clearInstallableUnits()
  {
    getDelegate().clearInstallableUnits();
  }

  @Override
  public org.eclipse.equinox.internal.p2.engine.Profile snapshot()
  {
    return getDelegate().snapshot();
  }

  @Override
  public void addInstallableUnitProperties(IInstallableUnit iu, Map<String, String> properties)
  {
    getDelegate().addInstallableUnitProperties(iu, properties);
  }

  @Override
  public void clearInstallableUnitProperties(IInstallableUnit iu)
  {
    getDelegate().clearInstallableUnitProperties(iu);
  }

  @Override
  public void clearOrphanedInstallableUnitProperties()
  {
    getDelegate().clearOrphanedInstallableUnitProperties();
  }

  @Override
  public long getTimestamp()
  {
    return getDelegate().getTimestamp();
  }

  @Override
  public void setTimestamp(long millis)
  {
    getDelegate().setTimestamp(millis);
  }

  @Override
  public void setSurrogateProfileHandler(ISurrogateProfileHandler surrogateProfileHandler)
  {
    getDelegate().setSurrogateProfileHandler(surrogateProfileHandler);
  }

  @Override
  public String toString()
  {
    return getProfileId();
  }
}

Back to the top