Search in sources :

Example 81 with Files

use of java.nio.file.Files in project mycore by MyCoRe-Org.

the class MCRURNGranularOAIRegistrationService method registerURNsDerivate.

private MCRDNBURN registerURNsDerivate(MCRBase obj, String additional, MCRObjectDerivate derivate) throws MCRPersistentIdentifierException {
    LOGGER.info("Add URNs to all files of {}", obj.getId());
    Session session = MCRHIBConnection.instance().getSession();
    Path path = MCRPath.getPath(obj.getId().toString(), "/");
    MCRFileCollectingFileVisitor<Path> collectingFileVisitor = new MCRFileCollectingFileVisitor<>();
    try {
        Files.walkFileTree(path, collectingFileVisitor);
    } catch (IOException e) {
        throw new MCRPersistentIdentifierException("Could not walk derivate file tree!", e);
    }
    List<String> ignoreFileNamesList = getIgnoreFileList();
    List<Predicate<String>> predicateList = ignoreFileNamesList.stream().map(Pattern::compile).map(Pattern::asPredicate).collect(Collectors.toList());
    List<MCRPath> pathList = collectingFileVisitor.getPaths().stream().filter(file -> predicateList.stream().noneMatch(p -> p.test(file.toString().split(":")[1]))).map(p -> (MCRPath) p).sorted().collect(Collectors.toList());
    MCRDNBURN newURN = getNewIdentifier(obj.getId(), additional);
    String setID = obj.getId().getNumberAsString();
    for (int pathListIndex = 0; pathListIndex < pathList.size(); pathListIndex++) {
        MCRDNBURN subURN = newURN.toGranular(setID, pathListIndex + 1, pathList.size());
        derivate.getOrCreateFileMetadata(pathList.get(pathListIndex), subURN.asString()).setUrn(subURN.asString());
        MCRPI databaseEntry = new MCRPI(subURN.asString(), getType(), obj.getId().toString(), pathList.get(pathListIndex).getOwnerRelativePath(), this.getRegistrationServiceID(), null);
        session.save(databaseEntry);
    }
    derivate.setURN(newURN.asString());
    MCRPI databaseEntry = new MCRPI(newURN.asString(), getType(), obj.getId().toString(), "", this.getRegistrationServiceID(), new Date());
    session.save(databaseEntry);
    return newURN;
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRBase(org.mycore.datamodel.metadata.MCRBase) Arrays(java.util.Arrays) Date(java.util.Date) Session(org.hibernate.Session) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRPI(org.mycore.pi.backend.MCRPI) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) LogManager(org.apache.logging.log4j.LogManager) Pattern(java.util.regex.Pattern) MCRPI(org.mycore.pi.backend.MCRPI) IOException(java.io.IOException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Date(java.util.Date) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRFileCollectingFileVisitor(org.mycore.pi.MCRFileCollectingFileVisitor) Session(org.hibernate.Session)

Example 82 with Files

use of java.nio.file.Files in project mycore by MyCoRe-Org.

the class MCRMetsSave method updateOnImageFile.

private static void updateOnImageFile(Document mets, String fileId, String path) {
    LOGGER.debug("FILE is a image!");
    // check if custom files are present and save the ids
    String[] customFileGroups = { MCRMetsFileUse.TRANSCRIPTION.toString(), MCRMetsFileUse.ALTO.toString(), MCRMetsFileUse.TRANSLATION.toString() };
    // add to structMap physical
    PhysicalSubDiv div = new PhysicalSubDiv(PhysicalSubDiv.ID_PREFIX + fileId, PhysicalSubDiv.TYPE_PAGE);
    div.add(new Fptr(fileId));
    Arrays.stream(customFileGroups).map(customFileGroup -> searchFileInGroup(mets, path, customFileGroup)).filter(Objects::nonNull).map(Fptr::new).forEach(div::add);
    // actually alter the mets document
    Element structMapPhys = getPhysicalStructmap(mets);
    structMapPhys.addContent(div.asElement());
    // add to structLink
    SmLink smLink = getDefaultSmLink(mets, div);
    Element structLink = getStructLink(mets);
    structLink.addContent(smLink.asElement());
}
Also used : Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) StructLink(org.mycore.mets.model.struct.StructLink) URISyntaxException(java.net.URISyntaxException) LOCTYPE(org.mycore.mets.model.struct.LOCTYPE) MCRMarkManager(org.mycore.datamodel.common.MCRMarkManager) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) Locale(java.util.Locale) FileGrp(org.mycore.mets.model.files.FileGrp) Map(java.util.Map) MCRMetsFileUse(org.mycore.mets.model.simple.MCRMetsFileUse) File(org.mycore.mets.model.files.File) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) PhysicalStructMap(org.mycore.mets.model.struct.PhysicalStructMap) Format(org.jdom2.output.Format) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Set(java.util.Set) Mets(org.mycore.mets.model.Mets) Collectors(java.util.stream.Collectors) LogicalDiv(org.mycore.mets.model.struct.LogicalDiv) XPathExpression(org.jdom2.xpath.XPathExpression) SmLink(org.mycore.mets.model.struct.SmLink) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) LogicalStructMap(org.mycore.mets.model.struct.LogicalStructMap) SAXException(org.xml.sax.SAXException) MCRStreamUtils(org.mycore.common.MCRStreamUtils) MCRPathContent(org.mycore.common.content.MCRPathContent) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Element(org.jdom2.Element) PhysicalDiv(org.mycore.mets.model.struct.PhysicalDiv) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MCRConfiguration(org.mycore.common.config.MCRConfiguration) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRDirectory(org.mycore.datamodel.ifs2.MCRDirectory) OutputStream(java.io.OutputStream) MCRContentTypes(org.mycore.datamodel.niofs.MCRContentTypes) Files(java.nio.file.Files) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv) XMLOutputter(org.jdom2.output.XMLOutputter) Attribute(org.jdom2.Attribute) Fptr(org.mycore.mets.model.struct.Fptr) Paths(java.nio.file.Paths) FLocat(org.mycore.mets.model.files.FLocat) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) SmLink(org.mycore.mets.model.struct.SmLink) Element(org.jdom2.Element) Objects(java.util.Objects) Fptr(org.mycore.mets.model.struct.Fptr) PhysicalSubDiv(org.mycore.mets.model.struct.PhysicalSubDiv)

