ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject ambari git commit: AMBARI-8447 - Update ConfigurationResourceProvider to handle Kerberos Administrative Credentials as a special case (tbeerbower)
Date Tue, 09 Dec 2014 00:58:19 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 15c65b930 -> f947bfb41


AMBARI-8447 - Update ConfigurationResourceProvider to handle Kerberos Administrative Credentials
as a special case (tbeerbower)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/f947bfb4
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/f947bfb4
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/f947bfb4

Branch: refs/heads/trunk
Commit: f947bfb4194bef1ab8346bf1d8b8ed7a216e5034
Parents: 15c65b9
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Mon Dec 8 19:58:02 2014 -0500
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Mon Dec 8 19:58:02 2014 -0500

----------------------------------------------------------------------
 .../AmbariManagementControllerImpl.java         |   4 +-
 .../server/controller/AmbariSessionManager.java |  74 ++++++++++--
 .../server/controller/ClusterRequest.java       |  50 ++++++--
 .../AbstractControllerResourceProvider.java     |   2 +-
 .../internal/ClusterResourceProvider.java       |  66 +++++++++--
 .../org/apache/ambari/server/state/Cluster.java |  14 +++
 .../apache/ambari/server/state/Clusters.java    |  17 +++
 .../server/state/cluster/ClusterImpl.java       |  43 ++++++-
 .../server/state/cluster/ClustersImpl.java      |  40 +++++--
 .../ambari/server/agent/AgentResourceTest.java  |   4 +
 .../AmbariManagementControllerImplTest.java     |  12 +-
 .../controller/AmbariSessionManagerTest.java    | 118 +++++++++++++++++++
 .../internal/ClusterResourceProviderTest.java   |   4 +-
 .../internal/JMXHostProviderTest.java           |   4 +-
 .../server/state/cluster/ClusterImplTest.java   |  89 ++++++++++++++
 .../server/state/cluster/ClustersImplTest.java  |  72 +++++++++++
 16 files changed, 560 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
index abd83fc..435c828 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java
@@ -1142,6 +1142,8 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
     for (ClusterRequest request : requests) {
       // TODO : Is there ever a real world case where we could have multiple non-null responses?
       response = updateCluster(request);
+      // set any session attributes for this cluster request
+      clusters.addSessionAttributes(request.getClusterName(), request.getSessionAttributes());
     }
     return response;
   }
