Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e6b7aee2a802063d1f3b79190e15145a992c598 (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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, 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.emf.cdo.releng.version;

import org.eclipse.emf.cdo.releng.version.Release.Element;
import org.eclipse.emf.cdo.releng.version.Release.Element.Type;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.IModel;
import org.eclipse.pde.core.plugin.IPluginModelBase;

import org.osgi.framework.Version;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.WeakHashMap;

/**
 * @author Eike Stepper
 */
public class ReleaseManager
{
  public static final ReleaseManager INSTANCE = new ReleaseManager();

  private Map<Release, Long> releases = new WeakHashMap<Release, Long>();

  private SAXParserFactory parserFactory;

  private ReleaseManager()
  {
  }

  private SAXParser getParser() throws ParserConfigurationException, SAXException
  {
    if (parserFactory == null)
    {
      parserFactory = SAXParserFactory.newInstance();
    }

    return parserFactory.newSAXParser();
  }

  public synchronized Release getRelease(IFile file) throws CoreException
  {
    try
    {
      for (Entry<Release, Long> entry : releases.entrySet())
      {
        Release release = entry.getKey();
        if (release.getFile().equals(file))
        {
          long timeStamp = entry.getValue();
          if (file.getLocalTimeStamp() == timeStamp)
          {
            return release;
          }

          releases.remove(release);
          break;
        }
      }

      if (!file.exists())
      {
        throw new FileNotFoundException(file.getFullPath().toString());
      }

      Release release = new Release(getParser(), file);
      releases.put(release, file.getLocalTimeStamp());
      return release;
    }
    catch (CoreException ex)
    {
      throw ex;
    }
    catch (Exception ex)
    {
      throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getLocalizedMessage(), ex));
    }
  }

  public synchronized Release createRelease(IFile file) throws CoreException, IOException
  {
    Release release = new Release(file);
    String path = file.getFullPath().toString();

    for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects())
    {
      if (project.isOpen())
      {
        IProjectDescription desc = project.getDescription();
        ICommand[] commands = desc.getBuildSpec();

        for (int i = 0; i < commands.length; ++i)
        {
          if (commands[i].getBuilderName().equals(VersionBuilder.BUILDER_ID))
          {
            Map<String, String> arguments = commands[i].getArguments();
            if (arguments != null)
            {
              String releasePath = arguments.get(VersionBuilder.RELEASE_PATH_ARGUMENT);
              if (path.equals(releasePath))
              {
                IModel componentModel = VersionBuilder.getComponentModel(project);
                Element element = createElement(componentModel, true);
                release.getElements().put(element, element);
              }
            }
          }
        }
      }
    }

    release.write();
    releases.put(release, file.getLocalTimeStamp());
    return release;
  }

  public Element createElement(IModel componentModel) throws CoreException
  {
    return createElement(componentModel, false);
  }

  private Element createElement(IModel componentModel, boolean withFeatureContent) throws CoreException
  {
    if (componentModel instanceof IPluginModelBase)
    {
      IPluginModelBase pluginModel = (IPluginModelBase)componentModel;
      BundleDescription description = pluginModel.getBundleDescription();

      String name = description.getSymbolicName();
      Version version = description.getVersion();
      return new Element(Type.PLUGIN, name, version);
    }

    return createFeatureElement(componentModel, withFeatureContent);
  }

  @SuppressWarnings("restriction")
  private Element createFeatureElement(IModel componentModel, boolean withContent)
  {
    org.eclipse.pde.internal.core.ifeature.IFeatureModel featureModel = (org.eclipse.pde.internal.core.ifeature.IFeatureModel)componentModel;
    org.eclipse.pde.internal.core.ifeature.IFeature feature = featureModel.getFeature();

    String name = feature.getId();
    Version version = new Version(feature.getVersion());
    Element element = new Element(Type.FEATURE, name, version);

    if (withContent)
    {
      for (org.eclipse.pde.internal.core.ifeature.IFeatureChild versionable : feature.getIncludedFeatures())
      {
        Element child = new Element(Element.Type.FEATURE, versionable.getId(), versionable.getVersion());
        element.getContent().add(child);
      }

      for (org.eclipse.pde.internal.core.ifeature.IFeaturePlugin versionable : feature.getPlugins())
      {
        Element child = new Element(Element.Type.PLUGIN, versionable.getId(), versionable.getVersion());
        element.getContent().add(child);
      }
    }

    return element;
  }
}

Back to the top