Example 83 with Files

use of java.nio.file.Files in project mycore by MyCoRe-Org.

the class MCRIView2Commands method fixDeadEntries.

@MCRCommand(syntax = "fix dead tile jobs", help = "Deletes entries for files which dont exist anymore!")
public static void fixDeadEntries() {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    TypedQuery<MCRTileJob> allTileJobQuery = em.createNamedQuery("MCRTileJob.all", MCRTileJob.class);
    List<MCRTileJob> tiles = allTileJobQuery.getResultList();
    tiles.stream().filter(tj -> {
        MCRPath path = MCRPath.getPath(tj.getDerivate(), tj.getPath());
        return !Files.exists(path);
    }).peek(tj -> LOGGER.info("Delete TileJob {}:{}", tj.getDerivate(), tj.getPath())).forEach(em::remove);
}
Also used : MCRIView2Tools(org.mycore.iview2.services.MCRIView2Tools) Enumeration(java.util.Enumeration) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MCRRecursiveDeleter(org.mycore.datamodel.niofs.utils.MCRRecursiveDeleter) DirectoryStream(java.nio.file.DirectoryStream) JDOMException(org.jdom2.JDOMException) MCRImage(org.mycore.imagetiler.MCRImage) MCRTileJob(org.mycore.iview2.services.MCRTileJob) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ZipFile(java.util.zip.ZipFile) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRTilingQueue(org.mycore.iview2.services.MCRTilingQueue) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) Files(java.nio.file.Files) MCRAbstractCommands(org.mycore.frontend.cli.MCRAbstractCommands) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRImageTiler(org.mycore.iview2.services.MCRImageTiler) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) FileSystem(java.nio.file.FileSystem) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) CRC32(java.util.zip.CRC32) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) EntityManager(javax.persistence.EntityManager) FileSystem(java.nio.file.FileSystem) MCRTileJob(org.mycore.iview2.services.MCRTileJob) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 84 with Files

use of java.nio.file.Files in project rest.li by linkedin.

