Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Error in Adapter Module to unzip files

Hello ,

I wish to unzip many files with use of an adapter module

To du this I use following code

package de.lsv.moduledevelopement;
import java.io.*;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;

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.ra.ms.api.Payload;
import com.sap.aii.af.ra.ms.spi.Trace;
import com.sap.aii.af.service.auditlog.Audit;
import com.sap.aii.af.service.auditlog.AuditDirection;
import com.sap.aii.af.service.auditlog.AuditLogStatus;
import com.sap.aii.af.service.auditlog.AuditMessageKey;

/**
 * @ejbLocal <{de.lsv.moduledevelopement.UnZipFilesLocal}>
 * @ejbLocalHome <{de.lsv.moduledevelopement.UnZipFilesLocalHome}>
 * @stateless 
 */
public class UnZipFilesBean implements SessionBean, Module {

	static AuditMessageKey amk = null;
	static String auditStr = "UnzipFilesBean - ";
	static ModuleException mEx = null;

	public static final String VERSION_ID =
		"$Id://de.lsv.moduledevelopement.UnZipFilesBean.java#1 $";

	private static final Trace TRACE = new Trace(VERSION_ID);
	public void ejbRemove() {
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

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

	private SessionContext myContext;
	/**
	 * Create Method.
	 */
	public void ejbCreate() throws CreateException {
		// TODO : Implement
	}

	/* (non-Javadoc)
	 * @see com.sap.aii.af.mp.module.Module#process(com.sap.aii.af.mp.module.ModuleContext, com.sap.aii.af.mp.module.ModuleData)
	 */
	public ModuleData process(
		ModuleContext moduleContext,
		ModuleData inputModuleData)
		throws ModuleException {
		Object obj = null;
		Message msg = null;
		Payload attachment = null;
		//initializing variables for module parameters
		String modkey = "";
		String fileMask = "";
		String directory = "";
		String saveDirectory = "";
		//	String newMimeType = "";
		//	String newAttachmentName = "";
		//	String newAttachmentExtension = "";
		//	String newAttachmentNameReplace = "";
		//this block is to manage the adapter when is not file/FTP
		try {
			//get the XI Message
			obj = inputModuleData.getPrincipalData();
			msg = (Message) obj;
			// create AuditMessageKey object to write audit log.
			if (msg.getMessageDirection() == MessageDirection.INBOUND)
				amk =
					new AuditMessageKey(
						msg.getMessageId(),
						AuditDirection.INBOUND);
			else
				amk =
					new AuditMessageKey(
						msg.getMessageId(),
						AuditDirection.OUTBOUND);
			// ******start get the module parameters from the configuration******
			modkey = moduleContext.getContextData("module.key");
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "Module Configuration Key: " + modkey);
			//***** einlesen der Moduke Parameter******************
			//***** DateiMaske
			fileMask = moduleContext.getContextData("FileMask");
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "FileMask: " + fileMask);

			// check ob fileMask definiert ist
			if (fileMask == null || fileMask.equalsIgnoreCase("")) {
				//if no attachment name is specified in the module configuration the module will end the execution
				throw mEx =
					new ModuleException(
						auditStr
							+ "!FileMask in Modulconfiguration nicht definiert!");
			}

			//***** DateiMaske2
			String fileMask2 = moduleContext.getContextData("FileMask2");
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "FileMask2: " + fileMask2);

