System.out.println("Usage: java SearchDatastore <url> "
+ "<username> <password>");
return;
}
ServiceInstance si = new ServiceInstance(
new URL(args[0]), args[1], args[2], true);
String hostname = "10.20.143.205";
String datastorePath = "[storage1 (2)]";
Folder rootFolder = si.getRootFolder();
HostSystem host = null;
host = (HostSystem) new InventoryNavigator(
rootFolder).searchManagedEntity("HostSystem", hostname);
if(host==null)
{
System.out.println("Host not found");
si.getServerConnection().logout();
return;
}
HostDatastoreBrowser hdb = host.getDatastoreBrowser();
System.out.println("print out the names of the datastores");
Datastore[] ds = hdb.getDatastores();
for(int i=0; ds!=null && i<ds.length; i++)
{
System.out.println("datastore["+i+"]:");
DatastoreInfo di = ds[i].getInfo();
System.out.println("Name:" + di.getName());
System.out.println("FreeSpace:" + di.getFreeSpace());
System.out.println("MaxFileSize:" + di.getMaxFileSize());
}
System.out.println("print out supported query types");
FileQuery[] fqs = hdb.getSupportedType();
for(int i=0; fqs!=null && i<fqs.length; i++)
{
System.out.println("FileQuery["+i+"]="
+ fqs[i].getClass().getName());
}
HostDatastoreBrowserSearchSpec hdbss =
new HostDatastoreBrowserSearchSpec();
hdbss.setQuery(new FileQuery[] { new VmDiskFileQuery()});
FileQueryFlags fqf = new FileQueryFlags();
fqf.setFileSize(true);
fqf.setModification(true);
hdbss.setDetails(fqf);
hdbss.setSearchCaseInsensitive(false);
hdbss.setMatchPattern(new String[] {"sdk*.*"});
Task task = hdb.searchDatastoreSubFolders_Task(
datastorePath, hdbss);
if(task.waitForMe()==Task.SUCCESS)
{
Object obj = task.getTaskInfo().getResult();
if(obj instanceof ArrayOfHostDatastoreBrowserSearchResults)
{
HostDatastoreBrowserSearchResults[] results =
((ArrayOfHostDatastoreBrowserSearchResults)
obj).getHostDatastoreBrowserSearchResults();
for(int i=0; i<results.length; i++)
{
HostDatastoreBrowserSearchResults result = results[i];
System.out.println("\nFolder:"
+ result.getFolderPath());
FileInfo[] fis = result.getFile();
for(int j=0; fis!=null && j<fis.length; j++)
{
System.out.println("Path:" + fis[j].getPath());
System.out.println("FileSize:"
+ fis[j].getFileSize());
System.out.println("Modified:"
+ fis[j].getModification().getTime());
if(fis[j] instanceof VmDiskFileInfo)
{
printExtraDiskFileInfo((VmDiskFileInfo)fis[j]);
}
}
}
}
si.getServerConnection().logout();
}
}