简体   繁体   中英

How to invoke a java component in Mule

I'm using the following SOAP client to inovke a web service in Apache OFBiz:

public class CreatePerson {

private static OMFactory fac;
private static OMNamespace omNs;
public static String fname;
public static String lname;

static {
      fac = OMAbstractFactory.getOMFactory();
      omNs = fac.createOMNamespace("http://my-ip-adress/service/", "ns1");
}

public static void main(String[] args) throws AxisFault {

      ServiceClient sc = new ServiceClient();
      Options opts = new Options();
      opts.setTo(new EndpointReference("http://my-ip-adress:port/webtools/control/SOAPService"));
      opts.setAction("createPerson");
      sc.setOptions(opts);
      OMElement res = sc.sendReceive(createPayLoad(fname, lname));
      System.out.println(res);
}

public static OMElement createPayLoad(@XPath("//person/return[1]")String firstName, @XPath("//person/return[2]")String lastName) {

      CreatePerson.fname = firstName;
      CreatePerson.lname = lastName;

      OMElement createPerson = fac.createOMElement("createPerson", omNs);
      OMElement mapMap = fac.createOMElement("map-Map", omNs);

      createPerson.addChild(mapMap);

      mapMap.addChild(createMapEntry("login.username", "admin"));
      mapMap.addChild(createMapEntry("login.password", "ofbiz"));
      // do the mapping here!
      mapMap.addChild(createMapEntry("firstName", firstName));
      mapMap.addChild(createMapEntry("lastName", lastName));

      return createPerson;
}

public static OMElement createMapEntry(String key, String val) {

      OMElement mapEntry = fac.createOMElement("map-Entry", omNs);

      // create the key
      OMElement mapKey = fac.createOMElement("map-Key", omNs);
      OMElement keyElement = fac.createOMElement("std-String", omNs);
      OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);

      mapKey.addChild(keyElement);
      keyElement.addAttribute(keyAttribute);

      // create the value
      OMElement mapValue = fac.createOMElement("map-Value", omNs);
      OMElement valElement = fac.createOMElement("std-String", omNs);
      OMAttribute valAttribute = fac.createOMAttribute("value", null, val);

      mapValue.addChild(valElement);
      valElement.addAttribute(valAttribute);

      // attach to map-Entry
      mapEntry.addChild(mapKey);
      mapEntry.addChild(mapValue);

      return mapEntry;
}
}

Next I want to use the following xml to get the values in return-element to "map" (AnnotatedEntryPointResolver) with firstName and lastName in my client above:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:getAllResponse xmlns:ns2="http://service.ofbiz.org/">
      <person>
        <return>Testname</return>
        <return>Test</return>
      </person>
    </ns2:getAllResponse>
  </soap:Body>
</soap:Envelope>

Herefore I'm using Mule. As you can see in my client code I added some XPath annotations to reference the xml values in return-element. For testing purposes my Mule config is simple:

<flow name="test_flow" doc:name="test_flow">
    <file:inbound-endpoint path="[mypath]\xml\in" responseTimeout="10000" doc:name="File"/>
    <component class="org.ofbiz.service.CreatePerson" doc:name="Java"/>
</flow>

I'm just using a file inbound endpoint and the java component referencing my client above. After running Mule "mapping" is done correctly within the createPayLoad()-method in my client. But it's not what I want to do. My question: How can I invoke the whole java component (including the main-method) using the AnnotatedEntryPointResolvers for this example? Is there an alternative or better solution as described above?

Invoking the main() is odd but... you can do it with:

<scripting:component>
    <scripting:script engine="groovy">CreatePerson.main([] as String[])</scripting:script>
</scripting:component>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM