|
| 1 | +/* |
| 2 | + * Copyright 2015 LinkedIn Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.linkedin.gradle.lihadoop |
| 17 | + |
| 18 | +/** |
| 19 | + * Helper class for loading LinkedIn-specific properties from an external resource file. |
| 20 | + * <p> |
| 21 | + * For open-source builds, the file will be missing. For internal builds, we will copy in an |
| 22 | + * external resource file with the correct values. |
| 23 | + */ |
| 24 | +class LiHadoopProperties { |
| 25 | + |
| 26 | + // The keys that should exist in this file |
| 27 | + public static final String ARTIFACTORY_REPO = "artifactoryRepo" |
| 28 | + public static final String AZKABAN_URL = "azkabanUrl" |
| 29 | + public static final String MESSAGE_URL = "messageUrl" |
| 30 | + public static final String HLD_GATEWAY_HOME = "hldGatewayHome" |
| 31 | + public static final String HLD_GATEWAY_NODE = "hldGatewayNode" |
| 32 | + public static final String HLD_NAME_NODE_HDFS = "hldNameNodeHdfs" |
| 33 | + public static final String HLD_NAME_NODE_WEBHDFS = "hldNameNodeWebHdfs" |
| 34 | + public static final String HLD_GRID_NAME = "hldGridName" |
| 35 | + public static final String PKE_GRID_NAME = "pkeGridName" |
| 36 | + public static final String SPD_GRID_NAME = "spdGridName" |
| 37 | + public static final String TRK_GRID_NAME = "trkGridName" |
| 38 | + public static final String WAR_GRID_NAME = "warGridName" |
| 39 | + public static final String DR_ELEPHANT_HLD_URL = "drElephantHldUrl" |
| 40 | + public static final String DR_ELEPHANT_PKE_URL = "drElephantPkeUrl" |
| 41 | + public static final String DR_ELEPHANT_SPD_URL = "drElephantSpdUrl" |
| 42 | + public static final String DR_ELEPHANT_TRK_URL = "drElephantTrkUrl" |
| 43 | + public static final String DR_ELEPHANT_WAR_URL = "drElephantWarUrl" |
| 44 | + public static final String OOZIE_URI = "oozieUri" |
| 45 | + public static final String REMOTE_PIG_COMMAND = "remotePigCommand" |
| 46 | + public static final String REMOTE_SPARK_COMMAND = "remoteSparkCommand" |
| 47 | + |
| 48 | + // For open-source builds the resource file will be missing, which should not cause an error |
| 49 | + private static boolean missing = true |
| 50 | + |
| 51 | + // Java properties object that will hold the key-value pairs from the embedded resource file |
| 52 | + private static Properties props = null |
| 53 | + |
| 54 | + /** |
| 55 | + * Helper function to load the embedded resource property file. This method is synchronized, so |
| 56 | + * that the property file will be loaded only once even in the context of multiple threads. |
| 57 | + * |
| 58 | + * @throws Exception If the embedded resource property file cannot be read |
| 59 | + */ |
| 60 | + static synchronized void checkLoadResourceProperties() throws Exception { |
| 61 | + if (props == null) { |
| 62 | + props = new Properties() |
| 63 | + |
| 64 | + // Reference to another class besides this one, since we are in the middle of initializing it |
| 65 | + InputStream inputStream = LiHadoopPlugin.class.getResourceAsStream("/linkedin.properties") |
| 66 | + if (inputStream != null) { |
| 67 | + props.load(inputStream) |
| 68 | + missing = false |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return the property value for the given property key. |
| 75 | + * <p> |
| 76 | + * Since the underlying Properties class returns null if the key is not found, we will explicitly |
| 77 | + * check if the properties object contains the key and throw an exception if it is not found |
| 78 | + * (unless this is an open-source build, in which case we return the empty string). |
| 79 | + * |
| 80 | + * @param key The property key |
| 81 | + * @return The property value for the given key |
| 82 | + * @throws IllegalArgumentException If the given property key does not exist |
| 83 | + */ |
| 84 | + static String get(String key) throws IllegalArgumentException { |
| 85 | + // Check if we need to load the embedded property file |
| 86 | + checkLoadResourceProperties() |
| 87 | + |
| 88 | + // If this is an open-source build, the resource file will be missing. This should not cause an |
| 89 | + // error, so just return the empty string. |
| 90 | + if (missing) { |
| 91 | + return "" |
| 92 | + } |
| 93 | + |
| 94 | + if (!props.containsKey(key)) { |
| 95 | + throw new IllegalArgumentException("The key " + key + " does not exist in the linkedin.properties file") |
| 96 | + } else { |
| 97 | + return props.getProperty(key) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments