Can any one convert this code into webmethods Java service code

import java.io.;
import java.util.
;
class FindFile
{
public void findFile(String name,File file)
{
File[] list = file.listFiles();
if(list!=null)
for (File fil : list)
{
if (fil.isDirectory())
{
findFile(name,fil);
}
else if (name.equalsIgnoreCase(fil.getName()))
{
System.out.println(fil.getParentFile());
}
}
}
public static void main(String[] args)
{
FindFile ff = new FindFile();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the file to be searched… " );
String name = scan.next();
System.out.println("Enter the directory where to search ");
String directory = scan.next();
ff.findFile(name,new File(directory));
}
}

Hi Sandeep Raj Deepala ,

I have created java service for you please refer the below code. You can also create same for your given code with following steps.

  1. Go Into Designer New>Java Service> Give Java Service name. Click Finished.
    2. Specify Input Output Parameters In your case . Input par > filename, directory and Output par > is isFound.

  2. Generate the code by right click on Java Service in Package Navigation. Following code snip will be generated. Paste it in file search Method.

  3.                         // pipeline
     IDataCursor pipelineCursor = pipeline.getCursor();
     	String	fileName = IDataUtil.getString( pipelineCursor, "fileName" );
     	String	directory = IDataUtil.getString( pipelineCursor, "directory" );
            pipelineCursor.destroy();
    
     // pipeline
            IDataCursor pipelineCursor_1 = pipeline.getCursor();
            IDataUtil.put( pipelineCursor_1, "isFound", "isFound" );
     pipelineCursor_1.destroy();  
    

    5 Then Change in you java service code According to your requirements. I have Created this service and tested as well with following code.

Note : As it is a java Service then System.out.println() and Scanner is not required here for print on console and take input .

-------------Java Service Code ------------<<<<<<<<<<<<<<<<<<<<<<<<

package TNSupport.work;

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;

public final class FileSearch_SVC

{

/** 
 * The primary method for the Java service
 *
 * @param pipeline
 *            The IData pipeline
 * @throws ServiceException
 */
public static final void FileSearch(IData pipeline) throws ServiceException {
	// local variable 
	String fileNameFound;
	
	// pipeline
	
	IDataCursor pipelineCursor = pipeline.getCursor();
		String	fileName = IDataUtil.getString( pipelineCursor, "fileName" );
		String	directory = IDataUtil.getString( pipelineCursor, "directory" );
	   
		File fdName = new File(directory) ;
		fileNameFound=findFile(fileName,fdName);
	  pipelineCursor.destroy();
	
	// pipeline
	IDataCursor pipelineCursor_1 = pipeline.getCursor();
	IDataUtil.put( pipelineCursor_1, "isFound", fileNameFound);
	pipelineCursor_1.destroy();
		
}

// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---

public static String findFile(String name,File file) 
{ 
File[] list = file.listFiles(); 
String status="false";
if(list!=null) 
for (File fil : list) 
{ 
if (fil.isDirectory()) 
{ 
findFile(name,fil); 
} 
else if (name.equalsIgnoreCase(fil.getName())) 
{ 
	status = "ture";  
} 

} 
return status ;
} 
	

// --- <<IS-END-SHARED-SOURCE-AREA>> ---

}

If you have more questions please feel free to ask

Regards,
Ashish Kumar

Sandeep – Impore the required libraries, place the needed jars at right loc, implement the same in java service, what challenges did you see ?

Thanks,