Android Open Source - SanDisk-HQME-SDK Rule






From Project

Back to project page SanDisk-HQME-SDK.

License

The source code is released under:

Apache License

If you think the Android project SanDisk-HQME-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/** 
* This reference code is an implementation of the IEEE P2200 standard.  It is not
* a contribution to the IEEE P2200 standard.
* /*  ww w . j  a v a  2  s. c o  m*/
* Copyright (c) 2011 SanDisk Corporation.  All rights reserved.
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License.  You may obtain a copy of the
* License at
* 
*        http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied.
* 
* See the License for the specific language governing permissions and limitations
* under the License.
*/

package com.hqme.cm.core;

import android.util.Xml;

import com.hqme.cm.util.CmClientUtil;
import com.hqme.cm.Property;

import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * The Rule class is used to express a condition that will evaluate to a boolean
 * depending on some conditions and values.
 * 
 * Inputs are the rule "key" (required), a 
 * "value" (required). 
 * 
 * The combination of these properties is used to evaluate the 
 * rule at any given time. Reflection is used to invoke the relevant evaluateRule method,
 * that is, when a rule is created the fields mRuleImplementation
 * and mEvaluateRuleMethod get set depending on the value of Rule.mName
 *     
 *     For instance with the input
 *      <Property key="RULE_FREE_SPACE">50</Property>,
 *     mRuleImplementation is the singleton class RULE_FREE_SPACE
 *     and mEvaluateRuleMethod is RULE_FREE_SPACE.evaluateRule(Rule)
 *     
 *     
 * Additional system properties can be taken account of in system state evaluation
 * by adding additional classes as necessary.
 * 
 * In practice, Rules are always applied as part of an overall policy, which is 
 * implemented by specifying rules as part of a RuleCollection
 *      
 */
public class Rule {

    public static final String NAMESPACE = null;

    public static final String TAG_RULE = "Property";

    public static final String TAG_KEY = "key";

    public Rule() {
    }

    // ==================================================================================================================================
    private String mName;

    private Object mRuleImplementation;

    private Method mEvaluateRuleMethod;

    public String getName() {
        return mName;
    }

    public void setName(String ruleName) {
        mName = ruleName;
    }

    // ==================================================================================================================================
    private String mValue;

    public String getValue() {
        return this.mValue;
    }

    public void setValue(String value) {
        this.mValue = value == null ? new String() : value;
    }

    // ==================================================================================================================================
    // Evaluation of mRules
    // ==================================================================================================================================
    public boolean evaluateRule(WorkOrder workOrder) {
        try {
            Object o = mEvaluateRuleMethod.invoke(mRuleImplementation, this, workOrder);
            return (Boolean) o;
        } catch (Exception fault) {
            CmClientUtil.debugLog(getClass(), "evaluateRule", fault);
        }
        return false;
    }

    // ==================================================================================================================================
    // Serialization Methods
    // ==================================================================================================================================

