EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.scriptbuilder.domain]

COVERAGE SUMMARY FOR SOURCE FILE [AppendFile.java]

nameclass, %method, %block, %line, %
AppendFile.java100% (1/1)100% (9/9)98%  (254/258)97%  (38/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AppendFile100% (1/1)100% (9/9)98%  (254/258)97%  (38/39)
appendToFile (String, String, OsFamily): Statement 100% (1/1)93%  (40/43)80%  (4/5)
AppendFile (String, Iterable, String): void 100% (1/1)97%  (28/29)99%  (6/6)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
AppendFile (String, Iterable): void 100% (1/1)100% (6/6)100% (2/2)
addSpaceToEnsureWeDontAccidentallyRedirectFd (String): String 100% (1/1)100% (12/12)100% (1/1)
escapeVarTokens (String, OsFamily): String 100% (1/1)100% (69/69)100% (9/9)
functionDependencies (OsFamily): Iterable 100% (1/1)100% (2/2)100% (1/1)
hereFile (String, StringBuilder): void 100% (1/1)100% (39/39)100% (5/5)
render (OsFamily): String 100% (1/1)100% (54/54)100% (9/9)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.scriptbuilder.domain;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Preconditions.checkState;
23import static org.jclouds.scriptbuilder.domain.Statements.interpret;
24 
25import java.util.Collections;
26import java.util.List;
27import java.util.Map;
28import java.util.Map.Entry;
29import java.util.regex.Pattern;
30 
31import com.google.common.collect.ImmutableList;
32import com.google.common.collect.Iterables;
33import com.google.common.collect.Lists;
34import com.google.common.collect.Maps;
35 
36/**
37 * Creates a run script
38 * 
39 * @author Adrian Cole
40 */
41public class AppendFile implements Statement {
42   public static final String MARKER = "END_OF_FILE";
43 
44   final String path;
45   final Iterable<String> lines;
46   final String marker;
47 
48   public AppendFile(String path, Iterable<String> lines) {
49      this(path, lines, MARKER);
50   }
51 
52   public AppendFile(String path, Iterable<String> lines, String marker) {
53      this.path = checkNotNull(path, "path");
54      this.lines = checkNotNull(lines, "lines");
55      this.marker = checkNotNull(marker, "marker");
56      checkState(Iterables.size(lines) > 0, "you must pass something to execute");
57   }
58 
59   public static String escapeVarTokens(String toEscape, OsFamily family) {
60      Map<String, String> inputToEscape = Maps.newHashMap();
61      for (ShellToken token : ImmutableList.of(ShellToken.VARL, ShellToken.VARR)) {
62         if (!token.to(family).equals("")) {
63            String tokenS = "{" + token.toString().toLowerCase() + "}";
64            inputToEscape.put(tokenS, "{escvar}" + tokenS);
65         }
66      }
67      for (Entry<String, String> entry : inputToEscape.entrySet()) {
68         toEscape = toEscape.replace(entry.getKey(), entry.getValue());
69      }
70      return toEscape;
71   }
72 
73   @Override
74   public Iterable<String> functionDependencies(OsFamily family) {
75      return Collections.emptyList();
76   }
77 
78   @Override
79   public String render(OsFamily family) {
80      List<Statement> statements = Lists.newArrayList();
81      if (family == OsFamily.UNIX) {
82         StringBuilder builder = new StringBuilder();
83         hereFile(path, builder);
84         statements.add(interpret(builder.toString()));
85      } else {
86         for (String line : lines) {
87            statements.add(appendToFile(line, path, family));
88         }
89      }
90      return new StatementList(statements).render(family);
91   }
92 
93   private void hereFile(String path, StringBuilder builder) {
94      builder.append("cat >> ").append(path).append(" <<'").append(marker).append("'\n");
95      for (String line : lines) {
96         builder.append(line).append("\n");
97      }
98      builder.append(marker).append("\n");
99   }
100 
101   private Statement appendToFile(String line, String path, OsFamily family) {
102      String quote = "";
103      if (!ShellToken.VQ.to(family).equals("")) {
104         quote = "'";
105      } else {
106         line = escapeVarTokens(line, family);
107      }
108      return interpret(addSpaceToEnsureWeDontAccidentallyRedirectFd(String.format("echo %s%s%s>>%s{lf}", quote, line,
109               quote, path)));
110   }
111 
112   public static final Pattern REDIRECT_FD_PATTERN = Pattern.compile(".*[0-2]>>.*");
113 
114   static String addSpaceToEnsureWeDontAccidentallyRedirectFd(String line) {
115      return REDIRECT_FD_PATTERN.matcher(line).matches() ? line.replace(">>", " >>") : line;
116   }
117 
118}

[all classes][org.jclouds.scriptbuilder.domain]
EMMA 2.0.5312 (C) Vladimir Roubtsov