merge if statement with enclsoing one
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / org / OrganizationFactory.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.org;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.cadi.config.Config;
32 import org.onap.aaf.cadi.util.FQI;
33 import org.onap.aaf.cadi.util.Split;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.impl.BasicEnv;
36
37 /**
38  * Organization Plugin Mechanism
39  *
40  * Define a NameSpace for the company (i.e. com.att), and put in Properties as
41  * "Organization.[your NS" and assign the supporting Class.
42  *
43  * Example:
44  * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
45  *
46  * @author Pavani, Jonathan
47  *
48  */
49 public class OrganizationFactory {
50     private static final String ORGANIZATION_DOT = "Organization.";
51     private static Organization defaultOrg = null;
52     private static Map<String,Organization> orgs = new ConcurrentHashMap<>();
53     public static Organization init(BasicEnv env) throws OrganizationException {
54         int idx = ORGANIZATION_DOT.length();
55         Organization org,firstOrg = null;
56
57         for (Entry<Object, Object> es : env.getProperties().entrySet()) {
58             String key = es.getKey().toString();
59             if (key.startsWith(ORGANIZATION_DOT)) {
60                 org = obtain(env,key.substring(idx));
61                 if (firstOrg==null) {
62                     firstOrg = org;
63                 }
64             }
65         }
66         if(firstOrg==null) { // attempt to load DefaultOrg
67             try {
68                 Class<?> cls = Class.forName("org.onap.aaf.org.DefaultOrg");
69                 @SuppressWarnings("unchecked")
70                 Constructor<Organization> cnst = (Constructor<Organization>)cls.getConstructor(Env.class,String.class);
71                 String realm = env.getProperty(Config.AAF_DEFAULT_REALM,"people.osaaf.org");
72                 defaultOrg = cnst.newInstance(env,realm);
73             } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
74                 env.init().log("Default Organization Module not linked in",e);
75             }
76         }
77         if (defaultOrg == null) {
78             defaultOrg = firstOrg;
79         }
80         return defaultOrg;
81     }
82     public static Organization obtain(Env env,final String theNS) throws OrganizationException {
83         String orgNS;
84         if (theNS.indexOf('@')>=0) {
85             orgNS=FQI.reverseDomain(theNS);
86         } else {
87             orgNS=theNS;
88         }
89         Organization org = orgs.get(orgNS);
90         if (org == null) {
91             env.debug().printf("Attempting to instantiate Organization %s",orgNS);
92
93             String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
94             if (orgClass == null) {
95                 env.warn().printf("There is no Organization.%s property",orgNS);
96             } else {
97                 try {
98                     Class<?> orgCls = Class.forName(orgClass);
99                     for (Organization o : orgs.values()) {
100                         if (o.getClass().isAssignableFrom(orgCls)) {
101                             org = o;
102                         }
103                     }
104                 } catch (ClassNotFoundException e1) {
105                     env.error().log(e1, orgClass + " is not on the Classpath.");
106                     throw new OrganizationException(e1);
107                 }
108                 if (org==null) {
109                     try {
110                         @SuppressWarnings("unchecked")
111                         Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
112                         Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
113                         org = cnst.newInstance(env,orgNS);
114                         String other_realms = env.getProperty(orgNS+".also_supports");
115                         if (other_realms!=null) {
116                             for (String r : Split.splitTrim(',', other_realms)) {
117                                 org.addSupportedRealm(r);
118                             }
119                         }
120                     } catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
121                             InstantiationException | IllegalAccessException | IllegalArgumentException |
122                             InvocationTargetException e) {
123                         env.error().log(e, "Error on Organization Construction");
124                         throw new OrganizationException(e);
125                     }
126                 }
127                 orgs.put(orgNS, org);
128                 boolean isDefault;
129                 if ((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
130                     defaultOrg = org;
131                 }
132                 env.init().printf("Instantiated %s with %s%s",orgNS,orgClass,(isDefault?" as default":""));
133             }
134             if ( (org==null) && (defaultOrg!=null)){
135                 
136                     org=defaultOrg;
137                     orgs.put(orgNS, org);
138                 }
139             }
140         
141
142         return org;
143     }
144
145     public static Organization get(AuthzTrans trans){
146         String domain = FQI.reverseDomain(trans.user());
147         Organization org = orgs.get(domain);
148         if (org==null) {
149             org = defaultOrg; // can be null, btw, unless set.
150         }
151         return org;
152     }
153 }