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.cmds; 020 021import java.io.File; 022 023import org.nuxeo.ecm.automation.client.Constants; 024import org.nuxeo.ecm.automation.client.OperationRequest; 025import org.nuxeo.ecm.automation.client.model.FileBlob; 026import org.nuxeo.shell.Argument; 027import org.nuxeo.shell.Command; 028import org.nuxeo.shell.Context; 029import org.nuxeo.shell.Parameter; 030import org.nuxeo.shell.ShellException; 031import org.nuxeo.shell.automation.ChainCompletor; 032import org.nuxeo.shell.automation.DocRefCompletor; 033import org.nuxeo.shell.automation.RemoteContext; 034import org.nuxeo.shell.utils.StringUtils; 035 036/** 037 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 038 */ 039@Command(name = "runonfile", help = "Run a server automation chain that accepts a file as an input") 040public class RunChainWithFile implements Runnable { 041 042 @Context 043 protected RemoteContext ctx; 044 045 @Parameter(name = "-void", hasValue = false, help = "Use this to avoid having the server sending back the result.") 046 protected boolean isVoid; 047 048 @Parameter(name = "-ctx", hasValue = true, help = "Use this to set execution context variables. Syntax is: k1=v1,k1=v2") 049 protected String ctxVars; 050 051 @Parameter(name = "-s", hasValue = true, help = "Use this to change the separator used in context variables. The default is ','") 052 protected String sep = ","; 053 054 @Argument(name = "chain", index = 0, required = true, completor = ChainCompletor.class, help = "The chain to run") 055 protected String chain; 056 057 @Argument(name = "file", index = 1, required = true, completor = DocRefCompletor.class, help = "A reference to the new context document to use. To use UID references prefix them with 'doc:'.") 058 protected File file; 059 060 public void run() { 061 try { 062 OperationRequest request = ctx.getSession().newRequest(chain).setInput(new FileBlob(file)); 063 if (ctxVars != null) { 064 for (String pair : ctxVars.split(sep)) { 065 String[] ar = StringUtils.split(pair, '=', true); 066 request.setContextProperty(ar[0], ar[1]); 067 } 068 } 069 if (isVoid) { 070 request.setHeader(Constants.HEADER_NX_VOIDOP, "true"); 071 } 072 request.execute(); 073 074 } catch (Exception e) { 075 throw new ShellException(e); 076 } 077 } 078 079}