View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, ebak@de.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * -------------------------------------------------------------------------------
15   * 1565892    2006-12-11  ebak         Make SBLIM client JSR48 compliant
16   * 1688273    2007-04-16  ebak         Full support of HTTP trailers
17   * 1708584    2007-04-27  ebak         CloseableIterator might not clean up streams
18   * 1737141    2007-06-18  ebak         Sync up with JSR48 evolution
19   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
20   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
21   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
22   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
23   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
24   * 2860081    2009-09-17  raman_arora  Pull Enumeration Feature (DOM Parser)
25   */
26  
27  package org.metricshub.wbem.sblim.cimclient.internal.wbem;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * WBEM Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 - 2025 MetricsHub
34   * ჻჻჻჻჻჻
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
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   * Class CloseableIteratorDOM is a CloseableIterator implementation for the
70   * CIM-XML DOM parser.
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  	 * Ctor.
92  	 *
93  	 * @param pStream
94  	 * @param pPath
95  	 * @throws WBEMException
96  	 * @throws IOException
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 	 * Ctor.
108 	 *
109 	 * @param pIs
110 	 * @param pLocalPath
111 	 * @throws WBEMException
112 	 */
113 	public CloseableIteratorDOM(InputSource pIs, CIMObjectPath pLocalPath) throws WBEMException {
114 		parse(pIs, pLocalPath);
115 	}
116 
117 	public void close() {
118 		// release things
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 	 * getParamValues : returns the list of CIMArgument parsed parameters and
150 	 * their values : String name, CIMDataType type, Object value
151 	 *
152 	 * @return List of CIMArgument
153 	 */
154 	public List<Object> getParamValues() {
155 		return this.outParamValues;
156 	}
157 
158 	private void parse(InputSource pIs, CIMObjectPath pLocalPath) throws WBEMException {
159 		// get the factory
160 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
161 		Document dom;
162 		try {
163 			// Using factory get an instance of document builder
164 			DocumentBuilder db = dbf.newDocumentBuilder();
165 			// parse using builder to get DOM representation of the XML file
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 }