/*
 * 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.camel.language.joor;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesBuilderLoader;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.IOHelper;
import org.joor.Reflect;

@JdkService(RoutesBuilderLoader.FACTORY_GROUP + "/" + JoorRoutesBuilderLoader.EXTENSION)
public class JoorRoutesBuilderLoader implements RoutesBuilderLoader, CamelContextAware {
    public static final String EXTENSION = "java";
    public static final Pattern PACKAGE_PATTERN = Pattern.compile(
            "^\\s*package\\s+([a-zA-Z][\\.\\w]*)\\s*;.*$", Pattern.MULTILINE);

    private CamelContext camelContext;

    @Override
    public CamelContext getCamelContext() {
        return camelContext;
    }

    @Override
    public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
    }

    @Override
    public String getSupportedExtension() {
        return EXTENSION;
    }

    @Override
    public RoutesBuilder loadRoutesBuilder(Resource resource) throws Exception {
        try (InputStream is = resource.getInputStream()) {
            final String content = IOHelper.loadText(is);
            final String name = determineName(resource, content);
            final Reflect compiled = Reflect.compile(name, content);

            return compiled.create().get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String determineName(Resource resource, String content) {
        String loc = resource.getLocation();
        // strip scheme to compute the name
        String scheme = ResourceHelper.getScheme(loc);
        if (scheme != null) {
            loc = loc.substring(scheme.length());
        }
        final String name = FileUtil.onlyName(loc, true);
        final Matcher matcher = PACKAGE_PATTERN.matcher(content);

        return matcher.find()
                ? matcher.group(1) + "." + name
                : name;
    }
}

@@ -841,36 +841,8 @@
 r;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -865,16 +865,16 @@
 Stream;%0A
+
 import j
@@ -1011,16 +1011,62 @@
 tAware;%0A
+import org.apache.camel.ExtendedCamelContext;%0A
 import o
@@ -1092,24 +1092,61 @@
 tesBuilder;%0A
+import org.apache.camel.StartupStep;%0A
 import org.a
@@ -1167,24 +1167,24 @@
 i.Resource;%0A
-
 import org.a
@@ -1216,24 +1216,73 @@
 lderLoader;%0A
+import org.apache.camel.spi.StartupStepRecorder;%0A
 import org.a
@@ -2293,16 +2293,129 @@
 ption %7B%0A
+        StartupStepRecorder recorder = camelContext.adapt(ExtendedCamelContext.class).getStartupStepRecorder();%0A%0A
         
@@ -2589,16 +2589,153 @@
 ntent);%0A
+%0A            StartupStep step = recorder.beginStep(JoorRoutesBuilderLoader.class, name, %22Compiling RouteBuilder%22);%0A            try %7B%0A    
         
@@ -2787,33 +2787,36 @@
 name, content);%0A
-%0A
+    
             retu
@@ -2855,76 +2855,77 @@
     
-%7D catch (IOException e) %7B%0A            throw new RuntimeException(e);
+    %7D finally %7B%0A                recorder.endStep(step);%0A            %7D
 %0A   

Add jfr step for compiling java route