View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.job;
20  
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  import org.apache.syncope.common.lib.SyncopeConstants;
24  import org.apache.syncope.core.persistence.api.entity.Report;
25  import org.apache.syncope.core.persistence.api.entity.task.Task;
26  import org.quartz.JobKey;
27  import org.quartz.Scheduler;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public final class JobNamer {
32  
33      private static final Logger LOG = LoggerFactory.getLogger(JobNamer.class);
34  
35      private static String getKeyFromJobName(final String name, final String pattern, final int prefixLength) {
36          String result = null;
37  
38          Matcher jobMatcher = Pattern.compile(pattern).matcher(name);
39          if (jobMatcher.matches()) {
40              try {
41                  result = name.substring(prefixLength);
42              } catch (IllegalArgumentException e) {
43                  LOG.error("Unparsable id: {}", name.substring(prefixLength), e);
44              }
45          }
46  
47          return result;
48      }
49  
50      public static String getTaskKeyFromJobName(final String name) {
51          return getKeyFromJobName(name, "taskJob" + SyncopeConstants.UUID_REGEX, 7);
52      }
53  
54      public static String getReportKeyFromJobName(final String name) {
55          return getKeyFromJobName(name, "reportJob" + SyncopeConstants.UUID_REGEX, 9);
56      }
57  
58      public static JobKey getJobKey(final Task<?> task) {
59          return new JobKey("taskJob" + task.getKey(), Scheduler.DEFAULT_GROUP);
60      }
61  
62      public static JobKey getJobKey(final Report report) {
63          return new JobKey("reportJob" + report.getKey(), Scheduler.DEFAULT_GROUP);
64      }
65  
66      public static String getTriggerName(final String jobName) {
67          return "Trigger_" + jobName;
68      }
69  
70      private JobNamer() {
71          // private constructor for static utility class
72      }
73  }