Package anvil.core.naming

Source Code of anvil.core.naming.AnyNamingContext

/*
* $Id: AnyNamingContext.java,v 1.13 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.naming;

import anvil.Log;
import anvil.database.PooledConnection;
import anvil.core.Any;
import anvil.core.AnyList;
import anvil.core.AnyMap;
import anvil.core.AnySequence;
import anvil.core.AnyUtils;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyBindingEnumeration;
import anvil.java.util.BindingEnumeration;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.ModificationItem;

/// @class NamingContext

/**
* class AnyNamingContext
*
* @author: Simo Tuokko
* @author: Jani Lehtim�ki
*/
public class AnyNamingContext extends AnyAbstractClass
{

  private ModificationItem buildItem(Any item)
  {
    if (item.isMap()) {
      AnyMap map = item.toMap();
      int modop = map.getLeft().toInt();
      Any right = map.getRight();
      if (right instanceof AnyAttribute) {
        Attribute attr = (Attribute)right.toObject();
        return new ModificationItem(modop, attr);
      }
    } else {
      if (item instanceof AnyAttribute) {
        Attribute attr = (Attribute)item.toObject();
        return new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
      }
    }
    return null;
  }
 

  public static final Any ADD = Any.create(DirContext.ADD_ATTRIBUTE);
  public static final Any REPLACE = Any.create(DirContext.REPLACE_ATTRIBUTE);
  public static final Any REMOVE = Any.create(DirContext.REMOVE_ATTRIBUTE);

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("NamingContext", AnyNamingContext.class,
    //DOC{{
    ""+
      " @class NamingContext\n" +
      " @method getName\n" +
      " @synopsis string getName()\n" +
      " @method list\n" +
      " @synopsis list list(string name)\n" +
      " @synopsis list list(list name)\n" +
      " Return list of SearchResult objects.\n" +
      "\n" +
      " Example.\n" +
      " ctx = anvil.naming.acquire('ldap_pool');\n" +
      " foreach (sr; anvil.naming.list('')) {\n" +
      "   println \"Found: \", sr.getName();\n" +
      " }\n" +
      " @method lookup\n" +
      " @synopsis object lookup(string name)\n" +
      " @synopsis object lookup(list name)\n" +
      " @method lookupLink\n" +
      " @synopsis object lookupLink(string name)\n" +
      " @synopsis object lookupLink(list name)\n" +
      " @method getNameParser\n" +
      " @synopsis NameParser getNameParser(string name)\n" +
      " @synopsis NameParser getNameParser(list name)\n" +
      " @method parse\n" +
      " @synopsis list parse(string name, string child)\n" +
      " @synopsis list parse(list name, string child)\n" +
      " @method compose\n" +
      " @synopsis list compose(string name, string prefix)\n" +
      " @synopsis list compose(list name, list prefix)\n" +
      " @method bind\n" +
      " @synopsis Context bind(string name, object value [, Attributes attributes] )\n" +
      " @synopsis Context bind(list name, object value [, Attributes attributes] )\n" +
      " @method rebind\n" +
      " @synopsis Context rebind(string name, object value [, Attributes attributes] )\n" +
      " @synopsis Context rebind(list name, object value [, Attributes attributes] )\n" +
      " @method unbind\n" +
      " @synopsis Context unbind(string name)\n" +
      " @synopsis Context unbind(list name)\n" +
      " @method destroy\n" +
      " @synopsis Context destroy(string name)\n" +
      " @synopsis Context destroy(list name)\n" +
      " @method create\n" +
      " @synopsis Context create(string name [, Attributes attributes ])\n" +
      " @synopsis Context create(list name [, Attributes attributes ])\n" +
      " @method rename\n" +
      " @synopsis Context rename(string fromname, string toname)\n" +
      " @synopsis Context rename(list fromname, list toname)\n" +
      " @method close\n" +
      " @synopsis void close()\n" +
      " @method release\n" +
      " @synopsis void release()\n" +
      " @method getAttributes\n" +
      " @synopsis Attributes getAttributes()\n" +
      " @synopsis Attributes getAttributes(string name)\n" +
      " @synopsis Attributes getAttributes(name name)\n" +
      " @synopsis Attributes getAttributes(string name, sequence attrsToReturn)\n" +
      " @synopsis Attributes getAttributes(name name, sequence attrsToReturn)\n" +
      " @method modifyAttributes\n" +
      " @synopsis Context modifyAttributes(Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(map mod, ...)\n" +
      " @synopsis Context modifyAttributes(map mod, ...)\n" +
      " @synopsis Context modifyAttributes(string name, Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(Name name, Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(string name, int modop, Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(Name name, int modop, Attributes attrs)\n" +
      " @synopsis Context modifyAttributes(string name, map modification, ...)\n" +
      " @synopsis Context modifyAttributes(Nane name, map modification, ...)\n" +
      " @param modification Mapping containing modification operation and attribute.\n" +
      "        \"modop=>Attribute\"\n" +
      " @method search\n" +
      " @synopsis list search(string name, string filter, SearchControls controls)\n" +
      " @synopsis list search(list name, string filter, SearchControls controls)\n"
    //}}DOC
    );
  static {
    NamingModule.class.getName();
  }


