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.policy;
20  
21  import org.apache.syncope.common.lib.policy.AccessPolicyTO;
22  import org.apache.syncope.common.lib.policy.AccountPolicyTO;
23  import org.apache.syncope.common.lib.policy.AttrReleasePolicyTO;
24  import org.apache.syncope.common.lib.policy.AuthPolicyTO;
25  import org.apache.syncope.common.lib.policy.PasswordPolicyTO;
26  import org.apache.syncope.common.lib.policy.PolicyTO;
27  import org.apache.syncope.common.lib.policy.PropagationPolicyTO;
28  import org.apache.syncope.common.lib.policy.PullPolicyTO;
29  import org.apache.syncope.common.lib.policy.PushPolicyTO;
30  import org.apache.syncope.common.lib.policy.TicketExpirationPolicyTO;
31  import org.apache.syncope.common.lib.types.PolicyType;
32  import org.apache.syncope.core.persistence.api.entity.policy.AccessPolicy;
33  import org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy;
34  import org.apache.syncope.core.persistence.api.entity.policy.AttrReleasePolicy;
35  import org.apache.syncope.core.persistence.api.entity.policy.AuthPolicy;
36  import org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy;
37  import org.apache.syncope.core.persistence.api.entity.policy.Policy;
38  import org.apache.syncope.core.persistence.api.entity.policy.PolicyUtils;
39  import org.apache.syncope.core.persistence.api.entity.policy.PolicyUtilsFactory;
40  import org.apache.syncope.core.persistence.api.entity.policy.PropagationPolicy;
41  import org.apache.syncope.core.persistence.api.entity.policy.PullPolicy;
42  import org.apache.syncope.core.persistence.api.entity.policy.PushPolicy;
43  import org.apache.syncope.core.persistence.api.entity.policy.TicketExpirationPolicy;
44  
45  public class JPAPolicyUtilsFactory implements PolicyUtilsFactory {
46  
47      @Override
48      public PolicyUtils getInstance(final PolicyType type) {
49          return new JPAPolicyUtils(type);
50      }
51  
52      @Override
53      public PolicyUtils getInstance(final Policy policy) {
54          PolicyType type;
55          if (policy instanceof AccountPolicy) {
56              type = PolicyType.ACCOUNT;
57          } else if (policy instanceof PasswordPolicy) {
58              type = PolicyType.PASSWORD;
59          } else if (policy instanceof PropagationPolicy) {
60              type = PolicyType.PROPAGATION;
61          } else if (policy instanceof PullPolicy) {
62              type = PolicyType.PULL;
63          } else if (policy instanceof PushPolicy) {
64              type = PolicyType.PUSH;
65          } else if (policy instanceof AuthPolicy) {
66              type = PolicyType.AUTH;
67          } else if (policy instanceof AccessPolicy) {
68              type = PolicyType.ACCESS;
69          } else if (policy instanceof AttrReleasePolicy) {
70              type = PolicyType.ATTR_RELEASE;
71          } else if (policy instanceof TicketExpirationPolicy) {
72              type = PolicyType.TICKET_EXPIRATION;
73          } else {
74              throw new IllegalArgumentException("Invalid policy: " + policy);
75          }
76  
77          return getInstance(type);
78      }
79  
80      @Override
81      public PolicyUtils getInstance(final Class<? extends PolicyTO> policyClass) {
82          PolicyType type;
83          if (policyClass == AccountPolicyTO.class) {
84              type = PolicyType.ACCOUNT;
85          } else if (policyClass == PasswordPolicyTO.class) {
86              type = PolicyType.PASSWORD;
87          } else if (policyClass == PropagationPolicyTO.class) {
88              type = PolicyType.PROPAGATION;
89          } else if (policyClass == PullPolicyTO.class) {
90              type = PolicyType.PULL;
91          } else if (policyClass == PushPolicyTO.class) {
92              type = PolicyType.PUSH;
93          } else if (policyClass == AuthPolicyTO.class) {
94              type = PolicyType.AUTH;
95          } else if (policyClass == AccessPolicyTO.class) {
96              type = PolicyType.ACCESS;
97          } else if (policyClass == AttrReleasePolicyTO.class) {
98              type = PolicyType.ATTR_RELEASE;
99          } else if (policyClass == TicketExpirationPolicyTO.class) {
100             type = PolicyType.TICKET_EXPIRATION;
101         } else {
102             throw new IllegalArgumentException("Invalid PolicyTO class: " + policyClass.getName());
103         }
104 
105         return getInstance(type);
106     }
107 
108     @Override
109     public PolicyUtils getInstance(final PolicyTO policyTO) {
110         return getInstance(policyTO.getClass());
111     }
112 }