Forked from SriniBlog/WriteDynamicConfiguration_AdapterModule.java
Created
February 9, 2018 01:17
-
-
Save viksingh/b39173ef0915e706e8c2c952bb749b70 to your computer and use it in GitHub Desktop.
This is the adapter module Utility to write the dynamic configuration from the channel module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
package com.sap.adaptermodule; | |
import javax.ejb.CreateException; | |
import javax.ejb.SessionBean; | |
import javax.ejb.SessionContext; | |
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; | |
//XML parsing and transformation classes | |
import javax.xml.parsers.*; | |
//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.*; | |
//HTTP Post Message | |
import java.io.BufferedReader; | |
import java.io.DataOutputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
/** | |
* @author srvan | |
* | |
*/ | |
public class WriteDynamicConfigBean implements SessionBean, Module { | |
private static final String VERSION_ID = "$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/WriteDynamicConfigBean.java#1 $"; | |
private static final long serialVersionUID = 7435850550539048631L; | |
private final String USER_AGENT = "Mozilla/5.0"; | |
private SessionContext myContext; | |
Location TRACE = null; | |
MessageKey key = null; | |
AuditAccess audit = null; | |
int MAX_KEYS = 500; | |
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)"; | |
boolean skipExecution = getBooleanParam(moduleContext, "skipExecution"); | |
boolean failOnError = getBooleanParam(moduleContext, "failOnError"); | |
/* | |
* START OF GLOBAL VARIABLE INITIALIZE | |
* SKIP TILL THE ACTUAL CODE | |
*/ | |
// Open the LOG File to write the TRACE | |
try { | |
TRACE = Location.getLocation(this.getClass().getName()); | |
}catch (Exception t) { | |
t.printStackTrace(); | |
if (failOnError) | |
{ | |
ModuleException me = new ModuleException("Unable to create trace location(TRACE)", t); | |
throw me; | |
} | |
return inputModuleData; | |
} | |
TRACE.entering("process(ModuleContext, ModuleData)", new Object[] { moduleContext, inputModuleData }); | |
//Check if the message is empty or not | |
Object o = inputModuleData.getPrincipalData(); | |
if (o == null) | |
{ | |
String error = "[E1] ModuleData contains null XI message"; | |
TRACE.errorT("process(ModuleContext, ModuleData)", error); | |
if (failOnError) | |
{ | |
ModuleException me = new ModuleException(error); | |
TRACE.throwing("process(ModuleContext, ModuleData)", me); | |
throw me; | |
} | |
TRACE.warningT("process(ModuleContext, ModuleData)", "failOnError false, ignore missing XI message and return"); | |
return inputModuleData; | |
} | |
//Extract the message details and actual message | |
Message message = null; | |
try | |
{ | |
message = (Message)o; | |
} | |
catch (ClassCastException e) | |
{ | |
TRACE.catching("process(ModuleContext, ModuleData)", e); | |
String error = "[E2] ModuleData does not contain an object that implements the XI message interface; object class is: " + o.getClass().getName(); | |
TRACE.errorT("process(ModuleContext, ModuleData)", error); | |
if (failOnError) | |
{ | |
ModuleException me = new ModuleException("WriteDynamicConfigBean - " + error, e); | |
TRACE.throwing("process(ModuleContext, ModuleData)", me); | |
throw me; | |
} | |
TRACE.warningT("process(ModuleContext, ModuleData)", "failOnError false, " + error); | |
return inputModuleData; | |
} | |
//Get the instance for Audit | |
try{ | |
key = new MessageKey(message.getMessageId(), message.getMessageDirection()); | |
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess(); | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig: Module called"); | |
}catch(Exception ee){ | |
TRACE.catching("process(ModuleContext, ModuleData)", ee); | |
String error = "[E3] Unable to get the audit trace instance" + ee; | |
TRACE.errorT("process(ModuleContext, ModuleData)", error); | |
if (failOnError) | |
{ | |
ModuleException me = new ModuleException("WriteDynamicConfigBean - " + error, ee); | |
TRACE.throwing("process(ModuleContext, ModuleData)", me); | |
throw me; | |
} | |
TRACE.warningT("process(ModuleContext, ModuleData)", "failOnError false, " + error); | |
return inputModuleData; | |
} | |
/* | |
* ACTUAL CODE STARTS FROM HERE | |
*/ | |
try{ | |
if(skipExecution){ | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig - Skipping execution"); | |
TRACE.exiting("process(ModuleContext, ModuleData)", inputModuleData); | |
return inputModuleData; | |
} /********** [skipExecution = TRUE] - END OF PROGRAM **********/ | |
String paramNamespace = getParameter(moduleContext, "namespace", ""); | |
String paramFieldName = getParameter(moduleContext, "fieldname", ""); | |
String paramFieldValue = getParameter(moduleContext, "fieldvalue", ""); | |
if( paramNamespace == null || paramNamespace.equals("") ){ | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig - Please provide the Namespace!"); | |
TRACE.exiting("process(ModuleContext, ModuleData)", inputModuleData); | |
return inputModuleData; | |
}else if( paramFieldName == null || paramFieldName.equals("") ){ | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig - Please provide the FieldName!"); | |
TRACE.exiting("process(ModuleContext, ModuleData)", inputModuleData); | |
return inputModuleData; | |
}else if( paramFieldValue == null || paramFieldValue.equals("") ){ | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig - Please provide the FieldValue!"); | |
TRACE.exiting("process(ModuleContext, ModuleData)", inputModuleData); | |
return inputModuleData; | |
} | |
//Validation completed | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig: Started Logic"); | |
MessagePropertyKey fileKey = new MessagePropertyKey(paramFieldName, paramNamespace); | |
message.setMessageProperty(fileKey, paramFieldValue); | |
/*This is the way to read Dynamic Config*/ | |
//MessagePropertyKey fileKey1 = new MessagePropertyKey(paramFieldName, paramNamespace); | |
//String value = message.getMessageProperty(fileKey1); | |
}catch (Exception ee){ | |
TRACE.catching("process(ModuleContext, ModuleData)", ee); | |
String error = "[E4] Failed: Error in the Main Logic" + ee; | |
TRACE.errorT("process(ModuleContext, ModuleData)", error); | |
info("process(ModuleContext, ModuleData)", error); | |
if (failOnError) | |
{ | |
ModuleException me = new ModuleException("WriteDynamicConfigBean - " + error, ee); | |
TRACE.throwing("process(ModuleContext, ModuleData)", me); | |
throw me; | |
} | |
return inputModuleData; | |
} | |
info("process(ModuleContext, ModuleData)", "WriteDynamicConfig: End Logic Successfully!"); | |
TRACE.exiting("process(ModuleContext, ModuleData)", inputModuleData); | |
return inputModuleData; | |
} | |
public boolean getBooleanParam(ModuleContext moduleContext, String paramField){ | |
boolean bparamValue = false; | |
try{ | |
String paramValue = moduleContext.getContextData(paramField); | |
if( paramValue != null && !paramValue.equals( "" )){ | |
bparamValue = Boolean.parseBoolean( paramValue ); | |
} | |
}catch(Exception ee){ bparamValue=false; } | |
return bparamValue; | |
} | |
public static String getParameter(ModuleContext context, String name, String sDefault) | |
{ | |
if ((name == null) || (name.equals(""))) { | |
return sDefault; | |
} | |
String val = context.getContextData(name); | |
if (val == null || val.equals("") ) { | |
return sDefault; | |
} | |
return val; | |
} | |
private void info(String signature, String msg) | |
{ | |
TRACE.infoT(signature, msg); | |
this.audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "WriteDynamicConfig Module: INFO: " + msg); | |
} | |
private void warning(String signature, String msg) | |
{ | |
TRACE.warningT(signature, msg); | |
this.audit.addAuditLogEntry(key, AuditLogStatus.WARNING, "WriteDynamicConfig Module: WARNING: " + msg); | |
} | |
private void error(String signature, String msg) | |
{ | |
TRACE.errorT(signature, msg); | |
this.audit.addAuditLogEntry(key, AuditLogStatus.ERROR, "WriteDynamicConfig Module: ERROR: " + msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment