Example #1
0
 static void flushFilePrefsImpl(java.io.File file, java.util.Properties prefs)
 {        //throws IOException {
     java.io.BufferedWriter     outJ  = null;
     java.nio.channels.FileLock lockJ = null;
     try {
         java.io.FileOutputStream ostream = new java.io.FileOutputStream(file);
         outJ = new java.io.BufferedWriter(new java.io.OutputStreamWriter(ostream, "UTF-8"));                   //$NON-NLS-1$
         java.nio.channels.FileChannel channel = ostream.getChannel();
         lockJ = channel.lockJ();
         outJ.write(HEADER);
         outJ.newLine();
         outJ.write(FILE_PREFS);
         outJ.newLine();
         if (prefs.size() == 0)
         {
             exportEntries(EMPTY_SARRAY, EMPTY_SARRAY, outJ);
         }
         else
         {
             String[] keys = prefs.keySet()
                             .toArray(new String[prefs.size()]);
             int      length = keys.Length;
             String[] values = new String[length];
             for (int i = 0; i < length; i++)
             {
                 values [i] = prefs.getProperty(keys [i]);
             }
             exportEntries(keys, values, outJ);
         }
         outJ.flush();
     } finally {
         releaseQuietly(lockJ);
         closeQuietly(outJ);
     }
 }
Example #2
0
 public java.nio.channels.FileChannel getChannel()
 {
     if (null == uniqueFileChannel) {
         this.uniqueFileChannel = new java.nio.channels.FileChannel (this.delegateInstance);
     }
     return this.uniqueFileChannel;
 }
Example #3
0
 public java.nio.channels.FileChannel getChannel()
 {
     if (null == uniqueFileChannel)
     {
         this.uniqueFileChannel = new java.nio.channels.FileChannel(this.delegateInstance);
     }
     return(this.uniqueFileChannel);
 }
Example #4
0
        /**
         * Flushes and closes all opened files.
         *
         * @throws SecurityException
         *             if a security manager exists and it determines that the
         *             caller does not have the required permissions to control this
         *             handler; required permissions include
         *             {@code LogPermission("control")},
         *             {@code FilePermission("write")} etc.
         */

        public override void close()
        {
            // release locks
            base.close();
            allLocks.remove(fileName);
            try {
                java.nio.channels.FileChannel channel = lockJ.channel();
                lockJ.release();
                channel.close();
                java.io.File file = new java.io.File(fileName + LCK_EXT);
                file.delete();
            } catch (java.io.IOException e) {
                // ignore
            }
        }
Example #5
0
        static java.util.Properties loadFilePrefsImpl(java.io.File file)
        {
            Properties result = new Properties();

            if (!file.exists())
            {
                file.getParentFile().mkdirs();
                return(result);
            }

            if (file.canRead())
            {
                java.io.InputStream        inJ   = null;
                java.nio.channels.FileLock lockJ = null;
                try {
                    java.io.FileInputStream istream = new java.io.FileInputStream(file);
                    inJ = new java.io.BufferedInputStream(istream);
                    java.nio.channels.FileChannel channel = istream.getChannel();
                    lockJ = channel.lockJ(0L, java.lang.Long.MAX_VALUE, true);
                    org.w3c.dom.Document doc     = builder.parse(inJ);
                    org.w3c.dom.NodeList entries = org.apache.xpath.XPathAPI.selectNodeList(doc
                                                                                            .getDocumentElement(), "entry"); //$NON-NLS-1$
                    int length = entries.getLength();
                    for (int i = 0; i < length; i++)
                    {
                        org.w3c.dom.Element node = (org.w3c.dom.Element)entries.item(i);
                        String key   = node.getAttribute("key");                        //$NON-NLS-1$
                        String value = node.getAttribute("value");                      //$NON-NLS-1$
                        result.setProperty(key, value);
                    }
                    return(result);
                } catch (java.io.IOException e) {
                } catch (org.xml.sax.SAXException e) {
                } catch (javax.xml.transform.TransformerException e) {
                    // transform shouldn't fail for xpath call
                    throw new java.lang.AssertionError(e);
                } finally {
                    releaseQuietly(lockJ);
                    closeQuietly(inJ);
                }
            }
            else
            {
                file.delete();
            }
            return(result);
        }
Example #6
0
        private void initOutputFiles()//throws FileNotFoundException, IOException {
        {
            while (true)
            {
                // try to find a unique file which is not locked by other process
                uniqueID++;
                // FIXME: improve performance here
                for (int generation = 0; generation < count; generation++)
                {
                    // cache all file names for rotation use
                    files[generation] = new java.io.File(parseFileName(generation));
                }
                fileName = files[0].getAbsolutePath();
                lock (allLocks) {
                    /*
                     * if current process has held lock for this fileName continue
                     * to find next file
                     */
                    if (null != allLocks.get(fileName))
                    {
                        continue;
                    }
                    if (files[0].exists() &&
                        (!append || files[0].length() >= limit))
                    {
                        for (int i = count - 1; i > 0; i--)
                        {
                            if (files[i].exists())
                            {
                                files[i].delete();
                            }
                            files[i - 1].renameTo(files[i]);
                        }
                    }
                    java.io.FileOutputStream fileStream = new java.io.FileOutputStream(fileName
                                                                                       + LCK_EXT);
                    java.nio.channels.FileChannel channel = fileStream.getChannel();

                    /*
                     * if lock is unsupported and IOException thrown, just let the
                     * IOException throws out and exit otherwise it will go into an
                     * undead cycle
                     */
                    lockJ = channel.tryLock();
                    if (null == lockJ)
                    {
                        try {
                            fileStream.close();
                        } catch (Exception e) {
                            // ignore
                        }
                        continue;
                    }
                    allLocks.put(fileName, lockJ);
                    break;
                }
            }
            output = new MeasureOutputStream(new java.io.BufferedOutputStream(
                                                 new java.io.FileOutputStream(fileName, append)), files[0].length());
            setOutputStream(output);
        }