2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.org;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
27 import java.util.Map.Entry;
28 import java.util.concurrent.ConcurrentHashMap;
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;
38 * Organization Plugin Mechanism
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.
44 * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
46 * @author Pavani, Jonathan
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;
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));
66 if(firstOrg==null) { // attempt to load DefaultOrg
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);
77 if (defaultOrg == null) {
78 defaultOrg = firstOrg;
82 public static Organization obtain(Env env,final String theNS) throws OrganizationException {
84 if (theNS.indexOf('@')>=0) {
85 orgNS=FQI.reverseDomain(theNS);
89 Organization org = orgs.get(orgNS);
91 env.debug().printf("Attempting to instantiate Organization %s",orgNS);
93 String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
94 if (orgClass == null) {
95 env.warn().printf("There is no Organization.%s property",orgNS);
98 Class<?> orgCls = Class.forName(orgClass);
99 for (Organization o : orgs.values()) {
100 if (o.getClass().isAssignableFrom(orgCls)) {
104 } catch (ClassNotFoundException e1) {
105 env.error().log(e1, orgClass + " is not on the Classpath.");
106 throw new OrganizationException(e1);
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);
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);
127 orgs.put(orgNS, org);
129 if ((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
132 env.init().printf("Instantiated %s with %s%s",orgNS,orgClass,(isDefault?" as default":""));
134 if ( (org==null) && (defaultOrg!=null)){
137 orgs.put(orgNS, org);
145 public static Organization get(AuthzTrans trans){
146 String domain = FQI.reverseDomain(trans.user());
147 Organization org = orgs.get(domain);
149 org = defaultOrg; // can be null, btw, unless set.