Skip to content

Commit

Permalink
Remove clearPersistedFlag upon startup
Browse files Browse the repository at this point in the history
The 'old' update mechanism edited the ini file *after* the upgrade and
added the -clearPersistedState flag. The current mechanism does not need
it, hence it must be removed if present
  • Loading branch information
buchen committed Jan 11, 2016
1 parent dd84912 commit e8c8c40
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions name.abuchen.portfolio.bootstrap/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bundle-SymbolicName: name.abuchen.portfolio.bootstrap;singleton:=true
Bundle-Version: 0.21.2.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.inject;version="1.0.0",
org.eclipse.core.runtime,
org.eclipse.core.runtime.preferences,
org.eclipse.e4.core.contexts,
org.eclipse.e4.core.di.annotations,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package name.abuchen.portfolio.bootstrap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IniFileManipulator
{
private static final String CLEAR_PERSISTED_STATE = "-clearPersistedState"; //$NON-NLS-1$

private List<String> lines = new ArrayList<String>();
private boolean isDirty = false;

public void load() throws IOException
{
lines = readAllLines(getIniFile());
}

public void save() throws IOException
{
write(getIniFile(), lines);
isDirty = false;
}

public File getIniFile()
{
String eclipseLauncher = System.getProperty("eclipse.launcher"); //$NON-NLS-1$

File path = new File(eclipseLauncher);

String executable = path.getName().toString();
int p = executable.lastIndexOf('.');
String iniFileName = (p > 0 ? executable.substring(0, p) : executable) + ".ini"; //$NON-NLS-1$

return new File(path.getParentFile(), iniFileName);
}

public void unsetClearPersistedState()
{
Iterator<String> iterator = lines.iterator();
while (iterator.hasNext())
{
String line = iterator.next();
if (line.trim().equals(CLEAR_PERSISTED_STATE))
{
iterator.remove();
isDirty = true;
return;
}
}
}

public boolean isDirty()
{
return isDirty;
}

private void write(File file, List<String> lines) throws IOException
{
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file), Charset.defaultCharset()));
try
{
for (String line : lines)
{
out.write(line);
out.write('\n');
}
}
finally
{
out.close();
}
}

private List<String> readAllLines(File file) throws IOException
{
List<String> answer = new ArrayList<String>();

FileInputStream in = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));

try
{
String line = null;
while ((line = reader.readLine()) != null)
answer.add(line);
}
finally
{
reader.close();
}

return answer;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package name.abuchen.portfolio.bootstrap;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.extensions.Preference;
Expand Down Expand Up @@ -44,6 +46,7 @@ public class LifeCycleManager
public void doPostContextCreate(IEclipseContext context)
{
checkForJava8();
removeClearPersistedStateFlag();
checkForModelChanges();
setupEventLoopAdvisor(context);
}
Expand All @@ -63,6 +66,31 @@ private void checkForJava8()
}
}

private void removeClearPersistedStateFlag()
{
// the 'old' update mechanism edited the ini file *after* the upgrade
// and added the -clearPersistedState flag. The current mechanism does
// not need it, hence it must be remove if present

// not applicable on Mac OS X because only update is not supported
if (Platform.OS_MACOSX.equals(Platform.getOS()))
return;

try
{
IniFileManipulator iniFile = new IniFileManipulator();
iniFile.load();
iniFile.unsetClearPersistedState();
if (iniFile.isDirty())
iniFile.save();
}
catch (IOException ignore)
{
// ignore: in production, it will anyway be removed during the next
// update; in development, it will annoy to always report this error
}
}

private void checkForModelChanges()
{
Version modelVersion = Version.parseVersion(preferences.get(MODEL_VERSION, null));
Expand Down

0 comments on commit e8c8c40

Please sign in to comment.