the class PegasusPlugin method configureRestModelGeneration.

protected void configureRestModelGeneration(Project project, SourceSet sourceSet) {
    if (sourceSet.getAllSource().isEmpty()) {
        project.getLogger().info("No source files found for sourceSet {}.  Skipping idl generation.", sourceSet.getName());
        return;
    }
    // afterEvaluate needed so that api project can be overridden via ext.apiProject
    project.afterEvaluate(p -> {
        // find api project here instead of in each project's plugin configuration
        // this allows api project relation options (ext.api*) to be specified anywhere in the build.gradle file
        // alternatively, pass closures to task configuration, and evaluate the closures when task is executed
        Project apiProject = getCheckedApiProject(project);
        // make sure the api project is evaluated. Important for configure-on-demand mode.
        if (apiProject != null) {
            project.evaluationDependsOn(apiProject.getPath());
            if (!apiProject.getPlugins().hasPlugin(_thisPluginType)) {
                apiProject = null;
            }
        }
        if (apiProject == null) {
            return;
        }
        Task untypedJarTask = project.getTasks().findByName(sourceSet.getJarTaskName());
        if (!(untypedJarTask instanceof Jar)) {
            return;
        }
        Jar jarTask = (Jar) untypedJarTask;
        String snapshotCompatPropertyName = findProperty(FileCompatibilityType.SNAPSHOT);
        if (project.hasProperty(snapshotCompatPropertyName) && "off".equalsIgnoreCase((String) project.property(snapshotCompatPropertyName))) {
            project.getLogger().lifecycle("Project {} snapshot compatibility level \"OFF\" is deprecated. Default to \"IGNORE\".", project.getPath());
        }
        // generate the rest model
        FileCollection restModelCodegenClasspath = project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION).plus(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)).plus(sourceSet.getRuntimeClasspath());
        String destinationDirPrefix = getGeneratedDirPath(project, sourceSet, REST_GEN_TYPE) + File.separatorChar;
        FileCollection restModelResolverPath = apiProject.files(getDataSchemaPath(project, sourceSet)).plus(getDataModelConfig(apiProject, sourceSet));
        Set<File> watchedRestModelInputDirs = buildWatchedRestModelInputDirs(project, sourceSet);
        Set<File> restModelInputDirs = difference(sourceSet.getAllSource().getSrcDirs(), sourceSet.getResources().getSrcDirs());
        Task generateRestModelTask = project.getTasks().create(sourceSet.getTaskName("generate", "restModel"), GenerateRestModelTask.class, task -> {
            task.dependsOn(project.getTasks().getByName(sourceSet.getClassesTaskName()));
            task.setCodegenClasspath(restModelCodegenClasspath);
            task.setWatchedCodegenClasspath(restModelCodegenClasspath.filter(file -> !"main".equals(file.getName()) && !"classes".equals(file.getName())));
            task.setInputDirs(restModelInputDirs);
            task.setWatchedInputDirs(watchedRestModelInputDirs.isEmpty() ? restModelInputDirs : watchedRestModelInputDirs);
            // we need all the artifacts from runtime for any private implementation classes the server code might need.
            task.setSnapshotDestinationDir(project.file(destinationDirPrefix + "snapshot"));
            task.setIdlDestinationDir(project.file(destinationDirPrefix + "idl"));
            @SuppressWarnings("unchecked") Map<String, PegasusOptions> pegasusOptions = (Map<String, PegasusOptions>) project.getExtensions().getExtraProperties().get("pegasus");
            task.setIdlOptions(pegasusOptions.get(sourceSet.getName()).idlOptions);
            task.setResolverPath(restModelResolverPath);
            if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
                task.setEnableArgFile(true);
            }
            task.onlyIf(t -> !isPropertyTrue(project, SKIP_GENERATE_REST_MODEL));
            task.doFirst(new CacheableAction<>(t -> deleteGeneratedDir(project, sourceSet, REST_GEN_TYPE)));
        });
        File apiSnapshotDir = apiProject.file(getSnapshotPath(apiProject, sourceSet));
        File apiIdlDir = apiProject.file(getIdlPath(apiProject, sourceSet));
        apiSnapshotDir.mkdirs();
        if (!isPropertyTrue(project, SKIP_IDL_CHECK)) {
            apiIdlDir.mkdirs();
        }
        CheckRestModelTask checkRestModelTask = project.getTasks().create(sourceSet.getTaskName("check", "RestModel"), CheckRestModelTask.class, task -> {
            task.dependsOn(generateRestModelTask);
            task.setCurrentSnapshotFiles(SharedFileUtils.getSnapshotFiles(project, destinationDirPrefix));
            task.setPreviousSnapshotDirectory(apiSnapshotDir);
            task.setCurrentIdlFiles(SharedFileUtils.getIdlFiles(project, destinationDirPrefix));
            task.setPreviousIdlDirectory(apiIdlDir);
            task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
            task.setModelCompatLevel(PropertyUtil.findCompatLevel(project, FileCompatibilityType.SNAPSHOT));
            task.onlyIf(t -> !isPropertyTrue(project, SKIP_IDL_CHECK));
            task.doLast(new CacheableAction<>(t -> {
                if (!task.isEquivalent()) {
                    _restModelCompatMessage.append(task.getWholeMessage());
                }
            }));
        });
        CheckSnapshotTask checkSnapshotTask = project.getTasks().create(sourceSet.getTaskName("check", "Snapshot"), CheckSnapshotTask.class, task -> {
            task.dependsOn(generateRestModelTask);
            task.setCurrentSnapshotFiles(SharedFileUtils.getSnapshotFiles(project, destinationDirPrefix));
            task.setPreviousSnapshotDirectory(apiSnapshotDir);
            task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
            task.setSnapshotCompatLevel(PropertyUtil.findCompatLevel(project, FileCompatibilityType.SNAPSHOT));
            task.onlyIf(t -> isPropertyTrue(project, SKIP_IDL_CHECK));
        });
        CheckIdlTask checkIdlTask = project.getTasks().create(sourceSet.getTaskName("check", "Idl"), CheckIdlTask.class, task -> {
            task.dependsOn(generateRestModelTask);
            task.setCurrentIdlFiles(SharedFileUtils.getIdlFiles(project, destinationDirPrefix));
            task.setPreviousIdlDirectory(apiIdlDir);
            task.setResolverPath(restModelResolverPath);
            task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
            task.setIdlCompatLevel(PropertyUtil.findCompatLevel(project, FileCompatibilityType.IDL));
            if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
                task.setEnableArgFile(true);
            }
            task.onlyIf(t -> !isPropertyTrue(project, SKIP_IDL_CHECK) && !"OFF".equals(PropertyUtil.findCompatLevel(project, FileCompatibilityType.IDL)));
        });
        // rest model publishing involves cross-project reference
        // configure after all projects have been evaluated
        // the file copy can be turned off by "rest.model.noPublish" flag
        Task publishRestliSnapshotTask = project.getTasks().create(sourceSet.getTaskName("publish", "RestliSnapshot"), PublishRestModelTask.class, task -> {
            task.dependsOn(checkRestModelTask, checkSnapshotTask, checkIdlTask);
            task.from(SharedFileUtils.getSnapshotFiles(project, destinationDirPrefix));
            task.into(apiSnapshotDir);
            task.setSuffix(SNAPSHOT_FILE_SUFFIX);
            task.onlyIf(t -> !isPropertyTrue(project, SNAPSHOT_NO_PUBLISH) && ((isPropertyTrue(project, SKIP_IDL_CHECK) && isTaskSuccessful(checkSnapshotTask) && checkSnapshotTask.getSummaryTarget().exists() && !isResultEquivalent(checkSnapshotTask.getSummaryTarget())) || (!isPropertyTrue(project, SKIP_IDL_CHECK) && isTaskSuccessful(checkRestModelTask) && checkRestModelTask.getSummaryTarget().exists() && !isResultEquivalent(checkRestModelTask.getSummaryTarget()))));
        });
        Task publishRestliIdlTask = project.getTasks().create(sourceSet.getTaskName("publish", "RestliIdl"), PublishRestModelTask.class, task -> {
            task.dependsOn(checkRestModelTask, checkIdlTask, checkSnapshotTask);
            task.from(SharedFileUtils.getIdlFiles(project, destinationDirPrefix));
            task.into(apiIdlDir);
            task.setSuffix(IDL_FILE_SUFFIX);
            task.onlyIf(t -> !isPropertyTrue(project, IDL_NO_PUBLISH) && ((isPropertyTrue(project, SKIP_IDL_CHECK) && isTaskSuccessful(checkSnapshotTask) && checkSnapshotTask.getSummaryTarget().exists() && !isResultEquivalent(checkSnapshotTask.getSummaryTarget(), true)) || (!isPropertyTrue(project, SKIP_IDL_CHECK) && ((isTaskSuccessful(checkRestModelTask) && checkRestModelTask.getSummaryTarget().exists() && !isResultEquivalent(checkRestModelTask.getSummaryTarget(), true)) || (isTaskSuccessful(checkIdlTask) && checkIdlTask.getSummaryTarget().exists() && !isResultEquivalent(checkIdlTask.getSummaryTarget()))))));
        });
        project.getLogger().info("API project selected for {} is {}", publishRestliIdlTask.getPath(), apiProject.getPath());
        jarTask.from(SharedFileUtils.getIdlFiles(project, destinationDirPrefix));
        // add generated .restspec.json files as resources to the jar
        jarTask.dependsOn(publishRestliSnapshotTask, publishRestliIdlTask);
        ChangedFileReportTask changedFileReportTask = (ChangedFileReportTask) project.getTasks().getByName("changedFilesReport");
        // Use the files from apiDir for generating the changed files report as we need to notify user only when
        // source system files are modified.
        changedFileReportTask.setIdlFiles(SharedFileUtils.getSuffixedFiles(project, apiIdlDir, IDL_FILE_SUFFIX));
        changedFileReportTask.setSnapshotFiles(SharedFileUtils.getSuffixedFiles(project, apiSnapshotDir, SNAPSHOT_FILE_SUFFIX));
        changedFileReportTask.mustRunAfter(publishRestliSnapshotTask, publishRestliIdlTask);
        changedFileReportTask.doLast(new CacheableAction<>(t -> {
            if (!changedFileReportTask.getNeedCheckinFiles().isEmpty()) {
                project.getLogger().info("Adding modified files to need checkin list...");
                _needCheckinFiles.addAll(changedFileReportTask.getNeedCheckinFiles());
                _needBuildFolders.add(getCheckedApiProject(project).getPath());
            }
        }));
    });
}
Also used : CompatibilityLogChecker(com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker) Arrays(java.util.Arrays) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) JavaPlugin(org.gradle.api.plugins.JavaPlugin) IdeaPlugin(org.gradle.plugins.ide.idea.IdeaPlugin) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) EclipsePlugin(org.gradle.plugins.ide.eclipse.EclipsePlugin) Task(org.gradle.api.Task) PublishingExtension(org.gradle.api.publish.PublishingExtension) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) Locale(java.util.Locale) Map(java.util.Map) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GradleVersion(org.gradle.util.GradleVersion) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) Delete(org.gradle.api.tasks.Delete) Project(org.gradle.api.Project) Collection(java.util.Collection) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) Set(java.util.Set) IvyPublishPlugin(org.gradle.api.publish.ivy.plugins.IvyPublishPlugin) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) Collectors(java.util.stream.Collectors) IvyPublication(org.gradle.api.publish.ivy.IvyPublication) List(java.util.List) IdlOptions(com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) Pattern(java.util.regex.Pattern) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) HashMap(java.util.HashMap) Function(java.util.function.Function) FileCollection(org.gradle.api.file.FileCollection) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SourceSet(org.gradle.api.tasks.SourceSet) HashSet(java.util.HashSet) Configuration(org.gradle.api.artifacts.Configuration) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) Sync(org.gradle.api.tasks.Sync) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Properties(java.util.Properties) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Files(java.nio.file.Files) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) IOException(java.io.IOException) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel) File(java.io.File) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) GradleException(org.gradle.api.GradleException) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) Copy(org.gradle.api.tasks.Copy) InputStream(java.io.InputStream) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) Task(org.gradle.api.Task) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) FileCollection(org.gradle.api.file.FileCollection) Project(org.gradle.api.Project) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Example 85 with Files

