package org.dbxml.core.objects;

/*
 * dbXML License, Version 1.0
 *
 *
 * Copyright (c) 1999-2001 The dbXML Group, L.L.C.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        The dbXML Group (http://www.dbxml.com/)."
 *    Alternately, this acknowledgment may appear in the software
 *    itself, if and wherever such third-party acknowledgments normally
 *    appear.
 *
 * 4. The names "dbXML" and "The dbXML Group" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    info@dbxml.com.
 *
 * 5. Products derived from this software may not be called "dbXML",
 *    nor may "dbXML" appear in their name, without prior written
 *    permission of The dbXML Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE DBXML GROUP OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: SimpleReflectable.java,v 1.18 2001/05/23 00:35:12 tom Exp $
 */

import org.dbxml.server.*;
import org.dbxml.core.*;
import org.dbxml.xml.dom.*;
import org.dbxml.server.util.*;

import java.io.*;
import java.util.*;

import org.w3c.dom.*;

/**
 * SimpleReflectable
 */

public class SimpleReflectable extends SimpleConfigurable implements Reflectable, Poolable {
   private static final String NAME = "name";
   private static final String PACKAGE = "package";
   private static final String CLASS = "class";
   private static final String METHOD = "method";
   private static final String PARAM = "param";
   private static final String TYPE = "type";
   private static final String DEFAULT = "default";

   private static Map metaCache = new HashMap();

   private Document intf = null;
   protected ObjectPool pool = null;
   protected Reflector meta = null;
   protected String name = null;

   public SimpleReflectable() {
      try {
         Class c = this.getClass();
         meta = (Reflector)metaCache.get(c);
         if ( meta == null ) {
            meta = new Reflector();
            reflect(this.getClass());
            metaCache.put(c, meta);
         }
      }
      catch ( Exception e ) {
         org.dbxml.Debug.printStackTrace(e);
      }
   }

   private void reflect(Class c) throws Exception {
      // Reflect the parents first.
      // Short-curcuit at SimpleReflectable
      if ( c != SimpleReflectable.class )
         reflect(c.getSuperclass());
      meta.reflect(c);
   }

   private String trimClassName(String name) {
      int i = name.lastIndexOf('.');
      if ( i != -1 )
         name = name.substring(i+1);
      return name;
   }

   public void setConfig(Configuration config) throws dbXMLException {
      super.setConfig(config);
      name = config.getAttribute(NAME);
   }

   public Reflector getReflector() {
      return meta;
   }

   public void setPool(ObjectPool pool) {
      this.pool = pool;
   }

   public void reclaim() {
      if ( pool != null )
         pool.putObject(this);
   }

   public String getName() {
      return name;
   }

   public Document queryInterface() {
      if ( intf == null ) {
         intf = new DocumentImpl();
         String[] methods = meta.listMethods();
         String cname;
         if ( name != null )
            cname = name;
         else
            cname = trimClassName(this.getClass().getName());

         Element root = intf.createElement(CLASS);
         root.setAttribute(NAME, cname);
         root.setAttribute(PACKAGE, this.getClass().getPackage().getName().replace('.', '/'));
         intf.appendChild(root);

         for ( int i = 0; i < methods.length; i++ ) {
            String type = Types.typeName(meta.getReturnType(methods[i]));
            Element method = intf.createElement(METHOD);
            method.setAttribute(NAME, methods[i]);
            method.setAttribute(TYPE, type);
            root.appendChild(method);

            String[] params = meta.listMethodParams(methods[i]);
            for ( int j = 0; params != null && j < params.length; j++ ) {
               type = Types.typeName(meta.getParamType(methods[i], params[j]));
               String defValue = meta.getParamDefault(methods[i], params[j]);
               if ( meta.isParamArray(methods[i], params[j]) )
                  type = type + "[]";
               Element param = intf.createElement(PARAM);
               param.setAttribute(NAME, params[j]);
               param.setAttribute(TYPE, type);
               if ( defValue != null )
                  param.setAttribute(DEFAULT, defValue);
               method.appendChild(param);
            }
         }
      }
      return intf;
   }
}



