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.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;
37 * Organization Plugin Mechanism
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.
43 * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
45 * @author Pavani, Jonathan
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<String,Organization>();
52 public static Organization init(BasicEnv env) throws OrganizationException {
53 int idx = ORGANIZATION_DOT.length();
54 Organization org,firstOrg = null;
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));
65 if(defaultOrg == null) {
66 defaultOrg = firstOrg;
70 public static Organization obtain(Env env,final String theNS) throws OrganizationException {
72 if(theNS.indexOf('@')>=0) {
73 orgNS=FQI.reverseDomain(theNS);
77 Organization org = orgs.get(orgNS);
79 env.debug().printf("Attempting to instantiate Organization %s\n",orgNS);
81 String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
82 if(orgClass == null) {
83 env.warn().log("There is no Organization." + orgNS + " property");
86 Class<?> orgCls = Class.forName(orgClass);
87 for(Organization o : orgs.values()) {
88 if(o.getClass().isAssignableFrom(orgCls)) {
92 } catch (ClassNotFoundException e1) {
93 env.error().log(e1, orgClass + " is not on the Classpath.");
94 throw new OrganizationException(e1);
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);
109 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
110 InstantiationException | IllegalAccessException | IllegalArgumentException |
111 InvocationTargetException e) {
112 env.error().log(e, "Error on Organization Construction");
113 throw new OrganizationException(e);
116 orgs.put(orgNS, org);
118 if((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
121 env.init().printf("Instantiated %s with %s%s\n",orgNS,orgClass,(isDefault?" as default":""));
124 if(defaultOrg!=null) {
126 orgs.put(orgNS, org);
134 public static Organization get(AuthzTrans trans) throws OrganizationException {
135 String domain = FQI.reverseDomain(trans.user());
136 Organization org = orgs.get(domain);
138 org = defaultOrg; // can be null, btw, unless set.