1 /*
2 * Copyright 2025 Bloomreach B.V. (https://www.bloomreach.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.onehippo.forge.camel.scheduling;
17
18 import javax.jcr.RepositoryException;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.onehippo.forge.camel.util.CamelContextUtils;
22 import org.onehippo.repository.scheduling.RepositoryJob;
23 import org.onehippo.repository.scheduling.RepositoryJobExecutionContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28 * Default {@link RepositoryJob} implementation invoking Camel endpoint.
29 * <P>
30 * This default {@link RepositoryJob} implementation reads attributes like the following from {@link RepositoryJobExecutionContext}:
31 * </P>
32 * <ul>
33 * <li><code>camel.endpoint.uri</code>: Camel Endpoint URI to invoke. This is required.</li>
34 * <li><code>camel.context.id</code>: Camel Context ID. If not provided, it retrieves the first one found.</li>
35 * </ul>
36 */
37 public class DefaultCamelRepositoryJob implements RepositoryJob {
38
39 private static Logger log = LoggerFactory.getLogger(DefaultCamelRepositoryJob.class);
40
41 public static final String CAMEL_ENDPOINT_URI_ATTR = "camel.endpoint.uri";
42
43 public static final String CAMEL_CONTEXT_ID_ATTR = "camel.context.id";
44
45 @Override
46 public void execute(RepositoryJobExecutionContext context) throws RepositoryException {
47 String endpointUri = context.getAttribute(CAMEL_ENDPOINT_URI_ATTR);
48
49 if (StringUtils.isBlank(endpointUri)) {
50 throw new RepositoryException("Invalid Camel Endpoint URI: '" + endpointUri + "'.");
51 }
52
53 String camelContextId = context.getAttribute(CAMEL_CONTEXT_ID_ATTR);
54
55 CamelContextUtils.invokeManagedCamelContextMBean(camelContextId,
56 "sendBody",
57 new Object[] { endpointUri, context },
58 new String[] { "java.lang.String", "java.lang.Object" });
59 }
60
61 }