Java tutorial
package com.github.igor_kudryashov.utils.notes; /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ import java.io.File; import java.io.FileWriter; import java.io.FilenameFilter; import java.io.IOException; import org.apache.commons.lang3.StringUtils; import lotus.domino.NotesException; import lotus.domino.Session; public class NotesTempFile { private String tempFolder; private Session session; /* * ~notetp2.reg file exists in your Lotus Domino data directory while the Lotus Domino server is * running The ~notetp2.reg file is always located in the Domino temporary directory. If the * file exists in your data directory, this indicates that your Domino data directory is set as * your temporary directory. If a data directory path has not been specified in either an * environment variable or notes.ini variable, the Domino data directory will be set as the * temporary directory by default. * */ private final String NOTES_REG_FILENAME = "~notetp2.reg"; /* * The Domino temporary directory is cleaned out at server shutdown. The ~notetmp.reg is a file * that is generated by Domino every time Domino needs a temporary file to contain names of * temporary files that need to be removed at server shutdown. * * Note: These files are in binary format and cannot be read in any text editor */ private final String NOTES_TMP_FILENAME = "~notetmp.reg"; public NotesTempFile(Session session) { this.session = session; tempFolder = getTempFolder(); } public String getTempFolder() { tempFolder = getTempDirFromIni(); if (tempFolder == null) { tempFolder = getTempDirFromData(); if (tempFolder == null) { tempFolder = getTempDirFromSystem(); } } return tempFolder; } /** * You can add the Notes.ini variable "NOTES_TEMPDIR" to specify the Notes Temporary directory. * * The "NOTES_TEMPDIR" value must reference the full path to the temporary directory. (e.g., * (<drive>:\Temp), The value must point to a "temp" directory (e.g., Notes_tempdir=C:\temp; not * Notes_tempdir=C:\test) Notes also appends its own folder to the end of the path, so it will * end up being something like C:\test\temp\NotesFCEE85 * * The folder referenced by this parameter must already exist. Notes does not create * C:\test\temp if it does not already exist. It does, however, create its own NotesXXXXXX * folder in this temp folder if it does indeed exist. If the folder does not exist, Notes does * not revert to the default temp location, but instead puts the temp files in the Notes\Data * directory. * * @return the Notes Temporary directory name. */ private String getTempDirFromIni() { String tempDir = null; try { String temp = session.getEnvironmentString("notes_tempdir", true); if (StringUtils.isNotBlank(temp)) { tempDir = getTemDirFromTempDir(temp); } } catch (NotesException e) { e.printStackTrace(); } return tempDir; } /** * Data directory also sometime can be used as temporary directory. * * @return the Notes Temporary directory name. */ private String getTempDirFromData() { try { String temp = session.getEnvironmentString("directory", true); if (StringUtils.isNotBlank(temp)) { // check "~notetp2.reg" file String regFilename = temp + File.separator + NOTES_REG_FILENAME; File regFile = new File(regFilename); if (regFile.exists()) { return temp; } } } catch (NotesException e) { e.printStackTrace(); } return null; } /** * Default Lotus Notes stores its temporary files typically in such locations as: C:\Documents * and Settings\UserName\Local Settings\Temp\notes6030C8 in Notes 6.x * * @return the Notes Temporary directory name. */ private String getTempDirFromSystem() { String tempDir = System.getProperty("java.io.tmpdir"); if (StringUtils.isNotBlank(tempDir)) { tempDir = getTemDirFromTempDir(tempDir); } return tempDir; } /** * Seeking notesXXXXXX folder in temporary directory and checks for file ~notetp2.reg in this * folder. * * @param tempDir * - temporary directory. * @return temporary directory if the file ~notetp2.reg exist in this directory. */ private String getTemDirFromTempDir(String tempDir) { String temp = null; File file = new File(tempDir); String[] directories = file.list(new FilenameFilter() { @Override public boolean accept(File current, String name) { return new File(current, name).isDirectory(); } }); for (int i = 0; i < directories.length; i++) { if (directories[i].startsWith("notes")) { // check "~notetp2.reg" file String regFilename = tempDir + File.separator + directories[i] + File.separator + NOTES_REG_FILENAME; File regFile = new File(regFilename); if (regFile.exists()) { return regFile.getParent(); } } } return temp; } /** * Returns a temporary file and stores it name in the store ~ notetmp.reg * * @param postfix * - postfix of temporary filename. * @return the temporary file. */ public File getTempFile(String postfix) { File file = null; String p = postfix; try { if (p == null) { p = ".tmp"; } else { if (!p.startsWith(".")) { p = "." + p; } } file = File.createTempFile("~$0", p, new File(tempFolder)); // add filename to store File storeFile = new File(tempFolder + File.separator + NOTES_TMP_FILENAME); String filename = file.getName(); String fixfilename = StringUtils.rightPad(filename, 257, '\0'); FileWriter fw = new FileWriter(storeFile, true); fw.write(fixfilename); fw.close(); } catch (IOException e) { e.printStackTrace(); } return file; } }