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;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import org.apache.syncope.common.lib.types.AnyTypeKind;
24  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
25  import org.apache.syncope.core.persistence.api.dao.GroupDAO;
26  import org.apache.syncope.core.persistence.api.dao.UserDAO;
27  import org.apache.syncope.core.persistence.api.entity.Any;
28  import org.apache.syncope.core.persistence.api.entity.AnyUtils;
29  import org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory;
30  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
31  import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
32  import org.apache.syncope.core.persistence.api.entity.group.Group;
33  import org.apache.syncope.core.persistence.api.entity.user.User;
34  import org.apache.syncope.core.spring.ApplicationContextProvider;
35  
36  public class JPAAnyUtilsFactory implements AnyUtilsFactory {
37  
38      protected final UserDAO userDAO;
39  
40      protected final GroupDAO groupDAO;
41  
42      protected final AnyObjectDAO anyObjectDAO;
43  
44      protected final EntityFactory entityFactory;
45  
46      protected final Map<AnyTypeKind, AnyUtils> instances = new HashMap<>(3);
47  
48      protected AnyUtils linkedAccountInstance;
49  
50      public JPAAnyUtilsFactory(
51              final UserDAO userDAO,
52              final GroupDAO groupDAO,
53              final AnyObjectDAO anyObjectDAO,
54              final EntityFactory entityFactory) {
55  
56          this.userDAO = userDAO;
57          this.groupDAO = groupDAO;
58          this.anyObjectDAO = anyObjectDAO;
59          this.entityFactory = entityFactory;
60      }
61  
62      @Override
63      public AnyUtils getInstance(final AnyTypeKind anyTypeKind) {
64          AnyUtils instance;
65          synchronized (instances) {
66              instance = instances.get(anyTypeKind);
67              if (instance == null) {
68                  instance = new JPAAnyUtils(userDAO, groupDAO, anyObjectDAO, entityFactory, anyTypeKind, false);
69                  ApplicationContextProvider.getBeanFactory().autowireBean(instance);
70                  instances.put(anyTypeKind, instance);
71              }
72          }
73  
74          return instance;
75      }
76  
77      @Override
78      public AnyUtils getInstance(final Any<?> any) {
79          AnyTypeKind type = null;
80          if (any instanceof User) {
81              type = AnyTypeKind.USER;
82          } else if (any instanceof Group) {
83              type = AnyTypeKind.GROUP;
84          } else if (any instanceof AnyObject) {
85              type = AnyTypeKind.ANY_OBJECT;
86          }
87  
88          if (type == null) {
89              throw new IllegalArgumentException("Any type not supported: " + any.getClass().getName());
90          }
91  
92          return getInstance(type);
93      }
94  
95      @Override
96      public AnyUtils getLinkedAccountInstance() {
97          synchronized (this) {
98              if (linkedAccountInstance == null) {
99                  linkedAccountInstance = new JPAAnyUtils(
100                         userDAO, groupDAO, anyObjectDAO, entityFactory, AnyTypeKind.USER, true);
101             }
102         }
103         return linkedAccountInstance;
104     }
105 }