/**
   * Obtains a {@link LoggingConfigurator} for the enviroment.
   *
   * @see {@link #getPermanentLoggingConfigurator()}
   * @return the start up configurations to be used permantly.
   */
  protected LoggingConfigurator getPermanentLoggingConfigurator() {
    final FileContext fileRepositoryService =
        wiringService.getInstance(FileContextService.class).getFileContext();

    ManagedFile configFolder = fileRepositoryService.getEnvironmentConfigRepository();

    if (configFolder != null && configFolder.exists() && configFolder.isReadable()) {
      ManagedFile configXML = configFolder.retrive("log-config.xml");
      if (configXML != null && configXML.exists() && configXML.isReadable()) {
        try {
          return new XMLLoggingConfigurator(configXML.getURI().toURL());
        } catch (MalformedURLException e) {
          throw new IllegalArgumentException(e);
        }
      }
    }

    // if no file is defined continue to use the startup configuration.
    return getStartUpLoggingConfigurator();
  }
  /** Start the environment */
  protected final void start() {

    StopWatch watch = StopWatch.start();

    // Phase 1 - default services

    // set Logging Service
    this.loggingService = this.resolveLoggingService();

    // Register Logging Service
    serviceRegistryContext.register(LoggingService.class, loggingService);

    // set default logger
    this.logger = new LogServiceDelegatorLogger(this.getClass().getName(), loggingService);

    LoggingConfigurator temporaryConfigurator = this.getStartUpLoggingConfigurator();

    ConfigurableLogListener listener =
        new ConfigurableLogListener(new LoggingConfiguration(null), temporaryConfigurator);
    this.loggingService.addLogListener(listener);

    serviceRegistryContext.register(LocalizationService.class, resolveLocalizationService());

    logger.info("Begin MiddleHeaven bootstrap");

    // set the environment resolver
    this.bootstrapEnvironmentResolver = this.bootstrapEnvironmentResolver();

    // Determine Bootstrap Service
    bootstrapService = new SimpleBootstrapService(this);

    // Register Bootstrap Service
    serviceRegistryContext.register(BootstrapService.class, bootstrapService);
    serviceRegistryContext.register(WiringService.class, wiringService);

    final Collection<Service> services = new LinkedHashSet<Service>();

    context = new DefaultBootstrapContext(this, services);
    // Resolve Container
    // Phase 2 - enviroment

    this.environment = bootstrapEnvironmentResolver.resolveEnvironment(context);

    logger.info("Using environment: {0}", environment.getName());

    // Resolve FileSystem
    FileContext fileSystem = environment.getFileContext();

    serviceRegistryContext.register(
        FileContextService.class, new FileSystemFileContextService(fileSystem));

    wiringService.addConfiguration(new FileContextBindConfiguration(fileSystem));

    // configure permanent logging
    this.loggingService.removeAllListeners();
    listener =
        new ConfigurableLogListener(
            new LoggingConfiguration(fileSystem.getAppLogRepository()),
            this.getPermanentLoggingConfigurator());
    this.loggingService.addLogListener(listener);

    doBeforeStart(context);

    // apply profiles to wiring service
    logger.trace("Configuring active environment profiles");

    String profiles = propertyManagers.getProperty("middleheaven.profiles.active", String.class);

    if (profiles != null) {
      wiringService
          .getActiveProfiles()
          .add(Splitter.on(',').trim().split(profiles).into(new LinkedList<String>()));
    }

    logger.debug("Register environment services");

    // activate services that can be overridden by the container or final
    // environment

    logger.debug("Scanning for extentions");

    List<BootstrapContainerExtention> extentions = new ArrayList<BootstrapContainerExtention>();

    readExtentions(extentions);

    // configurate container with chain of extentions
    BootstrapChain chain = new BootstrapChain(logger, extentions);

    logger.debug("Configuring context");

    logger.debug("Inicialize service discovery");

    environment.preConfigurate(context);

    // config any specific environment activators
    preConfig(context);

    // start bootstrap
    // any participant that wishes to add activators can do so now.
    bootstrapService.fireBootupStart();

    // configure using extentions
    chain.doChain(context);

    // call configuration
    posConfig(context);

    // at this point all needed activators are collected.

    // scan and activate all services
    activate(services, serviceRegistryContext);

    // Phase 3 - inicilize modules
    // ApplicationModulesResolver modulesResolver =
    // environment.getApplicationModulesResolver();

    // modulesResolver.resolveApplicationModules();

    // TODO usar applicationScanner
    StandardApplicationServiceActivator act = new StandardApplicationServiceActivator();

    act.activate(serviceRegistryContext);

    StandardApplicationScannerServiceActivator scannerServiceActivator =
        new StandardApplicationScannerServiceActivator();

    scannerServiceActivator.activate(serviceRegistryContext);

    serviceRegistryContext.getService(ApplicationScanningService.class).scan();

    //
    wiringService.refresh();

    doAfterStart(context);

    environment.posConfigurate(context);

    // inform the end of the bootstrap
    bootstrapService.fireBootupEnd();

    logger.info("MiddleHeaven bootstraped in {0} ms. ", watch.mark().milliseconds());

    logger.info("Starting environment");

    // start the environment. the ui is available.
    environment.start();
  }