3 Replies Latest reply on May 22, 2013 11:22 AM by Arun Raj

    Which Design pattern has been followed up for Plugins ? ? ?

    Arun Raj Expert

      Hi Developers,

       

          I am a newbie to java as well as RHQ. I feel discomfort to write plugin without knowing How to pass objects for multiple resources. Please help me to understand the design pattern which you have followed for plugins.

       

      Problem :

       

         I want to specify a different configuration for all resources. In order to do it, I want to know the design pattern of the class for my implementation perspective. Please help me to do it.

       

      Regards,

      ArunRaj

        • 1. Re: Which Design pattern has been followed up for Plugins ? ? ?
          jay shaughnessy Expert

          There is some analogy between RHQ Plugin design and Java's object oreiented design.   In Java you define a class and instances of the class are objects.  Each of those objects can have different values for the fields defined for the class.   In RHQ you define Resource Types (in the plugin descriptor) and instances of the Resource Type are Resources.  Each of those resources typically represents something being monitored in the real world and can have different values for the fields defined for the type, like plugin configuration (aka connection properties) and resource configuration (if defined for the type).

           

          When you say you want to "specify a different configuration" it's unclear whether you mean for every Resource Type or every instance (Resource) of a particular type.  But basically, if you keep the Class/Object ResourceType/Resource analogy in mind, it will hopefully become more clear.  Like classes, types are a fixed definition, and like objects, resources are instantiations with various values set for the fixed definition.

          • 2. Re: Which Design pattern has been followed up for Plugins ? ? ?
            Arun Raj Expert

            Hi Jay,

             

                I am very thankful to you for your simplest explanation.

            jay shaughnessy wrote:

             

            it's unclear whether you mean for every Resource Type or every instance (Resource) of a particular type.

             

               I mean for Resource Configuration. I want default values for Resource Type( only one ). Resources of that Resource Type should be configurable( each resource should support different configurations ). I am using Configuration Facet API to do that. In load function I am passing default values from the property files. Like that, In update function I am getting values from user and saving it back. But For me Updation process is not at all working. That is Whay I have asked about the design pattern to you.

             

            Following is my code for your reference.

             

            As per my knowledge, I think I am doing mistake in mCustomerDbConfigPropertyFile; ( Please dont mistake me if i said anything bad). Just push me one step forward, by specifying the mistakes in my code which I have done. That is enough for me.

             

             

            package org.rhq.plugins.testing;
            
            import java.io.InputStream;
            import java.io.FileOutputStream;
            import java.io.IOException;
            
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;
            
            import java.util.Hashtable;
            import java.util.HashSet;
            import java.util.Properties;
            import java.util.Set;
            
            import org.slf4j.Logger;
            import org.slf4j.LoggerFactory;
            
            import org.rhq.core.domain.configuration.Configuration;
            import org.rhq.core.domain.configuration.ConfigurationUpdateStatus;
            import org.rhq.core.domain.configuration.Property;
            import org.rhq.core.domain.configuration.PropertyList;
            import org.rhq.core.domain.configuration.PropertyMap;
            import org.rhq.core.domain.configuration.PropertySimple;
            import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
            import org.rhq.core.domain.configuration.definition.PropertyDefinitionMap;
            import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
            import org.rhq.core.domain.configuration.definition.PropertySimpleType;
            
            import org.rhq.core.domain.measurement.AvailabilityType;
            import org.rhq.core.domain.measurement.MeasurementDataTrait;
            import org.rhq.core.domain.measurement.MeasurementDataNumeric;
            import org.rhq.core.domain.measurement.MeasurementReport;
            import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
            
            import org.rhq.core.pluginapi.configuration.ConfigurationFacet;
            import org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport;
            import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
            import org.rhq.core.pluginapi.inventory.ResourceComponent;
            import org.rhq.core.pluginapi.inventory.ResourceContext;
            
            import org.rhq.core.pluginapi.measurement.MeasurementFacet;
            
            import org.rhq.core.util.jdbc.JDBCUtil;
            
            public class CustomerComponent implements ResourceComponent<CustomerComponent>, ConfigurationFacet, MeasurementFacet {
                private final Logger mLogger    =    LoggerFactory.getLogger(CustomerComponent.class);
                /**
                 * This attribute mActiveUser is for Storing "No of Active User" based on resources.
                 */
                private volatile Integer mActiveUser    =    null;
                /**
                 * This attribute mSuccessfullLogin is for Storing "No of Successfull Loggedin in" based on resources.
                 */
                private volatile Integer mSuccessfullLogin    =    null;
                /**
                 * This attribute mFailedLogin is for Storing "No of Failed logged in" based on resources.
                 */
                private volatile Integer mFailedLogin    =    null;
                /**
                 * This attribute mTotalEmail is for Storing "No of Total Email" based on resources.
                 */
                private volatile Integer mTotalEmail    =    null;
                /**
                 * This attribute mSuccessEmail is for Storing "No of Success Email" based on resources.
                 */
                private volatile Integer mSuccessEmail    =    null;
                /**
                 * This attribute mFailedEmail is for Storing "No of Failed Email" based on resources.
                 */
                private volatile Integer mFailedEmail    =    null;
                /**
                 * This attribute mFailedEmail is for Storing "No of Failed Emails" based on resources.
                 */
                private volatile String mFailedEmails    =    null;
                /**
                 * This attribute mFailedEmail is for Storing "No of Failed Logins" based on resources.
                 */
                private volatile String mFailedLogins    =    null;
                private String HOST            =    null;
                private String PORT            =    null;
                private String DATABASE     =    null;
                private String PRINCIPAL    =    null;
                private String CREDENTIAL    =    null;
                private String ACTIVEUSER    =    null;
                private String FAILEDLOG    =    null;
                private String SUCCESSLOG    =    null;
                private String TOTALEMAIL    =    null;
                private String SUCCESSEMAIL    =    null;
                private String FAILEDEMAIL    =    null;
                private String CustomerFile    =    "customers.properties";
                private String CustomerConfigFile    =    "customerConfig.properties";
                private String CustomerQueryFile    =    "customerQuery.properties";
                private ResourceContext<CustomerComponent> resourceContext;
                private String mCustomerResourceKey     =    null;
                private Configuration customerConfig     =    new Configuration();
            
                private Properties mCustomerPropertyFile;
            
                private Properties mCustomerDbConfigPropertyFile;
            
                private Properties mCustomerQueryConfigPropertyFile;
            
                public AvailabilityType getAvailability() {
                    return AvailabilityType.UP;
                }
            
                private Properties loadPropertyFile(String fileName)
                {
                        Properties propertyFile    =    new Properties();
                        InputStream inputStream    =    this.getClass().getResourceAsStream(fileName);
                        try {
                            if(inputStream == null) {
                                mLogger.error("Can not find customerConfig.properties. Might be does not exist");
                            }
                            propertyFile.load(inputStream);
                            inputStream.close();
                        }
                        catch(IOException ioException) {
                            ioException.printStackTrace();
                        }
                    return propertyFile;
                }
            
                public Configuration loadResourceConfiguration() throws Exception {
                    int index;
                    String customerName =    null;
                    if(mCustomerResourceKey!=null) {
                        try {
            
                            mCustomerPropertyFile=loadPropertyFile(CustomerFile);
                            mCustomerDbConfigPropertyFile=loadPropertyFile(CustomerConfigFile);
                            mCustomerQueryConfigPropertyFile    =    loadPropertyFile(CustomerQueryFile);
                            for(index=1;index<=mCustomerPropertyFile.size();index++) {
                                customerName    =    mCustomerPropertyFile.getProperty("customer_Name_"+index);
                                if(mCustomerResourceKey.contains(customerName)) {
                                    HOST        =    mCustomerDbConfigPropertyFile.getProperty("hostname");
                                    PORT        =    mCustomerDbConfigPropertyFile.getProperty("port");
                                    DATABASE    =    mCustomerDbConfigPropertyFile.getProperty("database_name");
                                    PRINCIPAL    =    mCustomerDbConfigPropertyFile.getProperty("principal");
                                    CREDENTIAL    =    mCustomerDbConfigPropertyFile.getProperty("credentials");
                                    ACTIVEUSER    =    mCustomerQueryConfigPropertyFile.getProperty("active_user");
                                    FAILEDLOG    =    mCustomerQueryConfigPropertyFile.getProperty("failed_log");
                                    SUCCESSLOG    =    mCustomerQueryConfigPropertyFile.getProperty("success_log");
                                    TOTALEMAIL    =    mCustomerQueryConfigPropertyFile.getProperty("total_email");
                                    SUCCESSEMAIL=    mCustomerQueryConfigPropertyFile.getProperty("success_email");
                                    FAILEDEMAIL    =    mCustomerQueryConfigPropertyFile.getProperty("failed_email");
                                    customerConfig.put(new PropertySimple("host",HOST)); 
                                    customerConfig.put(new PropertySimple("port",PORT));
                                    customerConfig.put(new PropertySimple("db",DATABASE));
                                    customerConfig.put(new PropertySimple("principal",PRINCIPAL));
                                    customerConfig.put(new PropertySimple("credentials",CREDENTIAL));
                                    customerConfig.put(new PropertySimple("activeuser",ACTIVEUSER));
                                    customerConfig.put(new PropertySimple("failedlog",FAILEDLOG));
                                    customerConfig.put(new PropertySimple("successlog",SUCCESSLOG));
                                    customerConfig.put(new PropertySimple("totalemail",TOTALEMAIL));
                                    customerConfig.put(new PropertySimple("successemail",SUCCESSEMAIL));
                                    customerConfig.put(new PropertySimple("failedemail",FAILEDEMAIL));
                                    System.out.println(".........LOADING........." + mCustomerResourceKey); // printed it for my reference
                                    System.out.println(HOST);
                                    System.out.println(PORT + mCustomerResourceKey);
                                    System.out.println(DATABASE + mCustomerResourceKey);
                                    System.out.println(PRINCIPAL + mCustomerResourceKey);
                                    System.out.println(CREDENTIAL + mCustomerResourceKey);
                                    System.out.println(ACTIVEUSER + mCustomerResourceKey);
                                    System.out.println(FAILEDLOG + mCustomerResourceKey);
                                    System.out.println(SUCCESSLOG + mCustomerResourceKey);
                                    System.out.println(TOTALEMAIL + mCustomerResourceKey);
                                    System.out.println(SUCCESSEMAIL + mCustomerResourceKey);
                                    System.out.println(FAILEDEMAIL + mCustomerResourceKey);
                                }
                            }
                        } catch(Exception exception) {
                            mLogger.warn(exception.getMessage());
                        }
                    }
                    return customerConfig;
                }
            
                public void updateResourceConfiguration(ConfigurationUpdateReport report) {
                    int index;
                    String customerName =    null;
            
                    if(mCustomerResourceKey!=null) {
                        try {
                                mCustomerPropertyFile=loadPropertyFile(CustomerFile);
                                mCustomerDbConfigPropertyFile=loadPropertyFile(CustomerConfigFile);
                                mCustomerQueryConfigPropertyFile    =    loadPropertyFile(CustomerQueryFile);
                                for(index=1;index<=mCustomerPropertyFile.size();index++) {
                                customerName    =    mCustomerPropertyFile.getProperty("customer_Name_"+index);
                                if(mCustomerResourceKey.contains(customerName)) {
                                    customerConfig    =    report.getConfiguration();
                                    HOST        =    customerConfig.getSimpleValue("host",mCustomerDbConfigPropertyFile.getProperty("hostname"));
                                    PORT        =    customerConfig.getSimpleValue("port",mCustomerDbConfigPropertyFile.getProperty("port"));
                                    DATABASE    =    customerConfig.getSimpleValue("db",mCustomerDbConfigPropertyFile.getProperty("database_name"));
                                    PRINCIPAL    =    customerConfig.getSimpleValue("principal",mCustomerDbConfigPropertyFile.getProperty("principal"));
                                    CREDENTIAL    =    customerConfig.getSimpleValue("credentials",mCustomerDbConfigPropertyFile.getProperty("credentials"));
                                        System.out.println("=====BEFORE====="); // printed it for my reference
                                        System.out.println(HOST);
                                        System.out.println(PORT);
                                        System.out.println(DATABASE);
                                        System.out.println(PRINCIPAL);
                                        System.out.println(CREDENTIAL);
                                        mCustomerDbConfigPropertyFile.setProperty("hostname",HOST);
                                        mCustomerDbConfigPropertyFile.setProperty("port",PORT);
                                        mCustomerDbConfigPropertyFile.setProperty("database_name",DATABASE);
                                        mCustomerDbConfigPropertyFile.setProperty("principal",PRINCIPAL);
                                        mCustomerDbConfigPropertyFile.setProperty("credentials",CREDENTIAL);
                                        customerConfig.put(new PropertySimple("host",HOST));
                                        customerConfig.put(new PropertySimple("port",PORT));
                                        customerConfig.put(new PropertySimple("db",DATABASE));
                                        customerConfig.put(new PropertySimple("principal",PRINCIPAL));
                                        customerConfig.put(new PropertySimple("credentials",CREDENTIAL));
                                        mCustomerDbConfigPropertyFile.store(new FileOutputStream("customerConfig.properties"),null);
                                        System.out.println("=====After Insertion=====");
                                        System.out.println(mCustomerDbConfigPropertyFile.getProperty("hostname"));
                                        System.out.println(mCustomerDbConfigPropertyFile.getProperty("port"));
                                        System.out.println(mCustomerDbConfigPropertyFile.getProperty("database_name"));
                                        System.out.println(mCustomerDbConfigPropertyFile.getProperty("principal"));
                                        System.out.println(mCustomerDbConfigPropertyFile.getProperty("credentials"));
                                        System.out.println("===========BEFORE===========");
                                        System.out.println(ACTIVEUSER);
                                        ACTIVEUSER    =    customerConfig.getSimpleValue("activeuser",mCustomerQueryConfigPropertyFile.getProperty("active_user"));
                                        FAILEDLOG    =    customerConfig.getSimpleValue("failedlog",mCustomerQueryConfigPropertyFile.getProperty("failed_log"));
                                        SUCCESSLOG    =    customerConfig.getSimpleValue("successlog",mCustomerQueryConfigPropertyFile.getProperty("success_log"));
                                        TOTALEMAIL    =    customerConfig.getSimpleValue("totalemail",mCustomerQueryConfigPropertyFile.getProperty("total_email"));
                                        SUCCESSEMAIL=    customerConfig.getSimpleValue("successemail",mCustomerQueryConfigPropertyFile.getProperty("success_email"));
                                        FAILEDEMAIL    =    customerConfig.getSimpleValue("failedemail",mCustomerQueryConfigPropertyFile.getProperty("failed_email"));
                                        mCustomerQueryConfigPropertyFile.setProperty("active_user",ACTIVEUSER);
                                        mCustomerQueryConfigPropertyFile.setProperty("failed_log",FAILEDLOG);
                                        mCustomerQueryConfigPropertyFile.setProperty("success_log",SUCCESSLOG);
                                        mCustomerQueryConfigPropertyFile.setProperty("total_email",TOTALEMAIL);
                                        mCustomerQueryConfigPropertyFile.setProperty("success_email",SUCCESSEMAIL);
                                        mCustomerQueryConfigPropertyFile.setProperty("failed_email",FAILEDEMAIL);
                                        customerConfig.put(new PropertySimple("activeuser",ACTIVEUSER));
                                        customerConfig.put(new PropertySimple("failedlog",FAILEDLOG));
                                        customerConfig.put(new PropertySimple("successlog",SUCCESSLOG));
                                        customerConfig.put(new PropertySimple("totalemail",TOTALEMAIL));
                                        customerConfig.put(new PropertySimple("successemail",SUCCESSEMAIL));
                                        customerConfig.put(new PropertySimple("failedemail",FAILEDEMAIL));
                                        mCustomerQueryConfigPropertyFile.store(new FileOutputStream("customerQuery.properties"),null);
                                        System.out.println("===========AFTER INSERTION===========");
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("active_user"));
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("failed_log"));
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("success_log"));
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("total_email"));
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("success_email"));
                                        System.out.println(mCustomerQueryConfigPropertyFile.getProperty("failed_email"));
                                }
                            }
                        } catch(IOException ioException) {
                            mLogger.warn(ioException.getMessage());
                        } catch (Exception exception) {
                            mLogger.warn(exception.getMessage());
                        }
                        return;
                    }
                }
                public void start(ResourceContext<CustomerComponent> context)
                        throws InvalidPluginConfigurationException, Exception {
                    resourceContext =    context;
                    mCustomerResourceKey    =    resourceContext.getResourceKey();
                    mLogger.debug("CustomerComponent:Entered into start() Method of the Datasource : " + mCustomerResourceKey);
                }
            
            }
            
            • 3. Re: Which Design pattern has been followed up for Plugins ? ? ?
              Arun Raj Expert

              Hi Developers,

               

                  I found the errors in my code. But I am struggling to handle property files for configure resources.

               

                 Please give me any example or any plugin which is containing best way of handling property file in RHQ. By looking the code, I will try to understand the object calling and handling mechanism of RHQ.

               

              Regards,

              ArunRaj.