StorageAnnotationListener.java 3.6 KB
Newer Older
彭勇升 pengys 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.skywalking.oap.server.core.storage.annotation;

21
import java.lang.annotation.Annotation;
彭勇升 pengys 已提交
22
import java.lang.reflect.Field;
23 24 25
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
26
import lombok.Getter;
27
import org.apache.skywalking.oap.server.core.analysis.indicator.annotation.IndicatorAnnotationUtils;
28
import org.apache.skywalking.oap.server.core.annotation.AnnotationListener;
29 30 31 32 33 34 35
import org.apache.skywalking.oap.server.core.source.Scope;
import org.apache.skywalking.oap.server.core.storage.model.ColumnName;
import org.apache.skywalking.oap.server.core.storage.model.IModelGetter;
import org.apache.skywalking.oap.server.core.storage.model.Model;
import org.apache.skywalking.oap.server.core.storage.model.ModelColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
彭勇升 pengys 已提交
36 37 38 39

/**
 * @author peng-yongsheng
 */
40
public class StorageAnnotationListener implements AnnotationListener, IModelGetter {
彭勇升 pengys 已提交
41

42
    private static final Logger logger = LoggerFactory.getLogger(StorageAnnotationListener.class);
彭勇升 pengys 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56
    @Getter private final List<Model> models;

    public StorageAnnotationListener() {
        this.models = new LinkedList<>();
    }

    @Override public Class<? extends Annotation> annotation() {
        return StorageEntity.class;
    }

    @Override public void notify(Class aClass) {
        logger.info("The owner class of storage annotation, class name: {}", aClass.getName());

彭勇升 pengys 已提交
57
        String modelName = StorageEntityAnnotationUtils.getModelName(aClass);
58
        boolean deleteHistory = StorageEntityAnnotationUtils.getDeleteHistory(aClass);
59
        Scope sourceScope = StorageEntityAnnotationUtils.getSourceScope(aClass);
60
        List<ModelColumn> modelColumns = new LinkedList<>();
61
        boolean isIndicator = IndicatorAnnotationUtils.isIndicator(aClass);
彭勇升 pengys 已提交
62
        retrieval(aClass, modelName, modelColumns);
63

64
        models.add(new Model(modelName, modelColumns, isIndicator, deleteHistory, sourceScope));
彭勇升 pengys 已提交
65 66
    }

彭勇升 pengys 已提交
67
    private void retrieval(Class clazz, String modelName, List<ModelColumn> modelColumns) {
彭勇升 pengys 已提交
68 69 70 71 72
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getAnnotation(Column.class);
73
                modelColumns.add(new ModelColumn(new ColumnName(column.columnName(), column.columnName()), field.getType(), column.matchQuery()));
彭勇升 pengys 已提交
74 75 76
                if (logger.isDebugEnabled()) {
                    logger.debug("The field named {} with the {} type", column.columnName(), field.getType());
                }
彭勇升 pengys 已提交
77 78 79
                if (column.isValue()) {
                    ValueColumnIds.INSTANCE.putIfAbsent(modelName, column.columnName(), column.function());
                }
彭勇升 pengys 已提交
80 81 82 83
            }
        }

        if (Objects.nonNull(clazz.getSuperclass())) {
彭勇升 pengys 已提交
84
            retrieval(clazz.getSuperclass(), modelName, modelColumns);
彭勇升 pengys 已提交
85 86 87
        }
    }
}