KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > fileobjects > WriteLockUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs.fileobjects;
21
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.nio.ByteBuffer JavaDoc;
26 import java.nio.channels.FileChannel JavaDoc;
27 import org.openide.util.Exceptions;
28
29
30 /**
31  * @author Radek Matous
32  */

33
34
35 public class WriteLockUtils {
36     static final String JavaDoc PREFIX = ".LCK";
37     static final String JavaDoc SUFFIX = "~";
38
39
40     private WriteLockUtils(){}
41
42     public static synchronized boolean hasActiveLockFileSigns(final String JavaDoc filePath) {
43         return filePath.indexOf(WriteLockUtils.PREFIX) != -1 && filePath.indexOf(WriteLockUtils.SUFFIX) != -1;
44     }
45     
46     public static synchronized boolean isActiveLockFile(final File JavaDoc file) {
47         final String JavaDoc name = file.getName();
48         boolean isActiveLockFile = hasActiveLockFileSigns(name);
49         if (isActiveLockFile) {
50             final String JavaDoc newName = name.substring(WriteLockUtils.PREFIX.length(), (name.length() - WriteLockUtils.SUFFIX.length()));
51             isActiveLockFile = new File JavaDoc(file.getParentFile(), newName).exists();
52         }
53         
54         return isActiveLockFile;
55     }
56     
57     public static File JavaDoc getAssociatedLockFile(File JavaDoc file) {
58         try {
59             file = file.getCanonicalFile();
60         } catch (IOException JavaDoc iex) {
61             Exceptions.printStackTrace(iex);
62         }
63         
64         final File JavaDoc parentFile = file.getParentFile();
65         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
66         
67         sb.append(WriteLockUtils.PREFIX);//NOI18N
68
sb.append(file.getName());//NOI18N
69
sb.append(WriteLockUtils.SUFFIX);//NOI18N
70

71         final String JavaDoc lckName = sb.toString();
72         final File JavaDoc lck = new File JavaDoc(parentFile, lckName);
73         return lck;
74     }
75     
76     static String JavaDoc getContentOfLckFile(File JavaDoc lckFile, FileChannel JavaDoc channel) throws IOException JavaDoc {
77         final byte[] readContent = new byte[(int) lckFile.length()];
78         channel.read(ByteBuffer.wrap(readContent));
79         
80         final String JavaDoc retVal = new String JavaDoc(readContent);
81         return (new File JavaDoc(retVal).exists()) ? retVal : null;
82     }
83     
84     static String JavaDoc writeContentOfLckFile(final File JavaDoc lck, FileChannel JavaDoc channel) throws IOException JavaDoc {
85         final String JavaDoc absolutePath = lck.getAbsolutePath();
86         final ByteBuffer JavaDoc buf = ByteBuffer.wrap(absolutePath.getBytes());
87         channel.write(buf);
88         return absolutePath;
89     }
90 }
91
Popular Tags