|
What this is
Other links
The source code/******************************************************************************* * Copyright (c) 2005 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package model; import java.io.File; import java.io.IOException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Calendar; import java.util.Collections; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.ui.examples.rcp.adventure.AdventurePackage; import org.eclipse.ui.examples.rcp.adventure.Catalog; import org.eclipse.ui.examples.rcp.adventure.GlobalSettings; /** * A caching adapter implementation of the catalog service. * This saves a cache of the catalog obtained from the catalog web service * as an XMI file in the plug-ins metadata area of the workspace. */ public class CachingCatalogService implements ICatalogService { private static final String FILE_NAME = "catalog.xmi"; private WebCatalogService webCatalogService; public CachingCatalogService(WebCatalogService webCatService) { this.webCatalogService = webCatService; } public Catalog getCatalog() { return (Catalog) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return getCatalog0(); } }); } private Catalog getCatalog0() { // for (int i = 0; i < 20; ++i) { // long t = System.currentTimeMillis(); // long lm = webCatalogService.getLastModified().getTimeInMillis(); // t = System.currentTimeMillis() - t; // System.out.println("t=" + t + " lm=" + lm); // } GlobalSettings settings = AdventureBuilderModel.getDefault().getGlobalSettings(); if (settings.isCacheCatalog()) { Catalog cachedCatalog = readCachedCatalog(); if (cachedCatalog != null) { if (settings.isDisconnectedMode()) { cachedCatalog.setSource("Cache (disconnected)"); return cachedCatalog; } Calendar webLastModified = webCatalogService.getLastModified(); if (webLastModified == null) { cachedCatalog.setSource("Cache (web service unreachable)"); return cachedCatalog; } if (cachedCatalog.getLastModified() == webLastModified.getTimeInMillis()) { cachedCatalog.setSource("Cache (same timestamp as Web Service)"); return cachedCatalog; } } } if (!settings.isDisconnectedMode()) { Catalog catalog = webCatalogService.getCatalog(); if (catalog != null) { if (settings.isCacheCatalog()) { cacheCatalog(catalog); } return catalog; } } return new DummyCatalogService().getCatalog(); } public void clearCache() { File cacheFile = new File(getCacheFileName()); if (cacheFile.exists()) { System.out.println("Clearing cached catalog."); cacheFile.delete(); if (cacheFile.exists()) { System.out.println("Error clearing cache. Cache file still exists."); } else { System.out.println("Cache cleared successfully."); } } else { System.out.println("No cached catalog to clear."); } } private Catalog readCachedCatalog() { long t = System.currentTimeMillis(); String cacheFileName = getCacheFileName(); if (!new File(cacheFileName).exists()) { return null; } // Create a resource set. ResourceSet resourceSet = new ResourceSetImpl(); // Register the default resource factory -- only needed for stand-alone! // resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( // Resource.Factory.Registry.DEFAULT_EXTENSION, new // XMIResourceFactoryImpl()); // Register the package -- only needed for stand-alone! AdventurePackage adventurePackage = AdventurePackage.eINSTANCE; // Get the URI of the model file. URI fileURI = URI.createFileURI(getCacheFileName()); try { // Demand load the resource for this file. Resource resource = resourceSet.getResource(fileURI, true); if (resource == null) { // TODO: Improve error handling System.out.println("Unable to get resource for cache file. Is org.eclipse.emf.ecore.xmi installed?"); return null; } if (resource.getContents().isEmpty()) { // TODO: Improve error handling System.out.println("Invalid cache file: no resources"); return null; } Object first = resource.getContents().get(0); if (!(first instanceof Catalog)) { // TODO: Improve error handling System.out.println("Invalid cache file: first resource is not a Catalog"); return null; } Catalog catalog = (Catalog) first; t = System.currentTimeMillis() - t; catalog.setTimeToRead(t); catalog.setSource("Cache"); return catalog; } catch (Exception e) { // TODO: Improve error handling System.out.println(e); return null; } } private void cacheCatalog(Catalog catalog) { // Create a resource set. ResourceSet resourceSet = new ResourceSetImpl(); // Register the default resource factory -- only needed for stand-alone! // resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( // Resource.Factory.Registry.DEFAULT_EXTENSION, new // XMIResourceFactoryImpl()); // Get the URI of the model file. URI fileURI = URI.createFileURI(getCacheFileName()); // Create a resource for this file. Resource resource = resourceSet.createResource(fileURI); if (resource == null) { // TODO: Improve error handling System.out.println("Unable to create resource for cache file. Is org.eclipse.emf.ecore.xmi installed?"); return; } // Add the catalog to the contents. resource.getContents().add(catalog); // Save the contents of the resource to the file system. try { resource.save(Collections.EMPTY_MAP); } catch (IOException e) { // TODO: Improve error handling System.out.println(e); } } private String getCacheFileName() { return AdventureBuilderModel.getDefault().getStateLocation().append( FILE_NAME).toOSString(); } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.