Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2008 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.launchConfigurations;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 import  org.apache.tools.ant.Project;
 import  org.apache.tools.ant.util.FileUtils;
Parts adapted from org.eclipse.jdt.internal.junit.ui.RemoteTestRunnerClient The client side of the RemoteAntBuildLogger. Handles the marshalling of the different messages.
 
 public class RemoteAntBuildListener implements ILaunchesListener {
     public abstract class ListenerSafeRunnable implements ISafeRunnable {
         public void handleException(Throwable exception) {
             AntUIPlugin.log(exception);
         }
     }

    
The server socket
 
     private ServerSocket fServerSocket;
     private Socket fSocket;
     private BufferedReader fBufferedReader;
     private IProcess fProcess;
     private String fProcessId;
     private File fBuildFileParentnull;
     private List fMessageQueue;
     protected ILaunch fLaunch;
     private Map fFileNameToIFilenew HashMap();
     private String fLastFileNamenull;
     private String fLastTaskNamenull;
     private boolean fBuildFailedfalse;
    
    
Reads the message stream from the RemoteAntBuildLogger
 
     private class ServerConnection extends Thread {
         private int fServerPort;
         
         public ServerConnection(int port) {
             super("Ant Build Server Connection"); //$NON-NLS-1$
             setDaemon(true);
             port;
         }
         
         public void run() {
             Exception exceptionnull;
             try {
                 new ServerSocket();
                 IPreferenceStore prefs = AntUIPlugin.getDefault().getPreferenceStore();
                 int socketTimeoutprefs.getInt(.);
                 .setSoTimeout(socketTimeout);
                 .accept();
                 new BufferedReader(new InputStreamReader(.getInputStream(), "UTF-8")); //$NON-NLS-1$
                 String message;
                 while( != null && (message.readLine()) != null) {
                     receiveMessage(message);
                }
            } catch (SocketException e) {
            } catch (SocketTimeoutException e) {
                exceptione;
            } catch (IOException e) {
                // fall through
                exceptione;
            }
            if (exception != null) {
                AntUIPlugin.log(exception);
            }
            shutDown();
        }
    }
    
    public RemoteAntBuildListener(ILaunch launch) {
        super();
        launch;
        DebugPlugin.getDefault().getLaunchManager().addLaunchListener(this);
    }

    
Start listening to an Ant build. Start a server connection that the RemoteAntBuildLogger can connect to.

Parameters:
eventPort The port number to create the server connection on
    public synchronized void startListening(int eventPort){
        ServerConnection connection = new ServerConnection(eventPort);
        connection.start();
    }
    protected synchronized void shutDown() {
        null;
        null;
        if (DebugPlugin.getDefault() != null) {
        	DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this);
        }
        try {
            if ( != null) {
                .close();
                null;
            }
        } catch(IOException e) {
        }   
        try{
            if( != null) {
                .close();
                null;
            }
        } catch(IOException e) {
        }
        try{
            if( != null) {
                .close();
                null;
            }
        } catch(IOException e) {
        }
    }
        
    protected void receiveMessage(String message) {
        if (message.startsWith(.)) {
            receiveTaskMessage(message);
        } else if (message.startsWith(.)) {
            receiveTargetMessage(message);
        } else if (message.startsWith(.)) {
            messagemessage.substring(..length());
            message;
        } else {
            int indexmessage.indexOf(',');
            if (index > 0) {
                int priority= Integer.parseInt(message.substring(0, index));
                messagemessage.substring(index + 1);
                
                writeMessage(message + System.getProperty("line.separator"), priority); //$NON-NLS-1$
                if (message.startsWith("BUILD FAILED")) { //$NON-NLS-1$
                    true;
                } else if () {
                	if (message.startsWith("Total time:")) { //$NON-NLS-1$
                		false;
                	} else {
                		AntUtil.linkBuildFailedMessage(messagegetProcess());
                	}
				}
            }
        }
    }
    private void receiveTargetMessage(String message) {
        messagemessage.substring(..length());
        StringTokenizer tokenizernew StringTokenizer(message","); //$NON-NLS-1$
        messagetokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
        	int locationLength= Integer.parseInt(tokenizer.nextToken());
        	String locationtokenizer.nextToken(); 
        	while (location.length() < locationLength) { //path with a comma in it
        		location+=","//$NON-NLS-1$
        		location+= tokenizer.nextToken();
        	}
            int lineNumber= Integer.parseInt(tokenizer.nextToken());
            generateLink(messagelocationlineNumber, 0, message.length() - 1);
        }
        writeMessage(message + System.getProperty("line.separator"), Project.MSG_INFO); //$NON-NLS-1$
    }
    private void receiveTaskMessage(String message) {
        messagemessage.substring(..length());
        
        int indexmessage.indexOf(',');
        int priority= Integer.parseInt(message.substring(0, index));
        int index2message.indexOf(','index + 1);
        String taskNamemessage.substring(index + 1, index2);
        if (taskName.length() == 0) {
            taskName;
        }
        int index3message.indexOf(','index2 + 1);
        int lineLength= Integer.parseInt(message.substring(index2 + 1, index3));
        int index4index3 + 1 + lineLength;
        
        String linemessage.substring(index3 + 1, index4);
        StringBuffer labelBuffnew StringBuffer();
        labelBuff.append('[');
        labelBuff.append(taskName);
        labelBuff.append("] "); //$NON-NLS-1$
        labelBuff.append(line);
        linelabelBuff.toString();
        
        taskName;
        
        int locationIndexmessage.indexOf(','index4 + 1);
        int finalIndexlocationIndex + 1;
        String fileNamemessage.substring(index4 + 1, locationIndex);
        int locationLength= 0;
        if (fileName.length() == 0) {
            fileName;
        } else {
        	finalIndexmessage.indexOf(','locationIndex) + 1;
        	locationLength= Integer.parseInt(fileName);
        	fileNamemessage.substring(finalIndexfinalIndex + locationLength);
        	locationLength+=1; //set past delimiter
        }
        fileName;
        int lineNumber= Integer.parseInt(message.substring(finalIndex + locationLength));
        int size = . - (taskName.length() + 3);
        int offset = Math.max(size - 2, 1);
        int length = . - size - 3;
        if (fileName != null) {
            generateLink(linefileNamelineNumberoffsetlength);
        }
        
        StringBuffer fullMessagenew StringBuffer();
        adornMessage(taskNamelinefullMessage);
        writeMessage(fullMessage.append(System.getProperty("line.separator")).toString(), priority); //$NON-NLS-1$
    }
    private void generateLink(String lineString fileNameint lineNumberint offsetint length) {
        IHyperlink taskLinknull;
        if (lineNumber == -1) {
            //fileName will actually be the String representation of Location
           taskLink = AntUtil.getLocationLink(fileName);
        } else {
            IFile file= (IFile.get(fileName);
            if (file == null) {
                file= AntUtil.getFileForLocation(fileName);
                if (file != null) {
                    .put(fileNamefile);
                    taskLinknew FileLink(filenull, -1, -1, lineNumber);
                } else {
                    File javaIOFile= FileUtils.getFileUtils().resolveFile(fileName);
                    if (javaIOFile.exists()) {
                        taskLinknew ExternalHyperlink(javaIOFilelineNumber);
                    }
                }
            } else {
                taskLinknew FileLink(filenull, -1, -1, lineNumber);
            }
        }
        if (taskLink != null) {
            TaskLinkManager.addTaskHyperlink(getProcess(), taskLinknew Region(offsetlength), line);
        }
    }
    
    
Returns the associated process, finding it if necessary.
    protected IProcess getProcess() {
        if ( == null) {
            if ( != null) {
                IProcess[] all = DebugPlugin.getDefault().getLaunchManager().getProcesses();
                for (int i = 0; i < all.lengthi++) {
                    IProcess process = all[i];
                    if (.equals(process.getAttribute(.))) {
                         = process;
                        break;
                    }
                }
            }
        }
        return ;
    }
    
    private AntStreamMonitor getMonitor(int priority) {
        IProcess processgetProcess();
        if (process == null) {
            return null;
        }
        AntStreamsProxy proxy = (AntStreamsProxy)process.getStreamsProxy();
        if (proxy == null) {
            return null;
        }
        AntStreamMonitor monitor = null;
        switch (priority) {
            case Project.MSG_INFO:
                monitor = (AntStreamMonitor)proxy.getOutputStreamMonitor();
                break;
            case Project.MSG_ERR:
                monitor = (AntStreamMonitor)proxy.getErrorStreamMonitor();
                break;
            case Project.MSG_DEBUG:
                monitor = (AntStreamMonitor)proxy.getDebugStreamMonitor();
                break;
            case Project.MSG_WARN:
                monitor = (AntStreamMonitor)proxy.getWarningStreamMonitor();
                break;
            case Project.MSG_VERBOSE:
                monitor = (AntStreamMonitor)proxy.getVerboseStreamMonitor();
                break;
        }
        return monitor;
    }
    
    
Builds a right justified task prefix for the given build event, placing it in the given string buffer.

Parameters:
event build event
fullMessage buffer to place task prefix in
    private void adornMessage(String taskNameString lineStringBuffer fullMessage) {
        if (taskName == null) {
            taskName = "null"//$NON-NLS-1$
        }
        
        int size = . - (taskName.length() + 6);
        for (int i = 0; i < sizei++) {
            fullMessage.append(' ');
        }
        
        fullMessage.append(line);
    }
    
    protected void writeMessage(String messageint priority) {
        AntStreamMonitor monitorgetMonitor(priority);
        if (monitor == null) {
            if ( == null) {
                new ArrayList();
            }
            .add(message);
            return;
        }
        if ( != null) {
            for (Iterator iter = .iterator(); iter.hasNext();) {
                String oldMessage = (Stringiter.next();
                monitor.append(oldMessage);
            }
            null;
        }
        monitor.append(message);
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.debug.core.ILaunchesListener#launchesAdded(org.eclipse.debug.core.ILaunch[])
     */
    public void launchesAdded(ILaunch[] launches) {
    }
    /* (non-Javadoc)
     * @see org.eclipse.debug.core.ILaunchesListener#launchesChanged(org.eclipse.debug.core.ILaunch[])
     */
    public void launchesChanged(ILaunch[] launches) {
    }
    /* (non-Javadoc)
     * @see org.eclipse.debug.core.ILaunchesListener#launchesRemoved(org.eclipse.debug.core.ILaunch[])
     */
    public void launchesRemoved(ILaunch[] launches) {
        for (int i = 0; i < launches.lengthi++) {
            ILaunch launch = launches[i];
            if (launch.equals()) {
                shutDown();
                return;
            }
        }
    }
New to GrepCode? Check out our FAQ X