package org.apache.maven.hibernate.beans;

/* ====================================================================
 *   Copyright 2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * Base class for Beans which serve as Proxy To Hibernate API
 * <br/>
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @author <a href="maven@felipeal.net">Felipe Leme</a> 
 * @version $Id$
 */
public abstract class SchemaBeanBase extends CommonOperationsBean
{

   private String properties = null;
   private String config = null;
   private boolean quiet = false;
   private boolean text = false;
   
   protected static final Log LOG = LogFactory.getLog(MappingsAggregatorBean.class);


   
   /**
    * @return
    */
   public boolean getQuiet()
   {
      return quiet;
   }

   /**
    * @return
    */
   public boolean getText()
   {
      return text;
   }
   
   /**
    * @param b
    */
   public void setQuiet(boolean b)
   {
      quiet = b;
   }

   /**
    * @param b
    */
   public void setText(boolean b)
   {
      text = b;
   }

   /**
    * @return
    */
   public String getConfig()
   {
      return config;
   }

   /**
    * @return
    */
   public String getProperties()
   {
      return properties;
   }

   /**
    * @param string
    */
   public void setConfig(String string)
   {
      config = string;
   }

   /**
    * @param string
    */
   public void setProperties(String string)
   {
      properties = string;
   }

   /**
    * 
    * Hibernate requires that 
    * Java classes (beans) are accesible on the
    * classpath. As they are not in plugin classpath
    * we have to take care about. 
    * To assure that we have them visible for plugin
    * classloader
    * we will make temporay change to context classloader
    * which will be restored when method terminates.
    * 
    */
   public void execute() throws Exception
   {

      Thread currentThread = Thread.currentThread();
      ClassLoader oldClassLoader = currentThread.getContextClassLoader();
      try
      {
  	    File [] baseDirs = getBaseDirs ();
  	    URL  [] urls = new URL [baseDirs.length];
  	    for (int i = 0; i < urls.length; i++) { 
  		    urls [i] = baseDirs [i].toURL ();
  	    }
  	
        URLClassLoader newClassLoader =
          new URLClassLoader(urls, getClass().getClassLoader());
        currentThread.setContextClassLoader(newClassLoader);
    
        Configuration cfg = getConfiguration();        
        executeSchema( cfg );

      }
      finally
      {
         currentThread.setContextClassLoader(oldClassLoader);
      }
   }

   /**
   * Method that does the real job
   */
  protected abstract void executeSchema(Configuration cfg) throws Exception;

  /**
    * 
    */
   private Configuration getConfiguration() throws HibernateException
   {
      Configuration cfg = new Configuration();
      if (getConfig() != null)
      {      	
      	 File f = new File(getConfig());
      	 LOG.debug("Hibernate Configuration File: " + f.getAbsolutePath());
         cfg.configure(f);
      }

      String[] files = getFileNames ();
      for (int i = 0; i < files.length; i++)
      {
         String filename = files[i];
         if (filename.endsWith(".jar"))
         {
            cfg.addJar(filename);
         }
         else
         {
            cfg.addFile(filename);
         }
      }
      return cfg;
   }

  private String schemaOutputFile = null;
  private String delimiter = null;



  /**
  * @return
  */
  public String getSchemaOutputFile() {
      return schemaOutputFile;
   }

  /**
  * @return
  */
  public String getOutputFile() {
      return schemaOutputFile;
   }

  /**
  * @return
  */
  public String getDelimiter() {
      return delimiter;
   }

  /**
  * @param string
  */
  public void setSchemaOutputFile(String string) {
      schemaOutputFile = string;
   }

  /**
  * @param string
  */
  public void setOutputFile(String string) {
      schemaOutputFile = string;
   }

  /**
  * @param string
  */
  public void setDelimiter(String string) {
      delimiter = string;
   }

}
