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;
}
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");
}
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");
}
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;
}
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;
}
Aggregations