@Transactional(readOnly = false) public Object doWork(Session session, ServiceFactory sf) { return addOrReplace(session, getSqlAction(), sf, repoFile, old); } });
public Long call() { Long id = scripts.findInDb(scriptPath, true); if (id == null) { return -1L; } else { return id; } }
public ScriptRepositoryI(ObjectAdapter oa, Registry reg, Executor ex, Principal p, ScriptRepoHelper helper, ReadOnlyStatus readOnly, PublicRepositoryI servant, Set<String> scriptRepoUuids) { super(oa, reg, ex, p, helper.getScriptDir(), readOnly, servant); this.helper = helper; scriptRepoUuids.add(helper.getUuid()); }
protected OriginalFile addOrReplace(Session session, SqlAction sqlAction, ServiceFactory sf, final RepoFile repoFile, final Long old) { if (old != null) { unregister(old, sqlAction); log.info("Unregistered " + old); } OriginalFile ofile = new OriginalFile(); return update(session, repoFile, sqlAction, sf, ofile); }
public boolean delete(long id) { final OriginalFile file = load(id, true); if (file == null) { return false; } simpleDelete(null, ex, p, id); FileUtils.deleteQuietly(new File(dir, file.getPath() + file.getName())); return true; }
protected List<OriginalFile> innerWork(Session session, ServiceFactory sf, boolean modificationCheck, String mimetype) { final Iterator<File> it = iterate(); final List<OriginalFile> rv = new ArrayList<OriginalFile>(); SqlAction sqlAction = getSqlAction(); f = it.next(); file = new RepoFile(dir, f); Long id = findInDb(sqlAction, file, false); // non-scripts count String hash = null; OriginalFile ofile = null; continue; } else { ofile = addOrReplace(session, sqlAction, sf, file, null); ofile = load(id, session, sqlAction, true); // checks for type & repo if (ofile == null) { continue; // wrong type or similar continue; } else { ofile = addOrReplace(session, sqlAction, sf, file, id); removeMissingFilesFromDb(sqlAction, session, list); return rv;
scripts.setMimetype(file); file = updateFile(file, __current); OriginalFile official = scripts.load(file.getId(), true); if (official != null) { String fullname = official.getPath() + official.getName(); RepoFile f = scripts.write(fullname, scriptText); file = scripts.update(f, file.getId(), __current.ctx); } else { file = writeContent(file, scriptText, __current);
public Long call() throws Exception { EventContext ec = factory.getEventContext(); if ( ! ec.isCurrentUserAdmin() ) { throw new omero.SecurityViolation(null, null, "User is not an administrator"); } try { // ticket:2356 - should only overwrite non-scripts Long scriptID = scripts.findInDb(path, true); Long fileID = scripts.findInDb(path, false); if (scriptID != null) { throw new ApiUsageException(null, null, "Path already exists: " + path + "\n" + "Use editScript to modify existing official scripts."); } else if (fileID != null) { log.info("Overwriting existing non-script: " + fileID); cache.removeParams(fileID); } RepoFile f = scripts.write(path, scriptText); OriginalFile file = scripts.addOrReplace(f, fileID); if (!scripts.isInert(file)) { validateParams(__current, file); } return file.getId(); } catch (IOException e) { omero.ServerError se = new omero.InternalException(null, null, "Cannot write " + path); IceMapper.fillServerError(se, e); throw se; } } });
/** * Checks if */ public void modificationCheck() { loadAll(true); }
private OriginalFile update(Session session, final RepoFile repoFile, SqlAction sqlAction, ServiceFactory sf, OriginalFile ofile) { ExperimenterGroup group = loadUserGroup(session); ChecksumAlgorithm hasher = loadChecksum(session, repoFile.hasher().getValue()); ofile.setPath(repoFile.dirname()); ofile.setName(fileRepoSecretKey + repoFile.basename()); ofile.setRepo(uuid); ofile.setHasher(hasher); ofile.setHash(repoFile.hash()); ofile.setSize(repoFile.length()); ofile.getDetails().setGroup(group); setMimetype(ofile); ofile = sf.getUpdateService().saveAndReturnObject(ofile); final String name = ofile.getName(); if (name.startsWith(fileRepoSecretKey)) { ofile.setName(name.substring(fileRepoSecretKey.length())); } return ofile; }
/** * Method to delete the original file * * @param file * the original file. * */ private void deleteOriginalFile(final OriginalFile file, final Ice.Current current) throws ServerError { if (file == null) { return; } if (scripts.delete(file.getId())) { return; } scripts.simpleDelete(current.ctx, factory.executor, factory.principal, file.getId()); }
/** * Given the current files on disk, {@link #unregister(Long, SqlAction)} * all files which have been removed from disk. */ public long removeMissingFilesFromDb(SqlAction sqlAction, Session session, List<OriginalFile> filesOnDisk) { List<Long> idsInDb = idsInDb(sqlAction); if (idsInDb.size() != filesOnDisk.size()) { log.info(String.format( "Script missing from disk: %s in db, %s on disk!", idsInDb.size(), filesOnDisk.size())); } Set<Long> setInDb = new HashSet<Long>(); Set<Long> setOnDisk = new HashSet<Long>(); setInDb.addAll(idsInDb); for (OriginalFile f : filesOnDisk) { setOnDisk.add(f.getId()); } // Now contains only those which are missing setInDb.removeAll(setOnDisk); for (Long l : setInDb) { if (readOnly.isReadOnlyDb()) { log.info("read-only database so ignoring missing script ID {}", l); } else { unregister(l, sqlAction); } } return setInDb.size(); }
private String loadText(final OriginalFile file, final Ice.Current current) throws ServerError { if (scripts.isInRepo(file.getId())) { try { return scripts.read(file.getPath() + file.getName()); } catch (IOException e) { omero.ResourceError re = new omero.ResourceError(null, null, "Failed to load " + file);
@Transactional(readOnly = true) public Object doWork(Session session, ServiceFactory sf) { return load(id, session, getSqlAction(), check); } });
@Transactional(readOnly = true) public Object doWork(SqlAction sql) { return isInRepo(sql, id); } });
@Transactional(readOnly = true) public Object doWork(SqlAction sql) { return idsInDb(sql); } });
@SuppressWarnings("unchecked") public LutProviderImpl( ScriptRepoHelper scriptRepoHelper, ScriptFileType lutType) { File root = new File(scriptRepoHelper.getScriptDir()); Iterator<File> scripts = FileUtils.iterateFiles( root, lutType.getFileFilter(), TrueFileFilter.TRUE); while (scripts.hasNext()) { RepoFile script = new RepoFile(root, scripts.next()); String basename = script.basename(); try { lutReaders.put( basename, LutReaderFactory.read(script.file())); log.debug("Successfully added LUT '{}'", basename); } catch (Exception e) { log.warn("Cannot read lookup table: '{}'", script.fullname(), e); } } log.info("Successfully added {} LUTs", lutReaders.size()); }
/** * Make the file, this is a temporary file which will be changed when the * script is validated. * * @param script * script. * @return OriginalFile tempfile.. * @throws ServerError */ private OriginalFile makeFile(final String path, final String script, Ice.Current current) throws ServerError { OriginalFile file = new OriginalFile(); file.setName(FilenameUtils.getName(path)); file.setPath(FilenameUtils.getFullPath(path)); file.setSize((long) script.getBytes().length); file.setHasher(new ChecksumAlgorithm("SHA1-160")); file.setHash(cpf.getProvider(ChecksumType.SHA1) .putBytes(script.getBytes()).checksumAsString()); scripts.setMimetype(file); return updateFile(file, current); }
/** * Walks all files in the repository (via {@link #iterate()} and adds them * if not found in the database. * * If modificationCheck is true, then a change in the hash for a file in * the repository will cause the old file to be removed from the repository * <pre>(uuid == null)</pre> and a new file created in its place. * * @param modificationCheck * @return See above. */ public List<OriginalFile> loadAll(final boolean modificationCheck) { return loadAll(modificationCheck, null); }
@Transactional(readOnly = false) public Object doWork(Session session, ServiceFactory sf) { OriginalFile ofile = load(id, session, getSqlAction(), true); return update(session, repoFile, getSqlAction(), sf, ofile); } });