Search in sources :

Example 1 with OALCompileException

use of org.apache.skywalking.oap.server.core.oal.rt.OALCompileException in project skywalking by apache.

the class OALRuntime method generateDispatcherClass.

/**
 * Generate SourceDispatcher class and inject it to classloader
 */
private Class generateDispatcherClass(String scopeName, DispatcherContext dispatcherContext) throws OALCompileException {
    String className = dispatcherClassName(scopeName, false);
    CtClass dispatcherClass = classPool.makeClass(dispatcherClassName(scopeName, true));
    try {
        CtClass dispatcherInterface = classPool.get(DISPATCHER_INTERFACE);
        dispatcherClass.addInterface(dispatcherInterface);
        /**
         * Set generic signature
         */
        String sourceClassName = oalDefine.getSourcePackage() + dispatcherContext.getSource();
        SignatureAttribute.ClassSignature dispatcherSignature = new SignatureAttribute.ClassSignature(null, null, // Set interface and its generic params
        new SignatureAttribute.ClassType[] { new SignatureAttribute.ClassType(SourceDispatcher.class.getCanonicalName(), new SignatureAttribute.TypeArgument[] { new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(sourceClassName)) }) });
        dispatcherClass.setGenericSignature(dispatcherSignature.encode());
    } catch (NotFoundException e) {
        log.error("Can't find Dispatcher interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (AnalysisResult dispatcherContextMetric : dispatcherContext.getMetrics()) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate("dispatcher/doMetrics.ftl").process(dispatcherContextMetric, methodEntity);
            dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
        } catch (Exception e) {
            log.error("Can't generate method do" + dispatcherContextMetric.getMetricsName() + " for " + className + ".", e);
            log.error("Method body as following" + System.lineSeparator() + "{}", methodEntity);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        StringWriter methodEntity = new StringWriter();
        configuration.getTemplate("dispatcher/dispatch.ftl").process(dispatcherContext, methodEntity);
        dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
    } catch (Exception e) {
        log.error("Can't generate method dispatch for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    Class targetClass;
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            targetClass = dispatcherClass.toClass(currentClassLoader, null);
        } else {
            targetClass = dispatcherClass.toClass(DispatcherClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(dispatcherClass, className, "dispatcher");
    return targetClass;
}
Also used : OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) DispatcherClassPackageHolder(org.apache.skywalking.oap.server.core.source.oal.rt.dispatcher.DispatcherClassPackageHolder) CannotCompileException(javassist.CannotCompileException) AnalysisResult(org.apache.skywalking.oal.rt.parser.AnalysisResult) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) StringWriter(java.io.StringWriter) CtClass(javassist.CtClass)

Example 2 with OALCompileException

use of org.apache.skywalking.oap.server.core.oal.rt.OALCompileException in project skywalking by apache.

the class OALRuntime method generateMetricsBuilderClass.

/**
 * Generate metrics class builder and inject it to classloader
 */
private void generateMetricsBuilderClass(AnalysisResult metricsStmt) throws OALCompileException {
    String className = metricsBuilderClassName(metricsStmt, false);
    CtClass metricsBuilderClass = classPool.makeClass(metricsBuilderClassName(metricsStmt, true));
    try {
        metricsBuilderClass.addInterface(classPool.get(storageBuilderFactory.builderTemplate().getSuperClass()));
    } catch (NotFoundException e) {
        log.error("Can't find StorageBuilder interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsBuilderClass);
        metricsBuilderClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (String method : METRICS_BUILDER_CLASS_METHODS) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate(storageBuilderFactory.builderTemplate().getTemplatePath() + "/" + method + ".ftl").process(metricsStmt, methodEntity);
            metricsBuilderClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsBuilderClass));
        } catch (Exception e) {
            log.error("Can't generate method " + method + " for " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            metricsBuilderClass.toClass(currentClassLoader, null);
        } else {
            metricsBuilderClass.toClass(MetricBuilderClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(metricsBuilderClass, className, "metrics/builder");
}
Also used : CtClass(javassist.CtClass) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) StringWriter(java.io.StringWriter) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 3 with OALCompileException

use of org.apache.skywalking.oap.server.core.oal.rt.OALCompileException in project incubator-skywalking by apache.

the class OALRuntime method generateMetricsBuilderClass.

/**
 * Generate metrics class builder and inject it to classloader
 */
private void generateMetricsBuilderClass(AnalysisResult metricsStmt) throws OALCompileException {
    String className = metricsBuilderClassName(metricsStmt, false);
    CtClass metricsBuilderClass = classPool.makeClass(metricsBuilderClassName(metricsStmt, true));
    try {
        metricsBuilderClass.addInterface(classPool.get(storageBuilderFactory.builderTemplate().getSuperClass()));
    } catch (NotFoundException e) {
        log.error("Can't find StorageBuilder interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsBuilderClass);
        metricsBuilderClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (String method : METRICS_BUILDER_CLASS_METHODS) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate(storageBuilderFactory.builderTemplate().getTemplatePath() + "/" + method + ".ftl").process(metricsStmt, methodEntity);
            metricsBuilderClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsBuilderClass));
        } catch (Exception e) {
            log.error("Can't generate method " + method + " for " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            metricsBuilderClass.toClass(currentClassLoader, null);
        } else {
            metricsBuilderClass.toClass(MetricBuilderClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(metricsBuilderClass, className, "metrics/builder");
}
Also used : CtClass(javassist.CtClass) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) StringWriter(java.io.StringWriter) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 4 with OALCompileException

use of org.apache.skywalking.oap.server.core.oal.rt.OALCompileException in project incubator-skywalking by apache.

the class OALRuntime method generateDispatcherClass.

/**
 * Generate SourceDispatcher class and inject it to classloader
 */
private Class generateDispatcherClass(String scopeName, DispatcherContext dispatcherContext) throws OALCompileException {
    String className = dispatcherClassName(scopeName, false);
    CtClass dispatcherClass = classPool.makeClass(dispatcherClassName(scopeName, true));
    try {
        CtClass dispatcherInterface = classPool.get(DISPATCHER_INTERFACE);
        dispatcherClass.addInterface(dispatcherInterface);
        /**
         * Set generic signature
         */
        String sourceClassName = oalDefine.getSourcePackage() + dispatcherContext.getSource();
        SignatureAttribute.ClassSignature dispatcherSignature = new SignatureAttribute.ClassSignature(null, null, // Set interface and its generic params
        new SignatureAttribute.ClassType[] { new SignatureAttribute.ClassType(SourceDispatcher.class.getCanonicalName(), new SignatureAttribute.TypeArgument[] { new SignatureAttribute.TypeArgument(new SignatureAttribute.ClassType(sourceClassName)) }) });
        dispatcherClass.setGenericSignature(dispatcherSignature.encode());
    } catch (NotFoundException e) {
        log.error("Can't find Dispatcher interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (AnalysisResult dispatcherContextMetric : dispatcherContext.getMetrics()) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate("dispatcher/doMetrics.ftl").process(dispatcherContextMetric, methodEntity);
            dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
        } catch (Exception e) {
            log.error("Can't generate method do" + dispatcherContextMetric.getMetricsName() + " for " + className + ".", e);
            log.error("Method body as following" + System.lineSeparator() + "{}", methodEntity);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        StringWriter methodEntity = new StringWriter();
        configuration.getTemplate("dispatcher/dispatch.ftl").process(dispatcherContext, methodEntity);
        dispatcherClass.addMethod(CtNewMethod.make(methodEntity.toString(), dispatcherClass));
    } catch (Exception e) {
        log.error("Can't generate method dispatch for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    Class targetClass;
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            targetClass = dispatcherClass.toClass(currentClassLoader, null);
        } else {
            targetClass = dispatcherClass.toClass(DispatcherClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(dispatcherClass, className, "dispatcher");
    return targetClass;
}
Also used : OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) DispatcherClassPackageHolder(org.apache.skywalking.oap.server.core.source.oal.rt.dispatcher.DispatcherClassPackageHolder) CannotCompileException(javassist.CannotCompileException) AnalysisResult(org.apache.skywalking.oal.rt.parser.AnalysisResult) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) StringWriter(java.io.StringWriter) CtClass(javassist.CtClass)

Example 5 with OALCompileException

use of org.apache.skywalking.oap.server.core.oal.rt.OALCompileException in project incubator-skywalking by apache.

the class OALRuntime method generateMetricsClass.

/**
 * Generate metrics class, and inject it to classloader
 */
private Class generateMetricsClass(AnalysisResult metricsStmt) throws OALCompileException {
    String className = metricsClassName(metricsStmt, false);
    CtClass parentMetricsClass = null;
    try {
        parentMetricsClass = classPool.get(METRICS_FUNCTION_PACKAGE + metricsStmt.getMetricsClassName());
    } catch (NotFoundException e) {
        log.error("Can't find parent class for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    CtClass metricsClass = classPool.makeClass(metricsClassName(metricsStmt, true), parentMetricsClass);
    try {
        metricsClass.addInterface(classPool.get(WITH_METADATA_INTERFACE));
    } catch (NotFoundException e) {
        log.error("Can't find WithMetadata interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    ClassFile metricsClassClassFile = metricsClass.getClassFile();
    ConstPool constPool = metricsClassClassFile.getConstPool();
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsClass);
        metricsClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Add fields with annotations.
     *
     * private ${sourceField.typeName} ${sourceField.fieldName};
     */
    for (SourceColumn field : metricsStmt.getFieldsFromSource()) {
        try {
            CtField newField = CtField.make("private " + field.getType().getName() + " " + field.getFieldName() + ";", metricsClass);
            metricsClass.addField(newField);
            metricsClass.addMethod(CtNewMethod.getter(field.getFieldGetter(), newField));
            metricsClass.addMethod(CtNewMethod.setter(field.getFieldSetter(), newField));
            AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
            /**
             * Add @Column(columnName = "${sourceField.columnName}")
             */
            Annotation columnAnnotation = new Annotation(Column.class.getName(), constPool);
            columnAnnotation.addMemberValue("columnName", new StringMemberValue(field.getColumnName(), constPool));
            if (field.getType().equals(String.class)) {
                columnAnnotation.addMemberValue("length", new IntegerMemberValue(constPool, field.getLength()));
            }
            annotationsAttribute.addAnnotation(columnAnnotation);
            newField.getFieldInfo().addAttribute(annotationsAttribute);
        } catch (CannotCompileException e) {
            log.error("Can't add field(including set/get) " + field.getFieldName() + " in " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    /**
     * Generate methods
     */
    for (String method : METRICS_CLASS_METHODS) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate("metrics/" + method + ".ftl").process(metricsStmt, methodEntity);
            metricsClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsClass));
        } catch (Exception e) {
            log.error("Can't generate method " + method + " for " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    /**
     * Add following annotation to the metrics class
     *
     * at Stream(name = "${tableName}", scopeId = ${sourceScopeId}, builder = ${metricsName}Metrics.Builder.class, processor = MetricsStreamProcessor.class)
     */
    AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
    Annotation streamAnnotation = new Annotation(Stream.class.getName(), constPool);
    streamAnnotation.addMemberValue("name", new StringMemberValue(metricsStmt.getTableName(), constPool));
    streamAnnotation.addMemberValue("scopeId", new IntegerMemberValue(constPool, metricsStmt.getFrom().getSourceScopeId()));
    streamAnnotation.addMemberValue("builder", new ClassMemberValue(metricsBuilderClassName(metricsStmt, true), constPool));
    streamAnnotation.addMemberValue("processor", new ClassMemberValue(METRICS_STREAM_PROCESSOR, constPool));
    annotationsAttribute.addAnnotation(streamAnnotation);
    metricsClassClassFile.addAttribute(annotationsAttribute);
    Class targetClass;
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            targetClass = metricsClass.toClass(currentClassLoader, null);
        } else {
            targetClass = metricsClass.toClass(MetricClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    log.debug("Generate metrics class, " + metricsClass.getName());
    writeGeneratedFile(metricsClass, metricsClass.getSimpleName(), "metrics");
    return targetClass;
}
Also used : ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) SourceColumn(org.apache.skywalking.oal.rt.parser.SourceColumn) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) MetricClassPackageHolder(org.apache.skywalking.oap.server.core.source.oal.rt.metrics.MetricClassPackageHolder) CannotCompileException(javassist.CannotCompileException) Annotation(javassist.bytecode.annotation.Annotation) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) StringWriter(java.io.StringWriter) Column(org.apache.skywalking.oap.server.core.storage.annotation.Column) SourceColumn(org.apache.skywalking.oal.rt.parser.SourceColumn) CtField(javassist.CtField) DataOutputStream(java.io.DataOutputStream) Stream(org.apache.skywalking.oap.server.core.analysis.Stream) FileOutputStream(java.io.FileOutputStream) CtClass(javassist.CtClass)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 CannotCompileException (javassist.CannotCompileException)6 CtClass (javassist.CtClass)6 NotFoundException (javassist.NotFoundException)6 OALCompileException (org.apache.skywalking.oap.server.core.oal.rt.OALCompileException)6 StorageException (org.apache.skywalking.oap.server.core.storage.StorageException)6 ModuleStartException (org.apache.skywalking.oap.server.library.module.ModuleStartException)6 CtConstructor (javassist.CtConstructor)4 DataOutputStream (java.io.DataOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 CtField (javassist.CtField)2 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)2 ClassFile (javassist.bytecode.ClassFile)2 ConstPool (javassist.bytecode.ConstPool)2 SignatureAttribute (javassist.bytecode.SignatureAttribute)2 Annotation (javassist.bytecode.annotation.Annotation)2 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)2 IntegerMemberValue (javassist.bytecode.annotation.IntegerMemberValue)2