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 // Author: Mike Brasher (mbrasher@bmc.com)
34 // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
35 // Sushma Fernandes, Hewlett-Packard Company
36 // (sushma_fernandes@hp.com)
37 // Carol Ann Krug Graves, Hewlett-Packard Company
38 // (carolann_graves@hp.com)
39 // David Dillard, VERITAS Software Corp.
40 // (david.dillard@veritas.com)
41 // Yi Zhou, Hewlett-Packard Company (yi.zhou@hp.com)
43 //%/////////////////////////////////////////////////////////////////////////////
45 #include "CIMInstanceRep.h"
46 #include "CIMInstance.h"
47 #include "CIMClassRep.h"
49 #include "DeclContext.h"
53 #include "Constants.h"
54 #include "XmlWriter.h"
55 #include "MofWriter.h"
60 PEGASUS_NAMESPACE_BEGIN
62 CIMInstanceRep::CIMInstanceRep(const CIMObjectPath& reference)
63 : CIMObjectRep(reference)
68 CIMInstanceRep::~CIMInstanceRep()
73 void CIMInstanceRep::resolve(
75 const CIMNamespaceName& nameSpace,
76 CIMConstClass& cimClassOut,
77 Boolean propagateQualifiers)
79 // ATTN: Verify that references are initialized.
83 throw InstanceAlreadyResolved();
89 //----------------------------------------------------------------------
90 // First obtain the class:
91 //----------------------------------------------------------------------
93 CIMConstClass cimClass =
94 context->lookupClass(nameSpace, _reference.getClassName());
96 if (cimClass.isUninitialized())
97 throw PEGASUS_CIM_EXCEPTION(CIM_ERR_INVALID_CLASS,
98 _reference.getClassName().getString ());
100 cimClassOut = cimClass;
103 if (!cimClass._rep->_resolved)
104 throw ClassNotResolved(_reference.getClassName());
107 //----------------------------------------------------------------------
108 // Disallow instantiation of abstract classes.
109 //----------------------------------------------------------------------
111 if (cimClass.isAbstract())
112 throw InstantiatedAbstractClass(_reference.getClassName().getString ());
114 //----------------------------------------------------------------------
115 // Validate and propagate qualifiers.
116 //----------------------------------------------------------------------
120 (cimClass.isAssociation()) ? CIMScope::ASSOCIATION : CIMScope::CLASS,
122 cimClass._rep->_qualifiers,
123 propagateQualifiers);
125 //----------------------------------------------------------------------
126 // First iterate the properties of this instance and verify that
127 // each one is defined in the class and then resolve each one.
128 //----------------------------------------------------------------------
130 CIMName className = cimClass.getClassName();
132 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
134 CIMProperty& property = _properties[i];
136 Uint32 index = cimClass.findProperty(property.getName());
138 if (index == PEG_NOT_FOUND)
141 // Allow addition of Creator property to Indication Subscription,
142 // Filter and Handler instances
144 // l10n add language property support
145 if (!(((className.equal
146 (CIMName (PEGASUS_CLASSNAME_INDSUBSCRIPTION))) ||
148 (CIMName (PEGASUS_CLASSNAME_FORMATTEDINDSUBSCRIPTION))) ||
150 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_CIMXML))) ||
152 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_CIMXML))) ||
154 (CIMName (PEGASUS_CLASSNAME_INDHANDLER_SNMP))) ||
155 #ifdef PEGASUS_ENABLE_SYSTEM_LOG_HANDLER
157 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_SYSTEM_LOG))) ||
159 #ifdef PEGASUS_ENABLE_EMAIL_HANDLER
161 (CIMName (PEGASUS_CLASSNAME_LSTNRDST_EMAIL))) ||
163 (className.equal (CIMName (PEGASUS_CLASSNAME_INDFILTER)))) &&
164 ((property.getName ().equal
165 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CREATOR))) ||
166 (property.getName ().equal
167 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_ACCEPTLANGS))) ||
168 (property.getName ().equal
169 (CIMName (PEGASUS_PROPERTYNAME_INDSUB_CONTENTLANGS))))))
171 throw NoSuchProperty(property.getName().getString ());
176 // resolve the property
177 Resolver::resolveProperty (property, context, nameSpace, true,
178 cimClass.getProperty (index), propagateQualifiers);
179 // Ensure that the PROPAGATED attribute is false
180 property.setPropagated(false);
184 //----------------------------------------------------------------------
185 // Inject all properties from the class that are not included in the
186 // instance. Copy over the class-origin and set the propagated flag
187 // to true. NOTE: The propagated flag indicates that the property
188 // was not part of the property set input with the create and
189 // was inherited from the default in the class (see cimxml spec sect 3.1.5)
190 //----------------------------------------------------------------------
192 for (Uint32 i = 0, m = 0, n = cimClass.getPropertyCount(); i < n; i++)
194 CIMConstProperty property = cimClass.getProperty(i);
195 const CIMName& name = property.getName();
197 // See if this instance already contains a property with this name:
199 Boolean found = false;
201 for (Uint32 j = m, n = _properties.size(); j < n; j++)
203 if (name.equal(_properties[j].getName()))
212 CIMProperty p = property.clone();
213 p.setPropagated(true);
214 _properties.insert(m++, p);
223 CIMInstanceRep::CIMInstanceRep()
228 CIMInstanceRep::CIMInstanceRep(const CIMInstanceRep& x) : CIMObjectRep(x)
233 void CIMInstanceRep::toXml(Buffer& out) const
235 // Class opening element:
237 out << STRLIT("<INSTANCE ");
238 out << STRLIT(" CLASSNAME=\"") << _reference.getClassName();
239 out << STRLIT("\" ");
240 out << STRLIT(">\n");
244 _qualifiers.toXml(out);
248 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
249 XmlWriter::appendPropertyElement(out, _properties[i]);
251 // Class closing element:
253 out << STRLIT("</INSTANCE>\n");
256 void CIMInstanceRep::toMof(Buffer& out) const
258 // Get and format the class qualifiers
259 out << STRLIT("\n//Instance of Class ") << _reference.getClassName();
260 if (_qualifiers.getCount())
262 _qualifiers.toMof(out);
264 // Separate qualifiers from Class Name
267 // output class statement
268 out << STRLIT("instance of class ") << _reference.getClassName();
270 out << STRLIT("\n{");
272 // format the Properties:
273 for (Uint32 i = 0, n = _properties.size(); i < n; i++)
275 // Generate MOF if this property not propagated
276 // Note that the test is required only because
277 // there is an error in getclass that does not
278 // test the localOnly flag.
279 if (!_properties[i].getPropagated())
280 MofWriter::appendPropertyElement(out, _properties[i]);
283 // Class closing element:
284 out << STRLIT("\n};\n");
287 CIMObjectPath CIMInstanceRep::buildPath(
288 const CIMConstClass& cimClass) const
290 //--------------------------------------------------------------------------
292 //--------------------------------------------------------------------------
294 CIMName className = getClassName();
296 //--------------------------------------------------------------------------
298 //--------------------------------------------------------------------------
300 Array<CIMName> keyNames;
301 cimClass.getKeyNames(keyNames);
303 if (keyNames.size() == 0)
304 return CIMObjectPath("", CIMNamespaceName(), className);
306 //--------------------------------------------------------------------------
307 // Get type and value for each key (building up key bindings):
308 //--------------------------------------------------------------------------
310 Array<CIMKeyBinding> keyBindings;
312 for (Uint32 i = 0, n = keyNames.size(); i < n; i++)
314 const CIMName& keyName = keyNames[i];
316 Uint32 index = findProperty(keyName);
317 if (index == PEG_NOT_FOUND)
319 throw NoSuchProperty(keyName.getString());
322 CIMConstProperty tmp = getProperty(index);
324 if (keyName.equal(tmp.getName()))
326 keyBindings.append(CIMKeyBinding(keyName, tmp.getValue()));
330 return CIMObjectPath(String(), CIMNamespaceName(), className, keyBindings);
333 // KS Mar 05 - The following removal functions are very inefficient and should
334 // be optimized to avoid the multiple memory moves. Actually, the remove
335 // qualifiers should be added as a function and optimized that once.
336 void CIMInstanceRep::filter(Boolean includeQualifiers, Boolean includeClassOrigin,
337 const CIMPropertyList& propertyList)
339 // Filter any qualifiers from this instance.
340 if (!includeQualifiers && _qualifiers.getCount() > 0)
342 while( _qualifiers.getCount() )
344 _qualifiers.removeQualifier(0);
348 // For each property, remove if not in propertylist
349 for (Uint32 i = 0 ; i < _properties.size(); i++)
351 CIMConstProperty p = getProperty(i);
352 CIMName name = p.getName();
353 Array<CIMName> pl = propertyList.getPropertyNameArray();
354 if (propertyList.isNull() || Contains(pl, name))
356 // test ClassOrigin and possibly remove
357 if (!includeClassOrigin)
359 _properties[i].setClassOrigin(CIMName());
361 // remove qualifiers if required.
362 if (!includeQualifiers && _properties[i].getQualifierCount() > 0)
364 while( _properties[i].getQualifierCount() > 0 )
366 _properties[i].removeQualifier(0);
372 _properties.remove(i--);
378 PEGASUS_NAMESPACE_END