List of usage examples for java.nio.file Files walkFileTree
public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException
From source file:org.olat.ims.qti.qpool.QTIImportProcessor.java
/** * Process the file of an item's package * @param item//w ww .j a v a 2 s . co m * @param itemInfos */ protected void processItemFiles(QuestionItemImpl item, DocInfos docInfos) { //a package with an item String dir = item.getDirectory(); String rootFilename = item.getRootFilename(); VFSContainer container = qpoolFileStorage.getContainer(dir); if (docInfos != null && docInfos.getRoot() != null) { try { Path destDir = ((LocalImpl) container).getBasefile().toPath(); //unzip to container Path path = docInfos.getRoot(); Files.walkFileTree(path, new CopyVisitor(path, destDir, new YesMatcher())); } catch (IOException e) { log.error("", e); } } else if (importedFilename.toLowerCase().endsWith(".zip")) { ZipUtil.unzipStrict(importedFile, container); } else { VFSLeaf endFile = container.createChildLeaf(rootFilename); OutputStream out = null; FileInputStream in = null; try { out = endFile.getOutputStream(false); in = new FileInputStream(importedFile); IOUtils.copy(in, out); } catch (IOException e) { log.error("", e); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } } }
From source file:org.apache.openaz.xacml.admin.components.PolicyWorkspace.java
protected void deleteSubDomain(final File subdomain) { String message = "Are you sure you want to delete subdomain\n" + subdomain.getName() + "\nThis will remove <B>ALL</B> of its subdomains and policy files."; ConfirmDialog dialog = ConfirmDialog.getFactory().create("Confirm SubDomain Deletion", message, "Delete", "Cancel"); dialog.setContentMode(ContentMode.HTML); dialog.show(getUI(), new ConfirmDialog.Listener() { private static final long serialVersionUID = 1L; @Override/*from ww w . jav a 2 s. c o m*/ public void onClose(ConfirmDialog dialog) { if (dialog.isConfirmed()) { // // Iterate the subdomain // try { Files.walkFileTree(Paths.get(subdomain.getAbsolutePath()), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path deleteFile, BasicFileAttributes attrs) throws IOException { try { boolean removeFromTree = deleteFile.getFileName().toString().endsWith(".xml"); Files.delete(deleteFile); if (removeFromTree) { self.treeWorkspace.removeItem(deleteFile.toFile()); } if (logger.isDebugEnabled()) { logger.debug("Deleted file: " + deleteFile.toString()); } } catch (IOException e) { logger.error("Failed to delete file: " + deleteFile.toString(), e); return FileVisitResult.TERMINATE; } return super.visitFile(deleteFile, attrs); } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { try { Files.delete(dir); self.treeWorkspace.removeItem(dir.toFile()); if (logger.isDebugEnabled()) { logger.debug("Deleted dir: " + dir.toString()); } } catch (IOException e) { logger.error("Failed to delete directory: " + dir.toString(), e); return FileVisitResult.TERMINATE; } return super.postVisitDirectory(dir, exc); } }); } catch (IOException e) { logger.error("Failed to walk subdomain: " + subdomain.getAbsolutePath(), e); } } } }, true); }
From source file:jduagui.Controller.java
public static long getSize(String startPath, Map<String, Long> dirs, Map<String, Long> files) throws IOException { final AtomicLong size = new AtomicLong(0); final AtomicLong subdirs = new AtomicLong(0); final AtomicLong fs = new AtomicLong(0); final File f = new File(startPath); final String str = ""; Path path = Paths.get(startPath); Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override/*from w w w . ja v a 2 s . co m*/ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { subdirs.incrementAndGet(); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fs.incrementAndGet(); size.addAndGet(attrs.size()); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { fs.incrementAndGet(); return FileVisitResult.CONTINUE; } }); if (subdirs.decrementAndGet() == -1) subdirs.incrementAndGet(); if (f.isDirectory()) { dirs.put(startPath, subdirs.get()); files.put(startPath, fs.get()); } return size.get(); }
From source file:org.mycore.common.MCRUtils.java
/** * Extracts files in a tar archive. Currently works only on uncompressed tar files. * //from ww w. j a v a 2 s .c om * @param source * the uncompressed tar to extract * @param expandToDirectory * the directory to extract the tar file to * @throws IOException * if the source file does not exists */ public static void untar(Path source, Path expandToDirectory) throws IOException { try (TarArchiveInputStream tain = new TarArchiveInputStream(Files.newInputStream(source))) { TarArchiveEntry tarEntry; FileSystem targetFS = expandToDirectory.getFileSystem(); HashMap<Path, FileTime> directoryTimes = new HashMap<>(); while ((tarEntry = tain.getNextTarEntry()) != null) { Path target = MCRPathUtils.getPath(targetFS, tarEntry.getName()); Path absoluteTarget = expandToDirectory.resolve(target).normalize().toAbsolutePath(); if (tarEntry.isDirectory()) { Files.createDirectories(expandToDirectory.resolve(absoluteTarget)); directoryTimes.put(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime())); } else { if (Files.notExists(absoluteTarget.getParent())) { Files.createDirectories(absoluteTarget.getParent()); } Files.copy(tain, absoluteTarget, StandardCopyOption.REPLACE_EXISTING); Files.setLastModifiedTime(absoluteTarget, FileTime.fromMillis(tarEntry.getLastModifiedDate().getTime())); } } //restore directory dates Files.walkFileTree(expandToDirectory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Path absolutePath = dir.normalize().toAbsolutePath(); Files.setLastModifiedTime(absolutePath, directoryTimes.get(absolutePath)); return super.postVisitDirectory(dir, exc); } }); } }
From source file:org.apache.cassandra.db.Directories.java
public long getTrueAllocatedSizeIn(File input) { if (!input.isDirectory()) return 0; TrueFilesSizeVisitor visitor = new TrueFilesSizeVisitor(); try {/* ww w. j av a 2 s . co m*/ Files.walkFileTree(input.toPath(), visitor); } catch (IOException e) { logger.error("Could not calculate the size of {}. {}", input, e); } return visitor.getAllocatedSize(); }
From source file:jduagui.Controller.java
public static void getExtensions(String startPath, Map<String, Extension> exts) throws IOException { final AtomicReference<String> extension = new AtomicReference<>(""); final File f = new File(startPath); final String str = ""; Path path = Paths.get(startPath); Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override//from w w w . j a v a 2 s. c o m public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { storageCache.put(file.toAbsolutePath().toString(), attrs.size()); extension.set(FilenameUtils.getExtension(file.toAbsolutePath().toString())); if (extension.get().equals(str)) { if (exts.containsKey(noExt)) { exts.get(noExt).countIncrement(); exts.get(noExt).increaseSize(attrs.size()); } else { exts.put(noExt, new Extension(new AtomicLong(1), new AtomicLong(attrs.size()))); } } else { if (exts.containsKey(extension.get())) { exts.get(extension.get()).countIncrement(); exts.get(extension.get()).increaseSize(attrs.size()); } else { exts.put(extension.get(), new Extension(new AtomicLong(1), new AtomicLong(attrs.size()))); } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } }); }
From source file:org.structr.rdfs.OWLParserv2.java
public void parse(final String fileName, final String blobsDirectory) { boolean success = true; try (final App app = StructrApp.getInstance()) { final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new File(fileName)); System.out.println("Parsing XML document.."); logger.println("Parsing XML document.."); // parse XML document parseDocument(doc.getDocumentElement(), 0); System.out.println("Filtering unwanted classes.."); logger.println("Filtering unwanted classes.."); // filter unwanted objects by their IDs filter(owlClassesByURI.values()); filter(owlPropertiesByURI.values()); if (importSchema) { // initialize class hierarchies System.out.println("Resolving " + owlClassesByURI.size() + " OWL superclasses.."); logger.println("Resolving " + owlClassesByURI.size() + " OWL superclasses.."); for (final OWLClass owlClass : owlClassesByURI.values()) { owlClass.resolveSuperclasses(owlClassesByURI); }//ww w . j av a 2 s.c om for (final OWLClass owlClass : owlClassesByURI.values()) { owlClass.resolveRelatedTypes(owlClassesByURI); } for (final OWLClass owlClass : owlClassesByURI.values()) { owlClass.resolveRelationshipTypes(owlClassesByURI); } // initialize classes with datatype properties System.out.println("Resolving " + owlPropertiesByURI.size() + " datatype properties.."); logger.println("Resolving " + owlPropertiesByURI.size() + " datatype properties.."); for (final OWLProperty owlProperty : owlPropertiesByURI.values()) { owlProperty.resolveSuperclasses(owlPropertiesByURI); owlProperty.resolveClassProperties(owlClassesByURI); } final JsonSchema schema = StructrSchema.newInstance(URI.create("http://localhost/test/#")); // create common base class final JsonType baseType = schema.addType("BaseNode"); final JsonType nameType = schema.addType("LocalizedName"); nameType.addStringProperty("locale").setIndexed(true); nameType.addStringProperty("name").setIndexed(true); baseType.addStringProperty("originId").setIndexed(true); baseType.addDateProperty("createdAt").setIndexed(true); baseType.addDateProperty("modifiedAt").setIndexed(true); baseType.addFunctionProperty("isFallbackLang", "ui").setContentType("text/structrscript") .setReadFunction( "(empty(get_or_null(first(filter(this.names, equal(data.locale, substring(locale, 0, 2)))), 'name')))") .setIndexed(true); baseType.addFunctionProperty("localizedName", "ui").setContentType("text/structrscript") .setReadFunction( "(if (equal('zh', substring(locale, 0, 2)),(if (empty(first(filter(this.names, equal(data.locale, 'zh')))),if (empty(first(filter(this.names, equal(data.locale, 'en')))),get_or_null(first(filter(this.names, equal(data.locale, 'de'))), 'name'),get(first(filter(this.names, equal(data.locale, 'en'))), 'name')),get(first(filter(this.names, equal(data.locale, 'zh'))), 'name'))),if (equal('de', substring(locale, 0, 2)),(if (empty(first(filter(this.names, equal(data.locale, 'de')))),if (empty(first(filter(this.names, equal(data.locale, 'en')))),get_or_null(first(filter(this.names, equal(data.locale, 'zh'))), 'name'),get(first(filter(this.names, equal(data.locale, 'en'))), 'name')),get(first(filter(this.names, equal(data.locale, 'de'))), 'name'))),(if (empty(first(filter(this.names, equal(data.locale, 'en')))),if (empty(first(filter(this.names, equal(data.locale, 'de')))),get_or_null(first(filter(this.names, equal(data.locale, 'zh'))), 'name'),get(first(filter(this.names, equal(data.locale, 'de'))), 'name')),get(first(filter(this.names, equal(data.locale, 'en'))), 'name'))))))") .setIndexed(true); baseType.addFunctionProperty("nameDE", "ui").setContentType("text/structrscript") .setReadFunction("get_or_null(first(filter(this.names, equal(data.locale, 'de'))), 'name')") .setWriteFunction( "(store('node', first(filter(this.names, equal(data.locale, 'de')))),if (empty(retrieve('node')),set(this, 'names', merge(this.names, create('LocalizedName', 'locale', 'de', 'name', value))),(if (empty(value),delete(retrieve('node')),set(retrieve('node'), 'name', value)))))") .setIndexed(true); baseType.addFunctionProperty("nameEN", "ui").setContentType("text/structrscript") .setReadFunction("get_or_null(first(filter(this.names, equal(data.locale, 'en'))), 'name')") .setWriteFunction( "(store('node', first(filter(this.names, equal(data.locale, 'en')))),if (empty(retrieve('node')),set(this, 'names', merge(this.names, create('LocalizedName', 'locale', 'en', 'name', value))),(if (empty(value),delete(retrieve('node')),set(retrieve('node'), 'name', value)))))") .setIndexed(true); baseType.addFunctionProperty("nameZH", "ui").setContentType("text/structrscript") .setReadFunction("get_or_null(first(filter(this.names, equal(data.locale, 'zh'))), 'name')") .setWriteFunction( "(store('node', first(filter(this.names, equal(data.locale, 'zh')))),if (empty(retrieve('node')),set(this, 'names', merge(this.names, create('LocalizedName', 'locale', 'zh', 'name', value))),(if (empty(value),delete(retrieve('node')),set(retrieve('node'), 'name', value)))))") .setIndexed(true); final JsonReferenceType names = ((JsonObjectType) baseType).relate((JsonObjectType) nameType, "HasName", Cardinality.OneToMany); names.setSourcePropertyName("isNameOf"); names.setTargetPropertyName("names"); final JsonReferenceType extensions = ((JsonObjectType) baseType).relate((JsonObjectType) baseType, "ExtendedBy", Cardinality.ManyToMany); extensions.setSourcePropertyName("extends"); extensions.setTargetPropertyName("extendedBy"); baseType.addStringProperty("name").setIndexed(true); System.out.println("Creating schema.."); logger.println("Creating schema.."); try (final Tx tx = StructrApp.getInstance().tx()) { for (final OWLClass owlClass : owlClassesByURI.values()) { final String name = owlClass.getStructrName(true); if (name != null && schema.getType(name) == null && owlClass.isPrimary()) { logger.println("Creating type " + name + ".."); schema.addType(name); } } StructrSchema.replaceDatabaseSchema(app, schema); tx.success(); } catch (FrameworkException fex) { System.out.println(fex.getErrorBuffer().getErrorTokens()); } // resolve inheritance System.out.println("Resolving class inheritance.."); logger.println("Resolving class inheritance.."); try (final Tx tx = StructrApp.getInstance().tx()) { for (final OWLClass owlClass : owlClassesByURI.values()) { final String name = owlClass.getStructrName(true); final JsonType type = schema.getType(name); final OWLClass superclass = owlClass.getSuperclass(); // type can be null if it is inverseOf another type if (type != null) { if (superclass != null) { final JsonType superType = schema.getType(superclass.getStructrName(true)); if (superType != null) { type.setExtends(superType); } else { type.setExtends(baseType); } } else { type.setExtends(baseType); } for (final Name localizedName : owlClass.getNames()) { app.create(Localization.class, new NodeAttribute(Localization.name, name), new NodeAttribute(Localization.localizedName, localizedName.name), new NodeAttribute(Localization.locale, localizedName.lang)); } } } StructrSchema.replaceDatabaseSchema(app, schema); tx.success(); } catch (FrameworkException fex) { System.out.println(fex.getErrorBuffer().getErrorTokens()); } // resolve relationship types System.out.println("Resolving relationship types.."); logger.println("Resolving relationship types.."); try (final Tx tx = StructrApp.getInstance().tx()) { for (final OWLClass possibleOutgoingRelationshipType : owlClassesByURI.values()) { final OWLClass possibleIncomingRelationshipType = possibleOutgoingRelationshipType .getInverse(); if (possibleOutgoingRelationshipType.isPrimary() && possibleIncomingRelationshipType != null) { // this is a relationship final List<OWLClass> sourceTypes = possibleOutgoingRelationshipType .getActualSourceTypes(); final List<OWLClass> targetTypes = possibleOutgoingRelationshipType .getActualTargetTypes(); for (final OWLClass sourceType : sourceTypes) { for (final OWLClass targetType : targetTypes) { final String sourceName = possibleOutgoingRelationshipType .getStructrName(false); final String targetName = possibleIncomingRelationshipType .getStructrName(false); final String sourceTypeName = sourceType.getStructrName(true); final String targetTypeName = targetType.getStructrName(true); final JsonType sourceJsonType = schema.getType(sourceTypeName); final JsonType targetJsonType = schema.getType(targetTypeName); if (sourceJsonType != null && targetJsonType != null) { final String relationshipTypeName = possibleOutgoingRelationshipType .getStructrName(true); final JsonObjectType relType = schema.addType(relationshipTypeName); final JsonObjectType srcType = (JsonObjectType) sourceJsonType; final JsonObjectType tgtType = (JsonObjectType) targetJsonType; srcType.relate(relType, sourceName, Cardinality.OneToMany, sourceType.getStructrName(false), sourceName); relType.relate(tgtType, targetName, Cardinality.ManyToOne, targetName, targetType.getStructrName(false)); possibleOutgoingRelationshipType.setIsRelationship(true); } } } } } StructrSchema.replaceDatabaseSchema(app, schema); tx.success(); } catch (FrameworkException fex) { System.out.println(fex.getErrorBuffer().getErrorTokens()); } System.out.println("Adding properties to types"); logger.println("Adding properties to types"); try (final Tx tx = StructrApp.getInstance().tx()) { for (final OWLClass owlClass : owlClassesByURI.values()) { final String typeName = owlClass.getStructrName(true); JsonType type = schema.getType(typeName); // type not found, try to set property on inverse type if (type == null) { final OWLClass inverse = owlClass.getInverse(); if (inverse != null) { type = schema.getType(inverse.getStructrName(true)); } } if (type != null) { for (final OWLProperty prop : owlClass.getAllProperties()) { addProperty(type, prop, prop.getStructrName(false)); } } else { System.out.println("Class: no type found for " + owlClass.getId()); } } StructrSchema.replaceDatabaseSchema(app, schema); tx.success(); } System.out.println("Adding metdata to node types"); logger.println("Adding metdata to node types"); try (final Tx tx = StructrApp.getInstance().tx()) { for (final OWLClass owlClass : owlClassesByURI.values()) { final String name = owlClass.getStructrName(true); final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName(name).getFirst(); String icon = owlClass.getIcon(); if (schemaNode != null) { // truncate icon name, use only the // part after the second dash if (icon != null && icon.contains("-")) { // start with final int pos = icon.indexOf("-", 7); if (pos > -1) { icon = icon.substring(pos + 1); } } schemaNode.setProperty(SchemaNode.icon, icon); } } tx.success(); } catch (FrameworkException fex) { System.out.println(fex.getErrorBuffer().getErrorTokens()); } // create instances System.out.println("Resolving instances.."); logger.println("Resolving instances.."); final Iterator<OWLInstance> instancesIterator = owlInstances.values().iterator(); final List<OWLInstance> newInstances = new LinkedList<>(); int count = 0; while (instancesIterator.hasNext()) { try (final Tx tx = StructrApp.getInstance().tx()) { while (instancesIterator.hasNext()) { final OWLInstance instance = instancesIterator.next(); final OWLClass owlType = instance.getType(); if (owlType != null) { instance.createDatabaseNode(app); instance.resolveProperties(); instance.resolveExtensions(app, owlClassesByFragment, owlInstances, newInstances); } if (++count == 100) { count = 0; break; } } tx.success(); } } // add newly created extension instances to global map for (final OWLInstance newInstance : newInstances) { owlInstances.put(newInstance.getId(), newInstance); } System.out.println("Resolving instance relationships.."); logger.println("Resolving instance relationships.."); final Iterator<OWLInstance> relationshipsIterator = owlInstances.values().iterator(); count = 0; while (relationshipsIterator.hasNext()) { try (final Tx tx = StructrApp.getInstance().tx()) { while (relationshipsIterator.hasNext()) { final OWLInstance instance = relationshipsIterator.next(); final OWLClass owlType = instance.getType(); if (owlType != null) { instance.resolveRelationships(schema, owlClassesByFragment, owlInstances, rdfDescriptions, owlPropertiesByName); } if (++count == 100) { count = 0; break; } } tx.success(); } } } final File blobs = new File(blobsDirectory); if (blobs.exists()) { final ConfigurationProvider config = StructrApp.getConfiguration(); final List<Tuple<Class, PropertyKey>> mapping = createPropertyKeyMapping(config); final Set<Path> files = new LinkedHashSet<>(); int count = 0; // collect all files Files.walkFileTree(blobs.toPath(), new Visitor(files)); if (createFileRelationships) { System.out.println("Resolving file relationships.."); logger.println("Resolving file relationships.."); // iterate over files to identify relationships and extend schema final Iterator<Path> pathIteratorForSchemaExtension = files.iterator(); try (final Tx tx = StructrApp.getInstance().tx()) { while (pathIteratorForSchemaExtension.hasNext()) { final Path file = pathIteratorForSchemaExtension.next(); final String name = file.getFileName().toString(); final int pos = name.indexOf(".", 7); final String idPart = name.substring(6, pos == -1 ? name.length() : pos); if (name.startsWith("KBlob-") && name.length() > 23) { for (final Tuple<Class, PropertyKey> entry : mapping) { final Class type = entry.getKey(); final PropertyKey key = entry.getValue(); Object value = idPart; if (key instanceof ArrayProperty) { value = new String[] { idPart }; } final Query<NodeInterface> query = app.nodeQuery(false).andType(type).and(key, value, false); final List<NodeInterface> nodes = query.getAsList(); if (nodes.size() == 1) { System.out.println( " ##########: " + nodes.size() + " results.."); // create schema relationship from schema type to file (once) // import file // link file final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class) .andName(type.getSimpleName()).getFirst(); if (schemaNode != null) { System.out.println(" ##########: found SchemaNode " + schemaNode.getUuid() + " (" + schemaNode.getName() + ")"); final SchemaNode fileSchemaNode = app.nodeQuery(SchemaNode.class) .andName(org.structr.dynamic.File.class.getSimpleName()) .getFirst(); if (fileSchemaNode != null) { final String capitalJsonName = StringUtils .capitalize(key.jsonName()); final String targetJsonName = "has" + capitalJsonName; final String sourceJsonName = "is" + capitalJsonName + "Of" + type.getSimpleName(); final SchemaRelationshipNode link = app .nodeQuery(SchemaRelationshipNode.class) .and(SchemaRelationshipNode.sourceNode, schemaNode) .and(SchemaRelationshipNode.targetNode, fileSchemaNode) .and(SchemaRelationshipNode.relationshipType, key.jsonName()) .getFirst(); if (link == null) { System.out.println("Creating link from " + schemaNode + " to " + fileSchemaNode + ", " + sourceJsonName + ", " + targetJsonName); app.create(SchemaRelationshipNode.class, new NodeAttribute(SchemaRelationshipNode.sourceNode, schemaNode), new NodeAttribute(SchemaRelationshipNode.targetNode, fileSchemaNode), new NodeAttribute( SchemaRelationshipNode.relationshipType, key.jsonName()), new NodeAttribute( SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute( SchemaRelationshipNode.targetMultiplicity, key instanceof ArrayProperty ? "*" : "1"), new NodeAttribute(SchemaRelationshipNode.sourceJsonName, sourceJsonName), new NodeAttribute(SchemaRelationshipNode.targetJsonName, targetJsonName)); } else { System.out.println("Link relationship already exists: " + link); } } else { System.out.println("NO SchemaNode found for type File!"); } } else { System.out.println( "NO SchemaNode found for type " + type.getSimpleName() + "!"); } // no need to search further //break; } } } } tx.success(); } } if (importFiles) { System.out.println("Importing files.."); logger.println("Importing files.."); final SecurityContext superUserSecurityContext = SecurityContext.getSuperUserInstance(); final Iterator<Path> pathIteratorForRelationshipCreation = files.iterator(); while (pathIteratorForRelationshipCreation.hasNext()) { try (final Tx tx = StructrApp.getInstance().tx()) { while (pathIteratorForRelationshipCreation.hasNext()) { final Path file = pathIteratorForRelationshipCreation.next(); final String name = file.getFileName().toString(); final int pos = name.indexOf(".", 7); final String idPart = name.substring(6, pos == -1 ? name.length() : pos); boolean found = false; if (name.startsWith("KBlob-") && name.length() > 23) { for (final Tuple<Class, PropertyKey> entry : mapping) { final Class type = entry.getKey(); final PropertyKey key = entry.getValue(); final boolean isMultiple = (key instanceof ArrayProperty); Object value = idPart; if (isMultiple) { value = new String[] { idPart }; } final Query<NodeInterface> query = app.nodeQuery(false).andType(type) .and(key, value, false); final List<NodeInterface> nodes = query.getAsList(); if (nodes.size() == 1) { final String capitalJsonName = StringUtils.capitalize(key.jsonName()); final String targetJsonName = "has" + capitalJsonName; final NodeInterface node = nodes.get(0); final PropertyKey fileRelationshipKey = config .getPropertyKeyForJSONName(type, targetJsonName, false); if (fileRelationshipKey != null) { try (final InputStream is = new FileInputStream(file.toFile())) { // import file.. final Class fileType = ImageHelper.isImageType(name) ? Image.class : org.structr.dynamic.File.class; if (isMultiple) { final String[] possibleNames = (String[]) node .getProperty(key); String actualName = name; for (final String possibleName : possibleNames) { if (possibleName.startsWith(name)) { actualName = possibleName .substring(name.length() + 1); break; } } logger.println( " Importing " + name + " => " + actualName); final FileBase importedFile = FileHelper.createFile( superUserSecurityContext, is, null, fileType, actualName); final List<FileBase> fileList = (List<FileBase>) node .getProperty(fileRelationshipKey); fileList.add(importedFile); node.setProperty(fileRelationshipKey, fileList); } else { final String possibleName = (String) node.getProperty(key); String actualName = name; if (possibleName != null) { actualName = possibleName.substring(name.length() + 1); } logger.println( " Importing " + name + " => " + actualName); final FileBase importedFile = FileHelper.createFile( superUserSecurityContext, is, null, fileType, actualName); node.setProperty(fileRelationshipKey, importedFile); } } catch (Throwable t) { t.printStackTrace(); } } else { System.out.println("############################# INVALID KEY " + type.getSimpleName() + "." + targetJsonName + ", not found??!"); logger.println("############################# INVALID KEY " + type.getSimpleName() + "." + targetJsonName + ", not found??!"); } found = true; // no need to search further break; } } } if (!found) { System.out.println("Found NO document for file " + name + ", importing without association"); logger.println("Found NO document for file " + name + ", importing without association"); try (final InputStream is = new FileInputStream(file.toFile())) { // import file.. final Class fileType = ImageHelper.isImageType(name) ? Image.class : org.structr.dynamic.File.class; FileHelper.createFile(superUserSecurityContext, is, null, fileType, name); } catch (Throwable t) { t.printStackTrace(); } } if (++count == 100) { count = 0; break; } } tx.success(); } } } } } catch (Throwable t) { t.printStackTrace(); success = false; } if (success) { System.out.println("Import successful"); logger.println("Import successful"); } logger.flush(); logger.close(); }
From source file:org.jenkinsci.plugins.workflow.support.steps.ExecutorStepTest.java
@Initializer(before = InitMilestone.JOB_LOADED) public static void replaceWorkspacePath() throws Exception { final File prj = new File(Jenkins.getInstance().getRootDir(), "jobs/p"); final File workspace = new File(prj, "workspace"); final String ORIG_WS = "/space/tmp/AbstractStepExecutionImpl-upgrade/jobs/p/workspace"; final String newWs = workspace.getAbsolutePath(); File controlDir = new File(workspace, ".eb6272d3"); if (!controlDir.isDirectory()) { return;// w ww .j av a2 s . c o m } System.err.println("Patching " + controlDir); RiverReader.customResolver = new ObjectResolver() { @Override public Object readResolve(Object replacement) { Class<?> c = replacement.getClass(); //System.err.println("replacing " + c.getName()); while (c != Object.class) { for (Field f : c.getDeclaredFields()) { if (f.getType() == String.class) { try { f.setAccessible(true); Object v = f.get(replacement); if (ORIG_WS.equals(v)) { //System.err.println("patching " + f); f.set(replacement, newWs); patchedFields.add(f.toString()); } else if (newWs.equals(v)) { //System.err.println(f + " was already patched, somehow?"); } else { //System.err.println("some other value " + v + " for " + f); } } catch (Exception x) { x.printStackTrace(); } } } c = c.getSuperclass(); } return replacement; } @Override public Object writeReplace(Object original) { throw new IllegalStateException(); } }; Files.walkFileTree(prj.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { File f = file.toFile(); String name = f.getName(); if (name.equals("program.dat")) { /* TODO could not get this to work; stream appeared corrupted: patchedFiles.add(name); String origContent = FileUtils.readFileToString(f, StandardCharsets.ISO_8859_1); String toReplace = String.valueOf((char) Protocol.ID_STRING_SMALL) + String.valueOf((char) ORIG_WS.length()) + ORIG_WS; int newLen = newWs.length(); String replacement = String.valueOf((char) Protocol.ID_STRING_MEDIUM) + String.valueOf((char) (newLen & 0xff00) >> 8) + String.valueOf((char) newLen & 0xff) + newWs; // TODO breaks if not ASCII String replacedContent = origContent.replace(toReplace, replacement); assertNotEquals("failed to replace " + toReplace + "", replacedContent, origContent); FileUtils.writeStringToFile(f, replacedContent, StandardCharsets.ISO_8859_1); */ } else { String origContent = FileUtils.readFileToString(f, StandardCharsets.ISO_8859_1); String replacedContent = origContent.replace(ORIG_WS, newWs); if (!replacedContent.equals(origContent)) { patchedFiles.add(name); FileUtils.writeStringToFile(f, replacedContent, StandardCharsets.ISO_8859_1); } } return super.visitFile(file, attrs); } }); FilePath controlDirFP = new FilePath(controlDir); controlDirFP.child("jenkins-result.txt").write("0", null); FilePath log = controlDirFP.child("jenkins-log.txt"); log.write(log.readToString() + "simulated later output\n", null); }
From source file:com.upplication.s3fs.util.AmazonS3ClientMock.java
@Override public void deleteBucket(String bucketName) throws AmazonClientException { try {/*from w ww . j a va 2s. c o m*/ Path bucket = base.resolve(bucketName); Files.walkFileTree(bucket, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new AmazonClientException(e); } }
From source file:org.olat.core.util.ZipUtil.java
/** * Add the content of a directory to a zip stream. * // ww w . j a va 2s . c o m * @param path * @param dirName * @param zout */ public static void addDirectoryToZip(final Path path, final String baseDirName, final ZipOutputStream zout) { try { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!attrs.isDirectory()) { Path relativeFile = path.relativize(file); String names = baseDirName + "/" + relativeFile.toString(); zout.putNextEntry(new ZipEntry(names)); try (InputStream in = Files.newInputStream(file)) { FileUtils.copy(in, zout); } catch (Exception e) { log.error("", e); } zout.closeEntry(); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { log.error("", e); } }