Home | History | Annotate | Download | only in srm
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright (c) 2001 by Sun Microsystems, Inc.
     23  * All rights reserved.
     24  *
     25  * SRMProvider.java
     26  */
     27 
     28 
     29 package com.sun.wbem.solarisprovider.srm;
     30 
     31 import javax.wbem.cim.*;
     32 import javax.wbem.client.*;
     33 import javax.wbem.provider.*;
     34 import javax.wbem.query.*;
     35 
     36 import com.sun.wbem.utility.log.*;
     37 import com.sun.wbem.utility.authorization.AuthorizationUtility;
     38 import com.sun.wbem.solarisprovider.common.ProviderUtility;
     39 
     40 import java.util.Vector;
     41 
     42 
     43 /**
     44  * This is the base class for the SRM providers.
     45  * It contains default implementations of the WBEM provider API methods which
     46  * return the CIM_ERR_NOTSUPPORTED error. Each concrete user manager provider
     47  * subclass overrides the methods with its own implementation. This class also
     48  * contains several utility methods which may be useful to the individual
     49  * provider classes.
     50  * @author Sun Microsystems
     51  */
     52 public abstract class SRMProvider
     53 	implements InstanceProvider, MethodProvider, SRMProviderProperties {
     54 
     55     /**
     56      * The handle to the CIMOM.
     57      */
     58     private CIMOMHandle cimomhandle = null;
     59 
     60     /**
     61      * Handle to the log service.
     62      */
     63     LogUtil logUtil = null;
     64 
     65     /**
     66      * Some often used exception are defined here to save some memory.
     67      */
     68     protected static final CIMProviderException notFoundEx =
     69     	new CIMProviderException(CIMException.CIM_ERR_NOT_FOUND);
     70 
     71     protected static final CIMProviderException generalEx =
     72     	new CIMProviderException(CIMProviderException.GENERAL_EXCEPTION);
     73 
     74     protected static final CIMException	notSupported =
     75     	new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED);
     76 
     77     /**
     78      * Severity indicator 'ERROR' for logging.
     79      */
     80     protected static final int LOGERROR = LogUtil.ERROR;
     81 
     82     /**
     83      * Severity indicator 'WARNING' for logging.
     84      */
     85     protected static final int LOGWARNING = LogUtil.WARNING;
     86 
     87     /**
     88      * Severity indicator 'INFO' for logging.
     89      */
     90     protected static final int LOGINFO = LogUtil.INFO;
     91 
     92     /**
     93      * Classname of resource messages for logging.
     94      */
     95     protected static final String RESOURCEBUNDLE =
     96     	"com.sun.wbem.solarisprovider.srm.resources.LogMessages";
     97 
     98     /**
     99      * Handle to the resource monitor, which controls the access
    100      * into the resource data cache (DataModel).
    101      */
    102     ResourceMonitor resourceMonitor;
    103 
    104     /**
    105      * This must be implemented by each subclass to make its
    106      * class name visible.
    107      * @returns String provider class name
    108      */
    109     protected abstract String getProviderName();
    110 
    111 
    112     //
    113     // Default implementations of the WBEM Provider API methods
    114     //
    115 
    116 
    117     /**
    118      * Called by the CIMOM when the provider is initialized.
    119      *
    120      * @exception   CIMException    the client connection failed
    121      */
    122     public void initialize(CIMOMHandle cimomhandle)
    123 	throws CIMException {
    124 
    125     	int updateTime = -1;
    126     	int rdsTimeout = -1;
    127     	int rdsInterval = -1;
    128 
    129 	// Save the cimomhandle.
    130 	this.cimomhandle = cimomhandle;
    131 
    132 	// Establish the logging facility
    133 	logUtil = LogUtil.getInstance(cimomhandle);
    134 
    135 	try {
    136 	    if (Util.propertyUPDATETIME != null) {
    137 	    	updateTime = Integer.parseInt(Util.propertyUPDATETIME);
    138 	    }
    139 	    if (Util.propertyRDSTIMEOUT != null) {
    140 	    	rdsTimeout = Integer.parseInt(Util.propertyRDSTIMEOUT);
    141 	    }
    142 	    if (Util.propertyRDSINTERVAL != null) {
    143 	    	rdsInterval = Integer.parseInt(Util.propertyRDSINTERVAL);
    144 	    }
    145 	} catch (Exception e)  { };
    146 
    147 	try {
    148 	    resourceMonitor = ResourceMonitor.getHandle();
    149 	    resourceMonitor.openDataModel(rdsTimeout, rdsInterval, updateTime);
    150 	} catch (Exception e) {
    151 	    String msg = writeLog(LOGERROR, "SRM_1000", "SRM_10000");
    152 	    writeLog(LOGERROR, e);
    153 	    throw new CIMException(CIMException.CIM_ERR_FAILED, msg);
    154 	}
    155 
    156     } // end initialize
    157 
    158     /**
    159      * Called by the CIMOM when the provider is removed. Currently the CIMOM
    160      * does not remove providers, but this method is provided for future
    161      * versions.
    162      *
    163      * @exception CIMException	The method cleanup() throws a CIMException.
    164      */
    165     public void cleanup() throws CIMException {
    166 
    167 	SRMDebug.trace(SRMDebug.METHOD_CALL, "closing rds data model");
    168 	resourceMonitor.closeDataModel();
    169     }
    170 
    171     /**
    172      * This method must be implemented by instance providers to create
    173      * the instance specified in the object path. If the instance does
    174      * exist, CIMInstanceException with ID CIM_ERR_ALREADY_EXISTS
    175      * must be thrown. The parameter should be the instance name.
    176      *
    177      * @param	op	The path of the instance to be set. The important part
    178      *			in this parameter is the namespace component.
    179      * @param	ci	The instance to be set.
    180      * @return	CIMObjectPath of the instance that was created.
    181      * @exception CIMException	This method throws a CIMException.
    182      */
    183     public synchronized CIMObjectPath createInstance(CIMObjectPath op,
    184 					CIMInstance ci)
    185 	    throws CIMException {
    186 
    187 	throw notSupported;
    188     }
    189 
    190     /**
    191      * Retrieves the instance specified in the argument CIMObjectPath.
    192      *
    193      * @param op - the name of the instance to be retrieved. This must include
    194      * all of the keys and values for the instance.
    195      * @param localOnly - if true, only the local properties of the class are
    196      * returned, otherwise all properties are required
    197      * @param includeQualifiers - if true, the qualifiers are returned as part
    198      * of of the returned instancei, otherwise no qualifiers will be returned
    199      * @param includeClassOrigin - if true, the class origin of each property
    200      * will be returned
    201      * @param String[] - if null, all properties are returned, otherwise only
    202      * the properties specified will be returned. Any duplicate properties will
    203      * be ignored.
    204      * @param cc - the class reference
    205      *
    206      * @return	CIMInstance the retrieved instance.
    207      * @exception CIMException - the method getInstance throws a CIMException
    208      * if the CIMObjectPath is incorrect or does not exist.
    209      */
    210     public synchronized CIMInstance getInstance(CIMObjectPath op,
    211 				   boolean localOnly,
    212 				   boolean includeQualifiers,
    213 				   boolean includeClassOrigin,
    214 				   String[] propList,
    215 				   CIMClass cc)
    216 	    throws CIMException {
    217 
    218 	throw notSupported;
    219     }
    220 
    221     /**
    222      * This method must be implemented by instance providers to set
    223      * the instance specified in the object path. If the instance does
    224      * not exist, CIMInstanceException with ID CIM_ERR_NOT_FOUND
    225      * must be thrown. The parameter should be the instance name.
    226      *
    227      * @param	op	The path of the instance to be set. The important part
    228      *			in this parameter is the namespace component.
    229      * @param	ci	The instance to be set.
    230      * @exception CIMException	The setInstance method throws a CIMException.
    231      */
    232     public synchronized void setInstance(CIMObjectPath op,
    233 			    CIMInstance ci)
    234 	    throws CIMException {
    235 
    236 	throw notSupported;
    237     }
    238 
    239     /**
    240      * This method must be implemented by instance providers to delete
    241      * the instance specified in the object path.
    242      *
    243      * @param	ci	The instance to be deleted.
    244      * @exception CIMException	The deleteInstance method throws a
    245      *				CIMException.
    246      */
    247     public synchronized void deleteInstance(CIMObjectPath op)
    248 	    throws CIMException {
    249 
    250 	throw notSupported;
    251     }
    252 
    253     /**
    254      * Enumerates all instances of the class which is specified by the
    255      * CIMObjectPath argument. The entire instances and not just the names
    256      * are returned.
    257      *
    258      * @param op - the object path specifies the class to be enumerated
    259      * localOnly - if true, only the local properties of the class are returned,
    260      * otherwise all properties are required
    261      * @param includeQualifiers - if true, the qualifiers are returned as part
    262      * of of the returned instancei, otherwise no qualifiers will be returned
    263      * @param includeClassOrigin - if true, the class origin of each property
    264      * will be returned
    265      * @param String[] - if null, all properties are returned, otherwise only
    266      * the properties specified will be
    267      * returned. Any duplicate properties will be ignored.
    268      * @param cc - the class reference
    269      * @return An array of CIMInstance
    270      * @exception CIMException - if the CIMObjectPath is incorrect or does not
    271      * exist.
    272      */
    273     public synchronized CIMInstance[] enumerateInstances(CIMObjectPath op,
    274 					    boolean localOnly,
    275 					    boolean includeQualifiers,
    276 					    boolean includeClassOrigin,
    277 					    String[] propList,
    278 					    CIMClass cc)
    279 	    throws CIMException {
    280 
    281 	throw notSupported;
    282     }
    283 
    284     /**
    285      * Enumerates all of the instances of the class which is specified by
    286      * the CIMObjectPath argument. Only the class name portion of the
    287      * CIMObjectPath argument is used, any additional information will be
    288      * ignored
    289      *
    290      * @param op - the class name to enumerate the instances
    291      * @param cc - the class reference passed to the provider
    292      * @return an array of CIMObjectPath containing names of the enumerated
    293      * instances.
    294      * @exception CIMException - if the classname is null or does not exist.
    295      */
    296     public synchronized CIMObjectPath[] enumerateInstanceNames(CIMObjectPath op,
    297 						boolean deep,
    298 						CIMClass cc)
    299 	    throws CIMException {
    300 
    301 	throw notSupported;
    302     }
    303 
    304     /**
    305      * This method must be implemented by instance providers to enumerate
    306      * instances of the class which is specified in op which meet the criteria
    307      * defined by the query string.
    308      *
    309      * @param	op	The object path specifies the class that must
    310      *			be enumerated.
    311      * @param	query	The criteria.
    312      * @param	ql	The CIM query.
    313      * @param	cc	The class reference.
    314      * @return CIMInstance The retrieved instance.
    315      * @exception CIMException	This method throws a CIMException message if the
    316      *				if operation is not supported.
    317      */
    318     public synchronized CIMInstance[] execQuery(CIMObjectPath op,
    319 				   String query,
    320 				   String ql,
    321 				   CIMClass cc)
    322 	    throws CIMException {
    323 
    324 	writeLog(LOGINFO,
    325 	  "SRM_0001",
    326 	  "SRM_5003",
    327 	  op.toString(), query, ql);
    328 
    329 	throw notSupported;
    330     }
    331 
    332     /**
    333      * This method contains the implementation for the method. The CIMOM calls
    334      * this method when the method specified in the parameters is to be invoked.
    335      * @param op	 Contains the path to the instance whose method must be
    336      *			 invoked.
    337      * @param methodName The name of the method.
    338      * @param inParams	 This is a vector of CIMValues which are the input
    339      *			 parameters for the method.
    340      * @param outParams  This is a vector of CIMValues which are the output
    341      *			 parameters for the method.
    342      * @return CIMValue  The return value of the method. If the method has no
    343      *			 return value, it must return null.
    344      * @exception CIMException	This method throws a CIMException
    345      */
    346     public synchronized CIMValue invokeMethod(CIMObjectPath op,
    347     	String methodName, Vector inParams, Vector outParams)
    348 	throws CIMException {
    349 
    350 	SRMDebug.trace(SRMDebug.METHOD_CALL, op.toString() + methodName);
    351 	if (methodName.equalsIgnoreCase("getBulkData")) {
    352 	    return getBulkData(outParams);
    353 	} else {
    354             throw new CIMMethodException(CIMMethodException.NO_SUCH_METHOD,
    355                 methodName, op.getObjectName());
    356 	}
    357 
    358     } // end invokeMethod
    359 
    360     /**
    361      * This method contains the implementation for the method. The CIMOM calls
    362      * this method when the method specified in the parameters is to be invoked.
    363      * @param op    Contains the path to the instance whose method must be
    364      *	    	    invoked.
    365      * @param methodName    The name of the method.
    366      * @param outParams This is a vector of CIMValues which are the output
    367      *      	    parameters for the method.
    368      * @return CIMValue The return value of the method. If the method has
    369      *	    	    no return value, it must return null.
    370      * @exception CIMException The invokeMethod method throws a CIMException.
    371      */
    372     protected CIMValue getBulkData(Vector outParams)
    373     	throws CIMException {
    374 	throw notSupported;
    375     }
    376 
    377     //
    378     // Logging methods.
    379     //
    380 
    381     /**
    382      * Utility logging method.
    383      * @return the log message.
    384      * @param 	severity 	the reported severity level.
    385      * @param 	x 		an Exception to be logged.
    386      */
    387     protected String writeLog(int severity, Exception x) {
    388 	return writeLog(severity, x.toString());
    389     }
    390 
    391 
    392     /**
    393      * Utility logging method.
    394      * @return the log message.
    395      * @param 	severity 	the reported severity level.
    396      * @param 	summary 	the short summary to be logged.
    397      * @param 	x 		an Exception to be logged.
    398      */
    399     protected String writeLog(int severity,
    400       			    String summary,
    401                             Exception x) {
    402 	return writeLog(severity, summary, x.toString());
    403     }
    404 
    405 
    406     /**
    407      * Utility logging method.
    408      * @return the log message.
    409      * @param 	severity 	the reported severity level.
    410      * @param 	summary 	the short summary to be logged.
    411      * @param 	detail 		the detailed message to be logged.
    412      * @param 	x 		an Exception to be logged.
    413      */
    414     protected String writeLog(int severity,
    415                             String summary,
    416                             String detail,
    417                             Exception x) {
    418 	return writeLog(severity, summary, detail, x.toString());
    419     }
    420 
    421 
    422     /**
    423      * Utility logging method.
    424      * @return the log message.
    425      * @param 	severity 	the reported severity level.
    426      * @param 	summary 	the short summary to be logged.
    427      */
    428     protected String writeLog(int severity, String summary) {
    429 	return writeLog(severity,
    430 	  summary,
    431 	  null,
    432 	  null,
    433 	  null,
    434 	  null,
    435 	  null);
    436     }
    437 
    438 
    439     /**
    440      * Utility logging method.
    441      * @return the log message.
    442      * @param 	severity 	the reported severity level.
    443      * @param 	summary 	the short summary to be logged.
    444      * @param 	detail 		the detailed message to be logged.
    445      */
    446     protected String writeLog(int severity,
    447       			    String summary,
    448       			    String detail) {
    449 	return writeLog(severity,
    450 	  summary,
    451 	  detail,
    452 	  null,
    453 	  null,
    454 	  null,
    455 	  null);
    456     }
    457 
    458 
    459     /**
    460      * Utility logging method.
    461      * @return the log message.
    462      * @param 	severity 	the reported severity level.
    463      * @param 	summary 	the short summary to be logged.
    464      * @param 	detail	 	the detailed message to be logged.
    465      * @param 	arg1 		the first parameter to substitute
    466      *				into the logged message.
    467      */
    468     protected String writeLog(int severity,
    469       			    String summary,
    470       			    String detail,
    471       			    String arg1) {
    472 	return writeLog(severity,
    473 	  summary,
    474 	  detail,
    475 	  arg1,
    476 	  null,
    477 	  null,
    478 	  null);
    479     }
    480 
    481 
    482     /**
    483      * Utility logging method.
    484      * @return the log message.
    485      * @param 	severity 	the reported severity level.
    486      * @param 	summary 	the short summary to be logged.
    487      * @param 	detail	 	the detailed message to be logged.
    488      * @param 	arg1 		the first parameter to substitute
    489      *				into the logged message.
    490      * @param 	arg2 		the second parameter to substitute
    491      *				into the logged message.
    492      */
    493     protected String writeLog(int severity,
    494       			    String summary,
    495       			    String detail,
    496       			    String arg1,
    497       			    String arg2) {
    498 	return writeLog(severity,
    499 	  summary,
    500 	  detail,
    501 	  arg1,
    502 	  arg2,
    503 	  null,
    504 	  null);
    505     }
    506 
    507 
    508     /**
    509      * Utility logging method.
    510      * @return the log message.
    511      * @param 	severity 	the reported severity level.
    512      * @param 	summary 	the short summary to be logged.
    513      * @param 	detail	 	the detailed message to be logged.
    514      * @param 	arg1 		the first parameter to substitute
    515      *				into the logged message.
    516      * @param 	arg2 		the second parameter to substitute
    517      *				into the logged message.
    518      * @param 	arg3 		the third parameter to substitute
    519      * 				into the logged message.
    520      */
    521     protected String writeLog(int severity, String summary,
    522       			    String detail,
    523       			    String arg1,
    524       			    String arg2,
    525       			    String arg3) {
    526 	return writeLog(severity,
    527 	  summary,
    528 	  detail,
    529 	  arg1,
    530 	  arg2,
    531 	  arg3,
    532 	  null);
    533     }
    534 
    535 
    536     /**
    537      * Utility logging method (bottom-level implementation).
    538      * @return a formatted version of the log message
    539      *           (<providerName>: <summaryMessage>).
    540      * @param severity the reported severity level.
    541      * @param summary the short summary to be logged.
    542      * @param detail the detailed message to be logged.
    543      * @param arg1 the first parameter to substitute into the logged message.
    544      * @param arg2 the second parameter to substitute into the logged message.
    545      * @param arg3 the third parameter to substitute into the logged message.
    546      * @param arg4 the fourth parameter to substitute into the logged message.
    547      */
    548     protected String writeLog(int severity,
    549       			    String summary,
    550       			    String detail,
    551       			    String arg1,
    552       			    String arg2,
    553       			    String arg3,
    554       			    String arg4) {
    555 	String[] args = {arg1, arg2, arg3, arg4};
    556 	String logmsg = "";
    557 
    558 	try {
    559 	    logUtil.writeLog(
    560 		getProviderName(),
    561 		  summary,
    562 		  detail,
    563 		  args,
    564 		  "",
    565 		  true,
    566 		  LogUtil.APPLICATION_LOG,
    567 		  severity,
    568 		  RESOURCEBUNDLE);
    569 
    570 	    logmsg = getProviderName() +
    571 	      		": " +
    572 	      		logUtil.getSummaryMesg(summary, true, RESOURCEBUNDLE);
    573 	} catch (Exception x) {
    574 	    System.err.println("writeLog threw " + x);
    575 	    x.printStackTrace();
    576 	}
    577 
    578 	return logmsg;
    579     }
    580 
    581     protected static String getBundleName() {
    582 	return (RESOURCEBUNDLE);
    583     }
    584 
    585 } // end class SRMProvider
    586