summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorAndreas Sewe2012-10-19 12:24:27 (EDT)
committer Andreas Sewe2012-10-30 07:12:39 (EDT)
commite45a83a98407ad3bad4906fb46422e7862ef76c2 (patch)
treeedf836a8afc5ccdf956faab054e2477742e36101
parentd54022c7184e757cc74c9c43353c0ebc531c3d93 (diff)
downloadorg.eclipse.recommenders-e45a83a98407ad3bad4906fb46422e7862ef76c2.zip
org.eclipse.recommenders-e45a83a98407ad3bad4906fb46422e7862ef76c2.tar.gz
org.eclipse.recommenders-e45a83a98407ad3bad4906fb46422e7862ef76c2.tar.bz2
[misc] Fixed bug #391849 by gracefully ignoring malformed JSON files.refs/changes/05/8305/2
Also, JSON files are now assumed to be UTF-8 encoded (previously undefined). Change-Id: I5fbd22f3c14f8998c55ddceb961a99382a353d27
-rw-r--r--plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/models/store/DefaultModelArchiveStore.java14
-rw-r--r--plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/Throws.java8
-rw-r--r--plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/gson/GsonUtil.java82
-rw-r--r--tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/models/DefaultModelArchiveStoreTest.java12
4 files changed, 83 insertions, 33 deletions
diff --git a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/models/store/DefaultModelArchiveStore.java b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/models/store/DefaultModelArchiveStore.java
index 6dc6ad3..2d7be13 100644
--- a/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/models/store/DefaultModelArchiveStore.java
+++ b/plugins/org.eclipse.recommenders.rcp/src/org/eclipse/recommenders/internal/rcp/models/store/DefaultModelArchiveStore.java
@@ -34,6 +34,7 @@ import org.eclipse.recommenders.internal.rcp.models.ModelArchiveMetadata.ModelAr
import org.eclipse.recommenders.internal.rcp.repo.RepositoryUtils;
import org.eclipse.recommenders.internal.rcp.wiring.RecommendersModule.AutoCloseOnWorkbenchShutdown;
import org.eclipse.recommenders.rcp.repo.IModelRepository;
+import org.eclipse.recommenders.utils.annotations.Testing;
import org.eclipse.recommenders.utils.gson.GsonUtil;
import org.eclipse.recommenders.utils.rcp.JdtUtils;
import org.slf4j.Logger;
@@ -72,8 +73,11 @@ public class DefaultModelArchiveStore<K extends IMember, V> implements Closeable
public void open() {
if (store.exists()) {
- mappings = GsonUtil.deserialize(store, new TypeToken<Map<File, ModelArchiveMetadata<K, V>>>() {
- }.getType());
+ try {
+ mappings = deserializeMapping();
+ } catch (final Exception e) {
+ log.warn("Loading mapping from '" + store + "' failed with exception.", e);
+ }
}
// GsonUtil may return null if the file exists but is empty
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=387837
@@ -82,6 +86,12 @@ public class DefaultModelArchiveStore<K extends IMember, V> implements Closeable
}
}
+ @Testing
+ protected Map<File, ModelArchiveMetadata<K, V>> deserializeMapping() {
+ return GsonUtil.deserialize(store, new TypeToken<Map<File, ModelArchiveMetadata<K, V>>>() {
+ }.getType());
+ }
+
@Override
public Optional<V> aquireModel(final K key) {
try {
diff --git a/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/Throws.java b/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/Throws.java
index 3b609df..aa9766b 100644
--- a/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/Throws.java
+++ b/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/Throws.java
@@ -43,14 +43,14 @@ public class Throws {
throw new IllegalStateException(formattedMessage);
}
- public static IllegalStateException throwUnhandledException(final Exception e) {
- throw new IllegalStateException(e);
+ public static RuntimeException throwUnhandledException(final Exception cause) {
+ throw new RuntimeException(cause);
}
- public static IllegalStateException throwUnhandledException(final Exception cause, final String msg,
+ public static RuntimeException throwUnhandledException(final Exception cause, final String msg,
final Object... args) {
final String formattedMessage = format(msg, args);
- throw new IllegalStateException(formattedMessage, cause);
+ throw new RuntimeException(formattedMessage, cause);
}
public static IllegalStateException throwUnreachable() {
diff --git a/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/gson/GsonUtil.java b/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/gson/GsonUtil.java
index 194aaad..bee5ec9 100644
--- a/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/gson/GsonUtil.java
+++ b/plugins/org.eclipse.recommenders.utils/src/org/eclipse/recommenders/utils/gson/GsonUtil.java
@@ -14,14 +14,16 @@ import static org.eclipse.recommenders.utils.Checks.ensureIsNotNull;
import static org.eclipse.recommenders.utils.Throws.throwUnhandledException;
import java.io.BufferedInputStream;
-import java.io.BufferedWriter;
+import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.Date;
@@ -30,7 +32,6 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.eclipse.recommenders.utils.IOUtils;
-import org.eclipse.recommenders.utils.Throws;
import org.eclipse.recommenders.utils.names.IFieldName;
import org.eclipse.recommenders.utils.names.IMethodName;
import org.eclipse.recommenders.utils.names.ITypeName;
@@ -88,28 +89,39 @@ public class GsonUtil {
public static <T> T deserialize(final String json, final Type classOfT) {
ensureIsNotNull(json);
ensureIsNotNull(classOfT);
- return getInstance().fromJson(json, classOfT);
+ try {
+ return getInstance().fromJson(json, classOfT);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
+ }
}
public static <T> T deserialize(final InputStream jsonStream, final Type classOfT) {
ensureIsNotNull(jsonStream);
ensureIsNotNull(classOfT);
- final InputStreamReader reader = new InputStreamReader(jsonStream);
- final T res = getInstance().fromJson(reader, classOfT);
- IOUtils.closeQuietly(reader);
- return res;
+ Reader reader = null;
+ try {
+ reader = new InputStreamReader(jsonStream, "UTF-8");
+ return getInstance().fromJson(reader, classOfT);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
+ } finally {
+ IOUtils.closeQuietly(reader);
+ }
}
public static <T> T deserialize(final File jsonFile, final Type classOfT) {
ensureIsNotNull(jsonFile);
ensureIsNotNull(classOfT);
- InputStream fis;
+ InputStream in = null;
try {
- fis = new BufferedInputStream(new FileInputStream(jsonFile));
- } catch (final FileNotFoundException e) {
- throw Throws.throwUnhandledException(e, "Unable to deserialize from file " + jsonFile.getAbsolutePath());
+ in = new BufferedInputStream(new FileInputStream(jsonFile));
+ return deserialize(in, classOfT);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
+ } finally {
+ IOUtils.closeQuietly(in);
}
- return deserialize(fis, classOfT);
}
public static String serialize(final Object obj) {
@@ -122,20 +134,38 @@ public class GsonUtil {
public static void serialize(final Object obj, final Appendable writer) {
ensureIsNotNull(obj);
ensureIsNotNull(writer);
- getInstance().toJson(obj, writer);
+ try {
+ getInstance().toJson(obj, writer);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
+ }
}
- public static void serialize(final Object obj, final File dest) {
- ensureIsNotNull(obj, "object to serialize cannot be null");
- ensureIsNotNull(dest, "serialization destination cannot be null");
- Writer fw = null;
+ public static void serialize(final Object obj, final File jsonFile) {
+ ensureIsNotNull(obj);
+ ensureIsNotNull(jsonFile);
+ OutputStream out = null;
+ try {
+ out = new BufferedOutputStream(new FileOutputStream(jsonFile));
+ serialize(obj, out);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
+ public static void serialize(final Object obj, final OutputStream out) {
+ ensureIsNotNull(obj);
+ ensureIsNotNull(out);
+ Writer writer = null;
try {
- fw = new BufferedWriter(new FileWriter(dest));
- getInstance().toJson(obj, fw);
- } catch (final IOException x) {
- throwUnhandledException(x);
+ writer = new OutputStreamWriter(out, "UTF-8");
+ getInstance().toJson(obj, writer);
+ } catch (final Exception e) {
+ throw throwUnhandledException(e);
} finally {
- IOUtils.closeQuietly(fw);
+ IOUtils.closeQuietly(writer);
}
}
@@ -149,9 +179,7 @@ public class GsonUtil {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
- final InputStreamReader reader = new InputStreamReader(zis);
- final T data = getInstance().fromJson(reader, classOfT);
- res.add(data);
+ res.add(GsonUtil.<T>deserialize(zis, classOfT));
}
}
} finally {
diff --git a/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/models/DefaultModelArchiveStoreTest.java b/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/models/DefaultModelArchiveStoreTest.java
index a89a370..b910f7a 100644
--- a/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/models/DefaultModelArchiveStoreTest.java
+++ b/tests/org.eclipse.recommenders.tests.rcp/src/org/eclipse/recommenders/tests/rcp/models/DefaultModelArchiveStoreTest.java
@@ -12,6 +12,7 @@ import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
+import java.util.Map;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragmentRoot;
@@ -91,6 +92,17 @@ public class DefaultModelArchiveStoreTest {
}
@Test
+ public void testUnparsebleStateFile() {
+ sut = new DefaultModelArchiveStore<IType, Object>(state, "some", repository, factory) {
+ @Override
+ protected Map<File, ModelArchiveMetadata<IType, Object>> deserializeMapping() {
+ throw new RuntimeException();
+ }
+ };
+ assertTrue(sut.getMetadata().isEmpty());
+ }
+
+ @Test
public void testResolutionSkippedIfAlreadyTriggeredBefore() {
meta.setStatus(ModelArchiveResolutionStatus.UNRESOLVED);
meta.setCoordinate("some:some:2.0.0");