001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.shell.automation;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.FileNotFoundException;
024import java.io.IOException;
025import java.io.InputStream;
026import java.net.URL;
027import java.util.HashMap;
028import java.util.Map;
029
030import org.nuxeo.ecm.automation.client.OperationRequest;
031import org.nuxeo.ecm.automation.client.model.Blob;
032import org.nuxeo.ecm.automation.client.model.FileBlob;
033import org.nuxeo.ecm.automation.client.model.StreamBlob;
034import org.nuxeo.shell.Shell;
035import org.nuxeo.shell.ShellException;
036import org.nuxeo.shell.fs.FileSystem;
037
038/**
039 * Helper class to run remote scripts.
040 *
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class Scripting {
044
045    public static String run(File script, Map<String, Object> args, Integer timeout) throws IOException {
046        FileInputStream in = new FileInputStream(script);
047        try {
048            return run(script.getName(), in, args, timeout);
049        } finally {
050            try {
051                in.close();
052            } catch (Exception e) {
053            }
054        }
055
056    }
057
058    public static String run(String resource, Map<String, Object> args, Integer timeout) throws IOException {
059        InputStream in = Scripting.class.getClassLoader().getResourceAsStream(resource);
060        if (in == null) {
061            throw new FileNotFoundException("No such resource: " + resource);
062        }
063        try {
064            return run(resource, in, args, timeout);
065        } finally {
066            try {
067                in.close();
068            } catch (Exception e) {
069            }
070        }
071    }
072
073    public static String run(URL url, Map<String, Object> args, Integer timeout) throws IOException {
074        InputStream in = url.openStream();
075        try {
076            return run(url.getFile(), in, args, timeout);
077        } finally {
078            try {
079                in.close();
080            } catch (Exception e) {
081            }
082        }
083    }
084
085    public static String run(String name, InputStream in, Map<String, Object> args, Integer timeout) {
086        try {
087            return runScript(Shell.get().getContextObject(RemoteContext.class), new StreamBlob(in, name, "text/plain"),
088                    args, timeout);
089        } catch (ShellException e) {
090            throw e;
091        } catch (Exception e) {
092            throw new ShellException(e);
093        }
094    }
095
096    public static String runScript(RemoteContext ctx, Blob blob, Map<String, Object> args, Integer timeout)
097            throws Exception {
098        String fname = blob.getFileName();
099        if (fname != null) {
100            if (fname.endsWith(".groovy")) {
101                fname = "groovy";
102            } else {
103                fname = null;
104            }
105        }
106        if (args == null) {
107            args = new HashMap<String, Object>();
108        }
109        OperationRequest req = ctx.getSession().newRequest("Context.RunInputScript", args).setInput(blob);
110
111        if (timeout != null) {
112            req.setHeader("Nuxeo-Transaction-Timeout", "" + timeout * 1000);
113        }
114        if (fname != null) {
115            req.set("type", fname);
116        }
117        Blob response = (Blob) req.execute();
118        if (response != null) {
119            InputStream in = response.getStream();
120            String str = null;
121            try {
122                str = FileSystem.readContent(in);
123            } finally {
124                in.close();
125                if (response instanceof FileBlob) {
126                    ((FileBlob) response).getFile().delete();
127                }
128            }
129            return str;
130        }
131        return null;
132    }
133
134}