View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.deployment.cli;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.util.Collection;
24  import java.util.LinkedList;
25  import java.util.List;
26  import javax.enterprise.deploy.spi.TargetModuleID;
27  import org.apache.geronimo.common.DeploymentException;
28  import org.apache.geronimo.deployment.plugin.ConfigIDExtractor;
29  import org.apache.geronimo.kernel.repository.Artifact;
30  
31  /**
32   * Various helpers for deployment.
33   *
34   * @version $Rev: 396315 $ $Date: 2006-04-23 13:48:50 -0700 (Sun, 23 Apr 2006) $
35   */
36  public class DeployUtils extends ConfigIDExtractor {
37      /**
38       * Split up an output line so it indents at beginning and end (to fit in a
39       * typical terminal) and doesn't break in the middle of a word.
40       * @param source The unformatted String
41       * @param indent The number of characters to indent on the left
42       * @param endCol The maximum width of the entire line in characters,
43       *               including indent (indent 10 with endCol 70 results
44       *               in 60 "usable" characters).
45       */
46      public static String reformat(String source, int indent, int endCol) {
47          if(endCol-indent < 10) {
48              throw new IllegalArgumentException("This is ridiculous!");
49          }
50          StringBuffer buf = new StringBuffer((int)(source.length()*1.1));
51          String prefix = indent == 0 ? "" : buildIndent(indent);
52          try {
53              BufferedReader in = new BufferedReader(new StringReader(source));
54              String line;
55              int pos;
56              while((line = in.readLine()) != null) {
57                  if(buf.length() > 0) {
58                      buf.append('\n');
59                  }
60                  while(line.length() > 0) {
61                      line = prefix + line;
62                      if(line.length() > endCol) {
63                          pos = line.lastIndexOf(' ', endCol);
64                          if(pos < indent) {
65                              pos = line.indexOf(' ', endCol);
66                              if(pos < indent) {
67                                  pos = line.length();
68                              }
69                          }
70                          buf.append(line.substring(0, pos)).append('\n');
71                          if(pos < line.length()-1) {
72                              line = line.substring(pos+1);
73                          } else {
74                              break;
75                          }
76                      } else {
77                          buf.append(line).append("\n");
78                          break;
79                      }
80                  }
81              }
82          } catch (IOException e) {
83              throw new AssertionError("This should be impossible");
84          }
85          return buf.toString();
86      }
87  
88      private static String buildIndent(int indent) {
89          StringBuffer buf = new StringBuffer(indent);
90          for(int i=0; i<indent; i++) {
91              buf.append(' ');
92          }
93          return buf.toString();
94      }
95  
96  
97  }