  private PooledConnection _connection;
  private javax.naming.Context _context;
 
 
  public AnyNamingContext(PooledConnection connection)
  {
    _connection = connection;
  }
 
 
  public AnyNamingContext(Context context)
  {
    _context = context;
 
 
 
  public anvil.script.ClassType classOf()
  {
    return __class__;
  }
 

  public Object toObject()
  {
    return getContext();
  }
 
 
  public javax.naming.Context getContext()
  {
    if (_context != null) {
      return _context;
    }
    if (_connection == null) {
      throw new RuntimeException("Context has been released");
    }
    Context ctx = (Context)_connection.getConnection();
    if (ctx == null) {
      throw new RuntimeException("Context has been released");
    }
    return ctx;
  }

 


  public BindingEnumeration enumeration()
  {
    try {
      Context ctx = getContext();
      return new ContextEnumeration(ctx.listBindings(""));
    } catch (NamingException e) {
      throw anvil.script.Context.getInstance().exception(e);
    }
  }


  /// @method getName
  /// @synopsis string getName()
  public Any m_getName(anvil.script.Context context, Any[] parameters)
  {
    Context ctx = getContext();
    try {
      return Any.create(ctx.getNameInNamespace());
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }



  /// @method list
  /// @synopsis list list(string name)
  /// @synopsis list list(list name)
  /// Return list of SearchResult objects.
  ///
  /// Example.
  /// ctx = anvil.naming.acquire('ldap_pool');
  /// foreach (sr; anvil.naming.list('')) {
  ///   println "Found: ", sr.getName();
  /// }
  public Any m_list(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "list");
    }
    Context ctx = getContext();
    try {
      NamingEnumeration enum;
      Any param = parameters[0];
      if (param instanceof AnyName) {
        enum = ctx.list((Name)param.toObject());
      } else {
        enum = ctx.list(param.toString());
      }
      AnyList list = new AnyList();
      while(enum.hasMoreElements()) {
        list.append(new AnySearchResult((NameClassPair)enum.nextElement()));
      }
      return list;
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 

  /// @method lookup
  /// @synopsis object lookup(string name)
  /// @synopsis object lookup(list name)
  public Any m_lookup(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "lookup");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      if (param instanceof AnyName) {
        return Any.create(ctx.lookup((Name)param.toObject()));
      } else {
        return Any.create(ctx.lookup(param.toString()));
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 

  /// @method lookupLink
  /// @synopsis object lookupLink(string name)
  /// @synopsis object lookupLink(list name)
  public Any m_lookupLink(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "lookupLink");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      if (param instanceof AnyName) {
        return Any.create(ctx.lookupLink((Name)param.toObject()));
      } else {
        return Any.create(ctx.lookupLink(param.toString()));
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 

  /// @method getNameParser
  /// @synopsis NameParser getNameParser(string name)
  /// @synopsis NameParser getNameParser(list name)
  public Any m_getNameParser(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "getNameParser");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      if (param instanceof AnyName) {
        return new AnyNameParser(ctx.getNameParser((Name)param.toObject()));
      } else {
        return new AnyNameParser(ctx.getNameParser(param.toString()));
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }



  /// @method parse
  /// @synopsis list parse(string name, string child)
  /// @synopsis list parse(list name, string child)
  public Any m_parse(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "parse");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      String child = parameters[1].toString();
      AnyList list;
      if (param instanceof AnyName) {
        return new AnyName(ctx.getNameParser(
          (Name)param.toObject()).parse(child));
      } else {
        return new AnyName(ctx.getNameParser(
          param.toString()).parse(child));
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 

  /// @method compose
  /// @synopsis list compose(string name, string prefix)
  /// @synopsis list compose(list name, list prefix)
  public Any m_compose(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "compose");
    }
    Context ctx = getContext();
    try {
      String prefix = parameters[1].toString();
      Any param1 = parameters[0];
      Any param2 = parameters[1];
      if (param1 instanceof AnyName && param2 instanceof AnyName) {
        return new AnyName(ctx.composeName(
          (Name)param1.toObject(), (Name)param2.toObject()));
      } else {
        return Any.create(ctx.composeName(param1.toString(), param2.toString()));
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }


  /// @method bind
  /// @synopsis Context bind(string name, object value [, Attributes attributes] )
  /// @synopsis Context bind(list name, object value [, Attributes attributes] )
  public Any m_bind(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "bind");
    }
    Context ctx = getContext();

    Any param;
    Object value = parameters[1].toObject();
    Attributes attrs = null;

    if (parameters.length > 2) {
      param = parameters[2];
      if (param instanceof AnyAttributes) {
        attrs = (Attributes)param.toObject();
      }
    }

    try {
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          dirctx.bind((Name)param.toObject(), value, attrs);
        } else {
          dirctx.bind(param.toString(), value, attrs);
        }
        return this;
      }

      param = parameters[0];
      if (param instanceof AnyName) {
        ctx.bind((Name)param.toObject(), value);
      } else {
        ctx.bind(param.toString(), value);
      }
     
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return this;
 
 
  /// @method rebind
  /// @synopsis Context rebind(string name, object value [, Attributes attributes] )
  /// @synopsis Context rebind(list name, object value [, Attributes attributes] )
  public Any m_rebind(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "rebind");
    }
    Context ctx = getContext();

    Any param;
    Object value = parameters[1].toObject();
    Attributes attrs = null;

    if (parameters.length > 2) {
      param = parameters[2];
      if (param instanceof AnyAttributes) {
        attrs = (Attributes)param.toObject();
      }
    }

    try {
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          dirctx.rebind((Name)param.toObject(), value, attrs);
        } else {
          dirctx.rebind(param.toString(), value, attrs);
        }
        return this;
      }

      param = parameters[0];
      if (param instanceof AnyName) {
        ctx.rebind((Name)param.toObject(), value);
      } else {
        ctx.rebind(param.toString(), value);
      }
     
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return this;
 
 
 
  /// @method unbind
  /// @synopsis Context unbind(string name)
  /// @synopsis Context unbind(list name)
  public Any m_unbind(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "unbind");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      if (param instanceof AnyName) {
        ctx.unbind((Name)param.toObject());
      } else {
        ctx.unbind(param.toString());
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return this;
  }
 

 
  /// @method destroy
  /// @synopsis Context destroy(string name)
  /// @synopsis Context destroy(list name)
  public Any m_destroy(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "destroy");
    }
    Context ctx = getContext();
    try {
      Any param = parameters[0];
      if (param instanceof AnyName) {
        ctx.destroySubcontext((Name)param.toObject());
      } else {
        ctx.destroySubcontext(param.toString());
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return this;
  }
 

  /// @method create
  /// @synopsis Context create(string name [, Attributes attributes ])
  /// @synopsis Context create(list name [, Attributes attributes ])
  public Any m_create(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "create");
    }
    Context ctx = getContext();

