Browse Source

AMBARI-4538 - Expose Ambari config properties through View context.

tbeerbower 11 years ago
parent
commit
ac8afa72e8

+ 5 - 0
ambari-server/src/main/java/org/apache/ambari/server/view/ViewContextImpl.java

@@ -77,6 +77,11 @@ public class ViewContextImpl implements ViewContext {
     return viewInstanceDefinition.getProperties();
   }
 
+  @Override
+  public String getAmbariProperty(String key) {
+    return viewInstanceDefinition.getViewDefinition().getAmbariProperty(key);
+  }
+
   @Override
   public ResourceProvider<?> getResourceProvider(String type) {
     return viewInstanceDefinition.getResourceProvider(type);

+ 21 - 3
ambari-server/src/main/java/org/apache/ambari/server/view/ViewDefinition.java

@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.view;
 
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.view.configuration.ResourceConfig;
 import org.apache.ambari.server.view.configuration.ViewConfig;
 import org.apache.ambari.server.controller.spi.Resource;
@@ -57,6 +58,10 @@ public class ViewDefinition {
    */
   private final Map<String, ViewInstanceDefinition> instanceDefinitions = new HashMap<String, ViewInstanceDefinition>();
 
+  /**
+   * The Ambari configuration properties.
+   */
+  private final Configuration ambariConfiguration;
 
   /**
    * The external resource type for the view.
@@ -69,10 +74,12 @@ public class ViewDefinition {
   /**
    * Construct a view definition from the given configuration.
    *
-   * @param configuration the view configuration
+   * @param configuration        the view configuration
+   * @param ambariConfiguration  the Ambari configuration
    */
-  public ViewDefinition(ViewConfig configuration) {
-    this.configuration = configuration;
+  public ViewDefinition(ViewConfig configuration, Configuration ambariConfiguration) {
+    this.configuration       = configuration;
+    this.ambariConfiguration = ambariConfiguration;
 
     this.externalResourceType =
         new Resource.Type(getQualifiedResourceTypeName(ResourceConfig.EXTERNAL_RESOURCE_PLURAL_NAME));
@@ -117,6 +124,17 @@ public class ViewDefinition {
     return configuration;
   }
 
+  /**
+   * Get a property for the given key from the ambari configuration.
+   *
+   * @param key  the property key
+   *
+   * @return the property value; null indicates that the configuration contains no mapping for the key
+   */
+  public String getAmbariProperty(String key) {
+    return ambariConfiguration.getProperty(key);
+  }
+
   /**
    * Add a resource provider for the given type.
    *

+ 5 - 5
ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java

@@ -229,7 +229,7 @@ public class ViewRegistry {
             JAXBContext    jaxbContext      = JAXBContext.newInstance(ViewConfig.class);
             Unmarshaller   jaxbUnmarshaller = jaxbContext.createUnmarshaller();
             ViewConfig     viewConfig       = (ViewConfig) jaxbUnmarshaller.unmarshal(configStream);
-            ViewDefinition viewDefinition   = installView(viewConfig, cl);
+            ViewDefinition viewDefinition   = installView(viewConfig, cl, configuration);
 
             List<InstanceConfig> instances = viewConfig.getInstances();
 
@@ -257,12 +257,12 @@ public class ViewRegistry {
   }
 
   // install a new view definition
-  private static ViewDefinition installView(ViewConfig viewConfig, ClassLoader cl)
+  private static ViewDefinition installView(ViewConfig viewConfig, ClassLoader cl, Configuration ambariConfig)
       throws ClassNotFoundException, IntrospectionException {
 
     List<ResourceConfig> resourceConfigurations = viewConfig.getResources();
 
-    ViewDefinition viewDefinition = new ViewDefinition(viewConfig);
+    ViewDefinition viewDefinition = new ViewDefinition(viewConfig, ambariConfig);
 
     Resource.Type externalResourceType = viewDefinition.getExternalResourceType();
 
@@ -303,7 +303,7 @@ public class ViewRegistry {
                                    ClassLoader cl,
                                    ServletContextHandler root,
                                    DelegatingFilterProxy springSecurityFilter,
-                                   Configuration configs) throws ClassNotFoundException {
+                                   Configuration ambariConfig) throws ClassNotFoundException {
 
     ViewInstanceDefinition viewInstanceDefinition = new ViewInstanceDefinition(viewDefinition, instanceConfig);
 
@@ -354,7 +354,7 @@ public class ViewRegistry {
       root.addServlet(sh, pathSpec);
       viewInstanceDefinition.addServletMapping(servletName, pathSpec);
 
-      if (configs.getApiAuthentication()) {
+      if (ambariConfig.getApiAuthentication()) {
         root.addFilter(new FilterHolder(springSecurityFilter), pathSpec, 1);
       }
     }

+ 20 - 4
ambari-server/src/test/java/org/apache/ambari/server/view/ViewDefinitionTest.java

@@ -18,7 +18,7 @@
 
 package org.apache.ambari.server.view;
 
-import org.apache.ambari.server.api.resources.BaseResourceDefinition;
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
 import org.apache.ambari.server.view.configuration.ResourceConfig;
@@ -29,6 +29,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.util.Collection;
+import java.util.Properties;
 import java.util.Set;
 
 import static org.easymock.EasyMock.createNiceMock;
@@ -42,11 +43,17 @@ import static org.easymock.EasyMock.verify;
 public class ViewDefinitionTest {
 
   public static ViewDefinition getViewDefinition() throws Exception {
-    return new ViewDefinition(ViewConfigTest.getConfig());
+    return getViewDefinition(ViewConfigTest.getConfig());
   }
 
-  public static ViewDefinition getViewDefinition(ViewConfig config) throws Exception {
-    return new ViewDefinition(config);
+  public static ViewDefinition getViewDefinition(ViewConfig viewConfig) throws Exception {
+    Properties properties = new Properties();
+    properties.put("p1", "v1");
+    properties.put("p2", "v2");
+    properties.put("p3", "v3");
+
+    Configuration ambariConfig = new Configuration(properties);
+    return new ViewDefinition(viewConfig, ambariConfig);
   }
 
   @Test
@@ -74,6 +81,15 @@ public class ViewDefinitionTest {
     Assert.assertEquals(viewConfig, viewDefinition.getConfiguration());
   }
 
+  @Test
+  public void testGetAmbariProperty() throws Exception {
+    ViewConfig viewConfig = ViewConfigTest.getConfig();
+    ViewDefinition viewDefinition = getViewDefinition(viewConfig);
+    Assert.assertEquals("v1", viewDefinition.getAmbariProperty("p1"));
+    Assert.assertEquals("v2", viewDefinition.getAmbariProperty("p2"));
+    Assert.assertEquals("v3", viewDefinition.getAmbariProperty("p3"));
+  }
+
   @Test
   public void testAddGetResourceProvider() throws Exception {
     ViewDefinition viewDefinition = getViewDefinition();

+ 9 - 0
ambari-views/src/main/java/org/apache/ambari/view/ViewContext.java

@@ -54,6 +54,15 @@ public interface ViewContext {
    */
   public Map<String, String> getProperties();
 
+  /**
+   * Get a property for the given key from the ambari configuration.
+   *
+   * @param key  the property key
+   *
+   * @return the property value; null indicates that the configuration contains no mapping for the key
+   */
+  public String getAmbariProperty(String key);
+
   /**
    * Get the view resource provider for the given resource type.
    *