Java tutorial
/* * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.dataone.proto.trove.mn.domain.v1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.dataone.service.types.v1.Node; import org.dataone.service.types.v1.NodeReference; import org.dataone.service.types.v1.NodeReplicationPolicy; import org.dataone.service.types.v1.NodeState; import org.dataone.service.types.v1.NodeType; import org.dataone.service.types.v1.Ping; import org.dataone.service.types.v1.Services; import org.dataone.service.types.v1.Subject; import org.dataone.service.types.v1.Synchronization; import org.dataone.service.util.TypeMarshaller; import org.jibx.runtime.JiBXException; import org.springframework.core.io.Resource; /** * Override the default constructor and only allow a resource to be used to initiate the node * * extends Node by overwritting * * @author waltz */ public class ResourceNode extends Node { Node node; private Resource myRourceNodeResource; private Boolean isModifiable = Boolean.FALSE; ResourceNode(Resource nodeResource) { myRourceNodeResource = nodeResource; try { InputStream nodeStream = nodeResource.getInputStream(); node = TypeMarshaller.unmarshalTypeFromStream(Node.class, nodeStream); nodeStream.close(); } catch (IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } catch (InstantiationException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } catch (IllegalAccessException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } catch (JiBXException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } } public Node getValue() { return node; } public void setValue(Node protoNode) { this.node = protoNode; saveFile(); } private void saveFile() { try { File resourceFile = myRourceNodeResource.getFile(); if (resourceFile.canWrite()) { FileOutputStream fileOutputStream = new FileOutputStream(resourceFile); TypeMarshaller.marshalTypeToOutputStream(node, fileOutputStream); } } catch (IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } catch (JiBXException ex) { ex.printStackTrace(); throw new RuntimeException(ex.getMessage()); } } /** * Get the 'identifier' element value. A unique identifier for the node of the form ``urn:node:NODEID`` where NODEID * is the node specific identifier. This value MUST NOT change for future implementations of the same node, whereas * the *baseURL* may change in the future. * * * @return value */ public NodeReference getIdentifier() { return node.getIdentifier(); } /** * Set the 'identifier' element value. A unique identifier for the node of the form ``urn:node:NODEID`` where NODEID * is the node specific identifier. This value MUST NOT change for future implementations of the same node, whereas * the *baseURL* may change in the future. * * * @param identifier */ @Override public void setIdentifier(NodeReference identifier) { node.setIdentifier(identifier); saveFile(); } /** * Get the 'name' element value. A human readable name of the Node. This name can be used as a label in many systems * to represent the node, and thus should be short, but understandable. * * @return value */ public String getName() { return node.getName(); } /** * Set the 'name' element value. A human readable name of the Node. This name can be used as a label in many systems * to represent the node, and thus should be short, but understandable. * * @param name */ public void setName(String name) { node.setName(name); saveFile(); } /** * Get the 'description' element value. Description of a Node, explaining the community it serves and other relevant * information about the node, such as what content is maintained by this node and any other free style notes. * * * @return value */ public String getDescription() { return node.getDescription(); } /** * Set the 'description' element value. Description of a Node, explaining the community it serves and other relevant * information about the node, such as what content is maintained by this node and any other free style notes. * * * @param description */ public void setDescription(String description) { node.setDescription(description); saveFile(); } /** * Get the 'baseURL' element value. The base URL of the node, indicating the protocol, fully qualified domain name, * and path to the implementing service, excluding the version of the API. e.g. * ``https://server.example.edu/app/d1/mn`` rather than ``https://server.example.edu/app/d1/mn/v1`` * * @return value */ @Override public String getBaseURL() { return node.getBaseURL(); } /** * Set the 'baseURL' element value. The base URL of the node, indicating the protocol, fully qualified domain name, * and path to the implementing service, excluding the version of the API. e.g. * ``https://server.example.edu/app/d1/mn`` rather than ``https://server.example.edu/app/d1/mn/v1`` * * @param baseURL */ public void setBaseURL(String baseURL) { node.setBaseURL(baseURL); saveFile(); } /** * Get the 'services' element value. A list of services that are provided by this node. Used in node descriptions so * that nodes can provide metadata about each service they implement and support. * * @return value */ public Services getServices() { return node.getServices(); } /** * Set the 'services' element value. A list of services that are provided by this node. Used in node descriptions so * that nodes can provide metadata about each service they implement and support. * * @param services */ public void setServices(Services services) { node.setServices(services); saveFile(); } /** * Get the 'synchronization' element value. Configuration information for the process by which content is harvested * from Member Nodes to Coordinating Nodes. This includes the schedule on which harvesting should occur, and * metadata about the last synchronization attempts for the node. * * @return value */ public Synchronization getSynchronization() { return node.getSynchronization(); } /** * Set the 'synchronization' element value. Configuration information for the process by which content is harvested * from Member Nodes to Coordinating Nodes. This includes the schedule on which harvesting should occur, and * metadata about the last synchronization attempts for the node. * * @param synchronization */ public void setSynchronization(Synchronization synchronization) { node.setSynchronization(synchronization); saveFile(); } /** * Get the 'nodeReplicationPolicy' element value. The replication policy for this node that expresses constraints on * object size, total objects, source nodes, and object format types. A node may want to restrict replication from * only certain peer nodes, may have file size limits, total allocated size limits, or may want to focus on being a * replica target for domain-specific object formats. * * @return value */ public NodeReplicationPolicy getNodeReplicationPolicy() { return node.getNodeReplicationPolicy(); } /** * Set the 'nodeReplicationPolicy' element value. The replication policy for this node that expresses constraints on * object size, total objects, source nodes, and object format types. A node may want to restrict replication from * only certain peer nodes, may have file size limits, total allocated size limits, or may want to focus on being a * replica target for domain-specific object formats. * * @param nodeReplicationPolicy */ public void setNodeReplicationPolicy(NodeReplicationPolicy nodeReplicationPolicy) { node.setNodeReplicationPolicy(nodeReplicationPolicy); saveFile(); } /** * Get the 'ping' element value. Stored results from the :func:`MNCore.ping` and :func:`CNCore.ping` methods. * * @return value */ public Ping getPing() { return node.getPing(); } /** * Set the 'ping' element value. Stored results from the :func:`MNCore.ping` and :func:`CNCore.ping` methods. * * @param ping */ public void setPing(Ping ping) { node.setPing(ping); saveFile(); } /** * Get the list of 'subject' element items. The :term:`Subject` of this node, which can be repeated as needed. The * *Node.subject* represents the identifier of the node that would be found in X.509 certificates used to securely * communicate with this node. Thus, it is an :term:`X.509 Distinguished Name` that applies to the host on which the * Node is operating. When (and if) this hostname changes the new subject for the node would be added to the Node to * track the subject that has been used in various access control rules over time. * * * @return list */ public List<Subject> getSubjectList() { return node.getSubjectList(); } /** * Set the list of 'subject' element items. The :term:`Subject` of this node, which can be repeated as needed. The * *Node.subject* represents the identifier of the node that would be found in X.509 certificates used to securely * communicate with this node. Thus, it is an :term:`X.509 Distinguished Name` that applies to the host on which the * Node is operating. When (and if) this hostname changes the new subject for the node would be added to the Node to * track the subject that has been used in various access control rules over time. * * * @param list */ public void setSubjectList(List<Subject> list) { node.setSubjectList(list); saveFile(); } /** * Get the number of 'subject' element items. * * @return count */ public int sizeSubjectList() { return node.sizeSubjectList(); } /** * Add a 'subject' element item. * * @param item */ public void addSubject(Subject item) { node.addSubject(item); saveFile(); } /** * Get 'subject' element item by position. * * @return item * @param index */ public Subject getSubject(int index) { return node.getSubject(index); } /** * Remove all 'subject' element items. */ public void clearSubjectList() { node.clearSubjectList(); saveFile(); } /** * Get the list of 'contactSubject' element items. The appropriate person or group to contact regarding the * disposition, management, and status of this Member Node. The *Node.contactSubject* is an :term:`X.509 * Distinguished Name` for a person or group that can be used to look up current contact details (e.g., name, email * address) for the contact in the DataONE Identity service. DataONE uses the *contactSubject* to provide notices of * interest to DataONE nodes, including information such as policy changes, maintenance updates, node outage * notifications, among other information useful for administering a node. Each node that is registered with DataONE * must provide at least one *contactSubject* that has been :term:`verified` with DataONE. * * @return list */ public List<Subject> getContactSubjectList() { return node.getContactSubjectList(); } /** * Set the list of 'contactSubject' element items. The appropriate person or group to contact regarding the * disposition, management, and status of this Member Node. The *Node.contactSubject* is an :term:`X.509 * Distinguished Name` for a person or group that can be used to look up current contact details (e.g., name, email * address) for the contact in the DataONE Identity service. DataONE uses the *contactSubject* to provide notices of * interest to DataONE nodes, including information such as policy changes, maintenance updates, node outage * notifications, among other information useful for administering a node. Each node that is registered with DataONE * must provide at least one *contactSubject* that has been :term:`verified` with DataONE. * * @param list */ public void setContactSubjectList(List<Subject> list) { node.setContactSubjectList(list); saveFile(); } /** * Get the number of 'contactSubject' element items. * * @return count */ public int sizeContactSubjectList() { return node.sizeContactSubjectList(); } /** * Add a 'contactSubject' element item. * * @param item */ public void addContactSubject(Subject item) { node.addContactSubject(item); saveFile(); } /** * Get 'contactSubject' element item by position. * * @return item * @param index */ public Subject getContactSubject(int index) { return node.getContactSubject(index); } /** * Remove all 'contactSubject' element items. */ public void clearContactSubjectList() { node.clearContactSubjectList(); saveFile(); } /** * * Get the 'replicate' attribute value. Set to *true* if the node is willing to be a :term:`replication target`, * otherwise *false*. * * @return value */ public boolean isReplicate() { return node.isReplicate(); } /** * * Set the 'replicate' attribute value. Set to *true* if the node is willing to be a :term:`replication target`, * otherwise *false*. * * @param replicate */ public void setReplicate(boolean replicate) { node.setReplicate(true); saveFile(); } /** * Get the 'synchronize' attribute value. Set to *true* if the node should be :term:`synchronized` by a Coordinating * Node, otherwise false*. * * @return value */ public boolean isSynchronize() { return node.isSynchronize(); } /** * Set the 'synchronize' attribute value. Set to *true* if the node should be :term:`synchronized` by a Coordinating * Node, otherwise false*. * * @param synchronize */ public void setSynchronize(boolean synchronize) { node.setSynchronize(true); saveFile(); } /** * Get the 'type' attribute value. The type of the node (Coordinating, Member), chosen from the * :class:`Types.NodeType` type. * * @return value */ public NodeType getType() { return node.getType(); } /** * Set the 'type' attribute value. The type of the node (Coordinating, Member), chosen from the * :class:`Types.NodeType` type. * * @param type */ public void setType(NodeType type) { node.setType(type); saveFile(); } /** * Get the 'state' attribute value. The state of the node (*up*, *down*), chosen from the :class:`Types.NodeState` * type. * * @return value */ public NodeState getState() { return node.getState(); } /** * Set the 'state' attribute value. The state of the node (*up*, *down*), chosen from the :class:`Types.NodeState` * type. * * @param state */ public void setState(NodeState state) { node.setState(state); saveFile(); } }