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"
42 #include "Constants.h"
43 #include "XmlWriter.h"
44 #include "MofWriter.h"
49 PEGASUS_NAMESPACE_BEGIN
51 CIMInstanceRep::CIMInstanceRep(const CIMObjectPath& reference)
52 : CIMObjectRep(reference)
57 CIMInstanceRep::~CIMInstanceRep()
62 void CIMInstanceRep::resolve(
64 const CIMNamespaceName& nameSpace,
65 CIMConstClass& cimClassOut,
66 Boolean propagateQualifiers)
68 // ATTN: Verify that references are initialized.
72 throw InstanceAlreadyResolved();
78 //----------------------------------------------------------------------
79 // First obtain the class:
80 //----------------------------------------------------------------------
82 CIMConstClass cimClass =
83 context->lookupClass(nameSpace, _reference.getClassName());
85 if (cimClass.isUninitialized())
86 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_INVALID_CLASS,
87 _reference.getClassName().getString ());
89 cimClassOut = cimClass;
92 if (!cimClass._rep->_resolved)
93 throw ClassNotResolved(_reference.getClassName());
96 //----------------------------------------------------------------------
97 // Disallow instantiation of abstract classes.
98 //----------------------------------------------------------------------
100 if (cimClass.isAbstract())
101 throw InstantiatedAbstractClass(_reference.getClassName().getString ());
103 //----------------------------------------------------------------------
104 // Validate and propagate qualifiers.
105 //----------------------------------------------------------------------
109 (cimClass.isAssociation()) ? CIMScope::ASSOCIATION : CIMScope::CLASS,
111 cimClass._rep->_qualifiers,
112 propagateQualifiers);
114 //----------------------------------------------------------------------
115 // First iterate the properties of this instance and verify that
116 // each one is defined in the class and then resolve each one.
117 //----------------------------------------------------------------------
119 CIMName className = cimClass.getClassName();
121 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
123 CIMProperty& property = _properties[i];
125 Uint32 index = cimClass.findProperty(property.getName());
127 if (index == PEG_NOT_FOUND)
130 // Allow addition of Creator property to Indication Subscription,
131 // Filter and Handler instances
133 // l10n add language property support
134 if (!(((className.equal
135 (CIMName (PEGASUS_CLASSNAME_INDSUBSCRIPTION))) ||
137 (CIMName (PEGASUS_CLASSNAME_FORMATTEDINDSUBSCRIPTION))) ||
139 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_CIMXML))) ||
141 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_CIMXML))) ||
143 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_SNMP))) ||
144 #ifdef PEGASUS_ENABLE_SYSTEM_LOG_HANDLER
146 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_SYSTEM_LOG))) ||
148 #ifdef PEGASUS_ENABLE_EMAIL_HANDLER
150 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_EMAIL))) ||
152 (className.equal (CIMName (PEGASUS_CLASSNAME_INDFILTER)))) &&
153 ((property.getName ().equal
154 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CREATOR))) ||
155 (property.getName ().equal
156 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_ACCEPTLANGS))) ||
157 (property.getName ().equal
158 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CONTENTLANGS))))))
160 throw NoSuchProperty(property.getName().getString ());
165 // resolve the property
166 Resolver::resolveProperty (property, context, nameSpace, true,
167 cimClass.getProperty (index), propagateQualifiers);
168 // Ensure that the PROPAGATED attribute is false
169 property.setPropagated(false);
173 //----------------------------------------------------------------------
174 // Inject all properties from the class that are not included in the
175 // instance. Copy over the class-origin and set the propagated flag
176 // to true. NOTE: The propagated flag indicates that the property
177 // was not part of the property set input with the create and
178 // was inherited from the default in the class (see cimxml spec sect 3.1.5)
179 //----------------------------------------------------------------------
181 for (Uint32 i = 0, m = 0, n = cimClass.getPropertyCount(); i < n; i++)
183 CIMConstProperty property = cimClass.getProperty(i);
184 const CIMName& name = property.getName();
186 // See if this instance already contains a property with this name:
188 Boolean found = false;
190 for (Uint32 j = m, n = _properties.size(); j < n; j++)
192 if (name.equal(_properties[j].getName()))
201 CIMProperty p = property.clone();
202 p.setPropagated(true);
203 _properties.insert(m++, p);
212 CIMInstanceRep::CIMInstanceRep()
217 CIMInstanceRep::CIMInstanceRep(const CIMInstanceRep& x) : CIMObjectRep(x)
222 void CIMInstanceRep::toXml(Buffer& out) const
224 // Class opening element:
226 out << STRLIT("<INSTANCE ");
227 out << STRLIT(" CLASSNAME=\"") << _reference.getClassName();
228 out << STRLIT("\" ");
229 out << STRLIT(">\n");
233 _qualifiers.toXml(out);
237 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
238 XmlWriter::appendPropertyElement(out, _properties[i]);
240 // Class closing element:
242 out << STRLIT("</INSTANCE>\n");
245 void CIMInstanceRep::toMof(Buffer& out) const
247 // Get and format the class qualifiers
248 out << STRLIT("\n//Instance of ") << _reference.getClassName();
249 if (_qualifiers.getCount())
251 _qualifiers.toMof(out);
253 // Separate qualifiers from Class Name
256 // output class statement
257 out << STRLIT("instance of ") << _reference.getClassName();
259 out << STRLIT("\n{");
261 // format the Properties:
262 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
264 // Generate MOF if this property not propagated
265 // Note that the test is required only because
266 // there is an error in getclass that does not
267 // test the localOnly flag.
268 // The false identifies this as value initializer, not
269 // property definition.
270 if (!_properties[i].getPropagated())
271 MofWriter::appendPropertyElement(false,out, _properties[i]);
274 // Class closing element:
275 out << STRLIT("\n};\n");
278 CIMObjectPath CIMInstanceRep::buildPath(
279 const CIMConstClass& cimClass) const
281 //--------------------------------------------------------------------------
283 //--------------------------------------------------------------------------
285 CIMName className = getClassName();
287 //--------------------------------------------------------------------------
289 //--------------------------------------------------------------------------
291 Array<CIMName> keyNames;
292 cimClass.getKeyNames(keyNames);
294 if (keyNames.size() == 0)
295 return CIMObjectPath("", CIMNamespaceName(), className);
297 //--------------------------------------------------------------------------
298 // Get type and value for each key (building up key bindings):
299 //--------------------------------------------------------------------------
301 Array<CIMKeyBinding> keyBindings;
303 for (Uint32 i = 0, n = keyNames.size(); i < n; i++)
305 const CIMName& keyName = keyNames[i];
307 Uint32 index = findProperty(keyName);
308 if (index == PEG_NOT_FOUND)
310 throw NoSuchProperty(keyName.getString());
313 CIMConstProperty tmp = getProperty(index);
315 if (keyName.equal(tmp.getName()))
317 keyBindings.append(CIMKeyBinding(keyName, tmp.getValue()));
321 return CIMObjectPath(String(), CIMNamespaceName(), className, keyBindings);
324 // KS Mar 05 - The following removal functions are very inefficient and should
325 // be optimized to avoid the multiple memory moves. Actually, the remove
326 // qualifiers should be added as a function and optimized that once.
327 void CIMInstanceRep::filter(
328 Boolean includeQualifiers,
329 Boolean includeClassOrigin,
330 const CIMPropertyList& propertyList)
332 // Filter any qualifiers from this instance.
333 if (!includeQualifiers && _qualifiers.getCount() > 0)
335 while (_qualifiers.getCount())
337 _qualifiers.removeQualifier(0);
341 // For each property, remove if not in propertylist
342 for (Uint32 i = 0 ; i < _properties.size(); i++)
344 CIMConstProperty p = getProperty(i);
345 CIMName name = p.getName();
346 Array<CIMName> pl = propertyList.getPropertyNameArray();
347 if (propertyList.isNull() || Contains(pl, name))
349 // test ClassOrigin and possibly remove
350 if (!includeClassOrigin)
352 _properties[i].setClassOrigin(CIMName());
354 // remove qualifiers if required.
355 if (!includeQualifiers && _properties[i].getQualifierCount() > 0)
357 while (_properties[i].getQualifierCount() > 0)
359 _properties[i].removeQualifier(0);
365 _properties.remove(i--);
371 PEGASUS_NAMESPACE_END