001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.shell.automation;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.URL;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.ecm.automation.client.OperationRequest;
029import org.nuxeo.ecm.automation.client.model.Blob;
030import org.nuxeo.ecm.automation.client.model.FileBlob;
031import org.nuxeo.ecm.automation.client.model.StreamBlob;
032import org.nuxeo.shell.Shell;
033import org.nuxeo.shell.ShellException;
034import org.nuxeo.shell.fs.FileSystem;
035
036/**
037 * Helper class to run remote scripts.
038 *
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class Scripting {
042
043    public static String run(File script, Map<String, Object> args, Integer timeout) throws IOException {
044        FileInputStream in = new FileInputStream(script);
045        try {
046            return run(script.getName(), in, args, timeout);
047        } finally {
048            try {
049                in.close();
050            } catch (Exception e) {
051            }
052        }
053
054    }
055
056    public static String run(String resource, Map<String, Object> args, Integer timeout) throws IOException {
057        InputStream in = Scripting.class.getClassLoader().getResourceAsStream(resource);
058        if (in == null) {
059            throw new FileNotFoundException("No such resource: " + resource);
060        }
061        try {
062            return run(resource, in, args, timeout);
063        } finally {
064            try {
065                in.close();
066            } catch (Exception e) {
067            }
068        }
069    }
070
071    public static String run(URL url, Map<String, Object> args, Integer timeout) throws IOException {
072        InputStream in = url.openStream();
073        try {
074            return run(url.getFile(), in, args, timeout);
075        } finally {
076            try {
077                in.close();
078            } catch (Exception e) {
079            }
080        }
081    }
082
083    public static String run(String name, InputStream in, Map<String, Object> args, Integer timeout) {
084        try {
085            return runScript(Shell.get().getContextObject(RemoteContext.class), new StreamBlob(in, name, "text/plain"),
086                    args, timeout);
087        } catch (ShellException e) {
088            throw e;
089        } catch (Exception e) {
090            throw new ShellException(e);
091        }
092    }
093
094    public static String runScript(RemoteContext ctx, Blob blob, Map<String, Object> args, Integer timeout)
095            throws Exception {
096        String fname = blob.getFileName();
097        if (fname != null) {
098            if (fname.endsWith(".groovy")) {
099                fname = "groovy";
100            } else {
101                fname = null;
102            }
103        }
104        if (args == null) {
105            args = new HashMap<String, Object>();
106        }
107        OperationRequest req = ctx.getSession().newRequest("Context.RunInputScript", args).setInput(blob);
108
109        if (timeout != null) {
110            req.setHeader("Nuxeo-Transaction-Timeout", "" + timeout * 1000);
111        }
112        if (fname != null) {
113            req.set("type", fname);
114        }
115        Blob response = (Blob) req.execute();
116        if (response != null) {
117            InputStream in = response.getStream();
118            String str = null;
119            try {
120                str = FileSystem.readContent(in);
121            } finally {
122                in.close();
123                if (response instanceof FileBlob) {
124                    ((FileBlob) response).getFile().delete();
125                }
126            }
127            return str;
128        }
129        return null;
130    }
131
132}