Skip to end of metadata
Go to start of metadata
GetHostNameBean.java
package com.teched06.usermodule;
// Classes for EJB
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
// Classes for Module development & Trace
import com.sap.aii.af.lib.mp.module.Module;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.aii.af.lib.trace.Trace;
// XML parsing and transformation classes
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.sap.engine.interfaces.messaging.api.XMLPayload;
import javax.xml.transform.*;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
/**
* @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}>
* @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}>
* @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}>
* @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}>
* @stateless
*/
public class GetHostName implements SessionBean, Module {

  public static final String VERSION_ID = "$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/GetHostName.java#1 $";
  private static final Trace TRACE = new Trace(VERSION_ID);
  private SessionContext myContext;

  public void ejbRemove() {
  }

  public void ejbActivate() {
  }

  public void ejbPassivate() {
  }

  public void setSessionContext(SessionContext context) {
    myContext = context;
  }

  public void ejbCreate() throws CreateException {
  }

  public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException {

    String SIGNATURE = "process(ModuleContext moduleContext, ModuleData inputModuleData)";
    Object obj = null;
    Message msg = null;
    String hostName = getHostName();
    MessageKey amk = null;

    try {
      obj = inputModuleData.getPrincipalData();
      msg = (Message) obj;
      amk = new MessageKey(msg.getMessageId(),msg.getMessageDirection());
      Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "GetHostName: Module called");
    }
    catch (Exception e) {
      ModuleException me = new ModuleException(e);
      throw me;
    }

    // Read the channel ID, channel and the module configuration
    String hostElementName = null;

    try {
    // CS_GETMODDAT START
      hostElementName = (String) moduleContext.getContextData("HostElementName");
    // CS_GETMODDAT END
      if (hostElementName == null) {
        TRACE.debugT(SIGNATURE, "HostElementName parameter is not set. Default used: HostName.");
        Audit.addAuditLogEntry(amk, AuditLogStatus.WARNING,
                               "HostElementName parameter is not set. Default used: HostName.");
        hostElementName = "HostName";
      }

      TRACE.debugT(SIGNATURE, "HostElementName is set to {0}", new Object[] {hostElementName});
      Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,
                             "HostElementName is set to {0}", new Object[] {hostElementName});
    } catch (Exception e) {
      TRACE.catching(SIGNATURE, e);
      TRACE.errorT(SIGNATURE, "Cannot read the module context and configuration data");
      Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR,
                             "Cannot read the module context and configuration data");
      ModuleException me = new ModuleException(e);
      TRACE.throwing(SIGNATURE, me);
      throw me;
    }

    try {
      XMLPayload xmlpayload = msg.getDocument();
      DocumentBuilderFactory factory;
      factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse((InputStream) xmlpayload.getInputStream());
      Element rootNode = document.getDocumentElement();
      if(rootNode != null) {
        Element childElement =
        document.createElement(hostElementName);
        childElement.appendChild(document.createTextNode(hostName));
        rootNode.appendChild(childElement);
      }

      // Transforming the DOM object to Stream object.
      TransformerFactory tfactory = TransformerFactory.newInstance();
      Transformer transformer = tfactory.newTransformer();
      Source src = new DOMSource(document);
      ByteArrayOutputStream myBytes = new ByteArrayOutputStream();
      Result dest = new StreamResult(myBytes);
      transformer.transform(src,dest);
      byte[] docContent = myBytes.toByteArray();

      if(docContent != null) {
        xmlpayload.setContent(docContent);
        inputModuleData.setPrincipalData(msg);
      }
    }
    catch (Exception e) {
      ModuleException me = new ModuleException(e);
      throw me;
    }

  return inputModuleData;
  }

  private static String getHostName() {
    String host = "unknown";
    try {
      InetAddress inet = InetAddress.getLocalHost();
      host = inet.getHostName().toLowerCase();
      int i = host.indexOf(".");
      if (i > 0) {
        host = host.substring(0, i);
      }
    }
    catch (UnknownHostException e) {
      host = "error";
    }
    return host;
  }
}
  • No labels