buildframework/helium/tools/common/java/src/com/nokia/ant/BSFJepEngine.java
changeset 179 d8ac696cc51f
parent 1 be27ed110b50
child 180 e02a83d4c571
child 592 3215c239276a
equal deleted inserted replaced
1:be27ed110b50 179:d8ac696cc51f
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17  
       
    18 package com.nokia.ant;
       
    19 
       
    20 import java.io.*;
       
    21 import org.apache.bsf.BSFException;
       
    22 import org.apache.bsf.BSFManager;
       
    23 
       
    24 /**
       
    25  * Override default implementation to support source with are not files.
       
    26  * 
       
    27  * @author Helium Team
       
    28  * @see jep.BSFJepEngine
       
    29  */
       
    30 public class BSFJepEngine extends jep.BSFJepEngine
       
    31 {
       
    32 
       
    33     static
       
    34     {
       
    35         BSFManager.registerScriptingEngine("jep", "com.nokia.ant.BSFJepEngine", new String[]
       
    36         { "py" });
       
    37     }
       
    38 
       
    39     /**
       
    40      * Execute a script.
       
    41      * 
       
    42      * @param source
       
    43      *            a <code>String</code> value
       
    44      * @param lineNo
       
    45      *            an <code>int</code> value
       
    46      * @param columnNo
       
    47      *            an <code>int</code> value
       
    48      * @param script
       
    49      *            an <code>Object</code> value
       
    50      * @exception BSFException
       
    51      *                if an error occurs
       
    52      */
       
    53     public final void exec(final String source, final int lineNo, final int columnNo, final Object script) throws BSFException
       
    54     {
       
    55         boolean deleteTemp = false;
       
    56         File file = null;
       
    57         try
       
    58         {
       
    59             file = new File(script.toString());
       
    60             if (file.exists() && file.isFile())
       
    61             {
       
    62                 super.exec(source, lineNo, columnNo, script);
       
    63             }
       
    64             else
       
    65             {
       
    66                 deleteTemp = true;
       
    67                 file = File.createTempFile("helium", null);
       
    68                 PrintWriter output = new PrintWriter(new FileOutputStream(file));
       
    69                 output.write(script.toString());
       
    70                 output.close();
       
    71                 super.exec(source, lineNo, columnNo, file.getAbsolutePath());
       
    72             }
       
    73         }
       
    74         catch (Exception e)
       
    75         {
       
    76             throw new BSFException(BSFException.REASON_EXECUTION_ERROR, e.toString(), e);
       
    77         }
       
    78         finally
       
    79         {
       
    80             terminate();
       
    81             if (deleteTemp && file != null && file.exists()) {
       
    82                 file.delete();
       
    83             }
       
    84         }
       
    85     }
       
    86 }