KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > configuration > LocalSystemInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.update.configuration;
12  
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.update.internal.core.UpdateCore;
17 import org.eclipse.update.internal.core.Volume;
18  
19 /**
20  * Utility class providing local file system information.
21  * The class attempts to load a native library implementation
22  * of its methods. If successful, the method calls are delegated
23  * to the native implementation. Otherwise a default non-native
24  * implementation is used. *
25  * <p>
26  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
27  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
28  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
29  * (repeatedly) as the API evolves.
30  * </p>
31  * @see ILocalSystemInfoListener
32  * @since 2.0
33  */

34 public class LocalSystemInfo {
35     
36     /**
37      * Indicates the amount of available free space is not known
38      *
39      * @see LocalSystemInfo#getFreeSpace(File)
40      * @since 2.0
41      * <p>
42      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
43      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
44      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
45      * (repeatedly) as the API evolves.
46      * </p>
47      */

48     public static final long SIZE_UNKNOWN = -1;
49     
50     /**
51      * Indicates the volume type is not known
52      *
53      * @since 2.0
54      * <p>
55      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
56      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
57      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
58      * (repeatedly) as the API evolves.
59      * </p>
60      */

61     public static final int VOLUME_UNKNOWN = -1;
62     
63     /**
64      * Indicates the volume could not be determined from path
65      *
66      * @since 2.0
67      * <p>
68      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
69      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
70      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
71      * (repeatedly) as the API evolves.
72      * </p>
73      */

74     public static final int VOLUME_INVALID_PATH = -2;
75     
76     /**
77      * Indicates the volume is removable (other than floppy disk)
78      *
79      * @since 2.0
80      * <p>
81      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
82      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
83      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
84      * (repeatedly) as the API evolves.
85      * </p>
86      */

87     public static final int VOLUME_REMOVABLE = 1;
88     
89     /**
90      * Indicates the volume is fixed (hard drive)
91      *
92      * @since 2.0
93      * <p>
94      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
95      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
96      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
97      * (repeatedly) as the API evolves.
98      * </p>
99      */

100     public static final int VOLUME_FIXED = 2;
101     
102     /**
103      * Indicates a remote (network) volume
104      *
105      * @since 2.0
106      * <p>
107      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
108      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
109      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
110      * (repeatedly) as the API evolves.
111      * </p>
112      */

113     public static final int VOLUME_REMOTE = 3;
114     
115     /**
116      * Indicates a cdrom volume (compact disc)
117      *
118      * @since 2.0
119      * <p>
120      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
121      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
122      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
123      * (repeatedly) as the API evolves.
124      * </p>
125      */

126     public static final int VOLUME_CDROM = 4;
127     
128     /**
129      * Indicates a ramdisk volume (memory)
130      *
131      * @since 2.0
132      * <p>
133      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
134      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
135      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
136      * (repeatedly) as the API evolves.
137      * </p>
138      */

139     public static final int VOLUME_RAMDISK = 5;
140
141     /**
142      * Indicates the volume is removable (floppy disk 5 1/4)
143      *
144      * @since 2.0
145      * <p>
146      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
147      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
148      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
149      * (repeatedly) as the API evolves.
150      * </p>
151      */

152     public static final int VOLUME_FLOPPY_5 = 6;
153     
154     /**
155      * Indicates the volume is removable (floppy disk 3 1/2)
156      *
157      * @since 2.0
158      * <p>
159      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
160      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
161      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
162      * (repeatedly) as the API evolves.
163      * </p>
164      */

165     public static final int VOLUME_FLOPPY_3 = 7;
166     
167     /**
168      * Indicates a new volume has been added
169      * @since 2.0
170      * <p>
171      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
172      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
173      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
174      * (repeatedly) as the API evolves.
175      * </p>
176      */

177     public static final int VOLUME_ADDED = 0;
178             
179     /**
180      * Indicates a volume has been removed
181      * @since 2.0
182      * <p>
183      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
184      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
185      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
186      * (repeatedly) as the API evolves.
187      * </p>
188      */

189     public static final int VOLUME_REMOVED = 1;
190
191     /**
192      * Indicates a volume has been changed
193      * @since 2.0
194      * <p>
195      * <b>Note:</b> This field is part of an interim API that is still under development and expected to
196      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
197      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
198      * (repeatedly) as the API evolves.
199      * </p>
200      */

201     public static final int VOLUME_CHANGED = 2;
202
203     
204     private static ArrayList JavaDoc listeners = new ArrayList JavaDoc();
205     private static boolean hasNatives = false;
206     static {
207         try {
208             System.loadLibrary("update"); //$NON-NLS-1$
209
hasNatives = true;
210         } catch (UnsatisfiedLinkError JavaDoc e) {
211             UpdateCore.warn("Unable to load native library 'update'."); //$NON-NLS-1$
212
hasNatives = false;
213         }
214     }
215     
216     /**
217      * Determines available free space on a volume.
218      * Returns the amount of free space available to this
219      * user on the volume containing the specified path. The
220      * method takes into account any space quotas or other
221      * native mechanisms that may restrict space usage
222      * on a given volume.
223      * @param path file path. May contain path elements beyond
224      * the volume "root"
225      * @return the amount of free space available (in units
226      * of Kbyte), or an indication the size is not known
227      * @see LocalSystemInfo#SIZE_UNKNOWN
228      * @since 2.0
229      * <p>
230      * <b>Note:</b> This method is part of an interim API that is still under development and expected to
231      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
232      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
233      * (repeatedly) as the API evolves.
234      * </p>
235      */

236     public static long getFreeSpace(File JavaDoc path) {
237         if (hasNatives) {
238             try {
239                 long bytes = nativeGetFreeSpace(path);
240                 return (bytes!=0)?bytes/1024:0;
241             } catch (UnsatisfiedLinkError JavaDoc e) {
242             }
243         }
244         return SIZE_UNKNOWN;
245     }
246
247
248     /**
249      * Lists the file system volume.
250      * @return array of volume representing mount
251      * points, or <code>null</code> if none found
252      * @since 2.0
253      * <p>
254      * <b>Note:</b> This method is part of an interim API that is still under development and expected to
255      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
256      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
257      * (repeatedly) as the API evolves.
258      * </p>
259      */

260     public static IVolume[] getVolumes() {
261         String JavaDoc[] mountPoints = listMountPoints();
262         Volume[] vol = new Volume[0];
263         if (mountPoints!=null){
264             vol = new Volume[mountPoints.length];
265             for (int i = 0; i < mountPoints.length; i++) {
266                 File JavaDoc root = new File JavaDoc(mountPoints[i]);
267                 String JavaDoc label = getLabel(root);
268                 int type = getType(root);
269                 long size = getFreeSpace(root);
270                 vol[i] = new Volume(root,label,type,size);
271                 vol[i].markReadOnly();
272             }
273         } else {
274             UpdateCore.warn("Unable to find mount points"); //$NON-NLS-1$
275
// fallback
276
File JavaDoc [] roots = File.listRoots();
277             if (roots.length == 1) {
278                 // just one root - skip it
279
File JavaDoc root = roots[0];
280                 roots = root.listFiles();
281             }
282             vol = new Volume[roots.length];
283             for (int i = 0; i < roots.length; i++) {
284                 vol[i] = new Volume(roots[i],null,LocalSystemInfo.VOLUME_UNKNOWN,LocalSystemInfo.SIZE_UNKNOWN);
285                 vol[i].markReadOnly();
286             }
287         }
288         return vol;
289     }
290
291     
292     /**
293      * Add local system change listener.
294      * Allows a listener to be added for monitoring changes
295      * in the local system information. The listener is notified
296      * each time there are relevant volume changes
297      * detected. This specifically includes changes to the
298      * list of volumes as a result of removable drive/ media
299      * operations (eg. CD insertion, removal), and changes to volume
300      * mount structure.
301      * @param listener change listener
302      * @since 2.0
303      * <p>
304      * <b>Note:</b> This method is part of an interim API that is still under development and expected to
305      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
306      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
307      * (repeatedly) as the API evolves.
308      * </p>
309      */

310     public static void addInfoListener(ILocalSystemInfoListener listener) {
311         if (!listeners.contains(listener))
312             listeners.add(listener);
313     }
314     
315     /**
316      * Remove local system change listener
317      * @param listener change listener
318      * @since 2.0
319      * <p>
320      * <b>Note:</b> This method is part of an interim API that is still under development and expected to
321      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
322      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
323      * (repeatedly) as the API evolves.
324      * </p>
325      */

326     public static void removeInfoListener(ILocalSystemInfoListener listener) {
327         listeners.remove(listener);
328     }
329         
330     /**
331      * Notify listeners of change.
332      *
333      * @param volume the volume representing the
334      * change file system structure. Any current paths beyond
335      * the specified "root" file of the volume are assumed to be invalidated.
336      * @param changeType type of the change that occured.
337      * @see LocalSystemInfo#VOLUME_ADDED
338      * @see LocalSystemInfo#VOLUME_REMOVED
339      * @see LocalSystemInfo#VOLUME_CHANGED
340      * <p>
341      * <b>Note:</b> This method is part of an interim API that is still under development and expected to
342      * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
343      * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
344      * (repeatedly) as the API evolves.
345      * </p>
346      */

347     public static void fireSystemInfoChanged(IVolume volume, int changeType) {
348         for (int i=0; i< listeners.size(); i++) {
349             ((ILocalSystemInfoListener)listeners.get(i)).systemInfoChanged(volume,changeType);
350         }
351     }
352         
353     /*
354      * Determines volume label.
355      * Returns the label of the volume containing the specified
356      * path.
357      * @param path file path. May contain path elements beyond
358      * the volume "root"
359      * @return volume label (as string), or <code>null</code> if
360      * the label cannot be determined.
361      * @since 2.0
362      */

363     private static String JavaDoc getLabel(File JavaDoc path) {
364         if (hasNatives) {
365             try {
366                 return nativeGetLabel(path);
367             } catch (UnsatisfiedLinkError JavaDoc e) {
368             }
369         }
370         return null;
371     }
372     
373     /*
374      * Determines volume type.
375      * Returns the type of the volume containing the specified
376      * path.
377      * @param path file path. May contain path elements beyond
378      * the volume "root"
379      * @return volume type
380      * @see LocalSystemInfo#VOLUME_UNKNOWN
381      * @see LocalSystemInfo#VOLUME_INVALID_PATH
382      * @see LocalSystemInfo#VOLUME_REMOVABLE
383      * @see LocalSystemInfo#VOLUME_FIXED
384      * @see LocalSystemInfo#VOLUME_REMOTE
385      * @see LocalSystemInfo#VOLUME_CDROM
386      * @see LocalSystemInfo#VOLUME_FLOPPY_3
387      * @see LocalSystemInfo#VOLUME_FLOPPY_5
388      * @since 2.0
389      */

390     private static int getType(File JavaDoc path) {
391         if (hasNatives) {
392             try {
393                 return nativeGetType(path);
394             } catch (UnsatisfiedLinkError JavaDoc e) {
395             }
396         }
397         return VOLUME_UNKNOWN;
398     }
399     
400     /*
401      * Lists the file system mount points.
402      * @return array of absolute file paths representing mount
403      * points, or <code>null</code> if none found
404      * @since 2.0
405      */

406     private static String JavaDoc[] listMountPoints() {
407         if (hasNatives) {
408             try {
409                 String JavaDoc[] mountPoints = nativeListMountPoints();
410                 return mountPoints;
411             } catch (UnsatisfiedLinkError JavaDoc e) {
412             }
413         }
414         return null;
415     }
416         
417     /*
418      * Native implementations.
419      */

420     private static native long nativeGetFreeSpace(File JavaDoc path);
421     private static native String JavaDoc nativeGetLabel(File JavaDoc path);
422     private static native int nativeGetType(File JavaDoc path);
423     private static native String JavaDoc[] nativeListMountPoints();
424 }
425
Popular Tags