Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.ant.internal.ui.antsupport.logger;
 
 
 import java.util.List;
 import java.util.Map;
 
 import  org.apache.tools.ant.BuildEvent;
 import  org.apache.tools.ant.BuildException;
 import  org.apache.tools.ant.DefaultLogger;
 import  org.apache.tools.ant.Location;
 import  org.apache.tools.ant.Project;
 import  org.apache.tools.ant.Target;
 import  org.apache.tools.ant.util.StringUtils;
Parts adapted from org.eclipse.jdt.internal.junit.runner.RemoteTestRunner A build logger that reports via a socket connection. See MessageIds for more information about the protocol.
 
 public class RemoteAntBuildLogger extends DefaultLogger {

    
Time of the start of the build
 
     private long fStartTime = System.currentTimeMillis();

    
The client socket.
 
     private Socket fEventSocket;
    
Print writer for sending messages
 
     private PrintWriter fWriter;
    
Host to connect to, default is the localhost
 
     protected String fHost""//$NON-NLS-1$
     
Port to connect to.
 
     private int fEventPort= -1;
     
     private String fProcessIdnull;
    
    
Is the debug mode enabled?
 
     protected boolean fDebugModefalse;    
     
     protected boolean fSentProcessIdfalse;
     
     private List fEventQueue;
     
     private String fLastFileNamenull;
     private String fLastTaskNamenull;
     
     /* (non-Javadoc)
      * @see org.apache.tools.ant.DefaultLogger#printMessage(java.lang.String, java.io.PrintStream, int)
      */
     protected void printMessage(String messagePrintStream streamint priority) {
         marshalMessage(prioritymessage);
     }
    
    
Connect to the remote Ant build listener.
 
     protected void connect() {
         if () {
             ..println("RemoteAntBuildLogger: trying to connect" +  + ":" + ); //$NON-NLS-1$ //$NON-NLS-2$
         }
         
         for (int i= 1; i < 5; i++) {
             try{
                 new Socket();
                 new PrintWriter(.getOutputStream(), true);
                 return;
             } catch(IOException e){
             }
            try {
                Thread.sleep(500);
            } catch(InterruptedException e) {
            }
        }
        shutDown();
    }

    
Shutdown the connection to the remote build listener.
    protected void shutDown() {
        if ( != null) {
            .clear();
        }
        if ( != null) {
            .close();
            null;
        }
        
        try {
            if ( != null) {
                .close();
                null;
            }
        } catch(IOException e) {
        }
    }
    private void sendMessage(String msg) {
        if ( == null) {
            return;
        }
        
        .println(msg);
    }
    
    /* (non-Javadoc)
     * @see org.apache.tools.ant.BuildListener#buildFinished(org.apache.tools.ant.BuildEvent)
     */
    public void buildFinished(BuildEvent event) {
        if (!) {
            establishConnection();
        }
        handleException(event);
        printMessagegetTimeString(System.currentTimeMillis() - ), out, Project.MSG_INFO); 
        shutDown();
    }
    
    protected void handleException(BuildEvent event) {
        Throwable exception = event.getException();
        if (exception == null || exception instanceof AntSecurityException) {
            return;
        }
        
         StringBuffer messagenew StringBuffer();
         message.append(StringUtils.LINE_SEP);
         message.append(RemoteAntMessages.getString("RemoteAntBuildLogger.1")); //$NON-NLS-1$
         message.append(StringUtils.LINE_SEP);
         if (Project.MSG_VERBOSE <= this. || !(exception instanceof BuildException)) {
             message.append(StringUtils.getStackTrace(exception));
         } else {
             if (exception instanceof BuildException) {
                 message.append(exception.toString()).append(StringUtils.LINE_SEP);
             } else {
                 message.append(exception.getMessage()).append(StringUtils.LINE_SEP);
             }
         }
        message.append(StringUtils.LINE_SEP);
        printMessage(message.toString(), out, Project.MSG_ERR); 
    }
    
    private String getTimeString(long milliseconds) {
        long seconds = milliseconds / 1000;
        long minutes = seconds / 60;
        secondsseconds % 60;
        StringBuffer resultnew StringBuffer(RemoteAntMessages.getString("RemoteAntBuildLogger.Total_time")); //$NON-NLS-1$
        if (minutes > 0) {
            result.append(minutes);
            if (minutes > 1) {
                result.append(RemoteAntMessages.getString("RemoteAntBuildLogger._minutes_2")); //$NON-NLS-1$
            } else {
                result.append(RemoteAntMessages.getString("RemoteAntBuildLogger._minute_3")); //$NON-NLS-1$
            }
        }
        if (seconds > 0) {
            if (minutes > 0) {
                result.append(' ');
            }
            result.append(seconds);
    
            if (seconds > 1) {
                result.append(RemoteAntMessages.getString("RemoteAntBuildLogger._seconds_4")); //$NON-NLS-1$
            } else {
                result.append(RemoteAntMessages.getString("RemoteAntBuildLogger._second_5")); //$NON-NLS-1$
            } 
        }
        if (seconds == 0 && minutes == 0) {
            result.append(milliseconds);
            result.append(RemoteAntMessages.getString("RemoteAntBuildLogger._milliseconds_6"));      //$NON-NLS-1$
        }
        return result.toString();
    }
            
    /* (non-Javadoc)
     * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
     */
    public void targetStarted(BuildEvent event) {
        if (!) {
            establishConnection();
        }
        if (Project.MSG_INFO <= msgOutputLevel) {
            marshalTargetMessage(event);
        }
    }
    protected void establishConnection() {
        if ( != -1) {
            connect();
        } else {
            shutDown();
            return;
        }
        
        true;
        StringBuffer messagenew StringBuffer(.);
        message.append();
        sendMessage(message.toString());
        if ( != null) {
            for (Iterator iter = .iterator(); iter.hasNext();) {
                processEvent((BuildEvent)iter.next());
            }
            null;
        }
    }
    /* (non-Javadoc)
     * @see org.apache.tools.ant.BuildListener#messageLogged(org.apache.tools.ant.BuildEvent)
     */
    public void messageLogged(BuildEvent event) {
        if (event.getPriority() > msgOutputLevel && event.getPriority() != .) {
            return;
        }
        
        if (!) {
            if (event.getPriority() == .) {
                if (Project.MSG_INFO > msgOutputLevel) {
                    return;
                }
                //no buildstarted or project started for project help option
                establishConnection();
                return;
            }
            if ( == null){
                new ArrayList(10);
            }
            .add(event);
            return;
        }
        
        processEvent(event);
    }
    private void processEvent(BuildEvent event) {
        if (event.getTask() != null & !emacsMode) {
            try {
                marshalTaskMessage(event);
            } catch (IOException e) {
            }
        } else {
            marshalMessage(event);
        }
    }
    
    private void marshalMessage(BuildEvent event) {
        String eventMessageevent.getMessage();
        if (eventMessage.length() == 0) {
            return;
        }
        marshalMessage(event.getPriority(), eventMessage);
    }
    protected void marshalMessage(int priorityString message) {
        try {
            BufferedReader r = new BufferedReader(new StringReader(message));
            String line = r.readLine();
            StringBuffer messageLine;
            while (line != null) {
                messageLinenew StringBuffer();
                if (priority != -1) {
                    messageLine.append(priority);
                    messageLine.append(',');
                }
                messageLine.append(line);
                sendMessage(messageLine.toString());
                line = r.readLine();
            }
        } catch (IOException e) {
        }
    }
    private void marshalTaskMessage(BuildEvent eventthrows IOException {
        String eventMessageevent.getMessage();
        if (eventMessage.length() == 0) {
            return;
        }
        BufferedReader r = new BufferedReader(new StringReader(eventMessage));
        String line = r.readLine();
        StringBuffer message;
        String taskNameevent.getTask().getTaskName();
        if (taskName != null && taskName.equals()) {
            taskName""//$NON-NLS-1$
        } else {
            taskName;
        }
        Location locationevent.getTask().getLocation();
        String fileNamenull;
        int lineNumber= -1;
        try {
            fileNamelocation.getFileName();
            lineNumberlocation.getLineNumber();
        } catch (NoSuchMethodError e) {
            //older Ant
            fileNamelocation.toString();
        }
        if (location.equals(Location.UNKNOWN_LOCATION)) {
            fileNamelocation.toString();
            lineNumber= -1;
        }
        int priorityevent.getPriority();
        while (line != null) {
            messagenew StringBuffer(.);
            message.append(priority);
            message.append(',');
            message.append(taskName);
            message.append(',');
            message.append(line.length());
            message.append(',');
            message.append(line);
            message.append(',');
            if (!fileName.equals()) {
            	message.append(fileName.length());
            	message.append(',');
                message.append(fileName);
            } 
            message.append(',');
            message.append(lineNumber);
            sendMessage(message.toString());
            fileName;
            liner.readLine();
        }
    }
    
    private void marshalTargetMessage(BuildEvent event) {
        Target targetevent.getTarget();
        Location location= AntDebugState.getLocation(target);
        
        StringBuffer messagenew StringBuffer();
        message.append(.);
        message.append(',');
        message.append(target.getName());
        message.append(':');
        message.append(',');
        if (location != null && location != Location.UNKNOWN_LOCATION) {
            //if a target has a valid location then we are on an Ant that is 
            //new enough to have the accessor methods on Location
        	String fileNamelocation.getFileName();
        	message.append(fileName.length());
        	message.append(',');
            message.append(fileName);
            message.append(',');
            message.append(location.getLineNumber());
        }
        sendMessage(message.toString());
    }
    /* (non-Javadoc)
     * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
     */
    public void buildStarted(BuildEvent event) {
        establishConnection();
        super.buildStarted(event);
    }
    
    public void configure(Map userProperties) {
        String portProperty= (StringuserProperties.remove("eclipse.connect.port"); //$NON-NLS-1$
        
        if (portProperty != null) {
            = Integer.parseInt(portProperty);
        }
        
        = (StringuserProperties.remove("org.eclipse.ant.core.ANT_PROCESS_ID"); //$NON-NLS-1$
    } 
New to GrepCode? Check out our FAQ X