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