Mass removal of all Tabs (Style Warnings)
[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.util.FQI;
32 import org.onap.aaf.cadi.util.Split;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.impl.BasicEnv;
35
36 /**
37  * Organization Plugin Mechanism
38  * 
39  * Define a NameSpace for the company (i.e. com.att), and put in Properties as 
40  * "Organization.[your NS" and assign the supporting Class.  
41  * 
42  * Example:
43  * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
44  *
45  * @author Pavani, Jonathan
46  *
47  */
48 public class OrganizationFactory {
49     private static final String ORGANIZATION_DOT = "Organization.";
50     private static Organization defaultOrg = null;
51     private static Map<String,Organization> orgs = new ConcurrentHashMap<>();
52     public static Organization init(BasicEnv env) throws OrganizationException {
53         int idx = ORGANIZATION_DOT.length();
54         Organization org,firstOrg = null;
55         
56         for(Entry<Object, Object> es : env.getProperties().entrySet()) {
57             String key = es.getKey().toString();
58             if(key.startsWith(ORGANIZATION_DOT)) {
59                 org = obtain(env,key.substring(idx));
60                 if(firstOrg==null) {
61                     firstOrg = org;
62                 }
63             }
64         }
65         if(defaultOrg == null) {
66             defaultOrg = firstOrg;
67         }
68         return defaultOrg;
69     }
70     public static Organization obtain(Env env,final String theNS) throws OrganizationException {
71         String orgNS;
72         if(theNS.indexOf('@')>=0) {
73             orgNS=FQI.reverseDomain(theNS);
74         } else {
75             orgNS=theNS;
76         }
77         Organization org = orgs.get(orgNS);
78         if(org == null) {
79             env.debug().printf("Attempting to instantiate Organization %s\n",orgNS);
80
81             String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
82             if(orgClass == null) {
83                 env.warn().log("There is no Organization." + orgNS + " property");
84             } else {
85                 try {
86                     Class<?> orgCls = Class.forName(orgClass);
87                     for(Organization o : orgs.values()) {
88                         if(o.getClass().isAssignableFrom(orgCls)) {
89                             org = o;
90                         }
91                     }
92                 } catch (ClassNotFoundException e1) {
93                     env.error().log(e1, orgClass + " is not on the Classpath.");
94                     throw new OrganizationException(e1);
95                 }
96                 if(org==null) {
97                     try {
98                         @SuppressWarnings("unchecked")
99                         Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
100                         Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
101                         org = cnst.newInstance(env,orgNS);
102                         String other_realms = env.getProperty(orgNS+".also_supports");
103                         if(other_realms!=null) {
104                             for(String r : Split.splitTrim(',', other_realms)) {
105                                 org.addSupportedRealm(r);
106                             }
107                         }
108                     } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | 
109                             InstantiationException | IllegalAccessException | IllegalArgumentException | 
110                             InvocationTargetException e) {
111                         env.error().log(e, "Error on Organization Construction");
112                         throw new OrganizationException(e);
113                     }
114                 }
115                 orgs.put(orgNS, org);
116                 boolean isDefault;
117                 if((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
118                     defaultOrg = org;
119                 }
120                 env.init().printf("Instantiated %s with %s%s\n",orgNS,orgClass,(isDefault?" as default":""));
121             }
122             if(org==null) {
123                 if(defaultOrg!=null) {
124                     org=defaultOrg;
125                     orgs.put(orgNS, org);
126                 }
127             }
128         }
129         
130         return org;
131     }
132
133     public static Organization get(AuthzTrans trans) throws OrganizationException {
134         String domain = FQI.reverseDomain(trans.user());
135         Organization org = orgs.get(domain);
136         if(org==null) {
137             org = defaultOrg; // can be null, btw, unless set.
138         }
139         return org;
140     }
141 }