    Any param;
    Attributes attrs = null;

    if (parameters.length > 1) {
      param = parameters[1];
      if (param instanceof AnyAttributes) {
        attrs = (Attributes)param.toObject();
      }
    }

    try {
   
      if ((attrs != null) && (ctx instanceof DirContext)) {
        DirContext dirctx = (DirContext)ctx;
        param = parameters[0];
        if (param instanceof AnyName) {
          return new AnyNamingContext(
            dirctx.createSubcontext((Name)param.toObject(), attrs));
        } else {
          return new AnyNamingContext(
            dirctx.createSubcontext(param.toString(), attrs));
        }
      }

      param = parameters[0];
      if (param instanceof AnyName) {
        return new AnyNamingContext(
          ctx.createSubcontext((Name)param.toObject()));
      } else {
        return new AnyNamingContext(
          ctx.createSubcontext(param.toString()));
      }
     
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
   
 
  /// @method rename
  /// @synopsis Context rename(string fromname, string toname)
  /// @synopsis Context rename(list fromname, list toname)
  public Any m_rename(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 2) {
      throw parametersMissing(context, "rename");
    }
    Context ctx = getContext();
    try {
      Any param1 = parameters[0];
      Any param2 = parameters[1];
      if (param1 instanceof AnyName && param2 instanceof AnyName) {
        ctx.rebind((Name)param1.toObject(), (Name)param2.toObject());
      } else if (parameters[0].isString()) {
        ctx.rebind(param1.toString(), param2.toString());
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return this;
 
 

  /// @method close
  /// @synopsis void close()
  public Any m_close(anvil.script.Context context, Any[] parameters)
  {
    Context ctx = getContext();
    try {
      ctx.close();
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return TRUE;
  }


  /// @method release
  /// @synopsis void release()
  public Any m_release(anvil.script.Context context, Any[] parameters)
  {
    if (_connection != null) {
      _connection.release();
    }
    _connection = null;
    _context = null;
    return TRUE;
  }


  /// @method getAttributes
  /// @synopsis Attributes getAttributes()
  /// @synopsis Attributes getAttributes(string name)
  /// @synopsis Attributes getAttributes(name name)
  /// @synopsis Attributes getAttributes(string name, sequence attrsToReturn)
  /// @synopsis Attributes getAttributes(name name, sequence attrsToReturn)
  public Any m_getAttributes(anvil.script.Context context, Any[] parameters)
  {
    Context ctx = getContext();
    if (!(ctx instanceof DirContext)) {
      return UNDEFINED;
    }
    DirContext dirctx = (DirContext)ctx;
   
    int nparams = parameters.length;
    if (nparams > 2) {
      nparams = 2;
    }
   
    try {
      switch(nparams) {
      case 2:
        {
          Any param = parameters[0];
          String[] attributes = AnyUtils.toStringArray(parameters[1]);
          if (param instanceof AnyName) {
            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject(), attributes));
          } else {
            return new AnyAttributes(dirctx.getAttributes(parameters[0].toString(), attributes));
          }
        }

      case 1:
        {
          Any param = parameters[0];
          if (param instanceof AnyName) {
            return new AnyAttributes(dirctx.getAttributes((Name)param.toObject()));
          } else {
            return new AnyAttributes(dirctx.getAttributes(param.toString()));
          }
        }
       
      case 0:
        return new AnyAttributes(dirctx.getAttributes(""));
      }
      throw context.BadParameter("Parameter combination not supported");
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 
 


  /// @method modifyAttributes
  /// @synopsis Context modifyAttributes(Attributes attrs)
  /// @synopsis Context modifyAttributes(Attributes attrs)
  /// @synopsis Context modifyAttributes(map mod, ...)
  /// @synopsis Context modifyAttributes(map mod, ...)
  /// @synopsis Context modifyAttributes(string name, Attributes attrs)
  /// @synopsis Context modifyAttributes(Name name, Attributes attrs)
  /// @synopsis Context modifyAttributes(string name, int modop, Attributes attrs)
  /// @synopsis Context modifyAttributes(Name name, int modop, Attributes attrs)
  /// @synopsis Context modifyAttributes(string name, map modification, ...)
  /// @synopsis Context modifyAttributes(Nane name, map modification, ...)
  /// @param modification Mapping containing modification operation and attribute.
  ///        "modop=>Attribute"
  public Any m_modifyAttributes(anvil.script.Context context, Any[] parameters)
  {
    int n = parameters.length;
    if (parameters.length < 1) {
      throw parametersMissing(context, "modifyAttributes");
    }
   
    int i = 0;
    Any param;

    Name name = null;
    String sname = null;
    Attributes attributes = null;
    ModificationItem[] modifications = null;
    int modop = DirContext.REPLACE_ATTRIBUTE;
   
    for(; i<n; i++) {
      param = parameters[i];
      if (param instanceof AnyName) {
        name = (Name)param.toObject();
      } else if (param instanceof AnyAttributes) {
        attributes = (Attributes)param.toObject();
      } else if (param.isString()) {
        sname = param.toString();
      } else if (param.isInt()) {
        modop = param.toInt();
      } else if (param.isMap()) {
        break;
      } else {
        break;
      }
    }

    if (name == null && sname == null) {
      sname = "";
    }
   
    if (i<n) {
      int size = n - i;
      modifications = new ModificationItem[n - i];
      int c = 0;
      for(i=0; i<n; i++) {
        ModificationItem item = buildItem(parameters[i]);
        if (item != null) {
          modifications[c++] = item;
        }
      }
      if (c < size) {
        ModificationItem[] newmodifications = new ModificationItem[c];
        System.arraycopy(modifications, 0, newmodifications, 0, c);
        modifications = newmodifications;
      }
    }

    Context ctx = getContext();
    if (!(ctx instanceof DirContext)) {
      return this;
    }
    DirContext dirctx = (DirContext)ctx;
   

    try {
      if (attributes != null) {
        if (name != null) {
          dirctx.modifyAttributes(name, modop, attributes);
        } else {
          dirctx.modifyAttributes(sname, modop, attributes);
        }
      } else if (modifications != null) {
        if (name != null) {
          dirctx.modifyAttributes(name, modifications);
        } else {
          dirctx.modifyAttributes(sname, modifications);
        }
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
   
    return this;
  }
   

  /// @method search
  /// @synopsis list search(string name, string filter, SearchControls controls)
  /// @synopsis list search(list name, string filter, SearchControls controls)
  public Any m_search(anvil.script.Context context, Any[] parameters)

  {
    if (parameters.length < 3) {
      throw parametersMissing(context, "search");
    }

    Context ctx = getContext();
    if (!(ctx instanceof DirContext)) {
      return UNDEFINED;
    }
    DirContext dirctx = (DirContext)ctx;
   
    int nparams = parameters.length;
    try {
      String filter = parameters[1].toString();
      SearchControls controls = null;
      Any param = parameters[2];
      if (param instanceof AnySearchControls) {
        controls = (SearchControls)param.toObject();
      } else {
        throw context.BadParameter("Excepted SearchControls as third parameter");
      }
      NamingEnumeration enum;
      param = parameters[0];
      if (param instanceof AnyName) {
        Name name = (Name)param.toObject();
        enum = dirctx.search(name, filter, controls);
      } else {
        String name = param.toString();
        enum = dirctx.search(name, filter, controls);
      }
     
      AnyList result = new AnyList();
      System.err.println("--doing list");
      while(enum.hasMoreElements()) {
        System.err.println("--found");
        result.append(new AnySearchResult((NameClassPair)enum.nextElement()));
      }
      return result;

    } catch (NamingException e) {
      throw context.exception(e);
    }
  }
 
 

  public class ContextEnumeration implements BindingEnumeration
  {
    private NamingEnumeration _enum;
    private int _index = 0;
   
    public ContextEnumeration(NamingEnumeration enum)
    {
      _enum = enum;
    }
   
    public boolean hasMoreElements()
    {
      return _enum.hasMoreElements();
    }

    public Object nextKey()
    {
      return Any.create(_index);
    }

    public Object nextElement()
    {
      try {
        NameClassPair pair = (NameClassPair)_enum.nextElement();
        _index++;
        return new AnySearchResult(pair);
      } catch (Exception e) {
        throw anvil.script.Context.getInstance().exception(e);
      }
    }
  }

}
TOP

Related Classes of anvil.core.naming.AnyNamingContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.