/** * Set the import log file's size in the database to its current size on the filesystem. * * @throws ServerError if the import log's size could not be updated in the database */ private void setLogFileSize() throws ServerError { final OriginalFile logFile = (OriginalFile) sf.getQueryService().get(OriginalFile.class.getSimpleName(), logPath.getId()); logFile.setSize(omero.rtypes.rlong(logPath.size())); sf.getUpdateService().saveObject(logFile); }
public void makeDir( CheckedPath checked, boolean parents, Session s, ServiceFactory sf, SqlAction sql, ome.system.EventContext effectiveEventContext) throws ServerError { final LinkedList<CheckedPath> paths = new LinkedList<CheckedPath>(); while (!checked.isRoot) { paths.addFirst(checked); checked = checked.parent(); if (!parents) { break; // Only include last element } } if (paths.size() == 0) { if (parents) { throw new omero.ResourceError(null, null, "Cannot re-create root!"); } else { log.debug("Ignoring re-creation of root"); return; } } makeCheckedDirs(paths, parents, s, sf, sql, effectiveEventContext); }
/** * Create, initialize, and register an {@link RepoRawFileStoreI} with the proper setting (read or * write). * * @param checked The file that will be read. Can't be null, and must have ID set. * @param mode The mode for writing. If null, read-only. * @param __current The current user's session information. * @return A proxy ready to be returned to the user. * @throws ServerError * @throws InternalException */ protected RawFileStorePrx createRepoRFS(CheckedPath checked, String mode, Current __current) throws ServerError, InternalException { final Ice.Current adjustedCurr = makeAdjustedCurrent(__current); final BlitzExecutor be = context.getBean("throttlingStrategy", BlitzExecutor.class); RepoRawFileStoreI rfs; try { final RawFileStore service = repositoryDao.getRawFileStore(checked.getId(), checked, mode, __current); rfs = new RepoRawFileStoreI(be, service, adjustedCurr); rfs.setApplicationContext(this.context); } catch (Throwable t) { if (t instanceof ServerError) { throw (ServerError) t; } else { omero.InternalException ie = new omero.InternalException(); IceMapper.fillServerError(ie, t); throw ie; } } final _RawFileStoreTie tie = new _RawFileStoreTie(rfs); Ice.ObjectPrx prx = registerServant(tie, rfs, adjustedCurr); return RawFileStorePrxHelper.uncheckedCast(prx); }
public Object step(int step) { helper.assertStep(step); try { MDC.put("fileset", logFilename); log.debug("Step " + step); Job j = activity.getChild(); if (j == null) { throw helper.cancel(new ERR(), null, "null-job"); } else if (!(j instanceof MetadataImportJob)) { throw helper.cancel(new ERR(), null, "unexpected-job-type", "job-type", j.ice_id()); } if (step == 0) { return importMetadata((MetadataImportJob) j); } else if (step == 1) { return pixelData(null); // (ThumbnailGenerationJob) j); } else if (step == 2) { return generateThumbnails(null); // (PixelDataJob) j); Nulls image } else if (step == 3) { // TODO: indexing and scripting here as well. store.launchProcessing(); return null; } else if (step == 4) { return objects; } else { throw helper.cancel(new ERR(), null, "bad-step", "step", "" + step); } } catch (MissingLibraryException mle) { notifyObservers(new ErrorHandler.MISSING_LIBRARY(fileName, mle, usedFiles, format)); throw helper.cancel(new ERR(), mle, "import-missing-library", "filename", fileName); } catch (UnsupportedCompressionException uce) { // Handling as UNKNOWN_FORMAT for 4.3.0 notifyObservers(new ErrorHandler.UNKNOWN_FORMAT(fileName, uce, this)); throw helper.cancel(new ERR(), uce, "import-unknown-format", "filename", fileName); } catch (UnknownFormatException ufe) { notifyObservers(new ErrorHandler.UNKNOWN_FORMAT(fileName, ufe, this)); throw helper.cancel(new ERR(), ufe, "import-unknown-format", "filename", fileName); } catch (IOException io) { notifyObservers(new ErrorHandler.FILE_EXCEPTION(fileName, io, usedFiles, format)); throw helper.cancel(new ERR(), io, "import-file-exception", "filename", fileName); } catch (FormatException fe) { notifyObservers(new ErrorHandler.FILE_EXCEPTION(fileName, fe, usedFiles, format)); throw helper.cancel(new ERR(), fe, "import-file-exception", "filename", fileName); } catch (Cancel c) { throw c; } catch (Throwable t) { notifyObservers( new ErrorHandler.INTERNAL_EXCEPTION( fileName, new RuntimeException(t), usedFiles, format)); throw helper.cancel(new ERR(), t, "import-request-failure"); } finally { try { long size = logPath.size(); store.updateFileSize(logFile, size); } catch (Throwable t) { throw helper.cancel(new ERR(), t, "update-log-file-size"); } MDC.clear(); } }
/** * Get an {@link OriginalFile} object based on its id. Returns null if the file does not exist or * does not belong to this repo. * * @param id long, db id of original file. * @return OriginalFile object. */ private CheckedPath checkId(final long id, final Ice.Current curr) throws SecurityViolation, ValidationException { // TODO: could getOriginalFile and getFile be reduced to a single call? final FsFile file = this.repositoryDao.getFile(id, curr, this.repoUuid); if (file == null) { throw new SecurityViolation(null, null, "FileNotFound: " + id); } final OriginalFile originalFile = this.repositoryDao.getOriginalFile(id, curr); if (originalFile == null) { /* reachable even if file != null because getFile uses SQL, * evading the filter on the HQL used here by getOriginalFile */ throw new SecurityViolation(null, null, "FileNotAccessible: " + id); } final CheckedPath checked = new CheckedPath( this.serverPaths, file.toString(), checksumProviderFactory, originalFile.getHasher()); checked.setId(id); return checked; }
protected OriginalFile findOrCreateInDb( CheckedPath checked, String mode, String mimetype, Ice.Current curr) throws ServerError { OriginalFile ofile = findInDb(checked, mode, curr); if (ofile != null) { return ofile; } if (checked.exists()) { omero.grid.UnregisteredFileException ufe = new omero.grid.UnregisteredFileException(); ofile = (OriginalFile) new IceMapper().map(checked.asOriginalFile(mimetype)); ufe.file = ofile; throw ufe; } ofile = repositoryDao.register(repoUuid, checked, null, curr); final long originalFileId = ofile.getId().getValue(); setOriginalFileHasherToSHA1(originalFileId, curr); checked.setId(originalFileId); return ofile; }
/** opens the file using the {@link FormatReader} instance */ private void open(OMEROWrapper reader, OMEROMetadataStoreClient store, CheckedPath targetFile) throws FormatException, IOException { // reader.close(); This instance is no longer re-used reader.setMetadataStore(store); reader.setMinMaxStore(store); store.setReader(reader.getImageReader()); targetFile.bfSetId(reader); // reset series count if (log.isDebugEnabled()) { log.debug("Image Count: " + reader.getImageCount()); } }
/** * Find the given path in the DB or create. * * <p>"requiresWrite" is set to true unless the mode is "r". If requiresWrite is true, then the * caller needs the file to be modifiable (both on disk and the DB). If this doesn't hold, then a * SecurityViolation will be thrown. */ protected OriginalFile findInDb(CheckedPath checked, String mode, Ice.Current current) throws ServerError { final OriginalFile ofile = repositoryDao.findRepoFile(repoUuid, checked, null, current); if (ofile == null) { return null; // EARLY EXIT! } boolean requiresWrite = true; if ("r".equals(mode)) { requiresWrite = false; } checked.setId(ofile.getId().getValue()); boolean canUpdate = repositoryDao.canUpdate(ofile, current); if (requiresWrite && !canUpdate) { throw new omero.SecurityViolation(null, null, "requiresWrite is true but cannot modify"); } return ofile; }
/** * Method called locally to the repository during {@link #rawAccess()}. Only the remoteable fields * should be accessed during this method call since this will be a copy of the object originally * called. * * @param abstractRepositoryI * @param servant * @param __current */ public void local( AbstractRepositoryI abstractRepositoryI, PublicRepositoryI servant, Current __current) throws Exception { if ("touch".equals(command)) { for (String arg : args) { final CheckedPath checked = servant.checkPath(parse(arg), null, __current); if (!checked.exists()) { final CheckedPath parent = checked.parent(); if (!(parent.isDirectory() || checked.parent().mkdirs())) { throw new RepositoryException(null, null, "cannot create directory: " + parent); } final FileBuffer buffer = checked.getFileBuffer("rw"); buffer.write(ByteBuffer.allocate(0)); buffer.close(); } else if (!checked.markModified()) { throw new RepositoryException(null, null, "cannot touch file: " + checked); } } } else if ("mkdir".equals(command)) { boolean parents = false; for (String arg : args) { if ("-p".equals(arg)) { parents = true; continue; } final CheckedPath checked = servant.checkPath(parse(arg), null, __current); if (parents) { checked.mkdirs(); } else { checked.mkdir(); } } } else if ("rm".equals(command)) { if (args.size() == 1) { final CheckedPath checked = servant.checkPath(parse(args.get(0)), null, __current); if (!checked.delete()) { throw new omero.grid.FileDeleteException( null, null, "Delete file failed: " + args.get(0)); } } else { throw new omero.ApiUsageException( null, null, "Command: " + command + " takes just one argument"); } } else if ("checksum".equals(command)) { if (args.size() == 3) { final String checksumType = args.get(0); final ChecksumAlgorithm algo = ChecksumAlgorithmMapper.getChecksumAlgorithm(checksumType); final String expectedHash = args.get(1); final CheckedPath checked = servant.checkPath(parse(args.get(2)), algo, __current); final String currentHash = checked.hash(); if (!currentHash.equals(expectedHash)) { // TODO: ADD ANNOTATION TO DATABASE HERE! throw new omero.ResourceError( null, null, String.format( "Checksum mismatch (%s): expected=%s found=%s", checksumType, expectedHash, currentHash)); } } else { throw new omero.ApiUsageException( null, null, "'checksum' requires HASHER HASH FILEPATH, not: " + args.toString()); } } else { throw new omero.ApiUsageException(null, null, "Unknown command: " + command); } }
/** * Internal method to be used by subclasses to perform any extra checks on the listed of {@link * CheckedPath} instances before allowing the creation of directories. * * @param paths Not null, not empty. (Will be emptied by this method.) * @param parents "mkdir -p" like flag. * @param __current */ protected void makeCheckedDirs( final LinkedList<CheckedPath> paths, boolean parents, Session s, ServiceFactory sf, SqlAction sql, ome.system.EventContext effectiveEventContext) throws ServerError { CheckedPath checked; // Since we now have some number of elements, we start at the most // parent element and work our way down through all the parents. // If the file exists, then we check its permissions. If it doesn't // exist, it gets created. while (paths.size() > 1) { // Only possible if `parents` checked = paths.removeFirst(); if (checked.exists()) { if (!checked.isDirectory()) { throw new omero.ResourceError(null, null, "Path is not a directory."); } else if (!checked.canRead()) { throw new omero.ResourceError(null, null, "Directory is not readable"); } assertFindDir(checked, s, sf, sql); } else { // This will fail if the directory already exists try { repositoryDao.register(repoUuid, checked, DIRECTORY_MIMETYPE, sf, sql); } catch (ValidationException ve) { if (ve.getCause() instanceof PSQLException) { // Could have collided with another thread also creating the directory. // See Trac #11096 regarding originalfile table uniqueness of columns repo, path, name. // So, give the other thread time to complete registration. SleepTimer.sleepFor(1000); if (checked.exists()) { // The path now exists! It did not a moment ago. // We are not going to rethrow the validation exception, // so we otherwise note that something unexpected did occur. log.warn( "retrying after exception in registering directory " + checked + ": " + ve.getCause()); // Another thread may have succeeded where this one failed, // so try this directory again. paths.add(0, checked); continue; } } // We cannot recover from the validation exception. throw ve; } } } // Now we are ready to work on the actual intended path. checked = paths.removeFirst(); // Size is now empty if (checked.exists()) { if (parents) { assertFindDir(checked, s, sf, sql); } else { throw new omero.ResourceError(null, null, "Path exists on disk: " + checked.fsFile); } } repositoryDao.register(repoUuid, checked, DIRECTORY_MIMETYPE, sf, sql); }
public void init(Helper helper) { this.helper = helper; helper.setSteps(5); final ImportConfig config = new ImportConfig(); final String sessionUuid = helper.getEventContext().getCurrentSessionUuid(); if (!(location instanceof ManagedImportLocationI)) { throw helper.cancel( new ERR(), null, "bad-location", "location-type", location.getClass().getName()); } ManagedImportLocationI managedLocation = (ManagedImportLocationI) location; logPath = managedLocation.getLogFile(); logFilename = logPath.getFullFsPath(); MDC.put("fileset", logFilename); file = ((ManagedImportLocationI) location).getTarget(); try { sf = reg.getInternalServiceFactory(sessionUuid, "unused", 3, 1, clientUuid); store = new OMEROMetadataStoreClient(); store.setCurrentLogFile(logFilename, token); store.initialize(sf); registerKeepAlive(); fileName = file.getFullFsPath(); shortName = file.getName(); format = null; usedFiles = new String[] {fileName}; open(reader, store, file); format = reader.getFormat(); if (reader.getUsedFiles() != null) { usedFiles = reader.getUsedFiles(); } if (usedFiles == null) { throw new NullPointerException("usedFiles must be non-null"); } // Process all information which has been passed in as annotations detectKnownAnnotations(); // Now that we've possibly updated the settings object, copy out // all the values needed by import. userSpecifiedTarget = settings.userSpecifiedTarget; userSpecifiedName = settings.userSpecifiedName == null ? null : settings.userSpecifiedName.getValue(); userSpecifiedDescription = settings.userSpecifiedDescription == null ? null : settings.userSpecifiedDescription.getValue(); userPixels = settings.userSpecifiedPixels; annotationList = settings.userSpecifiedAnnotationList; doThumbnails = settings.doThumbnails == null ? true : settings.doThumbnails.getValue(); noStatsInfo = settings.noStatsInfo == null ? false : settings.noStatsInfo.getValue(); IFormatReader baseReader = reader.getImageReader().getReader(); if (log.isInfoEnabled()) { log.info("File format: " + format); log.info("Base reader: " + baseReader.getClass().getName()); } notifyObservers(new ImportEvent.LOADED_IMAGE(shortName, 0, 0, 0)); formatString = baseReader.getClass().getSimpleName(); formatString = formatString.replace("Reader", ""); } catch (Cancel c) { throw c; } catch (Throwable t) { throw helper.cancel(new ERR(), t, "error-on-init"); } finally { MDC.clear(); } }