use of java.nio.file.Files in project rest.li by linkedin.

the class PegasusPlugin method configureRestClientGeneration.

// Generate rest client from idl files generated from java source files in the specified source set.
// 
// This generates rest client source files from idl file generated from java source files
// in the source set. The generated rest client source files will be in a new source set.
// It also compiles the rest client source files into classes, and creates both the
// rest model and rest client jar files.
// 
@SuppressWarnings("deprecation")
protected void configureRestClientGeneration(Project project, SourceSet sourceSet) {
    // idl directory for api project
    File idlDir = project.file(getIdlPath(project, sourceSet));
    if (SharedFileUtils.getSuffixedFiles(project, idlDir, IDL_FILE_SUFFIX).isEmpty()) {
        return;
    }
    File generatedRestClientDir = project.file(getGeneratedDirPath(project, sourceSet, REST_GEN_TYPE) + File.separatorChar + "java");
    // always include imported data template jars in compileClasspath of rest client
    FileCollection dataModelConfig = getDataModelConfig(project, sourceSet);
    // if data templates generated from this source set, add the generated data template jar to compileClasspath
    // of rest client.
    String dataTemplateSourceSetName = getGeneratedSourceSetName(sourceSet, DATA_TEMPLATE_GEN_TYPE);
    Jar dataTemplateJarTask = null;
    SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
    FileCollection dataModels;
    if (sourceSets.findByName(dataTemplateSourceSetName) != null) {
        if (debug) {
            System.out.println("sourceSet " + sourceSet.getName() + " has generated sourceSet " + dataTemplateSourceSetName);
        }
        dataTemplateJarTask = (Jar) project.getTasks().getByName(sourceSet.getName() + "DataTemplateJar");
        // FIXME change to #getArchiveFile(); breaks backwards-compatibility before 5.1
        dataModels = dataModelConfig.plus(project.files(dataTemplateJarTask.getArchivePath()));
    } else {
        dataModels = dataModelConfig;
    }
    // create source set for generated rest model, rest client source and class files.
    String targetSourceSetName = getGeneratedSourceSetName(sourceSet, REST_GEN_TYPE);
    SourceSet targetSourceSet = sourceSets.create(targetSourceSetName, ss -> {
        ss.java(sourceDirectorySet -> sourceDirectorySet.srcDir(generatedRestClientDir));
        ss.setCompileClasspath(dataModels.plus(project.getConfigurations().getByName("restClientCompile")));
    });
    project.getPlugins().withType(EclipsePlugin.class, eclipsePlugin -> {
        EclipseModel eclipseModel = (EclipseModel) project.getExtensions().findByName("eclipse");
        eclipseModel.getClasspath().getPlusConfigurations().add(project.getConfigurations().getByName("restClientCompile"));
    });
    // idea plugin needs to know about new rest client source directory and its dependencies
    addGeneratedDir(project, targetSourceSet, Arrays.asList(getDataModelConfig(project, sourceSet), project.getConfigurations().getByName("restClientCompile")));
    // generate the rest client source files
    GenerateRestClientTask generateRestClientTask = project.getTasks().create(targetSourceSet.getTaskName("generate", "restClient"), GenerateRestClientTask.class, task -> {
        task.dependsOn(project.getConfigurations().getByName("dataTemplate"));
        task.setInputDir(idlDir);
        task.setResolverPath(dataModels.plus(project.getConfigurations().getByName("restClientCompile")));
        task.setRuntimeClasspath(project.getConfigurations().getByName("dataModel").plus(project.getConfigurations().getByName("dataTemplate").getArtifacts().getFiles()));
        task.setCodegenClasspath(project.getConfigurations().getByName(PEGASUS_PLUGIN_CONFIGURATION));
        task.setDestinationDir(generatedRestClientDir);
        task.setRestli2FormatSuppressed(project.hasProperty(SUPPRESS_REST_CLIENT_RESTLI_2));
        task.setRestli1FormatSuppressed(project.hasProperty(SUPPRESS_REST_CLIENT_RESTLI_1));
        if (isPropertyTrue(project, ENABLE_ARG_FILE)) {
            task.setEnableArgFile(true);
        }
        if (isPropertyTrue(project, CODE_GEN_PATH_CASE_SENSITIVE)) {
            task.setGenerateLowercasePath(false);
        }
        if (isPropertyTrue(project, ENABLE_FLUENT_API)) {
            task.setGenerateFluentApi(true);
        }
        task.doFirst(new CacheableAction<>(t -> project.delete(generatedRestClientDir)));
    });
    if (dataTemplateJarTask != null) {
        generateRestClientTask.dependsOn(dataTemplateJarTask);
    }
    // TODO: Tighten the types so that _generateSourcesJarTask must be of type Jar.
    ((Jar) _generateSourcesJarTask).from(generateRestClientTask.getDestinationDir());
    _generateSourcesJarTask.dependsOn(generateRestClientTask);
    _generateJavadocTask.source(generateRestClientTask.getDestinationDir());
    _generateJavadocTask.setClasspath(_generateJavadocTask.getClasspath().plus(project.getConfigurations().getByName("restClientCompile")).plus(generateRestClientTask.getResolverPath()));
    _generateJavadocTask.dependsOn(generateRestClientTask);
    // make sure rest client source files have been generated before compiling them
    JavaCompile compileGeneratedRestClientTask = (JavaCompile) project.getTasks().getByName(targetSourceSet.getCompileJavaTaskName());
    compileGeneratedRestClientTask.dependsOn(generateRestClientTask);
    compileGeneratedRestClientTask.getOptions().getCompilerArgs().add("-Xlint:-deprecation");
    // create the rest model jar file
    Task restModelJarTask = project.getTasks().create(sourceSet.getName() + "RestModelJar", Jar.class, task -> {
        task.from(idlDir, copySpec -> {
            copySpec.eachFile(fileCopyDetails -> project.getLogger().info("Add idl file: {}", fileCopyDetails));
            copySpec.setIncludes(Collections.singletonList('*' + IDL_FILE_SUFFIX));
        });
        // FIXME change to #getArchiveAppendix().set(...); breaks backwards-compatibility before 5.1
        task.setAppendix(getAppendix(sourceSet, "rest-model"));
        task.setDescription("Generate rest model jar");
    });
    // create the rest client jar file
    Task restClientJarTask = project.getTasks().create(sourceSet.getName() + "RestClientJar", Jar.class, task -> {
        task.dependsOn(compileGeneratedRestClientTask);
        task.from(idlDir, copySpec -> {
            copySpec.eachFile(fileCopyDetails -> {
                project.getLogger().info("Add interface file: {}", fileCopyDetails);
                fileCopyDetails.setPath("idl" + File.separatorChar + fileCopyDetails.getPath());
            });
            copySpec.setIncludes(Collections.singletonList('*' + IDL_FILE_SUFFIX));
        });
        task.from(targetSourceSet.getOutput());
        // FIXME change to #getArchiveAppendix().set(...); breaks backwards-compatibility before 5.1
        task.setAppendix(getAppendix(sourceSet, "rest-client"));
        task.setDescription("Generate rest client jar");
    });
    // add the rest model jar and the rest client jar to the list of project artifacts.
    if (!isTestSourceSet(sourceSet)) {
        project.getArtifacts().add("restModel", restModelJarTask);
        project.getArtifacts().add("restClient", restClientJarTask);
    } else {
        project.getArtifacts().add("testRestModel", restModelJarTask);
        project.getArtifacts().add("testRestClient", restClientJarTask);
    }
}
Also used : CompatibilityLogChecker(com.linkedin.pegasus.gradle.internal.CompatibilityLogChecker) Arrays(java.util.Arrays) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) JavaPlugin(org.gradle.api.plugins.JavaPlugin) IdeaPlugin(org.gradle.plugins.ide.idea.IdeaPlugin) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) EclipsePlugin(org.gradle.plugins.ide.eclipse.EclipsePlugin) Task(org.gradle.api.Task) PublishingExtension(org.gradle.api.publish.PublishingExtension) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) ConfigurationContainer(org.gradle.api.artifacts.ConfigurationContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) Locale(java.util.Locale) Map(java.util.Map) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) Method(java.lang.reflect.Method) Path(java.nio.file.Path) GradleVersion(org.gradle.util.GradleVersion) IdeaModule(org.gradle.plugins.ide.idea.model.IdeaModule) Delete(org.gradle.api.tasks.Delete) Project(org.gradle.api.Project) Collection(java.util.Collection) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) Set(java.util.Set) IvyPublishPlugin(org.gradle.api.publish.ivy.plugins.IvyPublishPlugin) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) Collectors(java.util.stream.Collectors) IvyPublication(org.gradle.api.publish.ivy.IvyPublication) List(java.util.List) IdlOptions(com.linkedin.pegasus.gradle.PegasusOptions.IdlOptions) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) Pattern(java.util.regex.Pattern) ProcessResources(org.gradle.language.jvm.tasks.ProcessResources) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) HashMap(java.util.HashMap) Function(java.util.function.Function) FileCollection(org.gradle.api.file.FileCollection) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SourceSet(org.gradle.api.tasks.SourceSet) HashSet(java.util.HashSet) Configuration(org.gradle.api.artifacts.Configuration) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) Sync(org.gradle.api.tasks.Sync) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) JavaCompile(org.gradle.api.tasks.compile.JavaCompile) Properties(java.util.Properties) Jar(org.gradle.api.tasks.bundling.Jar) Action(org.gradle.api.Action) Files(java.nio.file.Files) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) IOException(java.io.IOException) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel) File(java.io.File) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) GradleException(org.gradle.api.GradleException) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) Copy(org.gradle.api.tasks.Copy) InputStream(java.io.InputStream) PublishRestModelTask(com.linkedin.pegasus.gradle.tasks.PublishRestModelTask) ValidateExtensionSchemaTask(com.linkedin.pegasus.gradle.tasks.ValidateExtensionSchemaTask) CheckSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckSnapshotTask) Task(org.gradle.api.Task) TranslateSchemasTask(com.linkedin.pegasus.gradle.tasks.TranslateSchemasTask) CheckPegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.CheckPegasusSnapshotTask) ChangedFileReportTask(com.linkedin.pegasus.gradle.tasks.ChangedFileReportTask) GeneratePegasusSnapshotTask(com.linkedin.pegasus.gradle.tasks.GeneratePegasusSnapshotTask) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) GenerateRestModelTask(com.linkedin.pegasus.gradle.tasks.GenerateRestModelTask) CheckRestModelTask(com.linkedin.pegasus.gradle.tasks.CheckRestModelTask) ValidateSchemaAnnotationTask(com.linkedin.pegasus.gradle.tasks.ValidateSchemaAnnotationTask) CheckIdlTask(com.linkedin.pegasus.gradle.tasks.CheckIdlTask) GenerateDataTemplateTask(com.linkedin.pegasus.gradle.tasks.GenerateDataTemplateTask) GenerateAvroSchemaTask(com.linkedin.pegasus.gradle.tasks.GenerateAvroSchemaTask) FileCollection(org.gradle.api.file.FileCollection) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) SourceSet(org.gradle.api.tasks.SourceSet) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) EclipseModel(org.gradle.plugins.ide.eclipse.model.EclipseModel) GenerateRestClientTask(com.linkedin.pegasus.gradle.tasks.GenerateRestClientTask) Jar(org.gradle.api.tasks.bundling.Jar) File(java.io.File) JavaCompile(org.gradle.api.tasks.compile.JavaCompile)

Aggregations

Files (java.nio.file.Files)243 IOException (java.io.IOException)210 Path (java.nio.file.Path)196 List (java.util.List)176 Collectors (java.util.stream.Collectors)154 Paths (java.nio.file.Paths)133 File (java.io.File)127 ArrayList (java.util.ArrayList)117 Map (java.util.Map)109 Set (java.util.Set)96 Collections (java.util.Collections)89 Arrays (java.util.Arrays)81 Stream (java.util.stream.Stream)77 HashMap (java.util.HashMap)74 HashSet (java.util.HashSet)58 InputStream (java.io.InputStream)55 Collection (java.util.Collection)55 Logger (org.slf4j.Logger)54 Pattern (java.util.regex.Pattern)53 Optional (java.util.Optional)51