package SOFA.demos.logdemo;

/**
  * Generated by SOFA CodeGen
  * Component builder for ::CUNI::SOFA::demos::logdemo::logdemo?nenya.ms.mff.cuni.cz!0
  * Friday, March 14, 2003 11:16:44 AM CET
  */

public class logdemoBuilder extends SOFA.Component.DCUP.ComponentBuilderImpl {
  // Defined in the ancestor
  // protected SOFA.Component.ComponentManager cm;
  // protected SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd;
  
  SOFA.Component.DCUP.DCUPComponentManager testercm = null;
  SOFA.Component.DCUP.DCUPComponentManager loggercm = null;
  
  
  private void initProperties() throws NumberFormatException {
  }

  
  public logdemoBuilder() {
  }
  
  public void initialize(SOFA.Component.ComponentManager cm, SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd) {
    super.initialize(cm, dd);
    initProperties();
    
    SOFA.SOFAnode.Run.Deployment.DeplDock dock = cm.getLocalDeplDock();
    SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor testerdd = dd.getSubComponentDeploymentDescriptor("tester");
    SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor loggerdd = dd.getSubComponentDeploymentDescriptor("logger");
    
    String location = null;
    SOFA.Connector.Reference testerref = null;
    location = dd.getSubComponentLocation("tester");
    if (SOFA.Util.SOFAURI.isLocal(location)) {
      try {
        String instName = "tester";
        testerref = dock.instantiate(testerdd, instName);
        testercm = getCMConnectorFromRef(testerref);
        cm.registerSubcomponent(instName, testerref);
      } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Component.InstantiationException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't obtain connectors to subcomponent CM", e);
      } catch (SOFA.Component.NamingException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't register subcomponent", e);
      }
    } else {
      try {
        SOFA.Connector.Reference subDDockRef = SOFA.SOFAnode.Run.Deployment.DeplDockImpl.bytesToSofaReference(SOFA.Util.SOFAURI.getReferenceToDeplDockFromSOFAURI(location));
        SOFA.SOFAnode.Run.Deployment.DeplDock subDDock = (SOFA.SOFAnode.Run.Deployment.DeplDock) SOFA.Connector.Boot.DeplDockConnector.createClt(subDDockRef);
        String instName = "tester";
        testerref = subDDock.instantiate(testerdd, instName);
        testercm = getCMConnectorFromRef(testerref);
        cm.registerSubcomponent(instName, testerref);
      } catch (SOFA.Connector.ConnectorException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Util.SOFAURI.BadSOFAURIException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Component.InstantiationException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't obtain connectors to subcomponent CM", e);
      } catch (SOFA.Component.NamingException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't register subcomponent", e);
      }
    }
    
    SOFA.Connector.Reference loggerref = null;
    location = dd.getSubComponentLocation("logger");
    if (SOFA.Util.SOFAURI.isLocal(location)) {
      try {
        String instName = "logger";
        loggerref = dock.instantiate(loggerdd, instName);
        loggercm = getCMConnectorFromRef(loggerref);
        cm.registerSubcomponent(instName, loggerref);
      } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Component.InstantiationException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't obtain connectors to subcomponent CM", e);
      } catch (SOFA.Component.NamingException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't register subcomponent", e);
      }
    } else {
      try {
        SOFA.Connector.Reference subDDockRef = SOFA.SOFAnode.Run.Deployment.DeplDockImpl.bytesToSofaReference(SOFA.Util.SOFAURI.getReferenceToDeplDockFromSOFAURI(location));
        SOFA.SOFAnode.Run.Deployment.DeplDock subDDock = (SOFA.SOFAnode.Run.Deployment.DeplDock) SOFA.Connector.Boot.DeplDockConnector.createClt(subDDockRef);
        String instName = "logger";
        loggerref = subDDock.instantiate(loggerdd, instName);
        loggercm = getCMConnectorFromRef(loggerref);
        cm.registerSubcomponent(instName, loggerref);
      } catch (SOFA.Connector.ConnectorException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Util.SOFAURI.BadSOFAURIException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't create subcomponent CM", e);
      } catch (SOFA.Component.InstantiationException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't obtain connectors to subcomponent CM", e);
      } catch (SOFA.Component.NamingException e) {
        throw new SOFA.Component.ComponentLifecycleException("Can't register subcomponent", e);
      }
    }
    
    
    
    try {
      SOFA.Connector.Reference pref;
      pref = loggercm.getProvisionReference("Log");
      testercm.setRequirement("Log", pref);
    } catch (SOFA.Component.NamingException e) {
      throw new SOFA.Component.ComponentLifecycleException("Can't bind subcomponents", e);
    }
    
    try {
      testercm.connectSubsumable();
      loggercm.connectSubsumable();
    } catch (SOFA.Component.NamingException e) {
      throw new SOFA.Component.ComponentLifecycleException("Can't subsume subcomponent's requirements", e);
    } catch (SOFA.Component.InstantiationException e) {
      throw new SOFA.Component.ComponentLifecycleException("Can't subsume subcomponent's requirements", e);
    }
    
    updatable = false;
  }
  
  public void onLeaving(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {
    if (!stopped) {
  
    } else {
      SOFA.SOFAnode.Run.Deployment.DeplDock dock = cm.getLocalDeplDock();
      try {
        dock.removeComponent(fullInstanceName);
      } catch (SOFA.SOFAnode.Run.Deployment.DeploymentException e) {
        throw new SOFA.Component.ComponentLifecycleException("Component can't be removed", e);
      }
    }
  }
  
  public void onArrival(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {
    testercm.createComponent(stateStore);
    loggercm.createComponent(stateStore);
  }
  
  public void store(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {
  
  }
  
  public void restore(SOFA.SOFAnode.Run.Storage stateStore) throws SOFA.Component.ComponentLifecycleException {
  
  }
}
