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.persistence.jpa.entity.task;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import org.apache.syncope.common.lib.to.MacroTaskTO;
24  import org.apache.syncope.common.lib.to.NotificationTaskTO;
25  import org.apache.syncope.common.lib.to.PropagationTaskTO;
26  import org.apache.syncope.common.lib.to.PullTaskTO;
27  import org.apache.syncope.common.lib.to.PushTaskTO;
28  import org.apache.syncope.common.lib.to.SchedTaskTO;
29  import org.apache.syncope.common.lib.to.TaskTO;
30  import org.apache.syncope.common.lib.types.TaskType;
31  import org.apache.syncope.core.persistence.api.entity.task.MacroTask;
32  import org.apache.syncope.core.persistence.api.entity.task.NotificationTask;
33  import org.apache.syncope.core.persistence.api.entity.task.PropagationTask;
34  import org.apache.syncope.core.persistence.api.entity.task.PullTask;
35  import org.apache.syncope.core.persistence.api.entity.task.PushTask;
36  import org.apache.syncope.core.persistence.api.entity.task.SchedTask;
37  import org.apache.syncope.core.persistence.api.entity.task.Task;
38  import org.apache.syncope.core.persistence.api.entity.task.TaskUtils;
39  import org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory;
40  import org.apache.syncope.core.spring.ApplicationContextProvider;
41  
42  public class JPATaskUtilsFactory implements TaskUtilsFactory {
43  
44      protected final Map<TaskType, TaskUtils> instances = new HashMap<>(5);
45  
46      @Override
47      public TaskUtils getInstance(final TaskType type) {
48          TaskUtils instance;
49          synchronized (instances) {
50              instance = instances.get(type);
51              if (instance == null) {
52                  instance = new JPATaskUtils(type);
53                  ApplicationContextProvider.getBeanFactory().autowireBean(instance);
54                  instances.put(type, instance);
55              }
56          }
57  
58          return instance;
59      }
60  
61      @Override
62      public TaskUtils getInstance(final Task<?> task) {
63          TaskType type;
64          if (task instanceof PullTask) {
65              type = TaskType.PULL;
66          } else if (task instanceof PushTask) {
67              type = TaskType.PUSH;
68          } else if (task instanceof MacroTask) {
69              type = TaskType.MACRO;
70          } else if (task instanceof SchedTask) {
71              type = TaskType.SCHEDULED;
72          } else if (task instanceof PropagationTask) {
73              type = TaskType.PROPAGATION;
74          } else if (task instanceof NotificationTask) {
75              type = TaskType.NOTIFICATION;
76          } else {
77              throw new IllegalArgumentException("Invalid task: " + task);
78          }
79  
80          return getInstance(type);
81      }
82  
83      @Override
84      public TaskUtils getInstance(final Class<? extends TaskTO> taskClass) {
85          TaskType type;
86          if (taskClass == PropagationTaskTO.class) {
87              type = TaskType.PROPAGATION;
88          } else if (taskClass == NotificationTaskTO.class) {
89              type = TaskType.NOTIFICATION;
90          } else if (taskClass == SchedTaskTO.class) {
91              type = TaskType.SCHEDULED;
92          } else if (taskClass == PullTaskTO.class) {
93              type = TaskType.PULL;
94          } else if (taskClass == PushTaskTO.class) {
95              type = TaskType.PUSH;
96          } else if (taskClass == MacroTaskTO.class) {
97              type = TaskType.MACRO;
98          } else {
99              throw new IllegalArgumentException("Invalid TaskTO class: " + taskClass.getName());
100         }
101 
102         return getInstance(type);
103     }
104 
105     @Override
106     public TaskUtils getInstance(final TaskTO taskTO) {
107         return getInstance(taskTO.getClass());
108     }
109 }