/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.net.axis;

import org.jboss.axis.AxisEngine;
import org.jboss.axis.ConfigurationException;
import org.jboss.axis.configuration.FileProvider;
import org.jboss.axis.deployment.wsdd.WSDDGlobalConfiguration;
import org.jboss.axis.utils.XMLUtils;

import java.io.InputStream;
import java.net.URL;

/**
 * <p>
 * A <code>FileProvider</code> that sits on a given URL and
 * that hosts classloader-aware deployment information.
 * </p>
 * @author <a href="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
 * @created 28. September 2001
 * @version $Revision: 1.4.2.5 $
 */

public class XMLResourceProvider extends FileProvider
{

   //
   // Attributes
   //

   /** the original resource that we host */
   final protected URL resource;

   /** input stream cache */
   protected InputStream is;

   /** the classloader that is used to interpret the deployment */
   protected ClassLoader contextLoader;
   
   //
   // Constructors
   //

   /**
    * construct a new XmlResourceProvider
    * @param resource url pointing to the deployment descriptor
    */
   public XMLResourceProvider(URL resource, ClassLoader loader)
   {
      super((InputStream)null);
      this.contextLoader = loader;
      this.resource = resource;
   }

   //
   // Public API
   //

   /** override input stream setter to sync protected cache */
   public void setInputStream(InputStream stream)
   {
      super.setInputStream(stream);
      is = stream;
   }

   /** configures the given AxisEngine with the given descriptor */
   public void configureEngine(AxisEngine engine) throws ConfigurationException
   {
      buildDeployment().configureEngine(engine);
      engine.refreshGlobalOptions();
   }

   /** constructs a new deployment */
   public synchronized Deployment buildDeployment()
           throws ConfigurationException
   {
      if (getDeployment() == null)
      {
         try
         {
            if (is == null)
            {
               setInputStream(resource.openStream());
            }

            setDeployment(Deployment.makeSafeDeployment(XMLUtils.newDocument(is).getDocumentElement(),
                    contextLoader));

            setInputStream(null);

            if (getDeployment().getGlobalConfiguration() == null)
            {
               WSDDGlobalConfiguration config = new WSDDGlobalConfiguration();
               config.setOptionsHashtable(new java.util.Hashtable());
               getDeployment().setGlobalConfiguration(config);
            }

         }
         catch (Exception e)
         {
            throw new ConfigurationException(e);
         }
      }
      return getMyDeployment();
   }

   /** returns out special deployment */
   public Deployment getMyDeployment()
   {
      return (Deployment)getDeployment();
   }

   /** not supported, yet. Should we use http-push or what? */
   public void writeEngineConfig(AxisEngine engine)
   {
      // NOOP
   }

}
