summaryrefslogtreecommitdiffstats
path: root/auth/auth-core/src/main/java/org/onap/aaf/auth/common/Define.java
blob: 1e7a0530a91253a4a44f7974fc1bcddb32d0bb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * *
 ******************************************************************************/
package org.onap.aaf.auth.common;

import java.util.Map.Entry;

import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.Access.Level;
import org.onap.aaf.cadi.config.Config;

public class Define {
	private static String ROOT_NS = null;
	private static String ROOT_COMPANY = null;

	private final static String MSG = ".set(Access access) must be called before use";
	public static final CharSequence ROOT_NS_TAG = "AAF_NS"; // use for certain Replacements in Location
	private static final String ROOT_NS_TAG_DOT = ROOT_NS_TAG +".";

	public static String ROOT_NS() {
		if(ROOT_NS==null) {
			throw new RuntimeException(Define.class.getName() + MSG);
		}
		return ROOT_NS;
	}
	
	public static String ROOT_COMPANY() {
		if(ROOT_NS==null) {
			throw new RuntimeException(Define.class.getName() + MSG);
		}
		return ROOT_COMPANY;
	}
	
	public static void set(Access access) throws CadiException {
		ROOT_NS = access.getProperty(Config.AAF_ROOT_NS,"org.osaaf.aaf");
		ROOT_COMPANY = access.getProperty(Config.AAF_ROOT_COMPANY,null);
		if(ROOT_COMPANY==null) {
			int last = ROOT_NS.lastIndexOf('.');
			if(last>=0) {
				ROOT_COMPANY = ROOT_NS.substring(0, last);
			} else {
				throw new CadiException(Config.AAF_ROOT_COMPANY + " or " + Config.AAF_ROOT_NS + " property with 3 positions is required.");
			}
		}
		
		for( Entry<Object, Object> es : access.getProperties().entrySet()) {
			if(es.getKey().toString().startsWith(ROOT_NS_TAG_DOT)) {
				access.getProperties().setProperty(es.getKey().toString(),varReplace(es.getValue().toString()));
			}
		}
		
		access.printf(Level.INIT,"AAF Root NS is %s, and AAF Company Root is %s",ROOT_NS,ROOT_COMPANY);
	}

	public static String varReplace(final String potential) {
		if(potential.startsWith(ROOT_NS_TAG_DOT)) {
			return ROOT_NS + potential.substring(6);
		} else {
			return potential;
		}
	}
	
}