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.misc.env.Env;
33 import org.onap.aaf.misc.env.impl.BasicEnv;
36 * Organization Plugin Mechanism
38 * Define a NameSpace for the company (i.e. com.att), and put in Properties as
39 * "Organization.[your NS" and assign the supporting Class.
42 * Organization.com.att=org.onap.aaf.auth.org.test.att.ATT
44 * @author Pavani, Jonathan
47 public class OrganizationFactory {
48 private static final String ORGANIZATION_DOT = "Organization.";
49 private static Organization defaultOrg = null;
50 private static Map<String,Organization> orgs = new ConcurrentHashMap<String,Organization>();
51 public static Organization init(BasicEnv env) throws OrganizationException {
52 int idx = ORGANIZATION_DOT.length();
53 Organization org,firstOrg = null;
55 for(Entry<Object, Object> es : env.getProperties().entrySet()) {
56 String key = es.getKey().toString();
57 if(key.startsWith(ORGANIZATION_DOT)) {
58 org = obtain(env,key.substring(idx));
64 if(defaultOrg == null) {
65 defaultOrg = firstOrg;
69 public static Organization obtain(Env env,final String theNS) throws OrganizationException {
71 if(theNS.indexOf('@')>=0) {
72 orgNS=FQI.reverseDomain(theNS);
76 Organization org = orgs.get(orgNS);
78 env.debug().printf("Attempting to instantiate Organization %s\n",orgNS);
80 String orgClass = env.getProperty(ORGANIZATION_DOT+orgNS);
81 if(orgClass == null) {
82 env.warn().log("There is no Organization." + orgNS + " property");
85 Class<?> orgCls = Class.forName(orgClass);
86 for(Organization o : orgs.values()) {
87 if(o.getClass().isAssignableFrom(orgCls)) {
91 } catch (ClassNotFoundException e1) {
92 env.error().log(e1, orgClass + " is not on the Classpath.");
93 throw new OrganizationException(e1);
97 @SuppressWarnings("unchecked")
98 Class<Organization> cls = (Class<Organization>) Class.forName(orgClass);
99 Constructor<Organization> cnst = cls.getConstructor(Env.class,String.class);
100 org = cnst.newInstance(env,orgNS);
101 } catch (ClassNotFoundException | NoSuchMethodException | SecurityException |
102 InstantiationException | IllegalAccessException | IllegalArgumentException |
103 InvocationTargetException e) {
104 env.error().log(e, "Error on Organization Construction");
105 throw new OrganizationException(e);
108 orgs.put(orgNS, org);
110 if((isDefault="true".equalsIgnoreCase(env.getProperty(orgNS+".default")))) {
113 env.init().printf("Instantiated %s with %s%s\n",orgNS,orgClass,(isDefault?" as default":""));
116 if(defaultOrg!=null) {
118 orgs.put(orgNS, org);
126 public static Organization get(AuthzTrans trans) throws OrganizationException {
127 String domain = FQI.reverseDomain(trans.user());
128 Organization org = orgs.get(domain);
130 org = defaultOrg; // can be null, btw, unless set.