| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2010 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.internal.p2.update; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.net.URL; |
| 18 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 19 |
|
| 20 |
/* |
| 21 |
* Class which controls the reading and writing of Configuration (platform.xml) objects. |
| 22 |
* We keep a local cached copy to avoid multiple reads. When we install new features we |
| 23 |
* seem to only write out the platform.xml in the "commit" phase so we don't need to |
| 24 |
* batch the writes. |
| 25 |
*/ |
| 26 |
public class ConfigurationIO { |
| 27 |
private static CacheEntry cache = null; |
| 28 |
|
| 29 |
// class used to represent cache values |
| 30 |
static class CacheEntry { |
| 31 |
Configuration config; |
| 32 |
URL osgiInstallArea; |
| 33 |
File location; |
| 34 |
long timestamp; |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
* Return the configuration object which is represented by the given file. |
| 39 |
*/ |
| 40 |
static Configuration read(File file, URL osgiInstallArea) throws ProvisionException { |
| 41 |
// check the cached copy first |
| 42 |
if (cache != null && file.lastModified() == cache.timestamp) |
| 43 |
return cache.config; |
| 44 |
|
| 45 |
// cache miss or file is out of date, read from disk |
| 46 |
Configuration config = ConfigurationParser.parse(file, osgiInstallArea); |
| 47 |
if (config == null) |
| 48 |
return null; |
| 49 |
|
| 50 |
// successful read, store in the cache before we return |
| 51 |
cache(file, config, osgiInstallArea); |
| 52 |
return config; |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* Store the given configuration file in the local cache. |
| 57 |
*/ |
| 58 |
private static void cache(File location, Configuration config, URL osgiInstallArea) { |
| 59 |
CacheEntry entry = new CacheEntry(); |
| 60 |
entry.config = config; |
| 61 |
entry.osgiInstallArea = osgiInstallArea; |
| 62 |
entry.location = location; |
| 63 |
entry.timestamp = location.lastModified(); |
| 64 |
cache = entry; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* Save the given configuration to the file-system. |
| 69 |
*/ |
| 70 |
static void write(File location, Configuration config, URL osgiInstallArea) throws ProvisionException { |
| 71 |
// write it to disk |
| 72 |
ConfigurationWriter.save(config, location, osgiInstallArea); |
| 73 |
// save a copy in the cache |
| 74 |
cache(location, config, osgiInstallArea); |
| 75 |
} |
| 76 |
|
| 77 |
} |