/*
 * 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.component.debug;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.debugger.DebuggerJmxConnectorService;
import org.apache.camel.impl.debugger.DefaultBacklogDebugger;
import org.apache.camel.spi.BacklogDebugger;
import org.apache.camel.spi.Debugger;
import org.apache.camel.spi.DebuggerFactory;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.LifecycleStrategySupport;

@JdkService(Debugger.FACTORY)
public class CamelDebuggerFactory implements DebuggerFactory {

    @Override
    // Debugger is created and added as a service. This method always returns a null object.
    public Debugger createDebugger(CamelContext camelContext) throws Exception {
        // only create a debugger if none already exists
        if (camelContext.hasService(BacklogDebugger.class) == null) {

            // NOTE: the AutoCloseable object is added as a Service, hence it is closed by Camel context
            // according to the object lifecycle.
            BacklogDebugger backlog = DefaultBacklogDebugger.createDebugger(camelContext); // NOSONAR
            backlog.setStandby(camelContext.isDebugStandby());

            // must enable source location and history
            // so debugger tooling knows to map breakpoints to source code
            camelContext.setSourceLocationEnabled(true);
            camelContext.setMessageHistory(true);

            // enable debugger on camel
            camelContext.setDebugging(true);

            // to make debugging possible for tooling we need to make it possible to do remote JMX connection
            DebuggerJmxConnectorService connector = new DebuggerJmxConnectorService();
            connector.setCreateConnector(true);
            camelContext.addService(connector);

            // we need to enable debugger after context is started
            camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
                @Override
                public void onContextStarted(CamelContext context) {
                    // only enable debugger if not in standby mode
                    if (!backlog.isStandby()) {
                        backlog.enableDebugger();
                    }
                }

                @Override
                public void onContextStopping(CamelContext context) {
                    backlog.disableDebugger();
                }
            });
            camelContext.addService(backlog, true, true);
        }

        // return null as we fool camel-core into using this backlog debugger as we added it as a service
        return null;
    }

    @Override
    public String toString() {
        return "camel-debug";
    }
}