    public String toString() {
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        try {
            serializer.setOutput(writer);
            serializer.startDocument("UTF-8", true);
            this.bodyToString(serializer);
            serializer.endDocument();
            return writer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void bodyToString(XmlSerializer serializer) {
        try {
            serializer.startTag(NAMESPACE, TAG_RULE);
            serializer.attribute(NAMESPACE, TAG_KEY, getName());

            if (mValue != null) 
                        serializer.text(mValue);
            serializer.endTag(NAMESPACE, TAG_RULE);
        } catch (Exception fault) {
            CmClientUtil.debugLog(getClass(), "bodyToString", fault);
        }
    }

    public void setRuleImplementationObject(Object ruleObject) {
        this.mRuleImplementation = ruleObject;
    }

    public Object getRuleImplementationObject() {
        return mRuleImplementation;
    }

    public void setEvaluateRuleMethod(Method evaluateRuleMethod) {
        this.mEvaluateRuleMethod = evaluateRuleMethod;
    }

    public Method getEvaluateRuleMethod() {
        return mEvaluateRuleMethod;
    }

    public boolean isValid() {
        
        Property p = new Property(this.getName(),this.getValue());
        
        try {
            Class<?> thisRuleClass = Class.forName(Rule.class.getPackage().getName() + "." + p.key);

            Method getInstanceMethod = thisRuleClass.getMethod("getInstance", new Class[0]);

            // store for future reference
            Object o = getInstanceMethod.invoke(thisRuleClass, new Object[0]);

            // initialize the class representing this type of rule
            Method isValidMethod = thisRuleClass.getMethod("isValid", String.class);

            return (Boolean) isValidMethod.invoke(o, p.value);                
        } catch (InvocationTargetException exec) {
            CmClientUtil
                    .debugLog(getClass(), "isValid function does not exist for this rule", exec);
            
        } catch (ClassNotFoundException exec) {
            CmClientUtil.debugLog(getClass(), "testing rule key and value are valid", exec);
            
        } catch (Exception exec) {
            CmClientUtil.debugLog(getClass(), "isValid()", exec);
        }

        return false;
    }

}




Java Source Code List

com.hqme.cm.Account.java
com.hqme.cm.EventsNotify.java
com.hqme.cm.HqmeError.java
com.hqme.cm.OriginACL.java
com.hqme.cm.Permission.java
com.hqme.cm.Property.java
com.hqme.cm.QueueRequestState.java
com.hqme.cm.ReqEvents.java
com.hqme.cm.VSDEvent.java
com.hqme.cm.VSDFunctionGroups.java
com.hqme.cm.VSDProperties.java
com.hqme.cm.cache.PlaybackTokens.java
com.hqme.cm.cache.StreamingServer.java
com.hqme.cm.cache.UntenCacheService.java
com.hqme.cm.cache.UntenMedia.java
com.hqme.cm.core.CmApplication.java
com.hqme.cm.core.DeviceDescription.java
com.hqme.cm.core.HQMEProvider.java
com.hqme.cm.core.HQME.java
com.hqme.cm.core.Package.java
com.hqme.cm.core.Policy.java
com.hqme.cm.core.ProtocolException.java
com.hqme.cm.core.ProtocolHandlerInputStream.java
com.hqme.cm.core.ProtocolHandler.java
com.hqme.cm.core.ProtocolManager.java
com.hqme.cm.core.ProtocolPluginHttp.java
com.hqme.cm.core.ProtocolPlugin.java
com.hqme.cm.core.QueueRequestObject.java
com.hqme.cm.core.QueueRequestProperties.java
com.hqme.cm.core.RULE_AVAILABLE_SPACE.java
com.hqme.cm.core.RULE_BANDWIDTH_LIMIT.java
com.hqme.cm.core.RULE_CHARGING_STATE.java
com.hqme.cm.core.RULE_CONNECTION_TYPE.java
com.hqme.cm.core.RULE_DOWNLOAD_LIMIT.java
com.hqme.cm.core.RULE_EXPIRE.java
com.hqme.cm.core.RULE_FREE_SPACE.java
com.hqme.cm.core.RULE_FUNCTIONGROUPS.java
com.hqme.cm.core.RULE_MANDATORY_TIME.java
com.hqme.cm.core.RULE_MAX_SIZE.java
com.hqme.cm.core.RULE_POWER_LEVEL.java
com.hqme.cm.core.RULE_PRIORITY.java
com.hqme.cm.core.RULE_ROAMING.java
com.hqme.cm.core.RULE_SCHEDULE.java
com.hqme.cm.core.RULE_TIME.java
com.hqme.cm.core.Record.java
com.hqme.cm.core.RuleBase.java
com.hqme.cm.core.RuleCollection.java
com.hqme.cm.core.Rule.java
com.hqme.cm.core.StorageManager.java
com.hqme.cm.core.WorkOrderManager.java
com.hqme.cm.core.WorkOrder.java
com.hqme.cm.core.policyParser.Expression.java
com.hqme.cm.core.policyParser.HqmePolicyException.java
com.hqme.cm.core.policyParser.LogicalExpression.java
com.hqme.cm.core.policyParser.NegateExpression.java
com.hqme.cm.core.policyParser.Operator.java
com.hqme.cm.core.policyParser.PolicyElementParser.java
com.hqme.cm.core.policyParser.PolicyExpression.java
com.hqme.cm.core.policyParser.Precedence.java
com.hqme.cm.core.policyParser.Token.java
com.hqme.cm.core.policyParser.Tokenizer.java
com.hqme.cm.sample.HqmeClientActivity.java
com.hqme.cm.sample.HqmeClientReceiver.java
com.hqme.cm.util.CmClientUtil.java
com.hqme.cm.util.CmDate.java
com.hqme.cm.util.CmNumber.java
com.hqme.cm.util.CmProperties.java
com.hqme.cm.util.CmUri.java