/** * Sets new cache file manager. If existing file manager was owned by the registry, * closes it. * * @param cacheBase the base location for the registry cache * @param isCacheReadOnly whether the file cache is read only */ protected void setFileManager(File cacheBase, boolean isCacheReadOnly) { if (cacheStorageManager != null) cacheStorageManager.close(); // close existing file manager first if (cacheBase != null) { cacheStorageManager = new StorageManager(cacheBase, isCacheReadOnly ? "none" : null, isCacheReadOnly); //$NON-NLS-1$ try { cacheStorageManager.open(!isCacheReadOnly); } catch (IOException e) { // Ignore the exception. The registry will be rebuilt from source. } } }
private InputStream getInputStream(String managedFiles, int openMask) throws IOException { if (useReliableFiles) { int id = getId(managedFiles); if (id == -1) return null; return new ReliableFileInputStream(new File(getBase(), managedFiles), id, openMask); } File lookup = lookup(managedFiles, false); if (lookup == null) return null; return new FileInputStream(lookup); }
public boolean isReadOnly() { return storageManager.isReadOnly(); }
private boolean update(String managedFile, String source) throws IOException { Entry entry = (Entry) table.get(managedFile); if (entry == null) add(managedFile); int newId = entry.getWriteId(); // attempt to rename the file to the next generation boolean success = move(getAbsolutePath(source), getAbsolutePath(managedFile) + '.' + newId); if (!success) { //possible the next write generation file exists? Lets determine the largest //generation number, then use that + 1. newId = findOldestGeneration(managedFile) + 1; success = move(getAbsolutePath(source), getAbsolutePath(managedFile) + '.' + newId); } if (!success) return false; // update the entry. read and write ids should be the same since // everything is in sync entry.setReadId(newId); entry.setWriteId(newId + 1); return true; }
if (readOnly) throw new IOException(Msg.fileManager_illegalInReadOnlyMode); if (!lock(true)) throw new IOException(Msg.fileManager_cannotLock); try { updateTable(); int[] originalReadIDs = new int[managedFiles.length]; boolean error = false; for (int i = 0; i < managedFiles.length; i++) { originalReadIDs[i] = getId(managedFiles[i]); if (!update(managedFiles[i], sources[i])) error = true; save(); //save only if no errors } finally { release();
private State internalReadState(StateImpl toRestore, File stateDirectory, long expectedTimestamp) throws IOException { File stateFile = new File(stateDirectory, StateReader.STATE_FILE); File lazyFile = new File(stateDirectory, StateReader.LAZY_FILE); if (!stateFile.exists() || !lazyFile.exists()) { StorageManager storageManager = new StorageManager(stateDirectory, "none", true); //$NON-NLS-1$ try { // if the directory is pointing at the configuration directory then the base files will not exist storageManager.open(true); // try using the storage manager to find the managed state files (bug 143255) File managedState = storageManager.lookup(StateReader.STATE_FILE, false); File managedLazy = storageManager.lookup(StateReader.LAZY_FILE, false); if (managedState != null && managedLazy != null) { stateFile = managedState; lazyFile = managedLazy; } } finally { storageManager.close(); } } StateReader reader = new StateReader(stateFile, lazyFile, false); if (!reader.loadState(toRestore, expectedTimestamp)) return null; return toRestore; }
if (!registryObjects.isDirty() || cacheStorageManager.isReadOnly()) { cacheStorageManager.close(); return; cacheStorageManager.lookup(TableReader.TABLE, true); cacheStorageManager.lookup(TableReader.MAIN, true); cacheStorageManager.lookup(TableReader.EXTRA, true); cacheStorageManager.lookup(TableReader.CONTRIBUTIONS, true); cacheStorageManager.lookup(TableReader.CONTRIBUTORS, true); cacheStorageManager.lookup(TableReader.NAMESPACES, true); cacheStorageManager.lookup(TableReader.ORPHANS, true); tableFile = File.createTempFile(TableReader.TABLE, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ mainFile = File.createTempFile(TableReader.MAIN, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ extraFile = File.createTempFile(TableReader.EXTRA, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ contributionsFile = File.createTempFile(TableReader.CONTRIBUTIONS, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ contributorsFile = File.createTempFile(TableReader.CONTRIBUTORS, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ namespacesFile = File.createTempFile(TableReader.NAMESPACES, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ orphansFile = File.createTempFile(TableReader.ORPHANS, ".new", cacheStorageManager.getBase()); //$NON-NLS-1$ theTableWriter.setTableFile(tableFile); theTableWriter.setExtraDataFile(extraFile); theTableWriter.setOrphansFile(orphansFile); } catch (IOException e) { cacheStorageManager.close(); return; //Ignore the exception since we can recompute the cache cacheStorageManager.update(new String[] {TableReader.TABLE, TableReader.MAIN, TableReader.EXTRA, TableReader.CONTRIBUTIONS, TableReader.CONTRIBUTORS, TableReader.NAMESPACES, TableReader.ORPHANS}, new String[] {tableFile.getName(), mainFile.getName(), extraFile.getName(), contributionsFile.getName(), contributorsFile.getName(), namespacesFile.getName(), orphansFile.getName()}); } catch (IOException e) {
private InputStream getInfoInputStream() throws IOException { StorageManager storageManager = getChildStorageManager(); InputStream storageStream = null; try { storageStream = storageManager.getInputStream(FRAMEWORK_INFO); } catch (IOException ex) { if (getConfiguration().getDebug().DEBUG_GENERAL) { Debug.println("Error reading framework.info: " + ex.getMessage()); //$NON-NLS-1$ Debug.printStackTrace(ex); } } finally { storageManager.close(); } if (storageStream == null && parentRoot != null) { StorageManager parentStorageManager = null; try { parentStorageManager = new StorageManager(parentRoot, LocationHelper.LOCKING_NONE, true); parentStorageManager.open(false); storageStream = parentStorageManager.getInputStream(FRAMEWORK_INFO); } catch (IOException e1) { // That's ok we will regenerate the framework.info } finally { if (parentStorageManager != null) { parentStorageManager.close(); } } } return storageStream; } }
protected StorageManager initFileManager(File baseDir, String lockMode, boolean readOnly) throws IOException { StorageManager sManager = new StorageManager(baseDir, lockMode, readOnly); try { sManager.open(!readOnly); } catch (IOException ex) { if (Debug.DEBUG_GENERAL) { Debug.println("Error reading framework metadata: " + ex.getMessage()); //$NON-NLS-1$ Debug.printStackTrace(ex); } String message = NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_FILEMANAGER_OPEN_ERROR, ex.getMessage()); FrameworkLogEntry logEntry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, message, 0, ex, null); adaptor.getFrameworkLog().log(logEntry); FrameworkProperties.setProperty(EclipseStarter.PROP_EXITCODE, "15"); //$NON-NLS-1$ String errorDialog = "<title>" + AdaptorMsg.ADAPTOR_STORAGE_INIT_FAILED_TITLE + "</title>" + NLS.bind(AdaptorMsg.ADAPTOR_STORAGE_INIT_FAILED_MSG, baseDir) + "\n" + ex.getMessage(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ FrameworkProperties.setProperty(EclipseStarter.PROP_EXITDATA, errorDialog); throw ex; } return sManager; }
private synchronized static void saveData(String fileName) { if (storageManager == null || storageManager.isReadOnly()) return; try { File data = storageManager.createTempFile(fileName); if (FILE_APPLOCKS.equals(fileName)) saveLocks(data); else if (FILE_APPSCHEDULED.equals(fileName)) saveSchedules(data); storageManager.lookup(fileName, true); storageManager.update(new String[] {fileName}, new String[] {data.getName()}); } catch (IOException e) { Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.ERROR, 0, NLS.bind(Messages.persistence_error_saving, fileName), 0, e, null)); } }
String target = smos.getTarget(); if (streamSet == null) { add(target, StorageManager.FILETYPE_RELIABLEFILE); update(new String[] {smos.getTarget()}, new String[] {file.getName()}); ReliableFile.fileUpdated(new File(getBase(), smos.getTarget())); String target = smos.getTarget(); if (streamSet == null) { add(target, StorageManager.FILETYPE_STANDARD); update(new String[] {target}, new String[] {smos.getOutputFile().getName()}); if (outputFile == null) { add(smos.getTarget(), StorageManager.FILETYPE_RELIABLEFILE); ReliableFileOutputStream rfos = (ReliableFileOutputStream) smos.getOutputStream(); ReliableFile.fileUpdated(new File(getBase(), smos.getTarget())); } else { add(smos.getTarget(), StorageManager.FILETYPE_STANDARD); sources[idx] = outputFile.getName(); update(targets, sources);
/** * Returns a <code>ManagedOutputStream</code> for a managed file. * Closing the ouput stream will update the storage manager with the * new content of the managed file. * * @param managedFile the name of the managed file to write. * @return a managed output stream for the managed file. * @throws IOException if an error occurs opening the managed file. */ public ManagedOutputStream getOutputStream(String managedFile) throws IOException { if (useReliableFiles) { ReliableFileOutputStream out = new ReliableFileOutputStream(new File(getBase(), managedFile)); return new ManagedOutputStream(out, this, managedFile, null); } File tmpFile = createTempFile(managedFile); return new ManagedOutputStream(new FileOutputStream(tmpFile), this, managedFile, tmpFile); }
stateManager.update(stateTmpFile, lazyTmpFile); storageManager.lookup(LocationManager.STATE_FILE, true); storageManager.lookup(LocationManager.LAZY_FILE, true); storageManager.update(new String[] {LocationManager.STATE_FILE, LocationManager.LAZY_FILE}, new String[] {stateTmpFile.getName(), lazyTmpFile.getName()}); } catch (IOException e) { adaptor.getFrameworkLog().log(new FrameworkEvent(FrameworkEvent.ERROR, context.getBundle(), e));
static void stop() { shutdown = true; stopTimer(); if (storageManager != null) { storageManager.close(); storageManager = null; } closeConfiguration(); context = null; }
private InputStream findStorageStream(String fileName) { InputStream storageStream = null; try { storageStream = storageManager.getInputStream(fileName); } catch (IOException ex) { if (Debug.DEBUG_GENERAL) { Debug.println("Error reading framework metadata: " + ex.getMessage()); //$NON-NLS-1$ Debug.printStackTrace(ex); } } if (storageStream == null) { Location currentConfiguration = LocationManager.getConfigurationLocation(); Location parentConfiguration = null; if (currentConfiguration != null && (parentConfiguration = currentConfiguration.getParentLocation()) != null) { try { File bundledataLocationDir = new File(parentConfiguration.getURL().getFile(), FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME); StorageManager newStorageManager = initFileManager(bundledataLocationDir, "none", true); //$NON-NLS-1$ storageStream = newStorageManager.getInputStream(fileName); newStorageManager.close(); } catch (MalformedURLException e1) { // This will not happen since all the URLs are derived by us // and we are GODS! } catch (IOException e1) { // That's ok we will regenerate the .bundleData } } } return storageStream; }
protected boolean checkCache() { for (int index = 0; index < strategy.getLocationsLength(); index++) { File possibleCacheLocation = strategy.getStorage(index); if (possibleCacheLocation == null) break; // bail out on the first null setFileManager(possibleCacheLocation, strategy.isCacheReadOnly(index)); if (cacheStorageManager != null) { // check this new location: File cacheFile = null; try { cacheFile = cacheStorageManager.lookup(TableReader.getTestFileName(), false); } catch (IOException e) { //Ignore the exception. The registry will be rebuilt from the xml files. } if (cacheFile != null && cacheFile.isFile()) return true; // found the appropriate location } } return false; }
void save0() throws IOException { StorageManager childStorageManager = null; DataOutputStream out = null; moduleDatabase.readLock(); try { synchronized (this.saveMonitor) { if (lastSavedTimestamp == moduleDatabase.getTimestamp()) return; childStorageManager = getChildStorageManager(); out = new DataOutputStream(new BufferedOutputStream(childStorageManager.getOutputStream(FRAMEWORK_INFO))); saveGenerations(out); savePermissionData(out); moduleDatabase.store(out, true); lastSavedTimestamp = moduleDatabase.getTimestamp(); } } finally { if (out != null) { try { out.close(); } catch (IOException e) { // tried our best } } if (childStorageManager != null) { childStorageManager.close(); } moduleDatabase.readUnlock(); } }
/** * Returns the actual file location to use when reading the given managed file. * A value of <code>null</code> can be returned if the given managed file name is not * managed and add is set to false. * <p> * The returned file should be considered read-only. Any updates to the content of this * file should be done using {@link #update(String[], String[])}. * * @param managedFile the managed file to lookup * @param add indicate whether the managed file name should be added to the manager if * it is not already managed. * @throws IOException if the add flag is set to true and the addition of the managed file failed * @return the absolute file location to use for the given managed file or * <code>null</code> if the given managed file is not managed */ public File lookup(String managedFile, boolean add) throws IOException { if (!open) throw new IOException(Msg.fileManager_notOpen); Entry entry = (Entry) table.get(managedFile); if (entry == null) { if (add) { add(managedFile); entry = (Entry) table.get(managedFile); } else { return null; } } return new File(getAbsolutePath(managedFile + '.' + entry.getReadId())); }
private StorageManager getStorageManager() { if (storageManagerClosed) try { storageManager.open(!LocationManager.getConfigurationLocation().isReadOnly()); storageManagerClosed = false; } catch (IOException e) { String message = NLS.bind(EclipseAdaptorMsg.ECLIPSE_STARTUP_FILEMANAGER_OPEN_ERROR, e.getMessage()); FrameworkLogEntry logEntry = new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, message, 0, e, null); adaptor.getFrameworkLog().log(logEntry); } return storageManager; }
/** * Add the given managed file name to the list of files managed by this manager. * * @param managedFile name of the file to manage * @throws IOException if there are any problems adding the given file name to the manager */ public void add(String managedFile) throws IOException { add(managedFile, FILETYPE_STANDARD); }