This sample demonstrates how to create a new data set member under a partitioned data set. It also demonstrates how to contribute a menu item in the pop-up menu for remote artifacts in the navigator views.
To use this sample, right click on a partitioned data set in the Remote Systems Explorer View, and select the API Sample Actions > Create Member action.
A member with the name TEMP is created in the selected data set.
The createPhysicalMember sample method starts by checking to make sure that the member to be created does not already exist, using the method findMember. The findMember method returns null if the resource is not found.
IPhysicalFile resource = (IPhysicalFile)
dataSet.findMember(MEMBER_NAME);
if (resource != null) {
System.err.println("Resource " + MEMBER_NAME +
" already exists");
return;
}
The member is then created as follows. An IPhysicalResource object referencing the member is created using the getPhysicalResource method of the ZOSPhysicalResourceFactory class. (The resource object returned by the getPhysicalResource method does not have to represent an actual existing host resource.) Once you have the IPhysicalResource, the actual member can then be created on the host by invoking create method.
ZOSPhysicalResourceFactory factory =
ZOSPhysicalResourceFactory.eINSTANCE;
resource = (IPhysicalFile) factory.getPhysicalResource(dataSet,
ZOSDataSetMember.class, MEMBER_NAME);
try {
resource.create(
new ByteArrayInputStream(MEMBER_CONTENT.getBytes()),
true, null);
} catch (OperationFailedException e) {
......
}
The create method expects an InputStream containing the content for the file to be created.
The process of creating a data set member in an MVS™ subproject is similar. Consider the createLogicalMember method in the CreateMemberAction sample class. It starts by obtaining the IPhysicalResource object that is referred to by the selected ILogicalResource, using the getPhysicalResource method.
IPhysicalContainer container = null;
if (dataSet.getState().isOnline()) {
container = (IPhysicalContainer) dataSet.
getPhysicalResource();
} else {
System.err.println("DataSet " + dataSet.getName() +
" should not be offline.");
return;
}
Processing of offline resources using the Remote Access API is not yet supported.
The code then proceeds to create a data set member IPhysicalResource as in Creating a Data Set Member IPhysicalResource.
IPhysicalFile resource = (IPhysicalFile) factory.getPhysicalResource(
container, ZOSDataSetMember.class, MEMBER_NAME);
try {
resource.create(
new ByteArrayInputStream(MEMBER_CONTENT.getBytes()),
true, null);
} catch (OperationFailedException e) {
......
}
The following code snippet then adds the resource to the appropriate subproject obtained through the getSubProject method. The code snippet accomplishes this by using the LogicalResourceFactory class. This method can be used to add any physical resource to a project, using the Add to Subproject menu action.
ILogicalSubProject subProject = dataSet.getSubProject();
if (project != null && resource != null) {
LogicalResourceFactoryFactory.getSingleton().getLogicalResource(
subProject, resource);
}
This example also demonstrates how to react to selection change events in Eclipse. The CreateMemberAction class implements the ISelectionListener interface. Its selectionChanged method implementation reacts to events referring to an IStructuredSelection, typically caused by a change in the current selection in a tree navigator. The example code iterates through the items contained in the current selection using an iterator associated with it. In this example, the first item selected is chosen.
selectedItem = null;
if (selection instanceof IStructuredSelection) {
IStructuredSelection currentSelection =
(IStructuredSelection) selection;
Iterator iterator = currentSelection.iterator();
while (iterator.hasNext()) {
Object item = iterator.next();
if (item instanceof LZOSPartitionedDataSet) {
selectedItem = item;
break;
} else if (item instanceof ZOSResourceReference) {
item = ((ZOSResourceReference) item).getReferent();
if (item instanceof ZOSPartitionedDataSet) {
selectedItem = item;
break;
}
}
}
}
An ILogicalResource object (LZOSPartitionedDataSet in this case) is returned in the selection object, if an object in a z/OS® project is selected. If an object in the MVS file system is selected on the Remote Systems Explorer View, a ZOSResourceReference is returned. In the latter case, you can retrieve the corresponding IPhysicalResource using the getReferent method.