KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > local > LocalFileNatives


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Martin Oberhuber (Wind River) - [184534] get attributes from native lib
11  *******************************************************************************/

12 package org.eclipse.core.internal.filesystem.local;
13
14 import org.eclipse.core.filesystem.IFileInfo;
15 import org.eclipse.core.filesystem.IFileSystem;
16 import org.eclipse.core.filesystem.provider.FileInfo;
17 import org.eclipse.core.internal.filesystem.Messages;
18 import org.eclipse.core.internal.filesystem.Policy;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.osgi.util.NLS;
21
22 abstract class LocalFileNatives {
23     private static boolean hasNatives = false;
24     private static boolean isUnicode = false;
25
26     /** instance of this library */
27     // The name convention is to use the plugin version at the time the library is changed.
28
private static final String JavaDoc LIBRARY_NAME = "localfile_1_0_0"; //$NON-NLS-1$
29

30     static {
31         try {
32             System.loadLibrary(LIBRARY_NAME);
33             hasNatives = true;
34             isUnicode = internalIsUnicode();
35         } catch (UnsatisfiedLinkError JavaDoc e) {
36             logMissingNativeLibrary(e);
37         }
38     }
39
40     /**
41      * Return the bit-mask of EFS attributes that this native
42      * file system implementation supports.
43      * <p>
44      * This is an optional method: if it has not been compiled
45      * into the native library, the client must catch the
46      * resulting UnsatisfiedLinkError and handle attributes
47      * as known by older version libraries.
48      * </p>
49      * @see IFileSystem#attributes()
50      * @return an integer bit mask of attributes.
51      */

52     private static final native int nativeAttributes();
53
54     /**
55      * Return the value that the native library thinks
56      * {@link IFileSystem#attributes()} should return.
57      *
58      * Returns -1 when the native library has not been
59      * loaded, or is a version that does not support
60      * this investigation method yet.
61      *
62      * @return an positive value that is a bit-mask
63      * suitable for use in {@link IFileSystem#attributes},
64      * or -1 if native attributes are not available.
65      */

66     public static int attributes() {
67         try {
68             return nativeAttributes();
69         } catch (UnsatisfiedLinkError JavaDoc e) {
70             //older native implementations did not support this
71
//call, so we cannot return supported attribute
72
//information for them.
73
return -1;
74         }
75     }
76     
77     /**
78      * Copies file attributes from source to destination. The copyLastModified attribute
79      * indicates whether the lastModified attribute should be copied.
80      * @param source
81      * @param destination
82      * @param copyLastModified
83      * @return <code>true</code> for success, and <code>false</code> otherwise.
84      */

85     public static boolean copyAttributes(String JavaDoc source, String JavaDoc destination, boolean copyLastModified) {
86         if (hasNatives)
87             // Note that support for copying last modified info is not implemented on Windows
88
return isUnicode ? internalCopyAttributesW(Convert.toPlatformChars(source), Convert.toPlatformChars(destination), copyLastModified) : internalCopyAttributes(Convert.toPlatformBytes(source), Convert.toPlatformBytes(destination), copyLastModified);
89         return false; // not supported
90
}
91
92     /**
93      * @param fileName
94      * @return The file info
95      */

96     public static FileInfo fetchFileInfo(String JavaDoc fileName) {
97         FileInfo info = new FileInfo();
98         if (isUnicode)
99             internalGetFileInfoW(Convert.toPlatformChars(fileName), info);
100         else
101             internalGetFileInfo(Convert.toPlatformBytes(fileName), info);
102         return info;
103     }
104
105     /**
106      * Copies file attributes from source to destination. The copyLastModified attribute
107      * indicates whether the lastModified attribute should be copied.
108      */

109     private static final native boolean internalCopyAttributes(byte[] source, byte[] destination, boolean copyLastModified);
110
111     /**
112      * Copies file attributes from source to destination. The copyLastModified attribute
113      * indicates whether the lastModified attribute should be copied (Unicode
114      * version - should not be called if <code>isUnicode</code> is
115      * <code>false</code>).
116      */

117     private static final native boolean internalCopyAttributesW(char[] source, char[] destination, boolean copyLastModified);
118
119     /**
120      * Stores the file information for the specified filename in the supplied file
121      * information object. This avoids multiple JNI calls.
122      */

123     private static final native boolean internalGetFileInfo(byte[] fileName, IFileInfo info);
124
125     /**
126      * Stores the file information for the specified filename in the supplied file
127      * information object. This avoids multiple JNI calls.
128      */

129     private static final native boolean internalGetFileInfoW(char[] fileName, IFileInfo info);
130
131     /**
132      * Returns <code>true</code> if the underlying file system API supports Unicode,
133      * <code>false</code> otherwise.
134      */

135     private static final native boolean internalIsUnicode();
136
137     /** Set the extended attributes specified in the IResource attribute. Only attributes
138      * that the platform supports will be set. */

139     private static final native boolean internalSetFileInfo(byte[] fileName, IFileInfo attribute);
140
141     /** Set the extended attributes specified in the IResource attribute object. Only
142      * attributes that the platform supports will be set. (Unicode version - should not
143      * be called if <code>isUnicode</code> is <code>false</code>). */

144     private static final native boolean internalSetFileInfoW(char[] fileName, IFileInfo attribute, int options);
145
146     private static void logMissingNativeLibrary(UnsatisfiedLinkError JavaDoc e) {
147         String JavaDoc libName = System.mapLibraryName(LIBRARY_NAME);
148         String JavaDoc message = NLS.bind(Messages.couldNotLoadLibrary, libName);
149         Policy.log(IStatus.INFO, message, e);
150     }
151
152     /**
153      * @param fileName
154      * @param info
155      * @param options
156      */

157     public static boolean setFileInfo(String JavaDoc fileName, IFileInfo info, int options) {
158         if (isUnicode)
159             return internalSetFileInfoW(Convert.toPlatformChars(fileName), info, options);
160         return internalSetFileInfo(Convert.toPlatformBytes(fileName), info);
161     }
162
163     /**
164      * Return <code>true</code> if we have found the core library and are using it for
165      * our file-system calls, and <code>false</code> otherwise.
166      * @return <code>true</code> if native library is available, and <code>false</code>
167      * otherwise.
168      */

169     public static boolean usingNatives() {
170         return hasNatives;
171     }
172 }
173
Popular Tags