Package org.apache.muse.core.platform.osgi.descriptor

Source Code of org.apache.muse.core.platform.osgi.descriptor.OSGiDeploymentDescriptor

/*=============================================================================*
*  Copyright 2006 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.muse.core.platform.osgi.descriptor;

import java.util.ArrayList;
import java.util.Collection;

import javax.xml.namespace.QName;

import org.apache.muse.core.Environment;
import org.apache.muse.core.descriptor.DeploymentDescriptor;
import org.apache.muse.core.descriptor.DescriptorConstants;
import org.apache.muse.core.descriptor.PersistenceDefinition;
import org.apache.muse.core.descriptor.ResourceDefinition;
import org.apache.muse.core.descriptor.ResourceDescriptor;
import org.apache.muse.core.descriptor.RouterDefinition;
import org.apache.muse.core.descriptor.RouterDescriptor;
import org.apache.muse.core.descriptor.SerializerDefinition;
import org.apache.muse.core.descriptor.SerializerDescriptor;
import org.apache.muse.core.descriptor.SimpleDeploymentDescriptor;
import org.apache.muse.core.descriptor.SimpleRouterDescriptor;
import org.apache.muse.core.descriptor.SimpleSerializerDescriptor;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.osgi.framework.Bundle;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
*
* OSGiDeploymentDescriptor is an implementation of <code>DeploymentDescriptor</code>
* specific to the OSGi environment
*
* @see DeploymentDescriptor
* @author Joel Hawkins (joelh)
*
*/

public class OSGiDeploymentDescriptor implements DeploymentDescriptor {
 
  //
  // Used to lookup all exception messages
  //
  private static Messages _MESSAGES = MessagesFactory
      .get(SimpleDeploymentDescriptor.class);

  //
  // Stores info on the concrete implementations of the resource type.
  // There will be at least one resource-instance in the factory.
  //
  private Collection _resourceDefinitions = null;

  //
  // The data used to instantiate the router
  //
  private static RouterDefinition _routerDefinition = null;
  private RouterDefinition _localRouterDefinition = null;

  private Collection _serializerDefinitions = null;
  private PersistenceDefinition _localPersistenceDefinition;

  /**
   * the Bundle the current deployment descriptor was loaded from
   */
  protected Bundle bundle;

  /**
   * setter for the decriptor Bundle
   * @param bundle
   */
  public void setBundle(Bundle bundle) {
    this.bundle = bundle;
  }

  /**
   * Loads the context paths from the muse.xml descriptor
   * @param xml
   * @param env
   * @return the context paths contianed by the descriptor
   * @throws SoapFault
   *
   * @see DeploymentDescriptor#loadContextPaths(Document, Environment)
   */
  public Collection loadContextPaths(Document xml, Environment env)
      throws SoapFault {
    if (xml == null)
      throw new NullPointerException(_MESSAGES
          .get("NullDescriptorDocument"));

    Element root = XmlUtils.getElement(xml, DescriptorConstants.MUSE_QNAME);

    Element[] resourceXML = XmlUtils.getElements(root,
        DescriptorConstants.RESOURCE_TYPE_QNAME);

    ArrayList contextPaths = new ArrayList(resourceXML.length);

    QName qname = DescriptorConstants.CONTEXT_PATH_QNAME;
    for (int n = 0; n < resourceXML.length; ++n) {
      String path = XmlUtils.getElementText(resourceXML[n], qname);

      if (path == null)
        continue;

      contextPaths.add(path);
    }
    return contextPaths;
  }
 
 
  public String getWsdlPathForContext(Document xml, String context) throws SoapFault {
    if (xml == null)
      throw new NullPointerException(_MESSAGES
          .get("NullDescriptorDocument"));

    if (context == null) return null;

    Element root = XmlUtils.getElement(xml, DescriptorConstants.MUSE_QNAME);

    Element[] resourceXML = XmlUtils.getElements(root,
        DescriptorConstants.RESOURCE_TYPE_QNAME);
    if(resourceXML == null) return null;
    QName qname = DescriptorConstants.CONTEXT_PATH_QNAME;
    for(int i=0;i<resourceXML.length;i++){
      String path = XmlUtils.getElementText(resourceXML[i], qname);
      if(context.equals(path)){
        Element wsdlElement = XmlUtils.getElement(resourceXML[i], DescriptorConstants.WSDL_QNAME);
        if(wsdlElement != null)
          return XmlUtils.getElementText(wsdlElement,DescriptorConstants.WSDL_FILE_QNAME);
        break;
      }
    }
    return null;
  }

