View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2009, 2013
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 : Ramandeep S Arora, IBM, arorar@us.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * ---------------------------------------------------------------------------
15   * 2878054    2009-10-25  raman_arora  Pull Enumeration Feature (PULL Parser)
16   *    2666    2013-09-19  blaschke-oss CR12: Remove ENUMERATIONCONTEXT
17   */
18  
19  package org.metricshub.wbem.sblim.cimclient.internal.wbem;
20  
21  /*-
22   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
23   * WBEM Java Client
24   * ჻჻჻჻჻჻
25   * Copyright 2023 - 2025 MetricsHub
26   * ჻჻჻჻჻჻
27   * Licensed under the Apache License, Version 2.0 (the "License");
28   * you may not use this file except in compliance with the License.
29   * You may obtain a copy of the License at
30   *
31   *      http://www.apache.org/licenses/LICENSE-2.0
32   *
33   * Unless required by applicable law or agreed to in writing, software
34   * distributed under the License is distributed on an "AS IS" BASIS,
35   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36   * See the License for the specific language governing permissions and
37   * limitations under the License.
38   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
39   */
40  
41  import java.io.IOException;
42  import java.io.InputStreamReader;
43  import java.util.ArrayList;
44  import javax.xml.parsers.ParserConfigurationException;
45  import org.metricshub.wbem.javax.cim.CIMArgument;
46  import org.metricshub.wbem.javax.cim.CIMObjectPath;
47  import org.metricshub.wbem.javax.wbem.CloseableIterator;
48  import org.metricshub.wbem.javax.wbem.WBEMException;
49  import org.metricshub.wbem.javax.wbem.client.EnumerateResponse;
50  import org.xml.sax.SAXException;
51  
52  /**
53   * Class EnumerateResponsePULL is responsible for all helper functions of PULL
54   * parser related with EnumerateResponse.
55   *
56   * @param <T>
57   */
58  public class EnumerateResponsePULL<T> {
59  	private EnumerateResponse<T> enumResponse;
60  
61  	/**
62  	 * Ctor.
63  	 *
64  	 * @param pStream
65  	 *            Input stream to be parsed
66  	 * @param pPath
67  	 *            CIMObject path
68  	 * @throws IOException
69  	 * @throws SAXException
70  	 * @throws ParserConfigurationException
71  	 * @throws WBEMException
72  	 */
73  	@SuppressWarnings("unchecked")
74  	public EnumerateResponsePULL(InputStreamReader pStream, CIMObjectPath pPath)
75  		throws IOException, SAXException, ParserConfigurationException, WBEMException {
76  		String enumContext = null;
77  		Boolean endOfSequence = null;
78  		ArrayList<T> list = new ArrayList<T>();
79  
80  		CloseableIterator<?> iter = new CloseableIteratorPULL(pStream, pPath);
81  
82  		// iterate through 'iter' for getCIMArguments to populate
83  		try {
84  			while (iter.hasNext()) list.add((T) iter.next());
85  		} catch (RuntimeException e) {
86  			iter.close();
87  			if (e.getCause() != null && e.getCause() instanceof WBEMException) {
88  				throw (WBEMException) e.getCause();
89  			}
90  			throw e;
91  		}
92  
93  		// pOutArgA can never be null
94  		CIMArgument<?>[] pOutArgA = ((CloseableIteratorPULL) iter).getCIMArguments();
95  		if (pOutArgA == null) {
96  			throw new IllegalArgumentException("Output auguments not found during CIM-XML PULL parser");
97  		}
98  
99  		for (int i = 0; i < pOutArgA.length; i++) {
100 			if (pOutArgA[i].getName().equals("EnumerationContext")) enumContext = (String) pOutArgA[i].getValue(); else if (
101 				pOutArgA[i].getName().equals("EndOfSequence")
102 			) endOfSequence = (Boolean) pOutArgA[i].getValue(); else throw new IllegalArgumentException(
103 				"Invalid argument : only EnumerationContext and EndOfSequence are allowed"
104 			);
105 		}
106 		// EndOfSequence can never be null
107 		if (endOfSequence == null) {
108 			throw new IllegalArgumentException("Invalid argument : EndOfSequence can never be null");
109 		}
110 
111 		// EnumerationContext can't be null if there is more data available
112 		if ((endOfSequence.booleanValue() == false) && (enumContext == null)) {
113 			throw new IllegalArgumentException(
114 				"Invalid argument : EnumerationContext cannot be null if there is more data available"
115 			);
116 		}
117 
118 		// create new closeableIterator as we cannot reuse 'iter'
119 		CloseableIterator<T> iterPull = (CloseableIterator<T>) new CloseableIteratorGeneric<T>(
120 			list.iterator(),
121 			iter.getWBEMException()
122 		);
123 
124 		this.enumResponse = new EnumerateResponse<T>(enumContext, iterPull, endOfSequence.booleanValue());
125 	}
126 
127 	/**
128 	 * Returns enumResponse
129 	 *
130 	 * @return The value of enumResponse.
131 	 */
132 	public EnumerateResponse<T> getEnumResponse() {
133 		return this.enumResponse;
134 	}
135 }