Search in sources :

Example 1 with FieldPrivilegedAction

use of com.huawei.gray.dubbo.utils.FieldPrivilegedAction in project Sermant by huaweicloud.

the class RegistrationServiceImpl method getVersion.

/**
 * 拦截点前执行
 *
 * @param obj 增强的类
 * @param arguments 增强方法的所有参数
 */
@Override
public void getVersion(Object obj, Object[] arguments) throws NoSuchFieldException, IllegalAccessException {
    if (arguments.length <= 2 || !(arguments[2] instanceof List<?>)) {
        return;
    }
    Field field = obj.getClass().getDeclaredField("microservice");
    AccessController.doPrivileged(new FieldPrivilegedAction(field));
    Microservice microservice = (Microservice) field.get(obj);
    GrayConfiguration grayConfiguration = LabelCache.getLabel(DubboCache.getLabelName());
    CurrentTag currentTag = grayConfiguration.getCurrentTag();
    currentTag.setRegisterVersion(microservice.getVersion());
    List<MicroserviceInstance> instances = (List<MicroserviceInstance>) arguments[2];
    for (MicroserviceInstance instance : instances) {
        for (String endpoint : instance.getEndpoints()) {
            AddrCache.setRegisterVersionCache(endpoint.substring(GrayConstant.DUBBO_PREFIX.length()), instance.getVersion());
        }
    }
}
Also used : Field(java.lang.reflect.Field) Microservice(org.apache.servicecomb.service.center.client.model.Microservice) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) List(java.util.List) FieldPrivilegedAction(com.huawei.gray.dubbo.utils.FieldPrivilegedAction)

Example 2 with FieldPrivilegedAction

use of com.huawei.gray.dubbo.utils.FieldPrivilegedAction in project Sermant by huaweicloud.

the class ObjectTypeStrategy method getValue.

@Override
public String getValue(Object arg, String type) {
    try {
        Field field = arg.getClass().getDeclaredField(getKey(type));
        AccessController.doPrivileged(new FieldPrivilegedAction(field));
        Object object = field.get(arg);
        return object == null ? null : String.valueOf(object);
    } catch (IllegalArgumentException e) {
        log(type);
        return null;
    } catch (IllegalAccessException e) {
        log(type);
        return null;
    } catch (NoSuchFieldException e) {
        log(type);
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) FieldPrivilegedAction(com.huawei.gray.dubbo.utils.FieldPrivilegedAction)

Aggregations

FieldPrivilegedAction (com.huawei.gray.dubbo.utils.FieldPrivilegedAction)2 Field (java.lang.reflect.Field)2 CurrentTag (com.huawei.route.common.gray.label.entity.CurrentTag)1 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)1 List (java.util.List)1 Microservice (org.apache.servicecomb.service.center.client.model.Microservice)1 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)1