  /**
   * loads the descriptor from the muse.xml document.
   * @see DeploymentDescriptor#load(Document, Environment)
   */
  public void load(Document xml, Environment environment) throws SoapFault {
    if (xml == null)
      throw new NullPointerException(_MESSAGES
          .get("NullDescriptorDocument"));

    Element root = XmlUtils.getElement(xml, DescriptorConstants.MUSE_QNAME);

    //
    // make sure we're starting with a valid DD (<muse/>)
    //
    // FIXME: if (root == null)

    //
    // any user-defined serializers must be loaded before we try to
    // load the operations/handlers
    //
    _serializerDefinitions = createSerializerDefinitions(root, environment);

    //
    // one router used to map all resource types...
    //
    // Ignored - OSGi Router mandated by default plugin
    RouterDefinition localRouter;
    if (_routerDefinition == null)
      _localRouterDefinition = _routerDefinition = createRouterDefinition(root, environment);
    else
      _localRouterDefinition = createRouterDefinition(root, environment);
   
    //
    // there will be one definition for each resource type
    //
    _resourceDefinitions = createResourceDefinitions(root, environment);

    if (_localRouterDefinition != null)
      _localRouterDefinition.setResourceDefinitions(_resourceDefinitions);
  }

  /**
   * creates the set of resource definitions for a deployment descriptor.
   * @param xml
   * @param env
   * @return a collection of <code>OSGiResourceDescriptor</code> objects
   * @throws SoapFault
   *
   * @see OSGiResourceDescriptor
   */
  protected Collection createResourceDefinitions(Element xml, Environment env)
      throws SoapFault {
    Element[] resourceXML = XmlUtils.getElements(xml,
        DescriptorConstants.RESOURCE_TYPE_QNAME);

    Collection definitions = new ArrayList(resourceXML.length);

    for (int n = 0; n < resourceXML.length; ++n) {
      OSGiResourceDescriptor rd = (OSGiResourceDescriptor) createResourceDescriptor();
      rd.setBundle(bundle);
      rd.load(resourceXML[n], env);

      ResourceDefinition definition = rd.getResourceDefinition();
      definitions.add(definition);
    }

    return definitions;
  }

  /**
   * factory method for creating ResourceDescriptors
   * @return an <code>OSGiResourceDescriptor</code> instance
   *
   * @see OSGiResourceDescriptor
   */
  protected ResourceDescriptor createResourceDescriptor() {
    return new OSGiResourceDescriptor();
  }

  /**
   * factory method for creating RouterDescriptors
   * @return a <code>SimpleRouterDescriptor</code> instance
   *
   * @see SimpleRouterDescriptor
   */
  protected RouterDescriptor createRouterDescriptor() {
    return new SimpleRouterDescriptor();
  }

  /** factory method for creating RouterDefinitions
   *
   * @param xml
   * @param env
   * @return a RouterDefinition instance
   * @throws SoapFault
   */
  protected RouterDefinition createRouterDefinition(Element xml,
      Environment env) throws SoapFault {
    Element routerXML = XmlUtils.getElement(xml,
        DescriptorConstants.ROUTER_QNAME);

    RouterDescriptor rd = createRouterDescriptor();
    rd.load(routerXML, env);

    return rd.getRouterDefinition();
  }

  protected SerializerDescriptor createSerializerDescriptor() {
    return new SimpleSerializerDescriptor();
  }

  public Collection createSerializerDefinitions(Element xml, Environment env)
      throws SoapFault {
    QName qname = DescriptorConstants.CUSTOM_SERIALIZER_QNAME;
    Element[] serializerXML = XmlUtils.getElements(xml, qname);

    Collection definitions = new ArrayList(serializerXML.length);

    for (int n = 0; n < serializerXML.length; ++n) {
      SerializerDescriptor sd = createSerializerDescriptor();
      sd.load(serializerXML[n], env);

      SerializerDefinition definition = sd.getSerializerDefinition();
      definitions.add(definition);
    }

    return definitions;
  }

  public Collection getSerializerDefinitions() {
    return _serializerDefinitions;
  }

  public Collection getResourceDefinitions() {
    return _resourceDefinitions;
  }

  public RouterDefinition getRouterDefinition() {
    return _localRouterDefinition;
  }

}
TOP

Related Classes of org.apache.muse.core.platform.osgi.descriptor.OSGiDeploymentDescriptor

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.