| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Arrays; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.jclouds.scriptbuilder.util.Utils; |
| 27 | |
| 28 | import com.google.common.collect.ImmutableList; |
| 29 | import com.google.common.collect.ImmutableMap; |
| 30 | |
| 31 | /** |
| 32 | * Statement used in a shell script |
| 33 | * |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public class Call implements Statement { |
| 37 | |
| 38 | public static final Map<OsFamily, String> OS_TO_CALL = ImmutableMap.of(OsFamily.UNIX, |
| 39 | "{function}{args} || return 1\n", OsFamily.WINDOWS, |
| 40 | "call :{function}{args}\r\nif errorlevel 1 goto abort\r\n"); |
| 41 | |
| 42 | private String function; |
| 43 | private String[] args; |
| 44 | |
| 45 | public Call(String function, String... args) { |
| 46 | this.function = checkNotNull(function, "function"); |
| 47 | this.args = checkNotNull(args, "args"); |
| 48 | } |
| 49 | |
| 50 | public String render(OsFamily family) { |
| 51 | StringBuilder args = new StringBuilder(); |
| 52 | for (String arg : this.args) { |
| 53 | args.append(" ").append(Utils.replaceTokens(arg, ShellToken.tokenValueMap(family))); |
| 54 | } |
| 55 | StringBuilder call = new StringBuilder(); |
| 56 | call.append(Utils.replaceTokens(OS_TO_CALL.get(family), ImmutableMap.of("function", function, |
| 57 | "args", args.toString()))); |
| 58 | return call.toString(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public int hashCode() { |
| 63 | final int prime = 31; |
| 64 | int result = 1; |
| 65 | result = prime * result + Arrays.hashCode(args); |
| 66 | result = prime * result + ((function == null) ? 0 : function.hashCode()); |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object obj) { |
| 72 | if (this == obj) |
| 73 | return true; |
| 74 | if (obj == null) |
| 75 | return false; |
| 76 | if (getClass() != obj.getClass()) |
| 77 | return false; |
| 78 | Call other = (Call) obj; |
| 79 | if (!Arrays.equals(args, other.args)) |
| 80 | return false; |
| 81 | if (function == null) { |
| 82 | if (other.function != null) |
| 83 | return false; |
| 84 | } else if (!function.equals(other.function)) |
| 85 | return false; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Iterable<String> functionDependencies(OsFamily family) { |
| 91 | return ImmutableList.of(function); |
| 92 | } |
| 93 | } |