Skip to main content
summaryrefslogtreecommitdiffstats
blob: d83d715f203a9533bf787e5149ebd029307c5ebd (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.update;

import java.io.File;
import java.net.URL;
import org.eclipse.equinox.p2.core.ProvisionException;

/*
 * Class which controls the reading and writing of Configuration (platform.xml) objects.
 * We keep a local cached copy to avoid multiple reads. When we install new features we
 * seem to only write out the platform.xml in the "commit" phase so we don't need to 
 * batch the writes.
 */
public class ConfigurationIO {
	private static CacheEntry cache = null;

	// class used to represent cache values
	static class CacheEntry {
		Configuration config;
		URL osgiInstallArea;
		File location;
		long timestamp;
	}

	/*
	 * Return the configuration object which is represented by the given file. 
	 */
	static Configuration read(File file, URL osgiInstallArea) throws ProvisionException {
		// check the cached copy first
		if (cache != null && file.lastModified() == cache.timestamp)
			return cache.config;

		// cache miss or file is out of date, read from disk
		Configuration config = ConfigurationParser.parse(file, osgiInstallArea);
		if (config == null)
			return null;

		// successful read, store in the cache before we return
		cache(file, config, osgiInstallArea);
		return config;
	}

	/*
	 * Store the given configuration file in the local cache.
	 */
	private static void cache(File location, Configuration config, URL osgiInstallArea) {
		CacheEntry entry = new CacheEntry();
		entry.config = config;
		entry.osgiInstallArea = osgiInstallArea;
		entry.location = location;
		entry.timestamp = location.lastModified();
		cache = entry;
	}

	/*
	 * Save the given configuration to the file-system.
	 */
	static void write(File location, Configuration config, URL osgiInstallArea) throws ProvisionException {
		// write it to disk 
		ConfigurationWriter.save(config, location, osgiInstallArea);
		// save a copy in the cache
		cache(location, config, osgiInstallArea);
	}

}

Back to the top