package brooklyn.entity.database.rubyrep;

import java.net.URI;

import brooklyn.entity.basic.SoftwareProcessImpl;
import brooklyn.entity.database.DatastoreMixins.DatastoreCommon;
import brooklyn.event.basic.DependentConfiguration;

public class RubyRepNodeImpl extends SoftwareProcessImpl implements RubyRepNode {

    @Override
    protected void connectSensors() {
        super.connectSensors();
        connectServiceUpIsRunning();
    }

    @Override
    public void disconnectSensors() {
        super.disconnectSensors();
        disconnectServiceUpIsRunning();
    }

    /**
     * {@inheritDoc}
     * <p/>
     * Adds support for binding to brooklyn DatabaseNodes (so the user doesn't have to call attributeWhenReady, etc)
     */
    @Override
    public void init() {
        super.init();

        DatastoreCommon leftNode = getConfig(LEFT_DATABASE);
        DatastoreCommon rightNode = getConfig(RIGHT_DATABASE);

        if (leftNode != null) {
            setAttribute(LEFT_DATASTORE_URL, DependentConfiguration.attributeWhenReady(leftNode, DatastoreCommon.DATASTORE_URL).getUnchecked());
        }
        if (rightNode != null) {
            setAttribute(RIGHT_DATASTORE_URL, DependentConfiguration.attributeWhenReady(rightNode, DatastoreCommon.DATASTORE_URL).getUnchecked());
        }
    }

    @Override
    public Class<?> getDriverInterface() {
        return RubyRepDriver.class;
    }

    /**
     * Accessors used in freemarker template processing
     */
    public int getReplicationInterval() {
        return getConfig(REPLICATION_INTERVAL);
    }
    
    public String getTableRegex() {
        return getConfig(TABLE_REGEXP);
    }
    
    public URI getLeftDatabaseUrl() {
        return URI.create(getAttribute(LEFT_DATASTORE_URL));
    }
    
    public String getLeftDatabaseName() {
        return getConfig(LEFT_DATABASE_NAME);
    }

    public String getLeftUsername() {
        return getConfig(LEFT_USERNAME);
    }

    public String getLeftPassword() {
        return getConfig(LEFT_PASSWORD);
    }

    public URI getRightDatabaseUrl() {
        return URI.create(getAttribute(RIGHT_DATASTORE_URL));
    }

    public String getRightDatabaseName() {
        return getConfig(RIGHT_DATABASE_NAME);
    }

    public String getRightUsername() {
        return getConfig(RIGHT_USERNAME);
    }

    public String getRightPassword() {
        return getConfig(RIGHT_PASSWORD);
    }
}

@@ -58,16 +58,55 @@
 t.URI;%0A%0A
+import brooklyn.entity.basic.Entities;%0A
 import b
@@ -532,43 +532,8 @@
 ) %7B%0A
-        super.disconnectSensors();%0A
     
@@ -576,168 +576,178 @@
     
-%7D%0A%0A
     
-/**%0A     * %7B@inheritDoc%7D%0A     * %3Cp/%3E%0A     * Adds support for binding to brooklyn DatabaseNodes (so the user doesn't have to call attributeWhenReady, etc)
+super.disconnectSensors();%0A    %7D%0A%0A    /**%0A     * Set the database %7B@link DatastoreCommon#DATASTORE_URL urls%7D as attributes when they become available on the entities.
 %0A   
@@ -774,22 +774,29 @@
    p
-ublic void ini
+rotected void preStar
 t() 
@@ -815,11 +815,15 @@
 per.
-ini
+preStar
 t();
@@ -889,72 +889,8 @@
 E);%0A
-        DatastoreCommon rightNode = getConfig(RIGHT_DATABASE);%0A%0A
     
@@ -953,32 +953,54 @@
 T_DATASTORE_URL,
+ Entities.submit(this,
  DependentConfig
@@ -1058,32 +1058,33 @@
 n.DATASTORE_URL)
+)
 .getUnchecked())
@@ -1090,24 +1090,88 @@
 );%0A        %7D
+%0A%0A        DatastoreCommon rightNode = getConfig(RIGHT_DATABASE);
 %0A        if 
@@ -1237,16 +1237,38 @@
 ORE_URL,
+ Entities.submit(this,
  Depende
@@ -1343,16 +1343,17 @@
 ORE_URL)
+)
 .getUnch

Move DATASTORE_URL attribute retrieval to preStart() from init()