/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.bundle;

import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.opengamma.util.ArgumentChecker;

/**
 * Factory to create a {@code BundleManager} from the Bundle XML configuration file.
 * <p>
 * This exists to handle the late arrival of the {@code ServletContext}
 */
public class BundleManagerFactory {
  
  private static final String DEFAULT_CONFIG_XML_PATH = "/WEB-INF/uiResourceConfig.xml";
  
  /**
   * The base directory.
   */
  private String _baseDir;
  /**
   * The config xml path
   */
  private String _configXmlPath;
  /**
   * The manager created from this factory.
   */
  private volatile BundleManager _manager;

  /**
   * Creates an instance.
   */
  public BundleManagerFactory() {
  }

  //-------------------------------------------------------------------------
  /**
   * Gets the base directory.
   * @return the base directory
   */
  public String getBaseDir() {
    return _baseDir;
  }

  /**
   * Sets the base directory.
   * 
   * @param baseDir  the base directory
   */
  public void setBaseDir(String baseDir) {
    _baseDir = baseDir;
  }
  
  /**
   * Gets the configXmlPath.
   * @return the configXmlPath
   */
  public String getConfigXmlPath() {
    return _configXmlPath;
  }

  /**
   * Sets the configXmlPath.
   * @param configXmlPath  the configXmlPath
   */
  public void setConfigXmlPath(String configXmlPath) {
    if (!StringUtils.isEmpty(configXmlPath)) {
      _configXmlPath = configXmlPath.startsWith("/") ? configXmlPath : "/" + configXmlPath;
    }
  }

  //-------------------------------------------------------------------------
  /**
   * Obtains a bundle manager using the servlet context.
   * <p>
   * This is used to obtain the manager from the servlet context, because the
   * servlet context is usually available after the factory is setup.
   * 
   * @param servletContext  the servlet context, not null
   * @return the manager, not null
   */
  public BundleManager get(ServletContext servletContext) {
    BundleManager manager = _manager;
    if (manager == null) {
      synchronized (this) {
        manager = _manager;
        if (manager == null) {
          _manager = manager = createManager(servletContext);  // CSIGNORE
        }
      }
    }
    return manager;
  }

  /**
   * Creates a bundle manager using the servlet context.
   * 
   * @param servletContext  the servlet context, not null
   * @return the manager, not null
   */
  protected BundleManager createManager(ServletContext servletContext) {
    ArgumentChecker.notNull(servletContext, "servletContext");
    InputStream uiResource = null;
    try {
      ServletContextUriProvider uriProvider = new ServletContextUriProvider(getBaseDir(), servletContext);
      uiResource = getXMLStream(servletContext);
      BundleParser parser = new BundleParser(uriProvider, getBaseDir());
      return parser.parse(uiResource);
    } finally {
      IOUtils.closeQuietly(uiResource);
    }
  }

  /**
   * Resolves the config file.
   * @param servletContext 
   * 
   * @return the resolved file
   */
  private InputStream getXMLStream(ServletContext servletContext) {
    String configXMlPath = _configXmlPath == null ? DEFAULT_CONFIG_XML_PATH : _configXmlPath;
    InputStream result = servletContext.getResourceAsStream(configXMlPath);
    if (result == null) {
      throw new IllegalStateException("Cannot find uiResourceConfig.xml in /WEB-INF web directory");
    }
    return result;
  }

}

@@ -895,16 +895,96 @@
 manager;
+%0A  /**%0A   * The uri provider.%0A   */%0A  private volatile UriProvider _uriProvider;
 %0A%0A  /**%0A
@@ -1851,24 +1851,337 @@
 h;%0A    %7D%0A  %7D
+%0A  %0A  /**%0A   * Gets the uriProvider.%0A   * @return the uriProvider%0A   */%0A  public UriProvider getUriProvider() %7B%0A    return _uriProvider;%0A  %7D%0A%0A  /**%0A   * Sets the uriProvider.%0A   * @param uriProvider  the uriProvider%0A   */%0A  public void setUriProvider(UriProvider uriProvider) %7B%0A    _uriProvider = uriProvider;%0A  %7D
 %0A%0A  //------
@@ -3361,24 +3361,59 @@
 etContext);%0A
+      setUriProvider(uriProvider);%0A
       uiReso
@@ -4055,55 +4055,41 @@
 ind 
-uiR
+r
 esource
-Config.xml in /WEB-INF web directory%22
+ XML in %22 + configXMlPath
 );%0A 

Add URIProvider to support compiling sass to css