cancel
Showing results for 
Search instead for 
Did you mean: 

Developing User Enhancement Modules in Adapter Engine Teched07 attender?

Former Member
0 Kudos

Hi,

I am looking for the Java code of one of the hands on curses in the SAP Teched07.

The course was a XI/PI, <i>Developing User Enhancement Modules in the Adapter Engine</i>, and I need the Java code of the Java Bean that was used.

The name of the file is GetHostNameXXBean

If someone can help me i would appreciate it.

Thanks in advance.

Eduardo

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

[code]package com.teched.userXX;

// 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.*;

import com.sap.engine.interfaces.messaging.api.*;

import com.sap.engine.interfaces.messaging.api.auditlog.*;

import com.sap.tc.logging.*;

// 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 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;

public class GetHostnameXXBean implements SessionBean, Module {

public static final String VERSION_ID = "$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/GetHostName.java#1 $";

static final long serialVersionUID = 7435850550539048631L;

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)";

Location location = null;

AuditAccess audit = null;

// Create the location always new to avoid serialization/transient of location

try {

location = Location.getLocation(this.getClass().getName());

} catch (Exception t) {

t.printStackTrace();

ModuleException me = new ModuleException("Unable to create trace location", t);

throw me;

}

Object obj = null;

Message msg = null;

String hostName = getHostName();

MessageKey key = null;

try {

obj = inputModuleData.getPrincipalData();

msg = (Message) obj;

key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());

audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();

audit.addAuditLogEntry(key, 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) {

location.debugT(SIGNATURE, "HostElementName parameter is not set. Default used: HostName.");

audit.addAuditLogEntry(key, AuditLogStatus.WARNING, "HostElementName parameter is not set. Default used: HostName.");

hostElementName = "HostName";

}

location.debugT(SIGNATURE, "HostElementName is set to ", new Object[] ); audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "HostElementName is set to ", new Object[] );

} catch (Exception e) {

location.catching(SIGNATURE, e);

location.errorT(SIGNATURE, "Cannot read the module context and configuration data");

audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "Cannot read the module context and configuration data");

ModuleException me = new ModuleException(e);

location.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;

}

}

[/code]

stefan_grube
Active Contributor
0 Kudos

This code is made for PI 7.1 and will not work for XI 3.0/PI 7.0

Regards

Stefan

stefan_grube
Active Contributor
0 Kudos

[code]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.mp.module.Module;

import com.sap.aii.af.mp.module.ModuleContext;

import com.sap.aii.af.mp.module.ModuleData;

import com.sap.aii.af.mp.module.ModuleException;

import com.sap.aii.af.ra.ms.api.Message;

import com.sap.aii.af.ra.ms.api.MessageDirection;

import com.sap.aii.af.service.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.aii.af.ra.ms.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.*;

/**

  • @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();

AuditMessageKey amk = null;

try {

obj = inputModuleData.getPrincipalData();

msg = (Message) obj;

if (msg.getMessageDirection().equals(MessageDirection.OUTBOUND))

amk = new AuditMessageKey(msg.getMessageId(), AuditDirection.OUTBOUND);

else

amk = new AuditMessageKey(msg.getMessageId(), AuditDirection.INBOUND);

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 ", new Object[] ); Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "HostElementName is set to ", new Object[] );

} 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;

}

}

[/code]

Former Member
0 Kudos

Hi Stefan,

thank you very much for the code.

Why this code is not valid for XI3.0 or PI7.0?

Thanks again.

Eduardo.

stefan_grube
Active Contributor
0 Kudos

The API changed in PI 7.1 slightly. If you compare the codes, you will see the difference.

Regards

Stefan

Answers (0)