DSL.java 4.4 KB
Newer Older
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.log.analyzer.dsl;

21
import com.google.common.collect.ImmutableList;
22
import groovy.lang.GroovyShell;
23
import groovy.transform.CompileStatic;
24
import groovy.util.DelegatingScript;
25 26 27
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
28 29
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
30
import org.apache.skywalking.oap.log.analyzer.dsl.spec.LALDelegatingScript;
31 32
import org.apache.skywalking.oap.log.analyzer.dsl.spec.filter.FilterSpec;
import org.apache.skywalking.oap.log.analyzer.provider.LogAnalyzerModuleConfig;
33
import org.apache.skywalking.oap.meter.analyzer.dsl.registry.ProcessRegistry;
34 35
import org.apache.skywalking.oap.server.library.module.ModuleManager;
import org.apache.skywalking.oap.server.library.module.ModuleStartException;
36 37 38 39
import org.codehaus.groovy.ast.stmt.DoWhileStatement;
import org.codehaus.groovy.ast.stmt.ForStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.ast.stmt.WhileStatement;
40
import org.codehaus.groovy.control.CompilerConfiguration;
41
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
42
import org.codehaus.groovy.control.customizers.ImportCustomizer;
43
import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
44 45 46

import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
47 48 49 50 51 52 53 54 55 56 57

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class DSL {
    private final DelegatingScript script;

    private final FilterSpec filterSpec;

    public static DSL of(final ModuleManager moduleManager,
                         final LogAnalyzerModuleConfig config,
                         final String dsl) throws ModuleStartException {
        final CompilerConfiguration cc = new CompilerConfiguration();
58 59 60 61 62 63 64 65 66
        final ASTTransformationCustomizer customizer =
            new ASTTransformationCustomizer(
                singletonMap(
                    "extensions",
                    singletonList(LALPrecompiledExtension.class.getName())
                ),
                CompileStatic.class
            );
        cc.addCompilationCustomizers(customizer);
67 68 69 70 71 72 73 74 75 76 77 78 79 80
        final SecureASTCustomizer secureASTCustomizer = new SecureASTCustomizer();
        secureASTCustomizer.setDisallowedStatements(
            ImmutableList.<Class<? extends Statement>>builder()
                         .add(WhileStatement.class)
                         .add(DoWhileStatement.class)
                         .add(ForStatement.class)
                         .build());
        // noinspection rawtypes
        secureASTCustomizer.setAllowedReceiversClasses(
            ImmutableList.<Class>builder()
                         .add(Object.class)
                         .add(Map.class)
                         .add(List.class)
                         .add(Array.class)
81 82
                         .add(String.class)
                         .add(ProcessRegistry.class)
83 84
                         .build());
        cc.addCompilationCustomizers(secureASTCustomizer);
85
        cc.setScriptBaseClass(LALDelegatingScript.class.getName());
86

87 88 89 90
        ImportCustomizer icz = new ImportCustomizer();
        icz.addImport("ProcessRegistry", ProcessRegistry.class.getName());
        cc.addCompilationCustomizers(icz);

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        final GroovyShell sh = new GroovyShell(cc);
        final DelegatingScript script = (DelegatingScript) sh.parse(dsl);
        final FilterSpec filterSpec = new FilterSpec(moduleManager, config);
        script.setDelegate(filterSpec);

        return new DSL(script, filterSpec);
    }

    public void bind(final Binding binding) {
        this.filterSpec.bind(binding);
    }

    public void evaluate() {
        script.run();
    }
}