package org.opengroupware.jope.appserver;

import java.io.InputStream;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.opengroupware.jope.foundation.NSJavaRuntime;

public class WOClassResourceManager extends WOResourceManager {

  protected Class  baseClass = null;
  protected String pkgName   = null;
  
  public WOClassResourceManager(Class _clazz, boolean _enableCaching) {
    super(_enableCaching);
    this.baseClass = _clazz;
    this.pkgName   = this.baseClass.getPackage().getName();
  }
  
  /* strings */
  
  public ResourceBundle stringTableWithName(String _name, String _fwname, 
                                            String[] _langs)
  {
    if (_fwname != null && _fwname.length() > 0) {
      // TODO: should we somehow match _fwname against our pkgname and reject
      //       creation for other namespaces.
    }
    
    if (_name == null) _name = "LocalizableStrings";
    try {
      if (log.isDebugEnabled())
        log.debug("lookup string table: " + this.pkgName + "." + _name);
      
      // TODO: the resourcebundle caches information so we can't reload on the
      //       fly
      //       => maybe we should maintain the properties on our own?
      return ResourceBundle.getBundle(this.pkgName + "." + _name, 
                                      localeForLanguages(_langs),
                                      this.baseClass.getClassLoader());
    }
    catch (MissingResourceException e) {
      if (log.isDebugEnabled())
        log.debug("  did not find string table.");
      return null;
    }
  }
  
  /* class lookup */
  
  public Class lookupClass(String _name) {
    // TODO: cache lookup results?
    Class cls;

    if (log.isDebugEnabled()) {
      log.debug("lookup relative (" + this.baseClass + 
                     ") class: " + _name);
    }

    /* look for the class in a "flat" location */
    cls = NSJavaRuntime.NSClassFromString(this.pkgName + "." + _name);
    if (cls != null)
      return cls;
    
    /* check whether the component has its own package with generic name */
    cls = NSJavaRuntime.NSClassFromString
      (this.pkgName + "." + _name + ".Component");
    if (cls != null)
      return cls;

    /* check whether the component has its own package with specific name */
    cls = NSJavaRuntime.NSClassFromString
      (this.pkgName + "." + _name + "." + _name);
    if (cls != null)
      return cls;
    
    /* try to lookup name as-is */
    return NSJavaRuntime.NSClassFromString(_name);
  }
  
  /* resources */
  
  public URL urlForResourceNamed(String _name, String[] _ls) {
    // TODO: add some kind of localization?
    if (log.isDebugEnabled()) {
      log.debug("getting URL for resource: " + _name +
                     " (base=" + this.baseClass + ")");
    }
    return this.baseClass.getResource(_name);
  }
  public InputStream inputStreamForResourceNamed(String _name, String[] _ls) {
    // TODO: add some kind of localization?
    if (log.isDebugEnabled()) {
      log.debug("getting stream for resource: " + _name +
                     " (base=" + this.baseClass + ")");
    }
    return this.baseClass.getResourceAsStream(_name);
  }
  
  /* description */
  
  public void appendAttributesToDescription(StringBuffer _d) {
    super.appendAttributesToDescription(_d);
    
    _d.append(" base=" + this.baseClass);
  }
}
