Package org.apache.wsdm.interop.wcm.impl

Source Code of org.apache.wsdm.interop.wcm.impl.WsImpl

/*
* Created on May 23, 2005
*
*/
package org.apache.wsdm.interop.wcm.impl;

import java.util.Observer;

import javax.xml.namespace.QName;

import org.apache.ws.addressing.EndpointReference;
import org.apache.ws.addressing.XmlBeansEndpointReference;
import org.apache.ws.addressing.v2004_08_10.AddressingConstants;
import org.apache.ws.muws.interop.client.FaultException;
import org.apache.ws.muws.interop.client.ResourceStub;
import org.apache.ws.muws.v1_0.MuwsConstants;
import org.apache.ws.muws.v1_0.capability.OperationalStatusCapability;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.wsdm.interop.wcm.model.IWs;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlStringImpl;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.OperationalStatusDocument;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.OperationalStatusPropertiesType;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.RelationshipParticipantType;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.impl.OperationalStatusDocumentImpl;
import org.oasisOpen.docs.wsdm.x2004.x12.muws.wsdmMuwsPart2.impl.RelationshipTypeImpl;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.SetResourcePropertiesDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.UpdateType;
import org.wsdmdemo.service.weatherClientConfig.WeatherStationReferenceDocument;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.AttributedURI;
import org.xmlsoap.schemas.ws.x2004.x08.addressing.EndpointReferenceType;


/**
* @author wire
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class WsImpl implements IWs {
  int status;
  String name;
  double temp;
  private EndpointReference wsEpr;
  private static final QName weatherStationNameQname= new QName("http://wsdmdemo.org/service/weather-station", "Name", "tns");
  private static final QName STATUS_TOPIC_QNAME=new QName(MuwsConstants.NSURI_MUWS_PART2_TOPICS,OperationalStatusCapability.TOPIC_NAME);
  private static final QName STATUS_QNAME=new QName("http://docs.oasis-open.org/wsdm/2004/12/muws/wsdm-muws-part2.xsd","OperationalStatus");
  private static final QName PRICE_QNAME= new QName("http://wsdmdemo.org/service/weather-station","Price");

  private String listenerURL;
  //private QName topicQName;
  private Observer observer;

  public WsImpl(RelationshipTypeImpl relationship,String listenerURL,Observer observer){
    this.listenerURL=listenerURL;
    //this.topicQName=topicQName;
    this.observer=observer;
    RelationshipParticipantType participant = relationship.getParticipantArray(1);
    EndpointReferenceType eprt = participant.getManageabilityEndpointReferenceArray(0);
    AttributedURI eprtAddr = eprt.getAddress();
    String addr = eprtAddr.getStringValue();
    addr=addr.replaceAll("192\\.168\\.0\\.20","12\\.35\\.246\\.160");
    eprtAddr.setStringValue(addr);
    wsEpr=(EndpointReference)new XmlBeansEndpointReference(eprt);
    getNameFromEpr();   
    try {
      requestNotificationFromEpr();
    } catch (FaultException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 
  /**
   * @throws FaultException
   *
   */
  private void requestNotificationFromEpr() throws FaultException {
    ResourceStub resource = new ResourceStub( wsEpr );
    resource.addObserver(observer);
    resource.subscribe(listenerURL,STATUS_TOPIC_QNAME);
    resource.deleteObserver(observer);
  }

  /**
   * @throws FaultException
   *
   */
  private void getNameFromEpr() {
   
    ResourceStub stub = new ResourceStub(wsEpr);
    stub.addObserver(observer);   
    XmlObject[] wsNameArry;
    try {
      wsNameArry = stub.getResourceProperty(weatherStationNameQname);
    } catch (FaultException e) {
      this.name="Unknown";
      return;
    }
   catch (RuntimeException e) {
    this.name="Unknown";
    return;
  } finally {
    stub.deleteObserver(observer);
  }
    XmlStringImpl wsName = (XmlStringImpl)wsNameArry[0];
    this.name=wsName.getStringValue();
    System.out.println();
  }

  /**
   * @param status
   * @param name
   * @param backup
   */
  public WsImpl(int status, String name, IWs backup) {
    super();
    this.status = status;
    this.name = name;
  }
 
  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#setStatus(int)
   */
  public void setStatus(int status) {
    this.status=status;
  }

  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#getStatus()
   */
  public int getStatus() {
    // TODO Auto-generated method stub
    return status;
  }

  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#setName(java.lang.String)
   */
  public void setName(String name) {
    this.name=name;

  }

  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#getName()
   */
  public String getName() {

    return name;
  }


  public String toString() {
   
    return "WS: "+getName()+" Temp:"+getTemperature();
  }

  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#getTemperature()
   */
  public double getTemperature() {
    // TODO Auto-generated method stub
    return temp;
  }

  /* (non-Javadoc)
   * @see com.hp.wsdm.interop.wcm.IWs#setTemperatire(double)
   */
  public void setTemperature(double temp) {
    this.temp=temp;
  }
 
  public String queryStatus(){
    ResourceStub stub = new ResourceStub(wsEpr);
    stub.addObserver(observer);   
    XmlObject[] wsNameArry;
    try {
      wsNameArry = stub.getResourceProperty(STATUS_QNAME);
    } catch (FaultException e) {
      return "Unknown";
    } catch (RuntimeException e) {
      return "Unknown";
    } finally {
      stub.deleteObserver(observer);
    }
    if(wsNameArry.length==0)
      return "Unknown";
    OperationalStatusDocumentImpl.OperationalStatusImpl wsName = (OperationalStatusDocumentImpl.OperationalStatusImpl)wsNameArry[0];
    return wsName.getStringValue();
  }
 

  public String queryPrice(){
    ResourceStub stub = new ResourceStub(wsEpr);
    stub.addObserver(observer);   
    XmlObject[] wsNameArry;
    try {
      wsNameArry = stub.getResourceProperty(PRICE_QNAME);
      XmlObject x=wsNameArry[0];
      String value=x.newCursor().getTextValue();
      if(wsNameArry.length==0)
        return "-1";
      return value;

    } catch (FaultException e) {
      return "-1";
    } catch (RuntimeException e) {
      return "-1";
    } finally {
      stub.deleteObserver(observer);
    }
  }

 
  public EndpointReference getEpr(){
    return wsEpr;
  }

  /**
   * @param reference
   *
   */
  public void makeActive(EndpointReference referenceToWcConfigEpr) {
    XmlObject newEpr = ((XmlBeansEndpointReference)wsEpr).getXmlObject(AddressingConstants.NSURI_ADDRESSING_SCHEMA);
    SetResourcePropertiesDocument setResourcePropertiesDocument = SetResourcePropertiesDocument.Factory.newInstance();
        SetResourcePropertiesDocument.SetResourceProperties setResourceProperties = setResourcePropertiesDocument.addNewSetResourceProperties();
        UpdateType updateType = UpdateType.Factory.newInstance();
        WeatherStationReferenceDocument weatherDoc = WeatherStationReferenceDocument.Factory.newInstance();
        weatherDoc.setWeatherStationReference((EndpointReferenceType) newEpr);
        XmlBeanUtils.addChildElement(updateType,weatherDoc);
        setResourceProperties.setUpdateArray(new UpdateType[]{updateType});
        ResourceStub stub = new ResourceStub(referenceToWcConfigEpr);
        stub.sendRequest(setResourcePropertiesDocument,"http://foo","X");
  }

}
TOP

Related Classes of org.apache.wsdm.interop.wcm.impl.WsImpl

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.