Skip to main content
summaryrefslogtreecommitdiffstats
blob: c7bd0f6b7def7f77b36077c2cb51a4566143927c (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
/**
 * Copyright (c) 2004 - 2011 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.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;

import org.osgi.framework.BundleContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Eike Stepper
 */
public class Activator extends Plugin
{
  public static final String PLUGIN_ID = "org.eclipse.emf.cdo.releng.version";

  private static Activator plugin;

  private Map<String, BuildState> buildStates;

  private File stateFile;

  public Activator()
  {
  }

  @Override
  public void start(BundleContext context) throws Exception
  {
    super.start(context);
    plugin = this;

    try
    {
      File stateFolder = Platform.getStateLocation(getBundle()).toFile();
      stateFile = new File(stateFolder, "buildStates.bin");
      if (stateFile.exists())
      {
        loadBuildStates();
        stateFile.delete(); // Future indication for possible workspace crash
      }
    }
    finally
    {
      if (buildStates == null)
      {
        buildStates = new HashMap<String, BuildState>();
      }

    }
  }

  @Override
  public void stop(BundleContext context) throws Exception
  {
    if (!buildStates.isEmpty())
    {
      saveBuildStates();
    }

    stateFile = null;
    plugin = null;
    super.stop(context);
  }

  private void loadBuildStates()
  {
    ObjectInputStream stream = null;

    try
    {
      stream = new ObjectInputStream(new FileInputStream(stateFile));
      @SuppressWarnings("unchecked")
      Map<String, BuildState> object = (Map<String, BuildState>)stream.readObject();
      buildStates = object;
    }
    catch (Exception ex)
    {
      log(ex);
    }
    finally
    {
      if (stream != null)
      {
        try
        {
          stream.close();
        }
        catch (Exception ex)
        {
          log(ex);
        }
      }
    }
  }

  private void saveBuildStates()
  {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (Iterator<Entry<String, BuildState>> it = buildStates.entrySet().iterator(); it.hasNext();)
    {
      Entry<String, BuildState> entry = it.next();
      IProject project = root.getProject(entry.getKey());
      if (!project.exists())
      {
        it.remove();
      }
    }

    ObjectOutputStream stream = null;

    try
    {
      stream = new ObjectOutputStream(new FileOutputStream(stateFile));
      stream.writeObject(buildStates);
    }
    catch (Exception ex)
    {
      log(ex);
    }
    finally
    {
      if (stream != null)
      {
        try
        {
          stream.close();
        }
        catch (Exception ex)
        {
          log(ex);
        }
      }
    }
  }

  public static BuildState getBuildState(IProject project)
  {
    String name = project.getName();
    BuildState buildState = plugin.buildStates.get(name);
    if (buildState == null)
    {
      buildState = new BuildState();
      plugin.buildStates.put(name, buildState);
    }

    return buildState;
  }

  public static void log(String message)
  {
    plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message));
  }

  public static void log(IStatus status)
  {
    plugin.getLog().log(status);
  }

  public static String log(Throwable t)
  {
    IStatus status = getStatus(t);
    log(status);
    return status.getMessage();
  }

  public static IStatus getStatus(Throwable t)
  {
    if (t instanceof CoreException)
    {
      CoreException coreException = (CoreException)t;
      return coreException.getStatus();
    }

    String msg = t.getLocalizedMessage();
    if (msg == null || msg.length() == 0)
    {
      msg = t.getClass().getName();
    }

    return new Status(IStatus.ERROR, PLUGIN_ID, msg, t);
  }
}

Back to the top