1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.metricshub.wbem.sblim.cimclient.internal.wbem;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.io.IOException;
50 import java.io.InputStreamReader;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.logging.Level;
54 import javax.xml.parsers.DocumentBuilder;
55 import javax.xml.parsers.DocumentBuilderFactory;
56 import org.metricshub.wbem.javax.cim.CIMObjectPath;
57 import org.metricshub.wbem.javax.wbem.CloseableIterator;
58 import org.metricshub.wbem.javax.wbem.WBEMException;
59 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.CIMMessage;
60 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.CIMResponse;
61 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.CIMXMLParseException;
62 import org.metricshub.wbem.sblim.cimclient.internal.cimxml.CIMXMLParserImpl;
63 import org.metricshub.wbem.sblim.cimclient.internal.http.io.TrailerException;
64 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
65 import org.w3c.dom.Document;
66 import org.xml.sax.InputSource;
67
68
69
70
71
72
73 public class CloseableIteratorDOM implements CloseableIterator<Object> {
74 private Iterator<Object> iItr;
75
76 private CIMResponse iRsp;
77
78 private WBEMException iWBEMException;
79
80 private List<Object> outParamValues;
81
82 private void trace(Level pLevel, String pMsg, Exception pE) {
83 LogAndTraceBroker.getBroker().trace(pLevel, pMsg, pE);
84 }
85
86 private void trace(Level pLevel, String pMsg) {
87 LogAndTraceBroker.getBroker().trace(pLevel, pMsg, null);
88 }
89
90
91
92
93
94
95
96
97
98 public CloseableIteratorDOM(InputStreamReader pStream, CIMObjectPath pPath) throws WBEMException, IOException {
99 try {
100 parse(new InputSource(pStream), pPath);
101 } finally {
102 pStream.close();
103 }
104 }
105
106
107
108
109
110
111
112
113 public CloseableIteratorDOM(InputSource pIs, CIMObjectPath pLocalPath) throws WBEMException {
114 parse(pIs, pLocalPath);
115 }
116
117 public void close() {
118
119 this.iItr = null;
120 this.iRsp = null;
121 this.iWBEMException = null;
122 this.outParamValues = null;
123 }
124
125 public boolean hasNext() {
126 if (this.iWBEMException != null) throw new RuntimeException(this.iWBEMException);
127 if (this.iRsp == null) return false;
128 try {
129 this.iRsp.checkError();
130 } catch (WBEMException e) {
131 throw new RuntimeException(e);
132 }
133 return this.iItr == null ? false : this.iItr.hasNext();
134 }
135
136 public Object next() {
137 return this.iItr.next();
138 }
139
140 public void remove() {
141 throw new UnsupportedOperationException();
142 }
143
144 public WBEMException getWBEMException() {
145 return this.iWBEMException;
146 }
147
148
149
150
151
152
153
154 public List<Object> getParamValues() {
155 return this.outParamValues;
156 }
157
158 private void parse(InputSource pIs, CIMObjectPath pLocalPath) throws WBEMException {
159
160 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
161 Document dom;
162 try {
163
164 DocumentBuilder db = dbf.newDocumentBuilder();
165
166 dom = db.parse(pIs);
167 } catch (TrailerException e) {
168 this.iWBEMException = e.getWBEMException();
169 return;
170 } catch (Exception e) {
171 String msg = "Exception occurred during DOM parsing!";
172 trace(Level.SEVERE, msg, e);
173 throw new WBEMException(WBEMException.CIM_ERR_FAILED, msg, null, e);
174 }
175 CIMXMLParserImpl.setLocalObjectPath(pLocalPath);
176 CIMMessage cimMsg;
177 try {
178 cimMsg = CIMXMLParserImpl.parseCIM(dom.getDocumentElement());
179 } catch (CIMXMLParseException e) {
180 String msg = "Exception occurred during parseCIM!";
181 trace(Level.SEVERE, msg, e);
182 throw new WBEMException(WBEMException.CIM_ERR_FAILED, msg, null, e);
183 }
184 if (!(cimMsg instanceof CIMResponse)) {
185 String msg = "CIM message must be response!";
186 trace(Level.SEVERE, msg);
187 throw new WBEMException(msg);
188 }
189 this.iRsp = (CIMResponse) cimMsg;
190 List<Object> retValVec = this.iRsp.getFirstReturnValue();
191 this.iItr = retValVec == null ? null : retValVec.iterator();
192 this.outParamValues = this.iRsp.getParamValues();
193 }
194 }