			//***** Directory
			directory = moduleContext.getContextData("Directory");
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "Directory: " + directory);

			// Fehler wenn Directory nicht definiert
			if (directory == null || directory.equalsIgnoreCase("")) {
				//the module will end the execution
				throw mEx =
					new ModuleException(
						auditStr
							+ "Directory in Modulconfiguration nicht definiert");
			}
			//***** Sicherungs Verzeichnis
			saveDirectory = moduleContext.getContextData("SaveDirectory");
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "SaveDirectory: " + saveDirectory);

			// entpacken aufrufen

			entpacke(directory, saveDirectory, fileMask);
			if (fileMask2.length() > 0) {
				entpacke(directory, saveDirectory, fileMask2);
			}

			//update the XI Message
			inputModuleData.setPrincipalData(msg);
		} catch (Exception e) {
			String errorMessage =
				auditStr + "Error bei der . Exception:" + e.getMessage();
			Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, errorMessage);
			throw mEx = new ModuleException(auditStr + errorMessage);
		}
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "Verarbeitung beendet ");
		return inputModuleData;
	}

	//+++++++++++++++++++++++++
	private static void entpacke(
		String directory,
		String saveDirectory,
		String mask)
		 {
		// TODO Auto-generated method stub
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "Funktion entpacke");
		if (!directory.endsWith("\")) {
			directory = directory + "\";
		} // if
		File f;
		f = new File(directory);
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "f = new File(directory)");
//		// Achtung Klasse in einer Klasse :-)
//		String[] strFiles = f.list(new FilenameFilter() {
//			public boolean accept(File file, String filename) {
//				Wildcard wildcard1 = new Wildcard(mask);
//				Audit.addAuditLogEntry(
//					amk,
//					AuditLogStatus.SUCCESS,
//					auditStr + "wc1");
//				return wildcard1.match(filename, false);
//			}
//		});

		String [] strFiles = f.list(new MyFilter(mask));

		// Liste der Gefundenen dateien
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "Anzahl gepackter Dateien: " + strFiles.length);

		//System.out.println( strFiles.length + " Dateien zum entpacken");
		for (int i = 0; i < strFiles.length; i++) {
			Audit.addAuditLogEntry(
				amk,
				AuditLogStatus.SUCCESS,
				auditStr + "verarbeite Datei: : " + strFiles<i>);
			//	System.out.println("f[" + i + "]=" + strFiles<i>);
		} // for

		// *****entpacken**********************
		for (int i = 0; i < strFiles.length; i++) {
			try {

				byte[] buf = new byte[4096];
				ZipInputStream in =
					new ZipInputStream(
						new FileInputStream(directory + strFiles<i>));

				Audit.addAuditLogEntry(
					amk,
					AuditLogStatus.SUCCESS,
					auditStr + "Entpacke " + directory + strFiles<i>);
				//System.out.println("Entpacke " + directory + strFiles<i>);
				int count = 0;
				while (true) {
					count++;
					// Nächsten Eintrag lesen
					ZipEntry entry = in.getNextEntry();
					if (entry == null) {
						break;
					}
					// Beschreibung ausgeben

					Audit.addAuditLogEntry(
						amk,
						AuditLogStatus.SUCCESS,
						auditStr + entry.getName());
					//System.out.println(entry.getName());
					// Ausgabedatei erzeugen
					FileOutputStream out =
						new FileOutputStream(directory + entry.getName());
					int len;
					while ((len = in.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
					out.close();
					// Eintrag schließen
					in.closeEntry();
				} // while
				in.close();
				Audit.addAuditLogEntry(
					amk,
					AuditLogStatus.SUCCESS,
					auditStr + count + "Dateien entpackt");
				//System.out.println(count + "Dateien entpackt");

				// verschieben
				if (saveDirectory.length() > 0) {
					File quelle = new File(directory + strFiles<i>);
					File ziel = new File(saveDirectory + strFiles<i>);
					boolean bla = quelle.renameTo(ziel);
					if (!bla) {
						Audit.addAuditLogEntry(
							amk,
							AuditLogStatus.SUCCESS,
							auditStr
								+ strFiles<i>
								+ " konnte nicht verschoben werden");
					} else {
						Audit.addAuditLogEntry(
							amk,
							AuditLogStatus.SUCCESS,
							auditStr
								+ strFiles<i>
								+ "verschoben nach "
								+ saveDirectory);

						//		System.out.println(
						//			strFiles<i> + "verscgoben nach " + directory + "sik//");
					}
				} // if

			} catch (Exception e) {
				String errorMessage =
					auditStr
						+ "Error in entpacken. Exception:"
						+ e.getMessage();
				Audit.addAuditLogEntry(amk, AuditLogStatus.ERROR, errorMessage);
			} // catch

			// *****entpacken*************
		} // for

	} //entpacke

} // UnzipFilesBean

