aboutsummaryrefslogtreecommitdiffstats
path: root/aai-resources/src/test/java/org/onap/aai/util/DbTestConfig.java
blob: 09e4cd72115cb64ad2c4be0bfdf82e700f868609 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 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.aai.util;

import java.io.*;
import java.net.InetAddress;
import java.util.*;

public class DbTestConfig {

	public static final String AUDIT_FILESEP = (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
	public static final String AUDIT_HOME = (System.getProperty("audit.home") == null) ? AUDIT_FILESEP + "opt" + AUDIT_FILESEP + "audit" : System.getProperty("audit.home"); 
	public static final String AuditPropFilename = "c:\\tmp\\auditConfig.prop";
	public static final String AUDIT_CONFIG_CHECKINGTIME = "audit.config.checktime";
	public static final String AUDIT_NODENAME = "localhost";
	public static final String AUDIT_DEBUG = "audit.config.debug";
	
	private static Properties serverProps;
    private static boolean propsInitialized = false;
    private static boolean timerSet = false;
    private static Timer timer = null;
    
    private static String propFile = null;

    /**
     * Inits the.
     *
     * @param propertyFile the property file
     */
    public synchronized static void init(String propertyFile) {
    	propFile = propertyFile;
    	init();
    }
    
	/**
	 * Inits the.
	 */
	public synchronized static void init() {
		  System.out.println("Initializing Config");
				
		  DbTestConfig.getConfigFile();
		  DbTestConfig.reloadConfig();
		  
		  if ( propFile == null)
			  propFile = AuditPropFilename;
		  TimerTask task = null;
		  task = new DbTestFileWatcher ( new File(propFile)) {
			  protected void onChange( File file ) {
				  // here we implement the onChange
				  DbTestConfig.reloadConfig();
			  }
		  };
		  
	      if (!timerSet) {
	          timerSet = true;
	          // repeat the check every second
	          timer = new Timer();
	          String fwi = DbTestConfig.get(AUDIT_CONFIG_CHECKINGTIME);
	          timer.schedule( task , new Date(), Integer.parseInt(fwi) );
	          System.out.println("Config Watcher Interval=" + fwi);

	          System.out.println("File" + propFile+" Loaded!");
	       }       

		}

	    /**
    	 * Cleanup.
    	 */
    	public static void cleanup() {
			timer.cancel();
		}

	    /**
    	 * Gets the config file.
    	 *
    	 * @return the config file
    	 */
    	public static String getConfigFile() {
	        return propFile;
	    }

    /**
     * Reload config.
     */
    public synchronized static void reloadConfig() {

        String propFileName = propFile;

        Properties newServerProps = null;
        
        System.out.println("Reloading config from "+propFileName);
        
        try {
            InputStream is = new FileInputStream(propFileName);
            newServerProps = new Properties();
            newServerProps.load(is);
            propsInitialized = true;

            serverProps = newServerProps;
            if (get(AUDIT_DEBUG).equals("on")) {
            	serverProps.list(System.out);
            }
            newServerProps = null;
        	
        } catch (FileNotFoundException fnfe) {
        	System.out.println("AuditConfig: " + propFileName + ". FileNotFoundException: "+fnfe.getMessage());
        } catch (IOException e) {
        	System.out.println("AuditConfig: " + propFileName + ". IOException: "+e.getMessage());
        }
    }
    
    /**
     * Gets the.
     *
     * @param key the key
     * @param defaultValue the default value
     * @return the string
     */
    public static String get(String key, String defaultValue) {
    	String result = defaultValue;
    	try {
    		result = get (key);
    	}
    	catch ( Exception a ) {
    	}
    	return result;
    }

    /**
     * Gets the.
     *
     * @param key the key
     * @return the string
     */
    public static String get(String key) {
    	String response = null;
    	
    	if (key.equals(AUDIT_NODENAME)) {
    		// Get this from InetAddress rather than the properties file
    		String nodeName = getNodeName();
    		if (nodeName != null) {
    			return nodeName;
    		}
    		// else get from property file
    	}
    	
    	if (!propsInitialized || (serverProps == null)) {
    		reloadConfig();
    	}
    	if (!serverProps.containsKey(key)) {
    		System.out.println( "Property key "+key+" cannot be found");
    	} else {
    		response = serverProps.getProperty(key);
    		if (response == null || response.isEmpty()) {
    			System.out.println("Property key "+key+" is null or empty");
    		}
    	}
    	return response;
	}

    /**
     * Gets the int.
     *
     * @param key the key
     * @return the int
     */
    public static int getInt(String key) {
    	return Integer.valueOf(DbTestConfig.get(key));
	}

	/**
	 * Gets the server props.
	 *
	 * @return the server props
	 */
	public static Properties getServerProps() {
		return serverProps;
	}
	
	/**
	 * Gets the node name.
	 *
	 * @return the node name
	 */
	public static String getNodeName() {
		try {
            InetAddress ip = InetAddress.getLocalHost();
            if (ip != null) {
                   String hostname = ip.getHostName();
                   if (hostname != null) {
                	   return hostname;
                   }
            }
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	  /**
	   * Extracts a specific property key subset from the known properties.
	   * The prefix may be removed from the keys in the resulting dictionary,
	   * or it may be kept. In the latter case, exact matches on the prefix
	   * will also be copied into the resulting dictionary.
	   *
	   * @param prefix is the key prefix to filter the properties by.
	   * @param keepPrefix if true, the key prefix is kept in the resulting
	   * dictionary. As side-effect, a key that matches the prefix exactly
	   * will also be copied. If false, the resulting dictionary's keys are
	   * shortened by the prefix. An exact prefix match will not be copied,
	   * as it would result in an empty string key.
	   * @return a property dictionary matching the filter key. May be
	   * an empty dictionary, if no prefix matches were found.
	   *
	   * @see #getProperty( String ) is used to assemble matches
	   */
	  public static Properties matchingSubset(String prefix, boolean keepPrefix) {
	    Properties result = new Properties();

	    // sanity check
	    if (prefix == null || prefix.length() == 0) {
	      return result;
	    }

	    String prefixMatch; // match prefix strings with this
	    String prefixSelf; // match self with this
	    if (prefix.charAt(prefix.length() - 1) != '.') {
	      // prefix does not end in a dot
	      prefixSelf = prefix;
	      prefixMatch = prefix + '.';
	    } else {
	      // prefix does end in one dot, remove for exact matches
	      prefixSelf = prefix.substring(0, prefix.length() - 1);
	      prefixMatch = prefix;
	    }
	    // POSTCONDITION: prefixMatch and prefixSelf are initialized!

	    // now add all matches into the resulting properties.
	    // Remark 1: #propertyNames() will contain the System properties!
	    // Remark 2: We need to give priority to System properties. This is done
	    // automatically by calling this class's getProperty method.
	    String key;
	    for (Enumeration e = serverProps.keys(); e.hasMoreElements(); ) {
	      key = (String) e.nextElement();

	      if (keepPrefix) {
	        // keep full prefix in result, also copy direct matches
	        if (key.startsWith(prefixMatch) || key.equals(prefixSelf)) {
	          result.setProperty(key, serverProps.getProperty(key));
	        }
	      } else {
	        // remove full prefix in result, dont copy direct matches
	        if (key.startsWith(prefixMatch)) {
	          result.setProperty(key.substring(prefixMatch.length()), serverProps.getProperty(key));
	        }
	      }
	    }

	    // done
	    return result;
	  }



	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DbTestConfig.init(  );

	}

}