brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [07/50] brooklyn-library git commit: AbstractController: use config/sensors instead of fields
Date Mon, 01 Feb 2016 17:46:10 GMT
AbstractController: use config/sensors instead of fields

Will make it easier to serialize/deserialize if using standard
mechanism of getAttribute rather than duplicating the values in
fields.

Also, "fixes" makeUrl which was called from checkEntity. It therefore
overwrote the value of SPECIFIED_URL attribute each time. And tests
only worked because of the call to makeUrl inside checkEntity, which
was very misleading.

I'm not sure about the old comment in preStart:
  previously we would attempt to infer values from a specified URL, but now we don't
  as that specified URL might be for another machine with port-forwarding
I've changed it so we really do believe the SPECIFIED_URL for reporting the
protocol and specified_url attributes.


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-library/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-library/commit/4e9a81a1
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-library/tree/4e9a81a1
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-library/diff/4e9a81a1

Branch: refs/heads/0.4.0
Commit: 4e9a81a18f4cecd317fb0d5ba70242f55bcf5515
Parents: 71c8f49
Author: Aled Sage <aled.sage@gmail.com>
Authored: Tue Sep 25 13:37:57 2012 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Sep 26 11:16:17 2012 +0100

----------------------------------------------------------------------
 .../entity/proxy/AbstractController.java        | 106 ++++++++++---------
 .../entity/proxy/nginx/NginxController.groovy   |   9 +-
 2 files changed, 57 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/4e9a81a1/software/webapp/src/main/java/brooklyn/entity/proxy/AbstractController.java
----------------------------------------------------------------------
diff --git a/software/webapp/src/main/java/brooklyn/entity/proxy/AbstractController.java b/software/webapp/src/main/java/brooklyn/entity/proxy/AbstractController.java
index eff7df1..520cc21 100644
--- a/software/webapp/src/main/java/brooklyn/entity/proxy/AbstractController.java
+++ b/software/webapp/src/main/java/brooklyn/entity/proxy/AbstractController.java
@@ -41,7 +41,7 @@ public abstract class AbstractController extends SoftwareProcessEntity implement
 
     /** sensor for port to forward to on target entities */
     @SetFromFlag("portNumberSensor")
-    public static final BasicConfigKey<AttributeSensor> PORT_NUMBER_SENSOR = new BasicConfigKey<AttributeSensor>(
+    public static final BasicAttributeSensorAndConfigKey<AttributeSensor> PORT_NUMBER_SENSOR
= new BasicAttributeSensorAndConfigKey<AttributeSensor>(
             AttributeSensor.class, "member.sensor.portNumber", "Port number sensor on members
(defaults to http.port)", Attributes.HTTP_PORT);
 
     @SetFromFlag("port")
@@ -73,14 +73,9 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
     
     public static final MethodEffector<Void> RELOAD = new MethodEffector(AbstractController.class,
"reload");
     
-    protected String domain;
-    protected Integer port;
-    protected String protocol;
-    protected String url;
-    protected AttributeSensor<Integer> portNumber;
-    protected boolean isActive = false;
-    protected boolean updateNeeded = true;
     protected Group serverPool;
+    protected boolean isActive;
+    protected boolean updateNeeded = true;
 
     AbstractMembershipTrackingPolicy policy;
     protected Set<String> addresses = new LinkedHashSet<String>();
@@ -137,47 +132,61 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
         }
     }
 
+    public boolean isActive() {
+    	return isActive;
+    }
+    
     public String getDomain() {
-        return domain;
+        return getAttribute(DOMAIN_NAME);
     }
     
     public Integer getPort() {
-        return port;
+        return getAttribute(PROXY_HTTP_PORT);
     }
 
     public String getProtocol() {
-        return protocol;
+        return getAttribute(PROTOCOL);
     }
 
     public String getUrl() {
-        return url;
+        return getAttribute(SPECIFIED_URL);
+    }
+
+    public AttributeSensor getPortNumberSensor() {
+        return getAttribute(PORT_NUMBER_SENSOR);
     }
 
     @Description("Forces reload of the configuration")
     public abstract void reload();
+
+    protected String inferProtocol(String url) {
+    	if (url!=null && !url.startsWith("null:")) {
+    		return url.substring(0, url.indexOf(':'));
+    	} else {
+    		return inferProtocol();
+    	}
+    }
     
