Java file does not receive parameters from Inputstream

Asked

Viewed 255 times

2

I have a problem with a class on my project application server. I need this class after compiled and running, to receive a message:

  • or "MSG_PV" or "MSG_RV" from my REDMINE, to publish projects
  • or generate them within my Repository.

The problem is I implemented the Publish Project now, being that of Beget was already working on my server. I read something on the net and in the documentation of JAVADOC that the method InputStream when used with the readLine(), becomes the InputStreamReader in Java 7.

How to proceed in this situation?

My code:

import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.*;

public class server_versao extends Thread
{
   private ServerSocket serverSocket;
   private boolean server_is_busy  = false;

   public server_versao(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
   }

   private boolean execute_MSG_RV(String branch){
      try{
        if (Integer.parseInt(branch.substring(1,5)) >= 1308) //Versoes B1308 pra cima.
            return release_branch("release1308 " + branch);
        else
            return release_branch("release " + branch);
      }catch(Exception ex){
        return false;
      }
   }

   private boolean release_branch(String comando){
     boolean result = false;
     InputStream is;
     Process p;
     BufferedReader br;
     BufferedWriter bw;
     Date today;
     SimpleDateFormat ft;
     File f;
     FileWriter fw; 
     String aux, arch = "";

     try{
        today = new Date();
        ft = new SimpleDateFormat("dd.MM.yyy-hh.mm");
        f = new File ("logs/release/log-"+ft.format(today)+".txt");
        fw = new FileWriter(f);
        bw = new BufferedWriter(fw);
        System.out.println("Executando comando: "+ comando);
        p = Runtime.getRuntime().exec ("cmd /c gera_release.bat"+comando.toUpperCase());
        is = p.getInputStream(); 
        br = new BufferedReader (new InputStreamReader (is)); 
        aux = br.readLine(); 

        while (aux!=null){ 
            arch += aux + "\r\n";
            if (aux.contains("BUILD SUCCESSFUL") == true)
                result = true;
            aux = br.readLine();
        }

        bw.write(arch);
        bw.close();
        fw.close();
     }catch(Exception e){
        result = false;
     }
     return result;
   }

  private boolean execute_MSG_PV(String branch){
      try{
        if (Integer.parseInt(branch.substring(1,5)) >= 1308) //Versoes B1308 pra cima.
            return publish_version("publicar versoes_novas " + branch);
        else
            return publish_version("publicar versoes_antigas " + branch);
      }catch(Exception ex){
        return false;
      }
   }

   private boolean publish_version(String comando){
     boolean result = false;
     InputStream is;
     Process p;
     BufferedReader br;
     BufferedWriter bw;
     Date today;
     SimpleDateFormat ft;
     File f;
     FileWriter fw; 
     String aux, arch = "";

     try{
        today = new Date();
        ft = new SimpleDateFormat("dd.MM.yyy-hh.mm");
        f = new File ("logs/publish/log-"+ft.format(today)+".txt");
        fw = new FileWriter(f);
        bw = new BufferedWriter(fw);
        System.out.println("Executando comando: "+ comando);
        p = Runtime.getRuntime().exec ("cmd /c publicar_versao.bat"+comando.toUpperCase());
        is = p.getInputStream(); 
        br = new BufferedReader (new InputStreamReader (is)); 
        aux = br.readLine(); 

        while (aux!=null){ 
            arch += aux + "\r\n";
            if (aux.contains("BUILD SUCCESSFUL") == true)
                result = true;
            aux = br.readLine();
        }

        bw.write(arch);
        bw.close();
        fw.close();
     }catch(Exception e){
        result = false;
     }
     return result;
   }

   public void run()
   {
      boolean success = false;
      String msg;
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to " + server.getRemoteSocketAddress());
            DataInputStream in = new DataInputStream(server.getInputStreamReader());
            msg = in.readLine();
            if (msg.equals("MSG_RV"))
                success = execute_MSG_RV(in.readLine());
            else if(msg.equals("MSG_PV"))
                success = execute_MSG_PV(in.readLine());

            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            if (success)
                out.writeUTF("MSG_SUC");
            else
                out.writeUTF("MSG_ERR");
            server.close();
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      int port = Integer.parseInt(args[0]);
      try
      {
         Thread t = new server_versao(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

When the file receives the parameters within this block:

DataInputStream in = new DataInputStream(server.getInputStreamReader()); 
msg = in.readLine(); 
if (msg.equals("MSG_RV")) 
    success = execute_MSG_RV(in.readLine()); 
else if(msg.equals("MSG_PV")) 
    success = execute_MSG_PV(in.readLine()); 

You should switch to the function called the one inside in.readLine() and complete the rest of the calls. But I debugged and it reads the data but does not process. There seems to be an error with my in.readline()

  • 2

    "But I debugged it and it reads the data but does not process it." Can you be more specific? How did you confirm that the data is being read? And "does not process" means what, that gives some error, that throws an exception... By the way, DataInputStream.readLine is obsolete, can cause encoding problems, use BufferedReader instead.

1 answer

2

According to the documentation the function readline of Datainputstrem this discontinued as can be seen here

Try switching to:

    BufferedReader d
      = new BufferedReader(server.getInputStreamReader());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.