class Wildcard {
	
	static AuditMessageKey amk = null;
	static String auditStr = "WildCard - ";
	static ModuleException mEx = null;
	private final String wildcardString;

	/**
	 * Der Konstruktor für die WildcardSuche.
	 * 
	 * @param wildcardString
	 *            Der WildcardString
	 */
	public Wildcard(String wildcardString) {
		this.wildcardString = wildcardString;
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "Constructor");

	}

	/**
	 * Die Methode, welche den Vergleich durchführt.
	 * 
	 * @param str
	 *            Der String, welcher darauf überprüft werden soll, ob er dem
	 *            WildcardString entspricht.
	 * @return true Wenn der String dem wildcardString entspricht.
	 */
	public boolean match(String str, boolean fCasesensitive) {
		if (str == null || wildcardString == null) {
			return false;
		}
		return recursiveMatch(wildcardString, str, fCasesensitive);
	}

	/**
	 * Hier wird der eigentliche Vergleich durchgeführt
	 * 
	 * @param strWildcard
	 *            Der Wildcardstring, z.B.: "Test*"
	 * @param str
	 *            Der zu überprüfende String, z.B. "Testlauf"
	 * @return true, wenn eine Übereinstimmung vorhanden ist, sonst false
	 */
	private boolean recursiveMatch(
		String strWildcard,
		String str,
		boolean fCasesensitive) {
		while (true) {
			if (strWildcard.length() == 0)
				return str.length() == 0;
			char ch = strWildcard.charAt(0);
			switch (ch) {
				case '?' :
					if (str.length() == 0)
						return false;
					break;
				case '*' :
					strWildcard = strWildcard.substring(1);
					while (true) {
						if (recursiveMatch(strWildcard, str, fCasesensitive))
							return true;
						if (str.length() == 0)
							return false;
						str = str.substring(1);
					}
				default :
					if (str.length() == 0)
						return false;
					if (fCasesensitive) {
						if (ch != str.charAt(0))
							return false;
					} else if (
						Character.toLowerCase(ch)
							!= Character.toLowerCase(str.charAt(0)))
						return false;
					break;
			}
			strWildcard = strWildcard.substring(1);
			str = str.substring(1);
		}
	}

	//++++++++++++++++++++++++++

} // class

/** Nun der eigentliche Filter */
class MyFilter implements FilenameFilter {
	
	static AuditMessageKey amk = null;
	static String auditStr = "MyFilter - ";
	static ModuleException mEx = null;

	
	String fileMask;

	public MyFilter(String fileMask) {
		this.fileMask = fileMask;
		Audit.addAuditLogEntry(
			amk,
			AuditLogStatus.SUCCESS,
			auditStr + "Constructor");
	} //constructor

	 public boolean accept(File file, String filename) {
		// TODO Auto-generated method stub
		//fileMask = "?5LBL*";
		Wildcard wildcard1 = new Wildcard(fileMask);
		return wildcard1.match(filename, false);
	}
} //class

But in Audit Protocol it failed after:

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - Module Configuration Key: 2

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - FileMask: ?5LBL???

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - FileMask2:

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - Directory: //transfer//X1E//100//99//lb//ein

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - SaveDirectory: //transfer//X1E//100//99//lb//ein_s

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - Funktion entpacke

17.09.2008 17:33:19 Erfolgreich UnzipFilesBean - f = new File(directory)

17.09.2008 17:33:19 Fehler UnzipFilesBean - Error bei der . Exception:null

I could not find the error??

Were there any log files to look into?

regarts

Zimmerningkat

Former Member
Not what you were looking for? View more on this topic or Ask a question