Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a18f7107cb9655108c1ba6fed771b74bf632aea9 (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
/*
 * Copyright (c) 2014, 2016 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.util.internal.pde;

import org.eclipse.oomph.util.IOUtil;
import org.eclipse.oomph.util.pde.TargetPlatformUtil;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.variables.IDynamicVariable;
import org.eclipse.core.variables.IDynamicVariableResolver;
import org.eclipse.equinox.frameworkadmin.BundleInfo;
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.TargetBundle;

import org.osgi.framework.Constants;

import java.io.File;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author Eike Stepper
 */
public class TargetPlatformClasspathFile implements IDynamicVariableResolver
{
  public String resolveValue(IDynamicVariable variable, String argument) throws CoreException
  {
    try
    {
      File tempFile = File.createTempFile("pde-", ".properties");

      ITargetDefinition targetDefinition = TargetPlatformUtil.getActiveTargetDefinition();
      if (!targetDefinition.isResolved())
      {
        targetDefinition.resolve(new NullProgressMonitor());
      }

      StringBuilder builder = new StringBuilder();

      TargetBundle[] bundles = targetDefinition.getAllBundles();
      if (bundles != null)
      {
        for (TargetBundle bundle : bundles)
        {
          BundleInfo info = bundle.getBundleInfo();
          if (info != null)
          {
            if (!info.getSymbolicName().endsWith(".source"))
            {
              URI location = info.getLocation();
              if (location != null)
              {
                String scheme = location.getScheme();
                if ("file".equals(scheme))
                {
                  appendBundleClasspath(builder, new File(location.getPath()));
                }
              }
            }
          }
        }
      }

      IOUtil.writeLines(tempFile, "UTF-8", Collections.singletonList(builder.toString()));
      return tempFile.getAbsolutePath();
    }
    catch (Exception ex)
    {
      UtilPDEPlugin.INSTANCE.coreException(ex);
    }

    return null;
  }

  public static void appendBundleClasspath(StringBuilder builder, File file) throws CoreException
  {
    if (file != null)
    {
      if (file.isDirectory())
      {
        appendFolder(builder, file);
      }
      else
      {
        appendFile(builder, file);
      }
    }
  }

  @SuppressWarnings("restriction")
  private static void appendFolder(StringBuilder builder, File bundleFolder) throws CoreException
  {
    @SuppressWarnings("all")
    Map<String, String> manifest = org.eclipse.pde.internal.core.util.ManifestUtils.loadManifest(bundleFolder);
    String bundleClasspath = manifest.get(Constants.BUNDLE_CLASSPATH);
    if (bundleClasspath != null)
    {
      StringTokenizer tokenizer = new StringTokenizer(bundleClasspath, ",");
      while (tokenizer.hasMoreTokens())
      {
        String token = tokenizer.nextToken();
        if (".".equals(token))
        {
          appendFile(builder, bundleFolder);
        }
        else
        {
          appendFile(builder, new File(bundleFolder, token));
        }
      }
    }
  }

  private static void appendFile(StringBuilder builder, File file)
  {
    if (builder.length() != 0)
    {
      builder.append(File.pathSeparatorChar);
    }

    builder.append(file.getAbsolutePath());
  }
}

Back to the top