components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/common/api/file/RemoteFile.java
changeset 827 0944d8c0158b
child 1410 ca9946e5736c
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.common.api.file;
       
    27 
       
    28 import java.io.*;
       
    29 import java.util.List;
       
    30 import com.oracle.solaris.rad.ObjectException;
       
    31 
       
    32 @SuppressWarnings({"serial"})
       
    33 public class RemoteFile extends File {
       
    34     //
       
    35     // Instance data
       
    36     //
       
    37 
       
    38     private FileBrowserMXBean browser;
       
    39     private FileSnapshot snapshot;
       
    40 
       
    41     //
       
    42     // Constructors
       
    43     //
       
    44 
       
    45     public RemoteFile(FileBrowserMXBean browser, FileSnapshot snapshot) {
       
    46 	super(snapshot.getPath());
       
    47 	this.browser = browser;
       
    48 	this.snapshot = snapshot;
       
    49     }
       
    50 
       
    51     //
       
    52     // File methods
       
    53     //
       
    54 
       
    55     @Override
       
    56     public boolean canExecute() {
       
    57 	// Operation not supported
       
    58 	return false;
       
    59     }
       
    60 
       
    61     @Override
       
    62     public boolean canRead() {
       
    63 	return snapshot.isReadable();
       
    64     }
       
    65 
       
    66     @Override
       
    67     public boolean canWrite() {
       
    68 	return snapshot.isWritable();
       
    69     }
       
    70 
       
    71     @Override
       
    72     public boolean createNewFile() throws IOException {
       
    73 	throw new IOException("operation not supported");
       
    74     }
       
    75 
       
    76     @Override
       
    77     public boolean delete() {
       
    78 	// Operation not supported
       
    79 	return false;
       
    80     }
       
    81 
       
    82     @Override
       
    83     public void deleteOnExit() {
       
    84 	// Operation not supported
       
    85     }
       
    86 
       
    87     @Override
       
    88     public boolean exists() {
       
    89 	return snapshot.isExists();
       
    90     }
       
    91 
       
    92     @Override
       
    93     public RemoteFile getAbsoluteFile() {
       
    94 	// RemoteFiles are based on FileSnapshots which are always absolute
       
    95 	return this;
       
    96     }
       
    97 
       
    98     @Override
       
    99     public String getAbsolutePath() {
       
   100 	return snapshot.getAbsolutePath();
       
   101     }
       
   102 
       
   103     @Override
       
   104     public RemoteFile getCanonicalFile() throws IOException {
       
   105 	if (snapshot.isCanonical()) {
       
   106 	    return this;
       
   107 	}
       
   108 	String path = snapshot.getCanonicalPath();
       
   109 	try {
       
   110 	    return new RemoteFile(browser, browser.getFile(path));
       
   111 	} catch (ObjectException e) {
       
   112 	    throw new IOException(e);
       
   113 	}
       
   114     }
       
   115 
       
   116     @Override
       
   117     public String getCanonicalPath() throws IOException {
       
   118 	String path = snapshot.getCanonicalPath();
       
   119 	if (path == null) {
       
   120 	    // FileSnapshot could not canonicalize path
       
   121 	    throw new IOException(
       
   122 		"unable to canonicalize path: " + getAbsolutePath());
       
   123 	}
       
   124 
       
   125 	return path;
       
   126     }
       
   127 
       
   128     @Override
       
   129     public long getFreeSpace() {
       
   130 	return snapshot.getFreeSpace();
       
   131     }
       
   132 
       
   133     @Override
       
   134     public RemoteFile getParentFile() {
       
   135 	String parent = getParent();
       
   136 	try {
       
   137 	    return parent == null ? null :
       
   138 		new RemoteFile(browser, browser.getFile(parent));
       
   139 	} catch (ObjectException e) {
       
   140 	    /* Not correct, but our choices are limited */
       
   141 	    return null;
       
   142 	}
       
   143     }
       
   144 
       
   145     @Override
       
   146     public long getTotalSpace() {
       
   147 	return snapshot.getTotalSpace();
       
   148     }
       
   149 
       
   150     @Override
       
   151     public long getUsableSpace() {
       
   152 	return snapshot.getUsableSpace();
       
   153     }
       
   154 
       
   155     @Override
       
   156     public boolean isAbsolute() {
       
   157 	return snapshot.isAbsolute();
       
   158     }
       
   159 
       
   160     @Override
       
   161     public boolean isDirectory() {
       
   162 	return snapshot.isDirectory();
       
   163     }
       
   164 
       
   165     @Override
       
   166     public boolean isFile() {
       
   167 	return snapshot.isFile();
       
   168     }
       
   169 
       
   170     @Override
       
   171     public boolean isHidden() {
       
   172 	return snapshot.isHidden();
       
   173     }
       
   174 
       
   175     @Override
       
   176     public long lastModified() {
       
   177 	return snapshot.getLastModified().getTime();
       
   178     }
       
   179 
       
   180     @Override
       
   181     public long length() {
       
   182 	return snapshot.getLength();
       
   183     }
       
   184 
       
   185     @Override
       
   186     public String[] list() {
       
   187 	try {
       
   188 	    List<FileSnapshot> snapshots = browser.getFiles(getAbsolutePath());
       
   189 	    String[] names = new String[snapshots.size()];
       
   190 
       
   191 	    int i = 0;
       
   192 	    for (FileSnapshot ss : snapshots)
       
   193 		names[i++] = ss.getBaseName();
       
   194 
       
   195 	    return names;
       
   196 	} catch (ObjectException e) {
       
   197 	    return null;
       
   198 	}
       
   199     }
       
   200 
       
   201     @Override
       
   202     public RemoteFile[] listFiles() {
       
   203 	try {
       
   204 	    List<FileSnapshot> snapshots = browser.getFiles(getAbsolutePath());
       
   205 	    return toFiles(snapshots);
       
   206 	} catch (ObjectException e) {
       
   207 	    return null;
       
   208 	}
       
   209     }
       
   210 
       
   211     @Override
       
   212     public boolean mkdir() {
       
   213 	// Operation not supported
       
   214 	return false;
       
   215     }
       
   216 
       
   217     @Override
       
   218     public boolean mkdirs() {
       
   219 	// Operation not supported
       
   220 	return false;
       
   221     }
       
   222 
       
   223     @Override
       
   224     public boolean renameTo(File dest) {
       
   225 	// Operation not supported
       
   226 	return false;
       
   227     }
       
   228 
       
   229     @Override
       
   230     public boolean setExecutable(boolean executable) {
       
   231 	// Operation not supported
       
   232 	return false;
       
   233     }
       
   234 
       
   235     @Override
       
   236     public boolean setExecutable(boolean executable, boolean ownerOnly) {
       
   237 	// Operation not supported
       
   238 	return false;
       
   239     }
       
   240 
       
   241     @Override
       
   242     public boolean setLastModified(long time) {
       
   243 	// Operation not supported
       
   244 	return false;
       
   245     }
       
   246 
       
   247     @Override
       
   248     public boolean setReadable(boolean readable) {
       
   249 	// Operation not supported
       
   250 	return false;
       
   251     }
       
   252 
       
   253     @Override
       
   254     public boolean setReadable(boolean readable, boolean ownerOnly) {
       
   255 	// Operation not supported
       
   256 	return false;
       
   257     }
       
   258 
       
   259     @Override
       
   260     public boolean setReadOnly() {
       
   261 	// Operation not supported
       
   262 	return false;
       
   263     }
       
   264 
       
   265     @Override
       
   266     public boolean setWritable(boolean writable) {
       
   267 	// Operation not supported
       
   268 	return false;
       
   269     }
       
   270 
       
   271     @Override
       
   272     public boolean setWritable(boolean writable, boolean ownerOnly) {
       
   273 	// Operation not supported
       
   274 	return false;
       
   275     }
       
   276 
       
   277     //
       
   278     // RemoteFile methods
       
   279     //
       
   280 
       
   281     public FileBrowserMXBean getBrowser() {
       
   282 	return browser;
       
   283     }
       
   284 
       
   285     public FileSnapshot getSnapshot() {
       
   286 	return snapshot;
       
   287     }
       
   288 
       
   289     protected RemoteFile[] toFiles(List<FileSnapshot> snapshots) {
       
   290 	return toFiles(browser, snapshots);
       
   291     }
       
   292 
       
   293     //
       
   294     // Static methods
       
   295     //
       
   296 
       
   297     public static RemoteFile[] toFiles(
       
   298 	FileBrowserMXBean browser, List<FileSnapshot> snapshots) {
       
   299 
       
   300 	RemoteFile[] files = new RemoteFile[snapshots.size()];
       
   301 
       
   302 	int i = 0;
       
   303 	for (FileSnapshot ss : snapshots)
       
   304 	    files[i++] = new RemoteFile(browser, ss);
       
   305 
       
   306 	return files;
       
   307     }
       
   308 
       
   309 }