0

I have a little issue with a Java service that I call in a Webmethod Flow. I call it with 2 args filepath and filename and retrieve the archive's filepath. This service is supposed to be call when I add files to a specific directory (using a filepolling). And everytime I try to pass several files in the folder I get this exception for the 1st file to be treated:

com.wm.app.b2b.server.ServiceException: 2.null
    at ma.sap.aladin.catalog.in.priv.utils.zip(utils.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ......

Here's the code of my zip service:

    import com.wm.data.*;
    import com.wm.util.Values;
    import com.wm.app.b2b.server.Service;
    import com.wm.app.b2b.server.ServiceException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    import com.wm.data.IData;
    import com.wm.data.IDataCursor;
    import com.wm.data.IDataUtil;
    
    public final class zip_SVC

{

    /** 
     * The primary method for the Java service
     *
     * @param pipeline
     *            The IData pipeline
     * @throws ServiceException
     */
    public static final void zip(IData pipeline) throws ServiceException {
        IDataCursor pipelineCursor = pipeline.getCursor();
        String  targetFolder = IDataUtil.getString( pipelineCursor, "targetFolder" );
        String  fileName = IDataUtil.getString( pipelineCursor, "fileName" );
        pipelineCursor.destroy();
        // pipeline
        
        if (targetFolder.charAt(targetFolder.length()-1)!='\\') targetFolder+="\\";
        
        ZipOutputStream zipOut = null;
        FileOutputStream fos = null;        
        FileInputStream fis = null;
        
        String pathPieces[]=fileName.split("\\\\");
        String nameWoPath=pathPieces[pathPieces.length-1].substring(12);
        
        String zipName=targetFolder+nameWoPath+"_zip.zip";
        
        try {
        
            fos = new FileOutputStream(zipName);
            zipOut = new ZipOutputStream(fos);
            File fileToZip = new File(fileName);
            fis = new FileInputStream(fileToZip);
            ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
        while((length = fis.read(bytes)) >= 0) zipOut.write(bytes,0,length);
        
        } catch (Exception e) {
            e.printStackTrace();
        throw new ServiceException("1."+e.getMessage());
        } finally {
            try {
                zipOut.finish();
                zipOut.flush();
                
                fis.close();
                fos.close();    
                zipOut.close();
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServiceException("2."+e.getMessage());
            }
        }
        
        // pipeline
        IDataCursor pipelineCursor_1 = pipeline.getCursor();
        IDataUtil.put( pipelineCursor_1, "zipName", zipName);
        pipelineCursor.destroy();
            
    }

Does anybody have an idea about what happened here ? Thanks in advance :)

Makoto
  • 104,088
  • 27
  • 192
  • 230
gdc
  • 11
  • 2
  • Running it on my machine works just fine. Have you tried to find out what should be null? – XtremeBaumer Apr 26 '22 at 15:21
  • Yeah I did find that I get a NullPointerException triggered by `FIleInputStream.close()` – gdc Apr 27 '22 at 08:59
  • Check [this answer](https://stackoverflow.com/a/156889/7109162) – XtremeBaumer Apr 27 '22 at 09:23
  • thx @XtremeBaumer, I tried to delare FileOutputStream, FileInputStream and ZipOutputStream directly in my try so I don't have to care for the close. But now I got errors `The system cannot find the file specified` while the file is present where it's supposed to be – gdc Apr 27 '22 at 13:58
  • Update the question with the new findings / code. Are the files on a remote machine? Can you replace the `IData pipeline` parameter with `String targetFolder, String fileName`? In general your code does work, I am just not sure about the `IData` code parts – XtremeBaumer Apr 27 '22 at 14:35
  • 1
    "Solved" isn't how we say that a problem is solved. Place an answer to your question and then accept it. I've done so, just copied your original answer into a Community Wiki. Feel free to accept that; I won't get any rep or anything. – Makoto Apr 27 '22 at 15:59

1 Answers1

0

I finally found out what the problem was, it seems that my java service was sometimes called before the end of the previous process, which meant that my filename was equal to null and was causing all the other problems, I solved it very simply by calling Thread.sleep(500) just before initializing all my streams.

Makoto
  • 104,088
  • 27
  • 192
  • 230