浏览代码

AMBARI-2731. Use httpcomponents-client for building url in AmbariManagementControllerImpl. (Andrew Onischuk via smohanty)

Sumit Mohanty 12 年之前
父节点
当前提交
ba1b3bbec0

+ 5 - 0
ambari-server/pom.xml

@@ -713,6 +713,11 @@
       <artifactId>postgresql</artifactId>
       <version>9.1-901.jdbc4</version>
     </dependency>
+    <dependency>
+	  <groupId>org.apache.httpcomponents</groupId>
+	  <artifactId>httpclient</artifactId>
+	  <version>4.2.5</version>
+    </dependency>
   </dependencies>
   <!--<reporting>
         <plugins>

+ 32 - 17
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java

@@ -100,6 +100,8 @@ import com.google.inject.Injector;
 import com.google.inject.Singleton;
 import com.google.inject.persist.Transactional;
 
+import org.apache.http.client.utils.URIBuilder;
+
 @Singleton
 public class AmbariManagementControllerImpl implements
     AmbariManagementController {
@@ -145,6 +147,8 @@ public class AmbariManagementControllerImpl implements
   private Configuration configs;
 
   final private String masterHostname;
+  final private Integer masterPort;
+  final private String masterProtocol;
 
   final private static String JDK_RESOURCE_LOCATION =
       "/resources/";
@@ -168,32 +172,43 @@ public class AmbariManagementControllerImpl implements
     LOG.info("Initializing the AmbariManagementControllerImpl");
     this.masterHostname =  InetAddress.getLocalHost().getCanonicalHostName();
 
-    if (configs != null) {
-      String protocol, port;
+    if(configs != null)
+    {
       if (configs.getApiSSLAuthentication() == true) {
-        protocol = "https";
-        port = String.valueOf(configs.getClientSSLApiPort());
+        this.masterProtocol = "https";
+        this.masterPort = configs.getClientSSLApiPort();
       } else {
-        protocol = "http";
-        port = String.valueOf(configs.getClientApiPort());
-      }
-
-      this.jdkResourceUrl = protocol + "://" + masterHostname + ":"
-          + port
-          + JDK_RESOURCE_LOCATION;
-      this.ojdbcUrl = protocol + "://" + masterHostname + ":"
-          + port + JDK_RESOURCE_LOCATION + "/" + configs.getOjdbcJarName();
-
-      this.mysqljdbcUrl = protocol + "://" + masterHostname + ":"
-          + port + JDK_RESOURCE_LOCATION + "/" + configs.getMySQLJarName();
+        this.masterProtocol = "http";
+        this.masterPort = configs.getClientApiPort();
+      }  
+      this.jdkResourceUrl = getAmbariServerURI(JDK_RESOURCE_LOCATION);    
+      this.ojdbcUrl = getAmbariServerURI(JDK_RESOURCE_LOCATION + "/" + configs.getOjdbcJarName());
+      this.mysqljdbcUrl = getAmbariServerURI(JDK_RESOURCE_LOCATION + "/" + configs.getMySQLJarName());
+     
       this.serverDB = configs.getServerDBName();
     } else {
-      this.jdkResourceUrl = null;
+      this.masterProtocol = null;
+      this.masterPort = null;
+      
+      this.jdkResourceUrl = null; 
       this.ojdbcUrl = null;
       this.mysqljdbcUrl = null;
       this.serverDB = null;
     }
   }
+  
+  public String getAmbariServerURI(String path) {
+    if(masterProtocol==null || masterHostname==null || masterPort==null)
+      return null;
+    
+    URIBuilder uriBuilder = new URIBuilder();
+    uriBuilder.setScheme(masterProtocol);
+    uriBuilder.setHost(masterHostname);
+    uriBuilder.setPort(masterPort);
+    uriBuilder.setPath(path);
+    
+    return uriBuilder.toString();
+  }
 
   private RoleCommandOrder getRCO(Cluster cluster) {
       RoleCommandOrder rco;

+ 67 - 1
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java

@@ -33,6 +33,7 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.lang.reflect.Type;
 import java.util.Collection;
 import java.util.Collections;
@@ -67,6 +68,7 @@ import org.apache.ambari.server.state.svccomphost.ServiceComponentHostOpSucceede
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.collections.Predicate;
 import org.easymock.Capture;
+import org.easymock.EasyMock;
 import org.junit.Test;
 
 import com.google.gson.Gson;
@@ -82,7 +84,71 @@ import com.google.inject.persist.PersistService;
 public class AmbariManagementControllerImplTest {
 
 
+  @Test
+  public void testgetAmbariServerURI() throws Exception {
+    // create mocks
+    Injector injector = createStrictMock(Injector.class);
+    Capture<AmbariManagementController> controllerCapture = new Capture<AmbariManagementController>();
+
+    // set expectations
+    injector.injectMembers(capture(controllerCapture));
+    expect(injector.getInstance(Gson.class)).andReturn(null);
+    
+    //replay
+    replay(injector);
+    
+    
+    AmbariManagementControllerImpl controller = new AmbariManagementControllerImpl(null, null, injector);
+    
+    class AmbariConfigsSetter{
+       public void setConfigs(AmbariManagementController controller, String masterProtocol, String masterHostname, Integer masterPort) throws Exception{
+         // masterProtocol
+         Class<?> c = controller.getClass();
+         Field f = c.getDeclaredField("masterProtocol");
+         f.setAccessible(true);
+         
+         Field modifiersField = Field.class.getDeclaredField("modifiers");
+         modifiersField.setAccessible(true);
+         modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
+         
+         f.set(controller, masterProtocol);
+         
+         // masterHostname
+         f = c.getDeclaredField("masterHostname");
+         f.setAccessible(true);
+         
+         modifiersField = Field.class.getDeclaredField("modifiers");
+         modifiersField.setAccessible(true);
+         modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
+         
+         f.set(controller, masterHostname);
+         
+         // masterPort
+         f = c.getDeclaredField("masterPort");
+         f.setAccessible(true);
+         
+         modifiersField = Field.class.getDeclaredField("modifiers");
+         modifiersField.setAccessible(true);
+         modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
+         
+         f.set(controller, masterPort);
+       }
+    }
+
+    AmbariConfigsSetter ambariConfigsSetter = new AmbariConfigsSetter();
+    
+    ambariConfigsSetter.setConfigs(controller, "http", "hostname", 8080);
+    assertEquals("http://hostname:8080/jdk_path", controller.getAmbariServerURI("/jdk_path"));
+    
+    ambariConfigsSetter.setConfigs(controller, "https", "somesecuredhost", 8443);
+    assertEquals("https://somesecuredhost:8443/mysql_path", controller.getAmbariServerURI("/mysql_path"));
 
+    ambariConfigsSetter.setConfigs(controller, "https", "othersecuredhost", 8443);
+    assertEquals("https://othersecuredhost:8443/oracle/ojdbc/", controller.getAmbariServerURI("/oracle/ojdbc/"));
+    
+    verify(injector);
+  }
+  
   @Test
   public void testGetClusters() throws Exception {
     // member state mocks
@@ -1513,7 +1579,7 @@ public class AmbariManagementControllerImplTest {
       protected void configure() {
         Properties properties = new Properties();
         properties.setProperty(Configuration.SERVER_PERSISTENCE_TYPE_KEY, "in-memory");
-
+        
         properties.setProperty(Configuration.METADETA_DIR_PATH,
             "src/main/resources/stacks");
         properties.setProperty(Configuration.SERVER_VERSION_FILE,