|
4 | 4 | */
|
5 | 5 | package org.jboss.as.controller;
|
6 | 6 |
|
| 7 | +import java.util.AbstractCollection; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.LinkedList; |
7 | 11 | import java.util.List;
|
| 12 | +import java.util.ListIterator; |
| 13 | +import java.util.Map; |
8 | 14 |
|
| 15 | +import javax.xml.namespace.QName; |
9 | 16 | import javax.xml.stream.XMLStreamException;
|
10 | 17 |
|
| 18 | +import org.jboss.as.controller.descriptions.ModelDescriptionConstants; |
| 19 | +import org.jboss.as.controller.persistence.xml.SubsystemResourceRegistrationXMLElement; |
| 20 | +import org.jboss.as.controller.persistence.xml.SubsystemResourceXMLSchema; |
| 21 | +import org.jboss.as.controller.xml.XMLCardinality; |
| 22 | +import org.jboss.as.controller.xml.XMLContentWriter; |
11 | 23 | import org.jboss.dmr.ModelNode;
|
| 24 | +import org.jboss.staxmapper.XMLElementReader; |
12 | 25 | import org.jboss.staxmapper.XMLExtendedStreamReader;
|
| 26 | +import org.jboss.staxmapper.XMLExtendedStreamWriter; |
13 | 27 |
|
14 | 28 | /**
|
15 | 29 | * Defines a versioned schema for a subsystem defined via a {@link PersistentResourceXMLDescription}.
|
16 | 30 | * @author Paul Ferraro
|
17 | 31 | * @param <S> the schema type
|
18 | 32 | */
|
19 |
| -public interface PersistentSubsystemSchema<S extends PersistentSubsystemSchema<S>> extends SubsystemSchema<S> { |
| 33 | +public interface PersistentSubsystemSchema<S extends PersistentSubsystemSchema<S>> extends SubsystemResourceXMLSchema<S> { |
20 | 34 |
|
21 | 35 | PersistentResourceXMLDescription getXMLDescription();
|
22 | 36 |
|
23 | 37 | @Override
|
24 |
| - default void readElement(XMLExtendedStreamReader reader, List<ModelNode> value) throws XMLStreamException { |
25 |
| - new PersistentResourceXMLDescriptionReader(this.getXMLDescription()).readElement(reader, value); |
| 38 | + default SubsystemResourceRegistrationXMLElement getSubsystemXMLElement() { |
| 39 | + PersistentResourceXMLDescription description = this.getXMLDescription(); |
| 40 | + return new SubsystemResourceRegistrationXMLElement() { |
| 41 | + @Override |
| 42 | + public XMLCardinality getCardinality() { |
| 43 | + return XMLCardinality.Single.REQUIRED; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public XMLElementReader<Map.Entry<PathAddress, Map<PathAddress, ModelNode>>> getReader() { |
| 48 | + return new XMLElementReader<>() { |
| 49 | + @Override |
| 50 | + public void readElement(XMLExtendedStreamReader reader, Map.Entry<PathAddress, Map<PathAddress, ModelNode>> context) throws XMLStreamException { |
| 51 | + description.parse(reader, context.getKey(), new OperationList(context.getValue())); |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public XMLContentWriter<ModelNode> getWriter() { |
| 58 | + return new XMLContentWriter<>() { |
| 59 | + @Override |
| 60 | + public void writeContent(XMLExtendedStreamWriter streamWriter, ModelNode model) throws XMLStreamException { |
| 61 | + description.persist(streamWriter, model); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean isEmpty(ModelNode content) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public QName getName() { |
| 73 | + return new QName(description.getNamespaceURI(), ModelDescriptionConstants.SUBSYSTEM); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public PathElement getPathElement() { |
| 78 | + return description.getPathElement(); |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + class OperationList extends AbstractCollection<ModelNode> implements List<ModelNode> { |
| 84 | + private final Map<PathAddress, ModelNode> operations; |
| 85 | + private final List<PathAddress> keys; |
| 86 | + |
| 87 | + OperationList(Map<PathAddress, ModelNode> operations) { |
| 88 | + this(operations, new LinkedList<>(operations.keySet())); |
| 89 | + } |
| 90 | + |
| 91 | + private OperationList(Map<PathAddress, ModelNode> operations, List<PathAddress> keys) { |
| 92 | + this.operations = operations; |
| 93 | + this.keys = keys; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean add(ModelNode operation) { |
| 98 | + PathAddress address = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)); |
| 99 | + if (this.operations.put(address, operation) == null) { |
| 100 | + this.keys.add(address); |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean remove(Object object) { |
| 107 | + ModelNode operation = (ModelNode) object; |
| 108 | + PathAddress address = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)); |
| 109 | + if (this.operations.remove(address) != null) { |
| 110 | + return this.keys.remove(address); |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public int size() { |
| 117 | + return this.keys.size(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Iterator<ModelNode> iterator() { |
| 122 | + return new OperationListIterator(this.operations, this.keys.listIterator()); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean addAll(int index, Collection<? extends ModelNode> operations) { |
| 127 | + int start = index; |
| 128 | + for (ModelNode operation : operations) { |
| 129 | + PathAddress address = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)); |
| 130 | + if (this.operations.put(address, operation) == null) { |
| 131 | + this.keys.add(start++, address); |
| 132 | + } |
| 133 | + } |
| 134 | + return !operations.isEmpty(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public ModelNode get(int index) { |
| 139 | + return this.operations.get(this.keys.get(index)); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public ModelNode set(int index, ModelNode operation) { |
| 144 | + return this.operations.put(this.keys.get(index), operation); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void add(int index, ModelNode operation) { |
| 149 | + PathAddress address = PathAddress.pathAddress(operation.get(ModelDescriptionConstants.OP_ADDR)); |
| 150 | + this.keys.add(index, address); |
| 151 | + this.operations.put(address, operation); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public ModelNode remove(int index) { |
| 156 | + return this.operations.remove(this.keys.remove(index)); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public int indexOf(Object object) { |
| 161 | + int result = -1; |
| 162 | + for (Map.Entry<PathAddress, ModelNode> entry : this.operations.entrySet()) { |
| 163 | + if (entry.getValue().equals(object)) { |
| 164 | + return this.keys.indexOf(entry.getKey()); |
| 165 | + } |
| 166 | + } |
| 167 | + return result; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public int lastIndexOf(Object object) { |
| 172 | + return this.indexOf(object); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public ListIterator<ModelNode> listIterator() { |
| 177 | + return new OperationListIterator(this.operations, this.keys.listIterator()); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public ListIterator<ModelNode> listIterator(int index) { |
| 182 | + return new OperationListIterator(this.operations, this.keys.listIterator(index)); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public List<ModelNode> subList(int fromIndex, int toIndex) { |
| 187 | + return new OperationList(this.operations, this.keys.subList(fromIndex, toIndex)); |
| 188 | + } |
| 189 | + |
| 190 | + private static class OperationListIterator implements ListIterator<ModelNode> { |
| 191 | + private final Map<PathAddress, ModelNode> operations; |
| 192 | + private final ListIterator<PathAddress> keys; |
| 193 | + |
| 194 | + OperationListIterator(Map<PathAddress, ModelNode> operations, ListIterator<PathAddress> keys) { |
| 195 | + this.operations = operations; |
| 196 | + this.keys = keys; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public boolean hasNext() { |
| 201 | + return this.keys.hasNext(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public ModelNode next() { |
| 206 | + return this.operations.get(this.keys.next()); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public boolean hasPrevious() { |
| 211 | + return this.keys.hasPrevious(); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public ModelNode previous() { |
| 216 | + return this.operations.get(this.keys.previous()); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public int nextIndex() { |
| 221 | + return this.keys.nextIndex(); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public int previousIndex() { |
| 226 | + return this.keys.previousIndex(); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void remove() { |
| 231 | + throw new UnsupportedOperationException(); |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public void set(ModelNode operation) { |
| 236 | + throw new UnsupportedOperationException(); |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public void add(ModelNode operation) { |
| 241 | + throw new UnsupportedOperationException(); |
| 242 | + } |
| 243 | + } |
26 | 244 | }
|
27 | 245 | }
|
0 commit comments