@@ -3316,7 +3318,7 @@ public class AmbariManagementControllerImpl implements AmbariManagementControlle
   private Set<OperatingSystemResponse> getOperatingSystems(
       OperatingSystemRequest request) throws AmbariException {
 
-    Set<OperatingSystemResponse> responses = new HashSet<OperatingSystemResponse>();;
+    Set<OperatingSystemResponse> responses = new HashSet<OperatingSystemResponse>();
 
     String stackName = request.getStackName();
     String stackVersion = request.getStackVersion();

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariSessionManager.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariSessionManager.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariSessionManager.java
index e6dd07f..721d95b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariSessionManager.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariSessionManager.java
@@ -47,19 +47,10 @@ public class AmbariSessionManager {
    * @return the current session id; null if no request is associated with the current thread
    */
   public String getCurrentSessionId() {
-    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
-
-    if (requestAttributes instanceof ServletRequestAttributes) {
-      ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
 
-      HttpServletRequest request = servletRequestAttributes.getRequest();
-      if (request != null) {
-        HttpSession session = request.getSession(true);
+    HttpSession session = getHttpSession();
 
-        return session.getId();
-      }
-    }
-    return null;
+    return session == null ? null : session.getId();
   }
 
   /**
@@ -70,4 +61,65 @@ public class AmbariSessionManager {
   public String getSessionCookie() {
     return sessionManager.getSessionCookie();
   }
+
+  /**
+   * Set an attribute value on the current session.
+   *
+   * @param name   the attribute name
+   * @param value  the attribute value
+   */
+  public void setAttribute(String name, Object value) {
+    HttpSession session = getHttpSession();
+    if (session != null) {
+      session.setAttribute(name, value);
+    }
+  }
+
+  /**
+   * Get an attribute value from the current session.
+   *
+   * @param name  the attribute name
+   *
+   * @return the attribute value
+   */
+  public Object getAttribute(String name) {
+    HttpSession session = getHttpSession();
+    if (session != null) {
+      return session.getAttribute(name);
+    }
+    return null;
+  }
+
+  /**
+   * Remove the attribute identified by the given name from the current session.
+   *
+   * @param name  the attribute name
+   */
+  public void removeAttribute(String name) {
+    HttpSession session = getHttpSession();
+    if (session != null) {
+      session.removeAttribute(name);
+    }
+  }
+
+
+  // ----- helper methods ----------------------------------------------------
+
+  /**
+   * Get the current session.
+   *
+   * @return the current session
+   */
+  protected HttpSession getHttpSession() {
+
+    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+
+    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes)
{
+
+      HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
+
+      return request == null ? null : request.getSession(true);
+    }
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterRequest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterRequest.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterRequest.java
index 8bbbd68..5dd0be0 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterRequest.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ClusterRequest.java
@@ -19,6 +19,7 @@
 package org.apache.ambari.server.controller;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -40,21 +41,39 @@ public class ClusterRequest {
 
   private ServiceConfigVersionRequest serviceConfigVersionRequest = null;
 
-  public ClusterRequest(Long clusterId, String clusterName, 
+  /**
+   * The cluster session attributes.
+   */
+  private final Map<String, Object> sessionAttributes;
+
+
+  // ----- Constructors ------------------------------------------------------
+
+  public ClusterRequest(Long clusterId, String clusterName,
       String stackVersion, Set<String> hostNames) {
     this(clusterId, clusterName, null, stackVersion, hostNames);
   }  
   
   public ClusterRequest(Long clusterId, String clusterName, 
       String provisioningState, String stackVersion, Set<String> hostNames) {
+    this(clusterId, clusterName, provisioningState, stackVersion, hostNames, null);
+  }
+
+  public ClusterRequest(Long clusterId, String clusterName,
+                        String provisioningState, String stackVersion,
+                        Set<String> hostNames, Map<String, Object> sessionAttributes)
{
     super();
-    this.clusterId = clusterId;
-    this.clusterName = clusterName;
+    this.clusterId         = clusterId;
+    this.clusterName       = clusterName;
     this.provisioningState = provisioningState;
-    this.stackVersion = stackVersion;
-    this.hostNames = hostNames;    
+    this.stackVersion      = stackVersion;
+    this.hostNames         = hostNames;
+    this.sessionAttributes = sessionAttributes;
   }
-  
+
+
+  // ----- ClusterRequest ----------------------------------------------------
+
   /**
    * @return the clusterId
    */
@@ -129,11 +148,12 @@ public class ClusterRequest {
   }
   
   /**
-   * Sets the configs requests (if any)
-   * @param configRequest
+   * Sets the configs requests (if any).
+   *
+   * @param configRequests  the list of configuration requests
    */
-  public void setDesiredConfig(List<ConfigurationRequest> configRequest) {
-    configs = configRequest;
+  public void setDesiredConfig(List<ConfigurationRequest> configRequests) {
+    configs = configRequests;
   }
   
   /**
@@ -168,11 +188,19 @@ public class ClusterRequest {
     return sb.toString();
   }
 
-
   public ServiceConfigVersionRequest getServiceConfigVersionRequest() {
     return serviceConfigVersionRequest;
   }
 
+  /**
+   * Get the session attributes of this request.
+   *
+   * @return the session attributes; may be null
+   */
+  public Map<String, Object> getSessionAttributes() {
+    return sessionAttributes;
+  }
+
   public void setServiceConfigVersionRequest(ServiceConfigVersionRequest serviceConfigVersionRequest)
{
     this.serviceConfigVersionRequest = serviceConfigVersionRequest;
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
index 6cb8fa4..11b0411 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java
@@ -90,7 +90,7 @@ public abstract class AbstractControllerResourceProvider extends AbstractResourc
 
     switch (type.getInternalType()) {
       case Cluster:
-        return new ClusterResourceProvider(propertyIds, keyPropertyIds, managementController);
+        return new ClusterResourceProvider(managementController);
       case Service:
         return resourceProviderFactory.getServiceResourceProvider(propertyIds, keyPropertyIds,
managementController);
       case Component:

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
index 7423c25..551698a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ClusterResourceProvider.java
@@ -68,6 +68,12 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
   protected static final String CLUSTER_TOTAL_HOSTS_PROPERTY_ID = PropertyHelper.getPropertyId("Clusters",
"total_hosts");
   protected static final String CLUSTER_HEALTH_REPORT_PROPERTY_ID = PropertyHelper.getPropertyId("Clusters",
"health_report");
   protected static final String BLUEPRINT_PROPERTY_ID = PropertyHelper.getPropertyId(null,
"blueprint");
+  protected static final String SESSION_ATTRIBUTES_PROPERTY_ID = "session_attributes";
+
+  /**
+   * The session attributes property prefix.
+   */
+  private static final String SESSION_ATTRIBUTES_PROPERTY_PREFIX = SESSION_ATTRIBUTES_PROPERTY_ID
+ "/";
 
   /**
    * Request info property ID.  Allow internal getResources call to bypass permissions check.
@@ -81,6 +87,31 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
       new HashSet<String>(Arrays.asList(new String[]{CLUSTER_ID_PROPERTY_ID}));
 
   /**
+   * The key property ids for a cluster resource.
+   */
+  private static Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type,
String>();
+  static {
+    keyPropertyIds.put(Resource.Type.Cluster, CLUSTER_NAME_PROPERTY_ID);
+  }
+
+  /**
+   * The property ids for a cluster resource.
+   */
+  private static Set<String> propertyIds = new HashSet<String>();
+  static {
+    propertyIds.add(CLUSTER_ID_PROPERTY_ID);
+    propertyIds.add(CLUSTER_NAME_PROPERTY_ID);
+    propertyIds.add(CLUSTER_VERSION_PROPERTY_ID);
+    propertyIds.add(CLUSTER_PROVISIONING_STATE_PROPERTY_ID);
+    propertyIds.add(CLUSTER_DESIRED_CONFIGS_PROPERTY_ID);
+    propertyIds.add(CLUSTER_DESIRED_SERVICE_CONFIG_VERSIONS_PROPERTY_ID);
+    propertyIds.add(CLUSTER_TOTAL_HOSTS_PROPERTY_ID);
+    propertyIds.add(CLUSTER_HEALTH_REPORT_PROPERTY_ID);
+    propertyIds.add(BLUEPRINT_PROPERTY_ID);
+    propertyIds.add(SESSION_ATTRIBUTES_PROPERTY_ID);
+  }
+
+  /**
    * Maps configuration type (string) to associated properties
    */
   private Map<String, Map<String, String>> mapClusterConfigurations =
@@ -97,14 +128,9 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
   /**
    * Create a  new resource provider for the given management controller.
    *
-   * @param propertyIds           the property ids
-   * @param keyPropertyIds        the key property ids
    * @param managementController  the management controller
    */
-  ClusterResourceProvider(Set<String> propertyIds,
-                          Map<Resource.Type, String> keyPropertyIds,
-                          AmbariManagementController managementController) {
-
+  ClusterResourceProvider(AmbariManagementController managementController) {
     super(propertyIds, keyPropertyIds, managementController);
   }
 
@@ -318,7 +344,6 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
   }
 
 
-
   // ----- utility methods ---------------------------------------------------
 
   /**
@@ -334,7 +359,8 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
         (String) properties.get(CLUSTER_NAME_PROPERTY_ID),
         (String) properties.get(CLUSTER_PROVISIONING_STATE_PROPERTY_ID),
         (String) properties.get(CLUSTER_VERSION_PROPERTY_ID),
-        null);
+        null,
+        getSessionAttributes(properties));
 
     List<ConfigurationRequest> configRequests = getConfigurationRequests("Clusters",
properties);
 
@@ -351,6 +377,28 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
   }
 
   /**
+   * Get the map of session attributes from the given property map.
+   *
+   * @param properties  the property map from the request
+   *
+   * @return the map of session attributes
+   */
+  private Map<String, Object> getSessionAttributes(Map<String, Object> properties)
{
+    Map<String, Object> sessionAttributes = new HashMap<String, Object>();
+
+    for (Map.Entry<String, Object> entry : properties.entrySet()) {
+
+      String property = entry.getKey();
+
+      if (property.startsWith(SESSION_ATTRIBUTES_PROPERTY_PREFIX)) {
+        String attributeName = property.substring(SESSION_ATTRIBUTES_PROPERTY_PREFIX.length());
+        sessionAttributes.put(attributeName, entry.getValue());
+      }
+    }
+    return sessionAttributes;
+  }
+
+  /**
    * Helper method for creating rollback request
    */
   protected ServiceConfigVersionRequest getServiceConfigVersionRequest(String parentCategory,
Map<String, Object> properties) {
@@ -371,10 +419,8 @@ public class ClusterResourceProvider extends BaseBlueprintProcessor {
         else if (propName.equals("service_config_version_note")) {
           serviceConfigVersionRequest.setNote(entry.getValue().toString());
         }
-
       }
     }
-
     return serviceConfigVersionRequest;
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
index fedfb8c..3308f83 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Cluster.java
@@ -412,4 +412,18 @@ public interface Cluster {
    * @return true if the access to this cluster is allowed
    */
   public boolean checkPermission(PrivilegeEntity privilegeEntity, boolean readOnly);
+
+  /**
+   * Add the given map of attributes to the session for this cluster.
+   *
+   * @param attributes  the session attributes
+   */
+  public void addSessionAttributes(Map<String, Object> attributes);
+
+  /**
+   * Get the map of session attributes for this cluster.
+   *
+   * @return the map of session attributes for this cluster; never null
+   */
+  public Map<String, Object> getSessionAttributes();
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
index 18f3a94..8676521 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Clusters.java
@@ -184,4 +184,21 @@ public interface Clusters {
    * @return true if access to the cluster is allowed
    */
   public boolean checkPermission(String clusterName, boolean readOnly);
+
+  /**
+   * Add the given map of attributes to the session for the cluster identified by the given
name.
+   *
+   * @param name        the cluster name
+   * @param attributes  the session attributes
+   */
+  public void addSessionAttributes(String name, Map<String, Object> attributes);
+
+  /**
+   * Get the map of session attributes for the cluster identified by the given name.
+   *
+   * @param name  the cluster name
+   *
+   * @return the map of session attributes for the cluster; never null
+   */
+  public Map<String, Object> getSessionAttributes(String name);
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index 5719c1c..a8abd71 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -44,6 +44,7 @@ import org.apache.ambari.server.ParentObjectNotFoundException;
 import org.apache.ambari.server.ServiceComponentHostNotFoundException;
 import org.apache.ambari.server.ServiceNotFoundException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
+import org.apache.ambari.server.controller.AmbariSessionManager;
 import org.apache.ambari.server.controller.ClusterResponse;
 import org.apache.ambari.server.controller.ConfigurationResponse;
 import org.apache.ambari.server.controller.MaintenanceStateHelper;
@@ -98,7 +99,6 @@ import org.apache.ambari.server.state.ServiceFactory;
 import org.apache.ambari.server.state.ServiceInfo;
 import org.apache.ambari.server.state.StackId;
 import org.apache.ambari.server.state.State;
-import org.apache.ambari.server.state.UpgradeState;
 import org.apache.ambari.server.state.configgroup.ConfigGroup;
 import org.apache.ambari.server.state.configgroup.ConfigGroupFactory;
 import org.apache.ambari.server.state.fsm.InvalidStateTransitionException;
@@ -125,6 +125,11 @@ public class ClusterImpl implements Cluster {
   private static final Logger configChangeLog =
     LoggerFactory.getLogger("configchange");
 
+  /**
+   * Prefix for cluster session attributes name.
+   */
+  private static final String CLUSTER_SESSION_ATTRIBUTES_PREFIX = "cluster_session_attributes:";
+
   @Inject
   private Clusters clusters;
 
@@ -213,6 +218,9 @@ public class ClusterImpl implements Cluster {
   @Inject
   private UpgradeDAO upgradeDAO;
 
+  @Inject
+  private AmbariSessionManager sessionManager;
+
   private volatile boolean svcHostsLoaded = false;
 
   private volatile Multimap<String, String> serviceConfigTypes;
@@ -2317,4 +2325,37 @@ public class ClusterImpl implements Cluster {
     }
     return false;
   }
+
+  @Override
+  public void addSessionAttributes(Map<String, Object> attributes) {
+    if (attributes != null && !attributes.isEmpty()) {
+
+      Map<String, Object>  sessionAttributes = new HashMap<String, Object>(getSessionAttributes());
+
+      sessionAttributes.putAll(attributes);
+
+      String attributeName = CLUSTER_SESSION_ATTRIBUTES_PREFIX + getClusterName();
+
+      getSessionManager().setAttribute(attributeName, sessionAttributes);
+    }
+  }
+
+  @Override
+  public Map<String, Object> getSessionAttributes() {
+    String attributeName = CLUSTER_SESSION_ATTRIBUTES_PREFIX + getClusterName();
+
+    Map<String, Object>  attributes =
+        (Map<String, Object>) getSessionManager().getAttribute(attributeName);
+
+    return attributes == null ? Collections.<String, Object>emptyMap() : attributes;
+  }
+
+  /**
+   * Get the associated session manager.
+   *
+   * @return the session manager
+   */
+  protected AmbariSessionManager getSessionManager() {
+    return sessionManager;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
index d2c7428..9ec8c36 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClustersImpl.java
@@ -47,7 +47,6 @@ import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
-import org.apache.ambari.server.orm.dao.ResourceDAO;
 import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterVersionEntity;
@@ -108,8 +107,6 @@ public class ClustersImpl implements Clusters {
   @Inject
   HostVersionDAO hostVersionDAO;
   @Inject
-  ResourceDAO resourceDAO;
-  @Inject
   ResourceTypeDAO resourceTypeDAO;
   @Inject
   ClusterFactory clusterFactory;
@@ -762,15 +759,44 @@ public class ClustersImpl implements Clusters {
   @Override
   public boolean checkPermission(String clusterName, boolean readOnly) {
 
+    Cluster cluster = findCluster(clusterName);
+
+    return (cluster == null && readOnly) || !configuration.getApiAuthentication()
+      || checkPermission(cluster, readOnly);
+  }
+
+  @Override
+  public void addSessionAttributes(String name, Map<String, Object> attributes) {
+    Cluster cluster = findCluster(name);
+    if (cluster != null) {
+      cluster.addSessionAttributes(attributes);
+    }
+  }
+
+  @Override
+  public Map<String, Object> getSessionAttributes(String name) {
+    Cluster cluster = findCluster(name);
+    return cluster == null ? Collections.<String, Object>emptyMap() : cluster.getSessionAttributes();
+  }
+
+
+  // ----- helper methods ---------------------------------------------------
+
+  /**
+   * Find the cluster for the given name.
+   *
+   * @param name  the cluster name
+   *
+   * @return the cluster for the given name; null if the cluster can not be found
+   */
+  protected Cluster findCluster(String name) {
     Cluster cluster = null;
     try {
-      cluster = clusterName == null ? null : getCluster(clusterName);
+      cluster = name == null ? null : getCluster(name);
     } catch (AmbariException e) {
       // do nothing
     }
-
-    return (cluster == null && readOnly) || !configuration.getApiAuthentication()
-      || checkPermission(cluster, readOnly);
+    return cluster;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
index bce66ee..c3e4b9b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/agent/AgentResourceTest.java
@@ -75,6 +75,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import org.eclipse.jetty.server.SessionManager;
 import org.junit.Test;
 import javax.ws.rs.core.MediaType;
 
@@ -88,6 +89,7 @@ public class AgentResourceTest extends JerseyTest {
   protected Client client;
   HeartBeatHandler handler;
   ActionManager actionManager;
+  SessionManager sessionManager;
   Injector injector;
   AmbariMetaInfo ambariMetaInfo;
   OsFamily os_family;
@@ -288,9 +290,11 @@ public class AgentResourceTest extends JerseyTest {
       actionManager = mock(ActionManager.class);
       ambariMetaInfo = mock(AmbariMetaInfo.class);
       actionDBAccessor = mock(ActionDBAccessor.class);
+      sessionManager = mock(SessionManager.class);
       bind(OsFamily.class).toInstance(os_family);
       bind(ActionDBAccessor.class).toInstance(actionDBAccessor);
       bind(ActionManager.class).toInstance(actionManager);
+      bind(SessionManager.class).toInstance(sessionManager);
       bind(AgentCommand.class).to(ExecutionCommand.class);
       bind(HeartBeatHandler.class).toInstance(handler);
       bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo);

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java
index 4b19443..4db01b9 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java
@@ -103,10 +103,11 @@ public class AmbariManagementControllerImplTest {
   private static final ActionDBAccessorImpl actionDBAccessor = createNiceMock(ActionDBAccessorImpl.class);
   private static final AmbariMetaInfo ambariMetaInfo = createMock(AmbariMetaInfo.class);
   private static final Users users = createMock(Users.class);
+  private static final AmbariSessionManager sessionManager = createNiceMock(AmbariSessionManager.class);
 
   @Before
   public void before() throws Exception {
-    reset(ldapDataPopulator, clusters,actionDBAccessor, ambariMetaInfo, users);
+    reset(ldapDataPopulator, clusters,actionDBAccessor, ambariMetaInfo, users, sessionManager);
   }
 
   @Test
@@ -514,7 +515,7 @@ public class AmbariManagementControllerImplTest {
     expectLastCall();
 
     // replay mocks
-    replay(actionManager, cluster, clusters, injector, clusterRequest);
+    replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager);
 
     // test
     AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager,
clusters, injector);
@@ -522,7 +523,7 @@ public class AmbariManagementControllerImplTest {
 
     // assert and verify
     assertSame(controller, controllerCapture.getValue());
-    verify(actionManager, cluster, clusters, injector, clusterRequest);
+    verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager);
   }
 
   /**
@@ -553,7 +554,7 @@ public class AmbariManagementControllerImplTest {
     expectLastCall().andThrow(new RollbackException());
 
     // replay mocks
-    replay(actionManager, cluster, clusters, injector, clusterRequest);
+    replay(actionManager, cluster, clusters, injector, clusterRequest, sessionManager);
 
     // test
     AmbariManagementController controller = new AmbariManagementControllerImpl(actionManager,
clusters, injector);
@@ -565,7 +566,7 @@ public class AmbariManagementControllerImplTest {
     }
     // assert and verify
     assertSame(controller, controllerCapture.getValue());
-    verify(actionManager, cluster, clusters, injector, clusterRequest);
+    verify(actionManager, cluster, clusters, injector, clusterRequest, sessionManager);
   }
 
   @Test
@@ -1573,6 +1574,7 @@ public class AmbariManagementControllerImplTest {
       binder.bind(ActionDBAccessorImpl.class).toInstance(actionDBAccessor);
       binder.bind(AmbariMetaInfo.class).toInstance(ambariMetaInfo);
       binder.bind(Users.class).toInstance(users);
+      binder.bind(AmbariSessionManager.class).toInstance(sessionManager);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariSessionManagerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariSessionManagerTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariSessionManagerTest.java
new file mode 100644
index 0000000..058baa1
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariSessionManagerTest.java
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.ambari.server.controller;
+
+import org.eclipse.jetty.server.SessionManager;
+import org.junit.Test;
+
+import javax.servlet.http.HttpSession;
+
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class AmbariSessionManagerTest {
+
+  @Test
+  public void testGetCurrentSessionId() throws Exception {
+
+    HttpSession session = createNiceMock(HttpSession.class);
+    AmbariSessionManager sessionManager =
+        createMockBuilder(AmbariSessionManager.class).addMockedMethod("getHttpSession").createMock();
+
+    expect(sessionManager.getHttpSession()).andReturn(session);
+    expect(sessionManager.getHttpSession()).andReturn(null);
+    expect(session.getId()).andReturn("SESSION_ID").anyTimes();
+
+    replay(session, sessionManager);
+
+    assertEquals("SESSION_ID", sessionManager.getCurrentSessionId());
+    assertNull(sessionManager.getCurrentSessionId());
+
+    verify(session, sessionManager);
+  }
+
+  @Test
+  public void testGetSessionCookie() throws Exception {
+    SessionManager sessionManager = createNiceMock(SessionManager.class);
+    AmbariSessionManager ambariSessionManager = new AmbariSessionManager();
+
+    ambariSessionManager.sessionManager = sessionManager;
+
+    expect(sessionManager.getSessionCookie()).andReturn("SESSION_COOKIE").anyTimes();
+
+    replay(sessionManager);
+
+    assertEquals("SESSION_COOKIE", ambariSessionManager.getSessionCookie());
+
+    verify(sessionManager);
+  }
+
+  @Test
+  public void testSetAttribute() throws Exception {
+    HttpSession session = createNiceMock(HttpSession.class);
+    AmbariSessionManager sessionManager =
+        createMockBuilder(AmbariSessionManager.class).addMockedMethod("getHttpSession").createMock();
+
+    expect(sessionManager.getHttpSession()).andReturn(session);
+    session.setAttribute("foo", "bar");
+
+    replay(session, sessionManager);
+
+    sessionManager.setAttribute("foo", "bar");
+
+    verify(session, sessionManager);
+  }
+
+  @Test
+  public void testGetAttribute() throws Exception {
+    HttpSession session = createNiceMock(HttpSession.class);
+    AmbariSessionManager sessionManager =
+        createMockBuilder(AmbariSessionManager.class).addMockedMethod("getHttpSession").createMock();
+
+    expect(sessionManager.getHttpSession()).andReturn(session);
+    expect(session.getAttribute("foo")).andReturn("bar");
+
+    replay(session, sessionManager);
+
+    assertEquals("bar", sessionManager.getAttribute("foo"));
+
+    verify(session, sessionManager);
+  }
+
+  @Test
+  public void testRemoveAttribute() throws Exception {
+    HttpSession session = createNiceMock(HttpSession.class);
+    AmbariSessionManager sessionManager =
+        createMockBuilder(AmbariSessionManager.class).addMockedMethod("getHttpSession").createMock();
+
+    expect(sessionManager.getHttpSession()).andReturn(session);
+    session.removeAttribute("foo");
+
+    replay(session, sessionManager);
+
+    sessionManager.removeAttribute("foo");
+
+    verify(session, sessionManager);
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
index f382588..bc2fc02 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterResourceProviderTest.java
@@ -3418,9 +3418,7 @@ public class ClusterResourceProviderTest {
                                 ResourceProvider hostComponentResourceProvider,
                                 ResourceProvider configGroupResourceProvider) {
 
-      super(PropertyHelper.getPropertyIds(Resource.Type.Cluster),
-            PropertyHelper.getKeyPropertyIds(Resource.Type.Cluster),
-            managementController);
+      super(managementController);
 
       this.serviceResourceProvider = serviceResourceProvider;
       this.componentResourceProvider = componentResourceProvider;

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/JMXHostProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/JMXHostProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/JMXHostProviderTest.java
index a11dc43..7cb9aba 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/JMXHostProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/JMXHostProviderTest.java
@@ -414,9 +414,7 @@ public class JMXHostProviderTest {
     AbstractProviderModule {
 
     ResourceProvider clusterResourceProvider = new
-      ClusterResourceProvider(PropertyHelper.getPropertyIds(Resource.Type
-      .Cluster), PropertyHelper.getKeyPropertyIds(Resource.Type.Cluster),
-      controller);
+      ClusterResourceProvider(controller);
 
     Injector injector = createNiceMock(Injector.class);
     MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
new file mode 100644
index 0000000..8d07b9c
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterImplTest.java
@@ -0,0 +1,89 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.ambari.server.state.cluster;
+
+import org.apache.ambari.server.controller.AmbariSessionManager;
+import org.junit.Test;
+
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.*;
+
+public class ClusterImplTest {
+
+  @Test
+  public void testAddSessionAttributes() throws Exception {
+    Map<String, Object> attributes = new HashMap<String, Object>();
+    attributes.put("foo", "bar");
+
+    AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class);
+
+    ClusterImpl cluster =
+        createMockBuilder(ClusterImpl.class).
+            addMockedMethod("getSessionManager").
+            addMockedMethod("getClusterName").
+            addMockedMethod("getSessionAttributes").
+            createMock();
+
+    expect(cluster.getSessionManager()).andReturn(sessionManager);
+    expect(cluster.getClusterName()).andReturn("c1");
+    expect(cluster.getSessionAttributes()).andReturn(attributes);
+    sessionManager.setAttribute("cluster_session_attributes:c1", attributes);
+
+    replay(sessionManager, cluster);
+
+    cluster.addSessionAttributes(attributes);
+
+    verify(sessionManager, cluster);
+  }
+
+  @Test
+  public void testGetSessionAttributes() throws Exception {
+    Map<String, Object> attributes = new HashMap<String, Object>();
+    attributes.put("foo", "bar");
+
+    AmbariSessionManager sessionManager = createMock(AmbariSessionManager.class);
+
+    ClusterImpl cluster =
+        createMockBuilder(ClusterImpl.class).
+            addMockedMethod("getSessionManager").
+            addMockedMethod("getClusterName").
+            createMock();
+
+    expect(cluster.getSessionManager()).andReturn(sessionManager).anyTimes();
+    expect(cluster.getClusterName()).andReturn("c1").anyTimes();
+    expect(sessionManager.getAttribute("cluster_session_attributes:c1")).andReturn(attributes);
+    expect(sessionManager.getAttribute("cluster_session_attributes:c1")).andReturn(null);
+
+    replay(sessionManager, cluster);
+
+    assertEquals(attributes, cluster.getSessionAttributes());
+    assertEquals(Collections.<String, Object>emptyMap(), cluster.getSessionAttributes());
+
+    verify(sessionManager, cluster);
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/f947bfb4/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersImplTest.java
new file mode 100644
index 0000000..8d83889
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClustersImplTest.java
@@ -0,0 +1,72 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+package org.apache.ambari.server.state.cluster;
+
+import org.apache.ambari.server.state.Cluster;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.createMockBuilder;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.*;
+
+public class ClustersImplTest {
+
+  @Test
+  public void testAddSessionAttributes() throws Exception {
+
+    Map<String, Object> attributes = new HashMap<String, Object>();
+    attributes.put("foo", "bar");
+
+    Cluster cluster = createMock(Cluster.class);
+    ClustersImpl clusters =
+        createMockBuilder(ClustersImpl.class).addMockedMethod("findCluster", String.class).createMock();
+
+    expect(clusters.findCluster("c1")).andReturn(cluster);
+    cluster.addSessionAttributes(attributes);
+    replay(clusters, cluster);
+
+    clusters.addSessionAttributes("c1", attributes);
+
+    verify(clusters, cluster);
+  }
+
+  @Test
+  public void testGetSessionAttributes() throws Exception {
+    Map<String, Object> attributes = new HashMap<String, Object>();
+    attributes.put("foo", "bar");
+
+    Cluster cluster = createMock(Cluster.class);
+    ClustersImpl clusters =
+        createMockBuilder(ClustersImpl.class).addMockedMethod("findCluster", String.class).createMock();
+
+    expect(clusters.findCluster("c1")).andReturn(cluster);
+    expect(cluster.getSessionAttributes()).andReturn(attributes);
+    replay(clusters, cluster);
+
+    assertEquals(attributes, clusters.getSessionAttributes("c1"));
+
+    verify(clusters, cluster);
+  }
+}
\ No newline at end of file


Mime
View raw message