private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException { try { ZipInputStream str = new ZipInputStream(source); ZipEntry entry; while ((entry = str.getNextEntry()) != null) { if (entry.isDirectory()) { FileUtil.createFolder(projectRoot, entry.getName()); } else { FileObject fo = FileUtil.createData(projectRoot, entry.getName()); FileLock lock = fo.lock(); try { OutputStream out = fo.getOutputStream(lock); try { FileUtil.copy(str, out); } finally { out.close(); } } finally { lock.releaseLock(); } } } } finally { source.close(); } }
@Override public Spatial loadAsset() { ProgressHandle handle = ProgressHandleFactory.createHandle("Converting OgreBinary"); handle.start(); // mesh OgreXMLConvertOptions options = new OgreXMLConvertOptions(getPrimaryFile().getPath()); options.setBinaryFile(true); OgreXMLConvert conv = new OgreXMLConvert(); conv.doConvert(options, handle); // try skeleton if (getPrimaryFile().existsExt("skeleton")) { OgreXMLConvertOptions options2 = new OgreXMLConvertOptions( getPrimaryFile() .getParent() .getFileObject(getPrimaryFile().getName(), "skeleton") .getPath()); options2.setBinaryFile(true); OgreXMLConvert conv2 = new OgreXMLConvert(); conv2.doConvert(options2, handle); } handle.progress("Convert Model"); ProjectAssetManager mgr = getLookup().lookup(ProjectAssetManager.class); if (mgr == null) { DialogDisplayer.getDefault() .notifyLater( new NotifyDescriptor.Message( "File is not part of a project!\nCannot load without ProjectAssetManager.")); return null; } String assetKey = mgr.getRelativeAssetPath(options.getDestFile()); FileLock lock = null; try { lock = getPrimaryFile().lock(); listListener.start(); Spatial spatial = mgr.loadModel(assetKey); // replace transient xml files in list of assets for this model replaceXmlFiles(); listListener.stop(); savable = spatial; lock.releaseLock(); File deleteFile = new File(options.getDestFile()); deleteFile.delete(); handle.finish(); return spatial; } catch (IOException ex) { Exceptions.printStackTrace(ex); if (lock != null) { lock.releaseLock(); } } File deleteFile = new File(options.getDestFile()); deleteFile.delete(); handle.finish(); return null; }
private void writeIntoFile(FileObject file, String what) throws Exception { FileLock lock = file.lock(); OutputStream out = file.getOutputStream(lock); try { out.write(what.getBytes()); } finally { out.close(); lock.releaseLock(); } }
/** * Create a new application manifest file with minimal initial contents. * * @param dir the directory to create it in * @param path the relative path of the file * @throws IOException in case of problems */ private static void createManifest(FileObject dir, String path) throws IOException { FileObject manifest = dir.createData(MANIFEST_FILE); FileLock lock = manifest.lock(); try { OutputStream os = manifest.getOutputStream(lock); try { PrintWriter pw = new PrintWriter(os); pw.println("Manifest-Version: 1.0"); // NOI18N pw.println("X-COMMENT: Main-Class will be added automatically by build"); // NOI18N pw.println(); // safest to end in \n\n due to JRE parsing bug pw.flush(); } finally { os.close(); } } finally { lock.releaseLock(); } }
/** * Save all cached project metadata. If <code>project.xml</code> was one of the modified files, * then {@link AntBasedProjectType#projectXmlSaved} is called, presumably creating <code> * build-impl.xml</code> and/or <code>build.xml</code>. */ public void save() throws IOException { assert ProjectManager.mutex().isWriteAccess(); if (!getProjectDirectory().isValid()) { // ProjectManager.saveProject() is called when project is deleted externally.. return; } Set<FileLock> locks = new HashSet<FileLock>(); try { synchronized (modifiedMetadataPaths) { assert !modifiedMetadataPaths.isEmpty(); Set<String> toBeCleared = new HashSet<String>(); try { for (String path : new TreeSet<String>(modifiedMetadataPaths)) { try { if (path.equals(PROJECT_XML_PATH)) { assert projectXml != null; locks.add(saveXml(projectXml, path)); } else if (path.equals(PRIVATE_XML_PATH)) { assert privateXml != null; locks.add(saveXml(privateXml, path)); } } catch (FileAlreadyLockedException x) { // #155037 LOG.log(Level.INFO, null, x); } // As metadata files are saved, take them off the modified list. toBeCleared.add(path); } } finally { modifiedMetadataPaths.removeAll(toBeCleared); LOG.log( Level.FINE, "saved {0} and have left {1}", new Object[] {toBeCleared, modifiedMetadataPaths}); } } } finally { // #57791: release locks outside synchronized block. locks.remove(null); for (FileLock lock : locks) { lock.releaseLock(); } } }
/** * Stores the Ant deployment properties in the specified file. * * @param create if false the deployment properties file won't be created if it does not exist. * @throws IOException if a problem occurs. */ public void storeAntDeploymentProperties(File file, boolean create) throws IOException { if (!create && !file.exists()) { return; } EditableProperties antProps = new EditableProperties(false); antProps.setProperty("tomcat.home", homeDir.getAbsolutePath()); // NOI18N antProps.setProperty("tomcat.url", getWebUrl()); // NOI18N antProps.setProperty("tomcat.username", getUsername()); // NOI18N file.createNewFile(); FileObject fo = FileUtil.toFileObject(file); FileLock lock = fo.lock(); try { OutputStream os = fo.getOutputStream(lock); try { antProps.store(os); } finally { os.close(); } } finally { lock.releaseLock(); } }
private void close(FileObject file) { if (writer != null) { try { writer.close(); } catch (IOException ex) { logger.log( Level.WARNING, "Unable to close OutputStream for DataSource: " + file.getPath(), ex); } } if (lock != null) { lock.releaseLock(); lock = null; } }
public static void overwriteFile(Node srcNode, Node destNode) throws IOException { DataObject srcDO = srcNode.getLookup().lookup(DataObject.class); FileObject srcFO = srcDO.getPrimaryFile(); DataObject destDO = destNode.getLookup().lookup(DataObject.class); FileObject destFO = destDO.getPrimaryFile(); // To avoid any confusion, save modified source first. if (srcDO.isModified()) { NotifyDescriptor d = new NotifyDescriptor.Confirmation( NbBundle.getMessage( FileNodeUtil.class, "MSG_SaveModifiedSource", srcFO.getNameExt()), // NOI18N NbBundle.getMessage(FileNodeUtil.class, "TTL_SaveModifiedSource"), // NOI18N NotifyDescriptor.OK_CANCEL_OPTION); if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.OK_OPTION) { EditorCookie srcEditorCookie = srcDO.getCookie(EditorCookie.class); srcEditorCookie.saveDocument(); } } // // Alternatively, we could use the in-memory copy of the source file // EditorCookie srcEditorCookie = // (EditorCookie) srcDO.getCookie(EditorCookie.class); // Document srcDocument = srcEditorCookie.getDocument(); // if (srcDocument == null) { // Task loadTask = srcEditorCookie.prepareDocument(); // new RequestProcessor().post(loadTask); // // loadTask.waitFinished(); // srcDocument = srcEditorCookie.getDocument(); // } // String srcText = srcDocument.getText(0, srcDocument.getLength()); // From now on, we will only be using the on-disk copy of the source file. if (destDO.isModified()) { NotifyDescriptor d = new NotifyDescriptor.Confirmation( NbBundle.getMessage( FileNodeUtil.class, "MSG_OverwriteModifiedDestination", destFO.getNameExt()), // NOI18N NbBundle.getMessage(FileNodeUtil.class, "TTL_OverwriteModifiedDestination"), // NOI18N NotifyDescriptor.OK_CANCEL_OPTION); if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.CANCEL_OPTION) { return; } EditorCookie destEditorCookie = destDO.getCookie(EditorCookie.class); InputStream inputStream = null; try { inputStream = srcFO.getInputStream(); String srcText = getInputStreamContents(inputStream); Document outputDocument = destEditorCookie.getDocument(); try { outputDocument.remove(0, outputDocument.getLength()); } catch (java.lang.Exception e) { // Ignore exception here on purpose. // One of the listener from xml module throws NPE: // at // org.netbeans.modules.xml.text.completion.GrammarManager.isGuarded(GrammarManager.java:170) // at // org.netbeans.modules.xml.text.completion.GrammarManager.removeUpdate(GrammarManager.java:140) // at // org.netbeans.lib.editor.util.swing.PriorityDocumentListenerList.removeUpdate(PriorityDocumentListenerList.java:63) // at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:242) // at org.netbeans.editor.BaseDocument.fireRemoveUpdate(BaseDocument.java:1305) // at org.netbeans.editor.BaseDocument.remove(BaseDocument.java:737) } outputDocument.insertString(0, srcText, null); destEditorCookie.saveDocument(); } catch (BadLocationException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) {; } } } else { FileLock lock = destFO.lock(); InputStream inputStream = null; OutputStream outputStream = null; try { outputStream = destFO.getOutputStream(lock); inputStream = srcFO.getInputStream(); FileUtil.copy(inputStream, outputStream); } finally { try { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } lock.releaseLock(); } catch (Exception e) {; } } } }