From c8e2e87c0cf684c2f475e2d72bfb090a74255e58 Mon Sep 17 00:00:00 2001 From: Ed Merks Date: Sun, 5 Nov 2023 10:16:32 +0100 Subject: [PATCH] Store the system encoding in encoding.info https://github.com/eclipse-oomph/oomph/issues/41 --- .../oomph/p2/internal/core/AgentImpl.java | 4 +- .../p2/internal/core/AgentManagerImpl.java | 41 ++++++- .../oomph/p2/internal/core/PersistentMap.java | 17 ++- .../p2/internal/core/ProfileReferencer.java | 30 ----- .../internal/core/ProfileReferencerImpl.java | 104 ------------------ 5 files changed, 55 insertions(+), 141 deletions(-) delete mode 100644 plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencer.java delete mode 100644 plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencerImpl.java diff --git a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentImpl.java b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentImpl.java index 81347f36b..b88040c87 100644 --- a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentImpl.java +++ b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentImpl.java @@ -126,7 +126,7 @@ public AgentImpl(AgentManagerImpl agentManager, File location) this.agentManager = agentManager; this.location = location; - bundlePoolMap = new PersistentMap<>(new File(location, "pools.info")) //$NON-NLS-1$ + bundlePoolMap = new PersistentMap<>(new File(location, "pools.info"), agentManager.getCharset()) //$NON-NLS-1$ { @Override protected BundlePool createElement(String key, String extraInfo) @@ -156,7 +156,7 @@ protected void initializeFirstTime() } }; - profileMap = new PersistentMap<>(new File(location, "profiles.info")) //$NON-NLS-1$ + profileMap = new PersistentMap<>(new File(location, "profiles.info"), agentManager.getCharset()) //$NON-NLS-1$ { @Override protected Profile createElement(String profileID, String extraInfo) diff --git a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentManagerImpl.java b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentManagerImpl.java index 83adab49c..c967dcc10 100644 --- a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentManagerImpl.java +++ b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/AgentManagerImpl.java @@ -32,6 +32,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -49,6 +51,8 @@ public class AgentManagerImpl implements AgentManager public static AgentManager instance; + private final Charset charset; + private final PersistentMap agentMap; private final File defaultAgentLocation; @@ -68,11 +72,41 @@ public AgentManagerImpl(final File userHome) { defaultAgentLocation = new File(userHome, ".p2"); //$NON-NLS-1$ + File encodingFile = new File(defaultAgentLocation, "encoding.info"); //$NON-NLS-1$ + Charset charset; + try + { + charset = new PersistentMap(encodingFile, StandardCharsets.UTF_8) + { + { + load(); + } + + @Override + protected Charset createElement(String key, String extraInfo) + { + return Charset.forName(key); + } + + @Override + protected void initializeFirstTime() + { + addElement(Charset.defaultCharset().name(), null); + } + }.getElements().iterator().next(); + } + catch (Exception ex) + { + charset = Charset.defaultCharset(); + } + + this.charset = charset; + File folder = P2CorePlugin.getUserStateFolder(userHome); File infoFile = new File(folder, "agents.info"); //$NON-NLS-1$ defaultsFile = new File(folder, "defaults.info"); //$NON-NLS-1$ - agentMap = new PersistentMap(infoFile) + agentMap = new PersistentMap(infoFile, charset) { @Override protected Agent loadElement(String key, String extraInfo) @@ -142,6 +176,11 @@ private void initializeFirstTime(File location) agentMap.load(); } + public Charset getCharset() + { + return charset; + } + public PersistentMap getAgentMap() { return agentMap; diff --git a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/PersistentMap.java b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/PersistentMap.java index 6e32ef8e7..b49a89a87 100644 --- a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/PersistentMap.java +++ b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/PersistentMap.java @@ -24,6 +24,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -39,6 +40,8 @@ */ public abstract class PersistentMap { + private final Charset charset; + private final File file; private final File tempFile; @@ -47,8 +50,9 @@ public abstract class PersistentMap private final Map elements = new LinkedHashMap<>(); - public PersistentMap(File file) + public PersistentMap(File file, Charset charset) { + this.charset = charset; this.file = file; if (file != null) @@ -125,6 +129,11 @@ public final File getFile() return file; } + public final Charset getCharset() + { + return charset; + } + public final synchronized Set getElementKeys() { return new HashSet<>(elements.keySet()); @@ -219,7 +228,7 @@ private void load(KeyHandler handler) try { - infoReader = new FileReader(file); + infoReader = new FileReader(file, charset); BufferedReader bufferedReader = new BufferedReader(infoReader); String line; @@ -279,7 +288,7 @@ public void save(String addedKey, final String removedKey) } } - tempWriter = new FileWriter(tempFile); + tempWriter = new FileWriter(tempFile, charset); BufferedWriter bufferedWriter = new BufferedWriter(tempWriter); List sortedKeys = new ArrayList<>(elements.keySet()); @@ -379,7 +388,7 @@ private FileWriter lock() { try { - return new FileWriter(lockFile); + return new FileWriter(lockFile, charset); } catch (IOException ex) { diff --git a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencer.java b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencer.java deleted file mode 100644 index 4a22f7ac7..000000000 --- a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencer.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2014 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.p2.internal.core; - -import java.io.File; - -/** - * @author Eike Stepper - */ -@Deprecated -public interface ProfileReferencer -{ - public File getFile(); - - public boolean isDirectory(); - - public boolean isReferenced(String profileID); - - public void reference(String profileID); - - public void unreference(String profileID); -} diff --git a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencerImpl.java b/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencerImpl.java deleted file mode 100644 index 7d6c61481..000000000 --- a/plugins/org.eclipse.oomph.p2.core/src/org/eclipse/oomph/p2/internal/core/ProfileReferencerImpl.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.p2.internal.core; - -import org.eclipse.oomph.p2.P2Exception; - -import org.eclipse.osgi.util.NLS; - -import java.io.File; - -/** - * @author Eike Stepper - */ -@Deprecated -public class ProfileReferencerImpl extends PersistentMap implements ProfileReferencer -{ - private final boolean directory; - - public ProfileReferencerImpl(File file, boolean directory) - { - super(directory ? null : file); - this.directory = directory; - - if (file.exists()) - { - if (directory && !file.isDirectory()) - { - throw new P2Exception(NLS.bind(Messages.ProfileReferencerImpl_NotDirectory_exception, file)); - } - - if (!directory && file.isDirectory()) - { - throw new P2Exception(NLS.bind(Messages.ProfileReferencerImpl_NotFile_exception, file)); - } - } - - load(); - } - - @Override - public boolean isDirectory() - { - return directory; - } - - @Override - public boolean isReferenced(String profileID) - { - if (!getFile().exists()) - { - return false; - } - - if (!directory) - { - if (getElement(profileID) == null) - { - return false; - } - } - - return true; - } - - @Override - public void reference(String profileID) - { - File file = getFile(); - if (!file.exists()) - { - if (directory) - { - file.mkdirs(); - } - else - { - addElement(profileID, null); - } - } - } - - @Override - public void unreference(String profileID) - { - if (!directory) - { - removeElement(profileID); - } - } - - @Override - protected Boolean createElement(String key, String extraInfo) - { - return Boolean.TRUE; - } -}