# HG changeset patch # User stechong # Date 1280353689 18000 # Node ID ff0db4bbc1890f0f6779be90b5a1eb43b473fe22 # Parent 94cdcd7c55508ee32546528c7a1f8a8cb7d71ef7 Updated SDKManager to use SDKCacheUtils for caching SDK info. diff -r 94cdcd7c5550 -r ff0db4bbc189 core/com.nokia.carbide.cpp.sdk.core.test/src/com/nokia/carbide/cpp/sdk/core/test/TestCarbideSDKCache.java --- a/core/com.nokia.carbide.cpp.sdk.core.test/src/com/nokia/carbide/cpp/sdk/core/test/TestCarbideSDKCache.java Wed Jul 28 15:19:12 2010 -0500 +++ b/core/com.nokia.carbide.cpp.sdk.core.test/src/com/nokia/carbide/cpp/sdk/core/test/TestCarbideSDKCache.java Wed Jul 28 16:48:09 2010 -0500 @@ -1,6 +1,7 @@ package com.nokia.carbide.cpp.sdk.core.test; -import java.io.File; +import java.io.Serializable; +import java.util.HashMap; import java.util.Map; import junit.framework.TestCase; @@ -11,7 +12,9 @@ import org.osgi.framework.Version; import com.nokia.carbide.cpp.internal.api.sdk.sbsv2.SBSv2QueryUtils; +import com.nokia.carbide.cpp.internal.sdk.core.model.AbstractSDKManager; import com.nokia.carbide.cpp.internal.sdk.core.model.SDKManager; +import com.nokia.carbide.cpp.internal.sdk.core.model.SDKManagerCacheEntry; import com.nokia.carbide.cpp.sdk.core.ISymbianSDK; import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin; import com.nokia.carbide.cpp.sdk.core.SymbianSDKFactory; @@ -19,9 +22,32 @@ public class TestCarbideSDKCache extends TestCase { private class TestSDKManager extends SDKManager { - private File cacheFile = new File(System.getProperty("user.home"), CARBIDE_SDK_CACHE_FILE_NAME); - public File getCacheFile() { - return cacheFile; + + @SuppressWarnings("unchecked") + public TestSDKManager() { + Map cache = SDKCorePlugin.getCache().getCachedData(SDK_MANAGER_CACHE_KEY, Map.class, 0); + if (cache == null) { + cache = new HashMap(); + SDKCorePlugin.getCache().putCachedData(SDK_MANAGER_CACHE_KEY, (Serializable)cache, 0); + } + } + + public void clearCache() { + clearSDKCache(); + } + + @SuppressWarnings("unchecked") + public Map getCache() { + Map cache = SDKCorePlugin.getCache().getCachedData(SDK_MANAGER_CACHE_KEY, Map.class, 0); + if (cache == null) { + cache = new HashMap(); + SDKCorePlugin.getCache().putCachedData(SDK_MANAGER_CACHE_KEY, (Serializable)cache, 0); + } + return cache; + } + + public SDKManagerCacheEntry getCacheEntry(String id) { + return getSDKCacheEntry(id); } public Job getScanJob() { @@ -39,10 +65,7 @@ public void testCacheCreation() throws Exception { final TestSDKManager manager = new TestSDKManager(); - File cacheFile = manager.getCacheFile(); - if (cacheFile != null && cacheFile.exists()) { - cacheFile.delete(); - } + manager.clearCache(); IJobChangeListener listener = new IJobChangeListener() { public void sleeping(IJobChangeEvent event) { @@ -55,9 +78,8 @@ } public void done(IJobChangeEvent event) { - File cacheFile = manager.getCacheFile(); - assertNotNull(cacheFile); - assertTrue(cacheFile.exists()); + Map cache = manager.getCache(); + assertNotNull(cache); } public void awake(IJobChangeEvent event) { @@ -74,24 +96,21 @@ public void testCacheModification() throws Exception { final TestSDKManager manager = new TestSDKManager(); - long orgTime = manager.getCacheFile().lastModified(); manager.scanSDKs(); manager.getScanJob().join(); - assertTrue(manager.getCacheFile().lastModified() > orgTime); + assertTrue(manager.getSDKList().isEmpty() == manager.getCache().isEmpty()); + manager.clearCache(); + assertNull(SDKCorePlugin.getCache().getCache(AbstractSDKManager.SDK_MANAGER_CACHE_KEY)); final String sdkId = "test"; ISymbianSDK sdk = SymbianSDKFactory.createInstance(sdkId, "C:\\", new Version("9.5")); - orgTime = manager.getCacheFile().lastModified(); - waitASecond(); manager.addSDK(sdk); - assertTrue(manager.getCacheFile().lastModified() > orgTime); - orgTime = manager.getCacheFile().lastModified(); - waitASecond(); + assertFalse(manager.getCache().isEmpty()); + assertNotNull(manager.getCacheEntry(sdkId)); manager.updateSDK(sdk); - assertTrue(manager.getCacheFile().lastModified() > orgTime); - orgTime = manager.getCacheFile().lastModified(); - waitASecond(); + assertFalse(manager.getCache().isEmpty()); + assertNotNull(manager.getCacheEntry(sdkId)); manager.removeSDK(sdkId); - assertTrue(manager.getCacheFile().lastModified() > orgTime); + assertNull(manager.getCacheEntry(sdkId)); } public void testSBSv2QueryCache() throws Exception { @@ -102,22 +121,9 @@ final TestSDKManager manager = new TestSDKManager(); manager.scanSDKs(); manager.getScanJob().join(); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.ALIAS_CACHE_KEY, Map.class, 0)); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.PRODUCT_CACHE_KEY, Map.class, 0)); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.CONFIG_CACHE_KEY, Map.class, 0)); - SDKCorePlugin.getCache().flushAll(); - SBSv2QueryUtils.removeAllCachedQueries(); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.ALIAS_CACHE_KEY, Map.class, 0)); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.PRODUCT_CACHE_KEY, Map.class, 0)); - assertNotNull(SDKCorePlugin.getCache().getCachedData(SBSv2QueryUtils.CONFIG_CACHE_KEY, Map.class, 0)); - } - - private void waitASecond() { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } + assertNull(SDKCorePlugin.getCache().getCache(SBSv2QueryUtils.ALIAS_CACHE_KEY)); + assertNull(SDKCorePlugin.getCache().getCache(SBSv2QueryUtils.PRODUCT_CACHE_KEY)); + assertNull(SDKCorePlugin.getCache().getCache(SBSv2QueryUtils.CONFIG_CACHE_KEY)); } } diff -r 94cdcd7c5550 -r ff0db4bbc189 core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/sdk/core/model/AbstractSDKManager.java --- a/core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/sdk/core/model/AbstractSDKManager.java Wed Jul 28 15:19:12 2010 -0500 +++ b/core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/sdk/core/model/AbstractSDKManager.java Wed Jul 28 16:48:09 2010 -0500 @@ -13,24 +13,15 @@ package com.nokia.carbide.cpp.internal.sdk.core.model; import java.io.BufferedReader; -import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Result; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; +import java.util.Map; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -42,17 +33,12 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.IJobChangeListener; import org.eclipse.core.runtime.jobs.Job; import org.osgi.framework.Version; -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.traversal.NodeIterator; import com.nokia.carbide.cpp.internal.api.sdk.BuildPlat; import com.nokia.carbide.cpp.internal.api.sdk.ICarbideDevicesXMLChangeListener; @@ -71,12 +57,10 @@ import com.nokia.carbide.cpp.sdk.core.ISymbianSDKFeatures; import com.nokia.carbide.cpp.sdk.core.SDKCorePlugin; import com.nokia.carbide.cpp.sdk.core.SymbianSDKFactory; -import com.nokia.cpp.internal.api.utils.core.FileUtils; import com.nokia.cpp.internal.api.utils.core.ListenerList; import com.nokia.cpp.internal.api.utils.core.Logging; import com.nokia.cpp.internal.api.utils.core.PathUtils; import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; -import com.sun.org.apache.xpath.internal.XPathAPI; public abstract class AbstractSDKManager implements ISDKManager, ISDKManagerInternal { @@ -84,12 +68,7 @@ protected HashMap missingSdkMap = new HashMap(); protected Job scanJob; - protected static final String CARBIDE_SDK_CACHE_FILE_NAME = "carbideSDKCache.xml"; - protected static final String SDK_CACHE_ID_ATTRIB = "id"; - protected static final String SDK_CACHE_ENABLED_ATTRIB = "isEnabled"; - protected static final String SDK_CACHE_OS_VERSION_ATTRIB = "osVersion"; - - protected static final String SDK_CACHE_EPOCROOT_ATTRIB = "epocroot"; + public static final String SDK_MANAGER_CACHE_KEY = "sdk_manager_cache"; protected static final String EMPTY_STRING = ""; protected static boolean enableBSFScanner; @@ -325,151 +304,41 @@ abstract protected boolean doRemoveSDK(String sdkId); protected void scanCarbideSDKCache(){ - DocumentBuilder docBuilder = null; - try { - docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - } catch (ParserConfigurationException e) { - ResourcesPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SDKCorePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e)); - throw new RuntimeException(e); - } - - try { - File carbideSDKCacheFile = getCardbieSDKCacheFile(); - if (!carbideSDKCacheFile.exists()){ - try { - FileUtils.writeFileContents(carbideSDKCacheFile, EMPTY_STRING.toCharArray(), null); - } catch (CoreException e){ - e.printStackTrace(); - } - } else if (carbideSDKCacheFile.length() > 0) { - Document lastKnownDoc = docBuilder.parse(carbideSDKCacheFile); - - NodeIterator ni = XPathAPI.selectNodeIterator(lastKnownDoc, "/sdks/sdk"); - for (Node n = ni.nextNode(); n != null; n = ni.nextNode()) { - - // get the unique ID - NamedNodeMap attribs = n.getAttributes(); - String id = attribs.getNamedItem(SDK_CACHE_ID_ATTRIB).getNodeValue(); - - ISymbianSDK sdk = getSDK(id, false); - if (sdk == null) { - // unable to find ID in current SDK list, create a new entry and add it to the list - - // get whether or not the SDK is enabled - String sdkEnabled = "true"; - Node sdkEnabledItem = attribs.getNamedItem(SDK_CACHE_ENABLED_ATTRIB); - if (sdkEnabledItem != null) - sdkEnabled = sdkEnabledItem.getNodeValue(); - - // get the os version - String osVersion = ""; - Node osVersionItem = attribs.getNamedItem(SDK_CACHE_OS_VERSION_ATTRIB); - if (osVersionItem != null) - osVersion = osVersionItem.getNodeValue(); - - // get the EPOCROOT - String epocRoot = null; - Node epocrootItem = attribs.getNamedItem(SDK_CACHE_EPOCROOT_ATTRIB); - if (epocrootItem != null) - epocRoot = epocrootItem.getNodeValue(); - - sdk = SymbianSDKFactory.createInstance(id, - epocRoot, - new Version(osVersion)); - if (sdkEnabled.equalsIgnoreCase("true")){ - ((SymbianSDK)sdk).setEnabled(true); - } else { - ((SymbianSDK)sdk).setEnabled(false); - } - synchronized (sdkList) { - sdkList.add(sdk); - } - } else { - // get whether or not the SDK is enabled - String sdkEnabled = "true"; - Node sdkEnabledItem = attribs.getNamedItem(SDK_CACHE_ENABLED_ATTRIB); - if (sdkEnabledItem != null) - sdkEnabled = sdkEnabledItem.getNodeValue(); - - if (sdkEnabled.equalsIgnoreCase("true")){ - ((SymbianSDK)sdk).setEnabled(true); - } else { - ((SymbianSDK)sdk).setEnabled(false); - } - } - } // for - } - } catch (Exception e) { - e.printStackTrace(); + List idList = getSDKCacheIdList(); + for (Iterator itr = idList.iterator(); itr.hasNext();) { + String id = itr.next(); + SDKManagerCacheEntry entry = getSDKCacheEntry(id); + ISymbianSDK sdk = getSDK(id, false); + if (sdk == null) { + sdk = SymbianSDKFactory.createInstance(id, + entry.getEpocRoot(), + new Version(entry.getOsVersion())); + ((SymbianSDK)sdk).setEnabled(entry.isEnabled()); + synchronized (sdkList) { + sdkList.add(sdk); + } + } else { + ((SymbianSDK)sdk).setEnabled(entry.isEnabled()); + } } } public void updateCarbideSDKCache() { if (!Platform.isRunning()) return; - - DocumentBuilder docBuilder = null; - try { - docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - } catch (ParserConfigurationException e) { - ResourcesPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SDKCorePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e)); - return; - } - - File carbideSDKCacheFile = getCardbieSDKCacheFile(); - if (!carbideSDKCacheFile.exists()){ - try { - FileUtils.writeFileContents(carbideSDKCacheFile, EMPTY_STRING.toCharArray(), null); - } catch (CoreException e){ - e.printStackTrace(); - } - } - - Document d = docBuilder.newDocument(); - Node sdks = d.appendChild(d.createElement("sdks")); - + + clearSDKCache(); synchronized(sdkList) { for (ISymbianSDK currSDK: sdkList) { - Node sdk = sdks.appendChild(d.createElement("sdk")); - NamedNodeMap attribs = sdk.getAttributes(); - Node idNode = d.createAttribute(SDK_CACHE_ID_ATTRIB); - idNode.setNodeValue(currSDK.getUniqueId()); - attribs.setNamedItem(idNode); - - // Hide the build config from view in the build config list? - Node enabledNode = d.createAttribute(SDK_CACHE_ENABLED_ATTRIB); - if (true == currSDK.isEnabled()) { - enabledNode.setNodeValue("true"); - } else { - enabledNode.setNodeValue("false"); - } - attribs.setNamedItem(enabledNode); - - Node osVerNode = d.createAttribute(SDK_CACHE_OS_VERSION_ATTRIB); - osVerNode.setNodeValue(currSDK.getOSVersion().toString()); - attribs.setNamedItem(osVerNode); - - Node sdkEpocRootNode = d.createAttribute(SDK_CACHE_EPOCROOT_ATTRIB); - sdkEpocRootNode.setNodeValue(currSDK.getEPOCROOT()); - attribs.setNamedItem(sdkEpocRootNode); + SDKManagerCacheEntry entry = new SDKManagerCacheEntry(); + entry.setId(currSDK.getUniqueId()); + entry.setEpocRoot(currSDK.getEPOCROOT()); + entry.setOsVersion(currSDK.getOSVersion().toString()); + entry.setEnabled(currSDK.isEnabled()); + setSDKCacheEntry(entry); } } - DOMSource domSource = new DOMSource(d); - TransformerFactory transFactory = TransformerFactory.newInstance(); - Result fileResult = new StreamResult(carbideSDKCacheFile); - try { - transFactory.newTransformer().transform(domSource, fileResult); - } catch (TransformerConfigurationException e) { - ResourcesPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SDKCorePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e)); - } catch (TransformerException e) { - ResourcesPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, SDKCorePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e)); - } - } - - protected File getCardbieSDKCacheFile() { - IPath path = new Path(System.getProperty("user.home")); - return path.append(CARBIDE_SDK_CACHE_FILE_NAME).toFile(); } /** @@ -752,6 +621,46 @@ return; } - - + + protected void clearSDKCache() { + SDKCorePlugin.getCache().removeCache(SDK_MANAGER_CACHE_KEY); + } + + @SuppressWarnings("unchecked") + protected List getSDKCacheIdList() { + Map cacheMap = SDKCorePlugin.getCache().getCachedData(SDK_MANAGER_CACHE_KEY, Map.class, 0); + List idList = new ArrayList(); + + if (cacheMap != null) { + idList.addAll(cacheMap.keySet()); + } + + return idList; + } + + @SuppressWarnings("unchecked") + protected SDKManagerCacheEntry getSDKCacheEntry(String id) { + SDKManagerCacheEntry entry = null; + Map cacheMap = SDKCorePlugin.getCache().getCachedData(SDK_MANAGER_CACHE_KEY, Map.class, 0); + + if (cacheMap != null) { + entry = cacheMap.get(id); + } + + return entry; + } + + @SuppressWarnings("unchecked") + protected void setSDKCacheEntry(SDKManagerCacheEntry entry) { + String id = entry.getId(); + Map cacheMap = SDKCorePlugin.getCache().getCachedData(SDK_MANAGER_CACHE_KEY, Map.class, 0); + + if (cacheMap == null) { + cacheMap = new HashMap(); + } + + cacheMap.put(id, entry); + SDKCorePlugin.getCache().putCachedData(SDK_MANAGER_CACHE_KEY, (Serializable)cacheMap, 0); + } + } diff -r 94cdcd7c5550 -r ff0db4bbc189 core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/sdk/core/model/SDKManagerCacheEntry.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/sdk/core/model/SDKManagerCacheEntry.java Wed Jul 28 16:48:09 2010 -0500 @@ -0,0 +1,102 @@ +package com.nokia.carbide.cpp.internal.sdk.core.model; + +import java.io.Serializable; + +public class SDKManagerCacheEntry implements Serializable { + + private static final long serialVersionUID = 3694246766733614433L; + + private String id; + private String epocRoot; + private String osVersion; + private boolean isEnabled; + + public SDKManagerCacheEntry() { + this.id = ""; + this.epocRoot = ""; + this.osVersion = ""; + this.isEnabled = false; + } + + public SDKManagerCacheEntry(String id, String epocRoot, String osVersion, boolean isEnabled) { + this.id = id; + this.epocRoot = epocRoot; + this.osVersion = osVersion; + this.isEnabled = isEnabled; + } + + public String getEpocRoot() { + return epocRoot; + } + + public String getId() { + return this.id; + } + + public String getOsVersion() { + return this.osVersion; + } + + public boolean isEnabled() { + return this.isEnabled; + } + + public void setEnabled(boolean isEnabled) { + this.isEnabled = isEnabled; + } + + public void setEpocRoot(String epocRoot) { + this.epocRoot = epocRoot; + } + + public void setId(String id) { + this.id = id; + } + + public void setOsVersion(String osVersion) { + this.osVersion = osVersion; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((epocRoot == null) ? 0 : epocRoot.hashCode()); + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + (isEnabled ? 1231 : 1237); + result = prime * result + + ((osVersion == null) ? 0 : osVersion.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SDKManagerCacheEntry other = (SDKManagerCacheEntry) obj; + if (epocRoot == null) { + if (other.epocRoot != null) + return false; + } else if (!epocRoot.equals(other.epocRoot)) + return false; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (isEnabled != other.isEnabled) + return false; + if (osVersion == null) { + if (other.osVersion != null) + return false; + } else if (!osVersion.equals(other.osVersion)) + return false; + return true; + } + +} \ No newline at end of file