buildframework/helium/sf/java/legacy/src/com/nokia/ant/taskdefs/HlmExecTask.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
* All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
* This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
* which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
* Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
* Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
* Description: 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
*/
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.ant.taskdefs;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    20
import java.util.concurrent.ExecutorService;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
import java.util.concurrent.Executors;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    22
import java.util.concurrent.ThreadPoolExecutor;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    23
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import org.apache.tools.ant.BuildException;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    25
import org.apache.tools.ant.taskdefs.ExecTask;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
 * Exec task using shared thread pool
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
 * @ant.task name="exec"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
public class HlmExecTask extends ExecTask
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
{   
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
    private static int poolSize = Runtime.getRuntime().availableProcessors();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
    private static int maxPoolSize = poolSize * 2;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
    private static ExecutorService threadPool = Executors.newFixedThreadPool(maxPoolSize);
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    private final Object semaphore = new Object();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
      * Submit exec into pool and throw exceptions
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
      */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    public void execute()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    43
        String property = getProject().getProperty("number.of.threads");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    44
        if (property != null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    45
            ((ThreadPoolExecutor)threadPool).setCorePoolSize(Integer.parseInt(property));
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    46
            ((ThreadPoolExecutor)threadPool).setMaximumPoolSize(Integer.parseInt(property));
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
        }
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    48
        TaskRunnable taskRunnable = new TaskRunnable();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    49
        threadPool.submit(taskRunnable);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
        try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
            synchronized (semaphore) {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    52
                while (!taskRunnable.isFinished()) {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
                    semaphore.wait();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
                }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
        } catch (InterruptedException e) { e.printStackTrace(); }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
          
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    58
        Throwable exception = taskRunnable.getException();
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    59
        if (exception != null)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    61
            if (exception instanceof BuildException) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    62
                throw (BuildException)exception;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
            } else {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    64
                exception.printStackTrace();
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    65
            }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
    private class TaskRunnable implements Runnable {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
        private boolean finished;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
        private volatile Thread thread;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
        private Throwable exception;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
         * Executes the task within a thread and takes care about
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
         * Exceptions raised within the task.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
         */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        public void run() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
            try {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
                thread = Thread.currentThread();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
                HlmExecTask.super.execute();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
            } // CheckStyle:IllegalCatch OFF
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
            catch (Throwable t) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
                exception = t;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
            } // CheckStyle:IllegalCatch ON
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
            finally {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
                synchronized (semaphore) {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
                    finished = true;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
                    semaphore.notifyAll();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
                }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
            }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
        /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
         * get any exception that got thrown during execution;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
         * @return an exception or null for no exception/not yet finished
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
         */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
        public Throwable getException() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
            return exception;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
        /**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
         * Provides the indicator that the task has been finished.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
         * @return Returns true when the task is finished.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
         */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
        boolean isFinished() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
            return finished;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
        void interrupt() {
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
            thread.interrupt();
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
}