-    protected void makeUrl() {
-        if (url==null || url.contains("://"+ANONYMOUS+":")) {
-            String hostname = domain;
-            // use 'hostname' instead of domain if domain is anonymous
-            if (hostname==null || hostname==ANONYMOUS) {
-                hostname = getAttribute(HOSTNAME);
-                if (hostname!=null) {
-                    domain = hostname;
-                    setConfigEvenIfOwned(DOMAIN_NAME, hostname);
-                    setAttribute(DOMAIN_NAME, hostname);
-                } else {
-                    LOG.warn("Unable to determine domain/hostname for "+this);
-                }
-            }
-            if (hostname==null) hostname = ANONYMOUS;
-            if (protocol==null) {
-                if (url!=null && !url.startsWith("null:")) protocol = url.substring(0,
url.indexOf(':'));
-                else protocol = getConfig(SSL_CONFIG)!=null ? "https" : "http";
+    protected String inferProtocol() {
+    	return getConfig(SSL_CONFIG)!=null ? "https" : "http";
+    }
+    
+    protected String inferHostname() {
+        String hostname = getDomain();
+        // use 'hostname' instead of domain if domain is anonymous
+        if (hostname==null || hostname.equals(ANONYMOUS)) {
+            hostname = getAttribute(HOSTNAME);
+            if (hostname == null) {
+                LOG.warn("Unable to determine domain/hostname for {}", this);
             }
-            url = protocol+"://"+hostname+":"+port+"/";
-            setAttribute(SPECIFIED_URL, url);
         }
+        if (hostname==null) hostname = ANONYMOUS;
+        
+        return hostname;
+    }
+    
+    protected String inferUrl() {
+        return getProtocol()+"://"+getDomain()+":"+getPort()+"/";
     }
     
     @Override
@@ -191,24 +200,24 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
         super.preStart();
         
         // use http port by default
-        portNumber = checkNotNull(getConfig(PORT_NUMBER_SENSOR));
-
-        port = getAttribute(PROXY_HTTP_PORT);
-        Preconditions.checkNotNull(port, "Port must be set for controller");
+        checkNotNull(getPortNumberSensor(), "port number sensor must not be null");
         
-        protocol = getConfig(PROTOCOL);
-        domain = getConfig(DOMAIN_NAME);
-
-        if (!groovyTruth(getConfig(SPECIFIED_URL))) {
+        if (groovyTruth(getConfig(SPECIFIED_URL))) {
             // previously we would attempt to infer values from a specified URL, but now
we don't
             // as that specified URL might be for another machine with port-forwarding
+            setAttribute(PROTOCOL, inferProtocol(getConfig(SPECIFIED_URL)));
+            setAttribute(HOSTNAME, inferHostname());
+            setAttribute(DOMAIN_NAME, getAttribute(HOSTNAME));
+            setAttribute(SPECIFIED_URL, getConfig(SPECIFIED_URL));
+
         } else {
-            makeUrl();
+            setAttribute(PROTOCOL, inferProtocol());
+            setAttribute(HOSTNAME, inferHostname());
+            setAttribute(DOMAIN_NAME, getAttribute(HOSTNAME));
+            setAttribute(SPECIFIED_URL, inferUrl());
         }
-        setAttribute(PROTOCOL, protocol);
-        setAttribute(DOMAIN_NAME, domain);
         
-        Preconditions.checkNotNull(domain, "Domain must be set for controller");
+        Preconditions.checkNotNull(getDomain(), "Domain must be set for controller");
     }
     
     public void checkEntity(Entity member) {
@@ -247,7 +256,7 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
             MachineLocation machine = (MachineLocation) loc;
             //use hostname as this is more portable (eg in amazon, ip doesn't resolve)
             String ip = machine.getAddress().getHostName();
-            Integer port = member.getAttribute(portNumber);
+            Integer port = member.getAttribute(getPortNumberSensor());
             if (ip==null || port==null) {
                 LOG.warn("Missing ip/port for web controller {} target {}, skipping", this,
member);
             } else {
@@ -260,9 +269,6 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
         }
         LOG.info("Adding to {}, new member {} in locations {}", new Object[] {getDisplayName(),
member.getDisplayName(), member.getLocations()});
         
-        // TODO shouldn't need to do this here? (no harm though)
-        makeUrl();
-        
         update();
         targets.add(member);
     }
@@ -274,7 +280,7 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
         for (Location loc : member.getLocations()) {
             MachineLocation machine = (MachineLocation) loc;
             String ip = machine.getAddress().getHostAddress();
-            int port = member.getAttribute(portNumber);
+            int port = member.getAttribute(getPortNumberSensor());
             addresses.remove(ip+":"+port);
         }
         if (addresses==oldAddresses) {
@@ -304,7 +310,7 @@ public abstract class AbstractController extends SoftwareProcessEntity
implement
     protected abstract void reconfigureService();
     
     public void update() {
-        if (!isActive) updateNeeded = true;
+        if (!isActive()) updateNeeded = true;
         else {
             updateNeeded = false;
             LOG.debug("Updating {} in response to changes", this);

http://git-wip-us.apache.org/repos/asf/brooklyn-library/blob/4e9a81a1/software/webapp/src/main/java/brooklyn/entity/proxy/nginx/NginxController.groovy
----------------------------------------------------------------------
diff --git a/software/webapp/src/main/java/brooklyn/entity/proxy/nginx/NginxController.groovy
b/software/webapp/src/main/java/brooklyn/entity/proxy/nginx/NginxController.groovy
index 824fc34..7324bf6 100644
--- a/software/webapp/src/main/java/brooklyn/entity/proxy/nginx/NginxController.groovy
+++ b/software/webapp/src/main/java/brooklyn/entity/proxy/nginx/NginxController.groovy
@@ -23,7 +23,6 @@ import brooklyn.util.ResourceUtils
 import brooklyn.util.flags.SetFromFlag
 import brooklyn.util.internal.TimeExtras
 
-import com.google.common.base.Predicates
 import com.google.common.collect.Iterables
 import com.google.common.collect.LinkedHashMultimap
 import com.google.common.collect.Multimap
@@ -107,8 +106,6 @@ public class NginxController extends AbstractController {
     public void connectSensors() {
         super.connectSensors();
         
-        makeUrl();
-        
         sensorRegistry.register(new ConfigSensorAdapter());
         
         HttpSensorAdapter http = sensorRegistry.register(
@@ -134,11 +131,6 @@ public class NginxController extends AbstractController {
         setAttribute(SERVICE_UP, false);
     }
     
-    protected void makeUrl() {
-        super.makeUrl();
-        setAttribute(ROOT_URL, url);
-    }
-    
     @Override
     public Class getDriverInterface() {
         return NginxDriver.class;
@@ -150,6 +142,7 @@ public class NginxController extends AbstractController {
     
     protected void preStart() {
         super.preStart();
+        setAttribute(ROOT_URL, getUrl());
     }
     
     protected void reconfigureService() {


Mime
View raw message