Bug 545788 - Add a check for cache version

A cache version is needed incase the format
of the cache needs to change in the future.

Change-Id: If5cf8d7efde1b0bee1381a570191c0d38627b1bd
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Activator.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Activator.java
index 446523d..67518dc 100644
--- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Activator.java
+++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Activator.java
@@ -231,7 +231,7 @@
 			try {
 				service.load(bundleCtx, logService, mtpTracker);
 			} catch (IOException e) {
-				logService.log(LogTracker.LOG_ERROR, "Error loading cached metatype info.", e); //$NON-NLS-1$
+				logService.log(LogTracker.LOG_WARNING, "Error loading cached metatype info.", e); //$NON-NLS-1$
 			}
 			ServiceRegistration<?> registration = bundleCtx.registerService(new String[] {MetaTypeService.class.getName(), EquinoxMetaTypeService.class.getName()}, service, properties);
 			synchronized (this) {
@@ -260,7 +260,7 @@
 			try {
 				service.save(bundleCtx);
 			} catch (IOException e) {
-				logService.log(LogTracker.LOG_ERROR, "Error saving metatype cache.", e); //$NON-NLS-1$
+				logService.log(LogTracker.LOG_WARNING, "Error saving metatype cache.", e); //$NON-NLS-1$
 			}
 		}
 	}
diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeServiceImpl.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeServiceImpl.java
index a87275e..31f56fd 100644
--- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeServiceImpl.java
+++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/MetaTypeServiceImpl.java
@@ -145,7 +145,10 @@
 		BundleContext systemContext = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getBundleContext();
 		if (cache.isFile()) {
 			try (Reader reader = new Reader(new DataInputStream(new BufferedInputStream(new FileInputStream(cache))))) {
-
+				if (!reader.isValidPersistenceVersion()) {
+					logger.log(LogTracker.LOG_INFO, "Metatype cache version is not supported.  Ignoring cache."); //$NON-NLS-1$
+					return;
+				}
 				int numService = reader.readInt();
 				for (int i = 0; i < numService; i++) {
 					long id = reader.readLong();
@@ -171,6 +174,7 @@
 	void save(BundleContext context) throws IOException {
 		File cache = context.getDataFile(CACHE_FILE);
 		try (Writer writer = new Writer(new DataOutputStream(new BufferedOutputStream(new FileOutputStream(cache))))) {
+			writer.writePersistenceVersion();
 			List<MetaTypeInformation> serviceInfos = new ArrayList<>();
 			List<MetaTypeInformationImpl> xmlInfos = new ArrayList<>();
 			synchronized (_mtps) {
diff --git a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Persistence.java b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Persistence.java
index da3212b..a0e6ea2 100644
--- a/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Persistence.java
+++ b/bundles/org.eclipse.equinox.metatype/src/org/eclipse/equinox/metatype/impl/Persistence.java
@@ -18,7 +18,7 @@
  *******************************************************************************/
 
 public class Persistence {
-
+	private static final int PERSISTENCE_VERSION = 0;
 	private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
 	private static final byte NULL = 0;
 	private static final byte OBJECT = 1;
@@ -36,6 +36,10 @@
 			this.in = in;
 		}
 
+		public boolean isValidPersistenceVersion() throws IOException {
+			return in.readInt() == PERSISTENCE_VERSION;
+		}
+
 		public void readIndexedStrings() throws IOException {
 			int num = in.readInt();
 			for (int i = 0; i < num; i++) {
@@ -117,6 +121,10 @@
 			this.out = out;
 		}
 
+		public void writePersistenceVersion() throws IOException {
+			out.writeInt(PERSISTENCE_VERSION);
+		}
+
 		public void writeIndexedStrings(Set<String> strings) throws IOException {
 			strings.remove(null); // do not index null
 			out.writeInt(strings.size());