Package org.apache.ws.metadata

Source Code of org.apache.ws.metadata.MetadataEntry

/*=============================================================================*
*  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.
*=============================================================================*/
package org.apache.ws.metadata;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.addressing.v2004_08.AddressingConstants;
import org.apache.xmlbeans.XmlObject;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2004.x09.mex.LocationDocument;
import org.xmlsoap.schemas.ws.x2004.x09.mex.MetadataReferenceDocument;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
* A WS-MEX metadata entry.
*/
public class MetadataEntry
{
   private static final Log LOG = LogFactory.getLog( MetadataEntry.class );
   private String           m_dialect;
   private String           m_identifier;
   private String           m_location;
   private XmlObject        m_metadata;

   /**
    * DOCUMENT_ME
    *
    * @param dialect DOCUMENT_ME
    */
   public void setDialect( String dialect )
   {
      m_dialect = dialect;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String getDialect(  )
   {
      return m_dialect;
   }

   /**
    * DOCUMENT_ME
    *
    * @param identifier DOCUMENT_ME
    */
   public void setIdentifier( String identifier )
   {
      m_identifier = identifier;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String getIdentifier(  )
   {
      return m_identifier;
   }

   /**
    * DOCUMENT_ME
    *
    * @param location DOCUMENT_ME
    */
   public void setLocation( String location )
   {
      m_location = location;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public String getLocation(  )
   {
      return m_location;
   }

   /**
    * DOCUMENT_ME
    *
    * @return DOCUMENT_ME
    */
   public synchronized XmlObject getMetadata(  )
   {
      if ( m_metadata == null )
      {
         initMetadata(  );
      }

      return m_metadata;
   }

   private MetadataReferenceDocument createMetadataReferenceDocument( EndpointReferenceType epr )
   {
      MetadataReferenceDocument metadataReferenceDoc = MetadataReferenceDocument.Factory.newInstance(  );
      metadataReferenceDoc.setMetadataReference( epr );
      return metadataReferenceDoc;
   }

   /**
    * This method will return either a MetadataReferenceDocument, LocationDocument or an XmlObject which has the
    * metadata document parsed into it.
    * <p/>
    * The operation ALWAYS avoids loading the file if the metadata contains a MetadataReference. The operation ALWAYS
    * avoids loading the file if the metadata contains a Location which is a standard Http URL.
    * <p/>
    * The operation WILL load the file if it is a file or a file URL. The operation WILL load the file if it is located
    * in the classpath.
    * <p/>
    * Note: The "load" scenarios are handled last, and if the documents are un-loadable, null will be returned
    */
   private void initMetadata(  )
   {
      //check for MetadataReference...if its there, we're finished.
      XmlObject metadata = null;

      //determine if its a valid URL
      if ( m_location != null )
      {
         try
         {
            URL url = new URL( m_location );

            //if we get here then is it a file url??? if so load it
            if ( url.getProtocol(  ).equals( "file" ) )
            {
               try
               {
                  metadata = XmlObject.Factory.parse( url );
               }
               catch ( Exception e )
               {
                  LOG.fatal( "*********Unable to load file from file url: " + url + " CAUSE:", e );
               }
            }
            else //if not then return a Location...let them do it....
            {
               LocationDocument locationDocument = LocationDocument.Factory.newInstance(  );
               locationDocument.setLocation( m_location );
               metadata = locationDocument;
            }
         }
         catch ( MalformedURLException e )
         {
            //not valid..is it a file withoud url prefix? ...load it
            File f = new File( m_location );
            if ( f.exists(  ) )
            {
               try
               {
                  metadata = XmlObject.Factory.parse( f );
               }
               catch ( Exception e1 )
               {
                  LOG.fatal( "*********Unable to load file: " + f + " CAUSE:", e );
               }
            }
            else //check classpath
            {
               URL resource = this.getClass(  ).getClassLoader(  ).getResource( m_location );
               try
               {
                  metadata = XmlObject.Factory.parse( resource );
               }
               catch ( Exception e1 )
               {
                  LOG.fatal( "*********Unable to load file: " + resource + " from classpath." + " CAUSE:", e );
               }
            }
         }

         metadata = wrapEndpointReferenceType( metadata );
      }

      m_metadata = metadata;
   }

   private XmlObject wrapEndpointReferenceType( XmlObject metadata )
   {
      if ( m_dialect.equals( AddressingConstants.NSURI_ADDRESSING_SCHEMA ) )
      {
         if ( metadata instanceof EndpointReferenceDocument )
         {
            EndpointReferenceDocument metadataReferenceDoc = (EndpointReferenceDocument) metadata;
            metadata = createMetadataReferenceDocument( metadataReferenceDoc.getEndpointReference(  ) );
         }
         else
         {
            LOG.error( "Error resolving MetadataEntry - dialect was " + m_dialect + ", but document at location "
                       + m_location + " was not of type {" + m_dialect + "}EndpointReference" );
         }
      }
      else if ( m_dialect.equals( org.apache.ws.addressing.v2003_03.AddressingConstants.NSURI_ADDRESSING_SCHEMA ) )
      {
         if ( metadata instanceof org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceDocument )
         {
            org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceDocument metadataReferenceDoc      =
               (org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceDocument) metadata;
            XmlBeansEndpointReference                                             xmlBeansEndpointReference =
               new XmlBeansEndpointReference( metadataReferenceDoc.getEndpointReference(  ) );
            EndpointReferenceType                                                 epr =
               (EndpointReferenceType) xmlBeansEndpointReference.getXmlObject( AddressingConstants.NSURI_ADDRESSING_SCHEMA );
            metadata = createMetadataReferenceDocument( epr );
         }
         else
         {
            LOG.error( "Error resolving MetadataEntry - dialect was " + m_dialect + ", but document at location "
                       + m_location + " was not of type {" + m_dialect + "}EndpointReference" );
         }
      }

      return metadata;
   }
}
TOP

Related Classes of org.apache.ws.metadata.MetadataEntry

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.