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-11-30  ebak         Make SBLIM client JSR48 compliant
16   * 1669961    2006-04-16  lupusalex    CIMTypedElement.getType() =>getDataType()
17   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
19   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
20   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
21   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
22   * 2797550    2009-06-01  raman_arora  JSR48 compliance - add Java Generics
23   */
24  
25  package org.metricshub.wbem.sblim.cimclient.internal.cim;
26  
27  /*-
28   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
29   * WBEM Java Client
30   * ჻჻჻჻჻჻
31   * Copyright 2023 - 2025 MetricsHub
32   * ჻჻჻჻჻჻
33   * Licensed under the Apache License, Version 2.0 (the "License");
34   * you may not use this file except in compliance with the License.
35   * You may obtain a copy of the License at
36   *
37   *      http://www.apache.org/licenses/LICENSE-2.0
38   *
39   * Unless required by applicable law or agreed to in writing, software
40   * distributed under the License is distributed on an "AS IS" BASIS,
41   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42   * See the License for the specific language governing permissions and
43   * limitations under the License.
44   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
45   */
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import org.metricshub.wbem.javax.cim.CIMObjectPath;
50  import org.metricshub.wbem.javax.cim.CIMProperty;
51  
52  /**
53   * Class CIMInstanceBuilder provides help for CIMInstance(CIMObjectPath,
54   * CIMProperty[]) constructor.<br>
55   * <br>
56   * CIMObjectPath param has to contain the key properties only.<br>
57   * ( VALUE.NAMEDINSTANCE->INSTANCENAME->KEYBINDING )<br>
58   * CIMProperty[] param has to contain all properties, including key properties.<br>
59   * ( VALUE.NAMEDINSTANCE->INSTANCE->PROPERTY* )<br>
60   * The implementation merges the properties from both params.<br>
61   * From CIMObjectPath's keys only the type and value information is considered.<br>
62   *
63   */
64  public class CIMInstanceBuilder {
65  	private CIMProperty<?>[] iProperties;
66  
67  	private static final Object[] EMPTY_RPOP_A = new CIMProperty[0];
68  
69  	/**
70  	 * Ctor.
71  	 *
72  	 * @param pPath
73  	 * @param pProps
74  	 * @throws IllegalArgumentException
75  	 */
76  	public CIMInstanceBuilder(CIMObjectPath pPath, CIMProperty<?>[] pProps) throws IllegalArgumentException {
77  		this.iProperties = pProps != null ? pProps : new CIMProperty[0];
78  		CIMElementSorter.sort(this.iProperties);
79  		addPathKeys(pPath);
80  	}
81  
82  	/**
83  	 * Extends the keys of the passed CIMObjectPath.
84  	 *
85  	 * @param pPath
86  	 * @return the new CIMObjectPath
87  	 */
88  	public CIMObjectPath setKeys(CIMObjectPath pPath) {
89  		List<CIMProperty<?>> keys = new ArrayList<CIMProperty<?>>();
90  		for (int i = 0; i < this.iProperties.length; ++i) {
91  			CIMProperty<?> prop = this.iProperties[i];
92  			if (prop.isKey()) keys.add(prop);
93  		}
94  
95  		/*
96  		 * CIMObjectPath( String scheme, String host, String port, String
97  		 * namespace, String objectName, CIMProperty[] keys )
98  		 */
99  		return new CIMObjectPath(
100 			pPath.getScheme(),
101 			pPath.getHost(),
102 			pPath.getPort(),
103 			pPath.getNamespace(),
104 			pPath.getObjectName(),
105 			(CIMProperty[]) keys.toArray(EMPTY_RPOP_A)
106 		);
107 	}
108 
109 	/**
110 	 * getAllPropertis
111 	 *
112 	 * @return all properties in an ordered way
113 	 */
114 	public CIMProperty<?>[] getAllPropertis() {
115 		return this.iProperties;
116 	}
117 
118 	private void addPathKeys(CIMObjectPath pPath) throws IllegalArgumentException {
119 		CIMProperty<?>[] keys = pPath.getKeys();
120 		for (int i = 0; i < keys.length; i++) {
121 			CIMProperty<?> key = keys[i];
122 			int pos = CIMElementSorter.findIdx(this.iProperties, key.getName());
123 			if (pos < 0) {
124 				pos = -pos - 1;
125 				CIMProperty<?>[] newArray = new CIMProperty[this.iProperties.length + 1];
126 				System.arraycopy(this.iProperties, 0, newArray, 0, pos);
127 				newArray[pos] = key;
128 				System.arraycopy(this.iProperties, pos, newArray, pos + 1, this.iProperties.length - pos);
129 				this.iProperties = newArray;
130 			} else {
131 				CIMProperty<?> prop = this.iProperties[pos];
132 				// typeAndValueCheck(key, prop);
133 				if (!prop.isKey()) this.iProperties[pos] = mkKey(prop);
134 			}
135 		}
136 	}
137 
138 	private static CIMProperty<Object> mkKey(CIMProperty<?> pProp) {
139 		return new CIMProperty<Object>(
140 			pProp.getName(),
141 			pProp.getDataType(),
142 			pProp.getValue(),
143 			true,
144 			pProp.isPropagated(),
145 			pProp.getOriginClass()
146 		);
147 	}
148 	/*
149 	 * private static void typeAndValueCheck(CIMProperty pPathProp, CIMProperty
150 	 * pArrayProp) throws IllegalArgumentException { CIMDataType pType =
151 	 * pPathProp.getDataType(), aType = pArrayProp.getDataType(); if (pType ==
152 	 * null ? aType != null : pType.getType() != aType.getType()) throw new
153 	 * IllegalArgumentException( pPathProp.getName() + " property presents in
154 	 * CIMObjectPath param and CIMProperty[] param " + "with different types!");
155 	 * Object pValue = pPathProp.getValue(), aValue = pArrayProp.getValue(); if
156 	 * (pValue == null ? aValue != null : !pValue.equals(aValue)) throw new
157 	 * IllegalArgumentException( pPathProp.getName() + " property conatins
158 	 * different values in CIMObjectPath and " + "CIMProperty params!"); }
159 	 */
160 
161 }