I have a problem with a class on my project application server. I need this class after compiled and running, get a message:
- or "MSG_PV" or "MSG_RV" from my REDMINE to publish projects
- or generate them, within my Repository.
The problem is that I deployed the Publish Project function now, whereas Generate was already running on my server. I have read on the net and in the documentation of JAVADOC
that the InputStream
method when used with readLine()
, becomes 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 move to the function called what is inside in.readLine()
and complete the rest of the calls. But I did a debug and it reads the data but does not process. There seems to be an error with my in.readline()