1 //%2006////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //==============================================================================
32 //%/////////////////////////////////////////////////////////////////////////////
34 #include "CIMInstanceRep.h"
35 #include "CIMInstance.h"
36 #include "CIMClassRep.h"
38 #include "DeclContext.h"
41 #include "Constants.h"
42 #include "XmlWriter.h"
43 #include "MofWriter.h"
48 PEGASUS_NAMESPACE_BEGIN
50 CIMInstanceRep::CIMInstanceRep(const CIMObjectPath& reference)
51 : CIMObjectRep(reference)
56 CIMInstanceRep::~CIMInstanceRep()
61 void CIMInstanceRep::resolve(
63 const CIMNamespaceName& nameSpace,
64 CIMConstClass& cimClassOut,
65 Boolean propagateQualifiers)
67 // ATTN: Verify that references are initialized.
71 throw InstanceAlreadyResolved();
77 //----------------------------------------------------------------------
78 // First obtain the class:
79 //----------------------------------------------------------------------
81 CIMConstClass cimClass =
82 context->lookupClass(nameSpace, _reference.getClassName());
84 if (cimClass.isUninitialized())
85 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_INVALID_CLASS,
86 _reference.getClassName().getString ());
88 cimClassOut = cimClass;
91 if (!cimClass._rep->_resolved)
92 throw ClassNotResolved(_reference.getClassName());
95 //----------------------------------------------------------------------
96 // Disallow instantiation of abstract classes.
97 //----------------------------------------------------------------------
99 if (cimClass.isAbstract())
100 throw InstantiatedAbstractClass(_reference.getClassName().getString ());
102 //----------------------------------------------------------------------
103 // Validate and propagate qualifiers.
104 //----------------------------------------------------------------------
108 (cimClass.isAssociation()) ? CIMScope::ASSOCIATION : CIMScope::CLASS,
110 cimClass._rep->_qualifiers,
111 propagateQualifiers);
113 //----------------------------------------------------------------------
114 // First iterate the properties of this instance and verify that
115 // each one is defined in the class and then resolve each one.
116 //----------------------------------------------------------------------
118 CIMName className = cimClass.getClassName();
120 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
122 CIMProperty& property = _properties[i];
124 Uint32 index = cimClass.findProperty(property.getName());
126 if (index == PEG_NOT_FOUND)
129 // Allow addition of Creator property to Indication Subscription,
130 // Filter and Handler instances
132 // l10n add language property support
133 if (!(((className.equal
134 (CIMName (PEGASUS_CLASSNAME_INDSUBSCRIPTION))) ||
136 (CIMName (PEGASUS_CLASSNAME_FORMATTEDINDSUBSCRIPTION))) ||
138 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_CIMXML))) ||
140 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_CIMXML))) ||
142 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_SNMP))) ||
143 #ifdef PEGASUS_ENABLE_SYSTEM_LOG_HANDLER
145 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_SYSTEM_LOG))) ||
147 #ifdef PEGASUS_ENABLE_EMAIL_HANDLER
149 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_EMAIL))) ||
151 (className.equal (CIMName (PEGASUS_CLASSNAME_INDFILTER)))) &&
152 ((property.getName ().equal
153 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CREATOR))) ||
154 (property.getName ().equal
155 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_ACCEPTLANGS))) ||
156 (property.getName ().equal
157 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CONTENTLANGS))))))
159 throw NoSuchProperty(property.getName().getString ());
164 // resolve the property
165 Resolver::resolveProperty (property, context, nameSpace, true,
166 cimClass.getProperty (index), propagateQualifiers);
170 //----------------------------------------------------------------------
171 // Inject all properties from the class that are not included in the
172 // instance. Copy over the class-origin and set the propagated flag
173 // to true. NOTE: The propagated flag indicates that the property
174 // was not part of the property set input with the create and
175 // was inherited from the default in the class (see cimxml spec sect 3.1.5)
176 //----------------------------------------------------------------------
178 for (Uint32 i = 0, m = 0, n = cimClass.getPropertyCount(); i < n; i++)
180 CIMConstProperty property = cimClass.getProperty(i);
181 const CIMName& name = property.getName();
183 // See if this instance already contains a property with this name:
185 Boolean found = false;
187 for (Uint32 j = m, n = _properties.size(); j < n; j++)
189 if (name.equal(_properties[j].getName()))
198 CIMProperty p = property.clone();
199 p.setPropagated(true);
200 _properties.insert(m++, p);
209 CIMInstanceRep::CIMInstanceRep(const CIMInstanceRep& x) : CIMObjectRep(x)
213 void CIMInstanceRep::toXml(Buffer& out) const
215 // Class opening element:
217 out << STRLIT("<INSTANCE ");
218 out << STRLIT(" CLASSNAME=\"") << _reference.getClassName();
219 out << STRLIT("\" ");
220 out << STRLIT(">\n");
224 _qualifiers.toXml(out);
228 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
229 XmlWriter::appendPropertyElement(out, _properties[i]);
231 // Class closing element:
233 out << STRLIT("</INSTANCE>\n");
236 void CIMInstanceRep::toMof(Buffer& out) const
238 // Get and format the class qualifiers
239 out << STRLIT("\n//Instance of ") << _reference.getClassName();
240 if (_qualifiers.getCount())
242 _qualifiers.toMof(out);
244 // Separate qualifiers from Class Name
247 // output class statement
248 out << STRLIT("instance of ") << _reference.getClassName();
250 out << STRLIT("\n{");
252 // format the Properties:
253 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
255 // Generate MOF if this property not propagated
256 // Note that the test is required only because
257 // there is an error in getclass that does not
258 // test the localOnly flag.
259 // The false identifies this as value initializer, not
260 // property definition.
261 if (!_properties[i].getPropagated())
262 MofWriter::appendPropertyElement(false,out, _properties[i]);
265 // Class closing element:
266 out << STRLIT("\n};\n");
269 CIMObjectPath CIMInstanceRep::buildPath(
270 const CIMConstClass& cimClass) const
272 //--------------------------------------------------------------------------
274 //--------------------------------------------------------------------------
276 CIMName className = getClassName();
278 //--------------------------------------------------------------------------
280 //--------------------------------------------------------------------------
282 Array<CIMName> keyNames;
283 cimClass.getKeyNames(keyNames);
285 if (keyNames.size() == 0)
286 return CIMObjectPath("", CIMNamespaceName(), className);
288 //--------------------------------------------------------------------------
289 // Get type and value for each key (building up key bindings):
290 //--------------------------------------------------------------------------
292 Array<CIMKeyBinding> keyBindings;
294 for (Uint32 i = 0, n = keyNames.size(); i < n; i++)
296 const CIMName& keyName = keyNames[i];
298 Uint32 index = findProperty(keyName);
299 if (index == PEG_NOT_FOUND)
301 throw NoSuchProperty(keyName.getString());
304 CIMConstProperty tmp = getProperty(index);
306 if (keyName.equal(tmp.getName()))
308 keyBindings.append(CIMKeyBinding(keyName, tmp.getValue()));
312 return CIMObjectPath(String(), CIMNamespaceName(), className, keyBindings);
315 // KS Mar 05 - The following removal functions are very inefficient and should
316 // be optimized to avoid the multiple memory moves. Actually, the remove
317 // qualifiers should be added as a function and optimized that once.
318 void CIMInstanceRep::filter(
319 Boolean includeQualifiers,
320 Boolean includeClassOrigin,
321 const CIMPropertyList& propertyList)
323 // Filter any qualifiers from this instance.
324 if (!includeQualifiers && _qualifiers.getCount() > 0)
326 while (_qualifiers.getCount())
328 _qualifiers.removeQualifier(0);
332 // For each property, remove if not in propertylist
333 for (Uint32 i = 0 ; i < _properties.size(); i++)
335 CIMConstProperty p = getProperty(i);
336 CIMName name = p.getName();
337 Array<CIMName> pl = propertyList.getPropertyNameArray();
338 if (propertyList.isNull() || Contains(pl, name))
340 // test ClassOrigin and possibly remove
341 if (!includeClassOrigin)
343 _properties[i].setClassOrigin(CIMName());
345 // remove qualifiers if required.
346 if (!includeQualifiers && _properties[i].getQualifierCount() > 0)
348 while (_properties[i].getQualifierCount() > 0)
350 _properties[i].removeQualifier(0);
356 _properties.remove(i--);
362 PEGASUS_NAMESPACE_END