Retrieving file content

This sample demonstrates how to retrieve the contents of a file (data set member or sequential data set) on the MVS™ file system.

Sample scenario

To retrieve the content for the resource, make sure that the resource is selected in the navigator, then select the API Samples > Get File Contents action.

The contents in the file are displayed in the standard output that is associated with the workbench. The contents can be found in the Console view of the workbench.

Note: The standard output for a workbench started with the standard Eclipse icon might not be visible.
Artwork for getContentsResults

Sample code walk-through

The complete source code can be found at GetContentsAction.java.

Reading from the remote artifact

The following code snippet from the run method of the class GetContentsAction demonstrates how to obtain the content of an MVS file (PDS member or sequential data set). The snippet differentiates between an IPhysicalFile and an ILogicalFile, but the overall flow is the same for both. The code first starts the getContents method to obtain an InputStream. After you have an InputStream, the standard Java™ methods such as read can be started to retrieve the content of the file.

 InputStream is = null;
	if (selectedItem instanceof ILogicalFile) {
		// Project proxy for resource
		ILogicalFile file = (ILogicalFile) selectedItem;
		filename = file.getName();
		is = file.getContents(); 
	} else if (selectedItem instanceof IPhysicalFile) {
		// Physical Resource
		IPhysicalFile file = (IPhysicalFile) selectedItem;
		filename = file.getName();
		is = file.getContents();
	}
	int ch = is.read();
	while (ch != -1) {
		System.out.write(ch);
		ch = is.read();
	}

Handling selection changed events

This example also demonstrates how to react to selection change events in Eclipse. The GetContentsAction class implements the ISelectionListener interface. Its selectionChanged method implementation reacts to events that refer to an IStructuredSelection, typically caused by a change in the current selection in a tree navigator. The sample code iterates through the items in the current selection by using an iterator that is associated with it and adding each item to a list.

 public void selectionChanged(IAction action, ISelection selection) {
		fTargetList = null;
		if (selection instanceof IStructuredSelection) {
			fTargetList = new ArrayList();
			IStructuredSelection selections = (IStructuredSelection)
				selection;
			for (Iterator e = selections.iterator(); e.hasNext();) {
				Object next = e.next();
				fTargetList.add(next);
			}
		}
	}

Contribution of menu item

The menu item is contributed through a simple use of the Eclipse org.eclipse.ui.actionSets extension point.

   <extension
         id=“com.ibm.ftt.api.samples.actionSets”
         name=“
         point=“org.eclipse.ui.actionSets”>
      <actionSet
            label=“com.ibm.ftt.api.samples.actionSet1”
            description=“Action set for the API samples”
            visible=“true”
            id=“com.ibm.ftt.api.samples.actionSet1”>
         <menu
               label=“
               id=“com.ibm.ftt.api.samples.apiMenu”>
            <separator name=“com.ibm.ftt.api.samples.apiMenu.resourcesAPI”/>
         </menu>
         <action
               label=“
               class=“com.ibm.ftt.api.samples.resources.GetContentsAction”
               style=“push”
               menubarPath=“com.ibm.ftt.api.samples.apiMenu/com.ibm.ftt.api.samples.apiMenu.resourcesAPI”
               id=“com.ibm.ftt.api.samples.getContentAction”/>
         ......
      </actionSet>
   </extension>

A menu action is defined at the group that is associated with the samples menu, which is specified with the menu path com.ibm.ftt.api.samples.apiMenu/com.ibm.ftt.api.samples.apiMenu.resourcesAPI. Furthermore, the menu action is associated with the GetContentsAction class. This class is started when you click the menu item.