View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF 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.apache.maven.surefire.booter;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.util.Collections;
24  import java.util.List;
25  
26  /**
27   * Configuration that is used by the SurefireStarter but does not make it into the provider itself.
28   *
29   * @author Kristian Rosenvold
30   */
31  public class StartupConfiguration {
32      private static final String SUREFIRE_TEST_CLASSPATH = "surefire.test.class.path";
33  
34      private final String providerClassName;
35      private final AbstractPathConfiguration classpathConfiguration;
36      private final ClassLoaderConfiguration classLoaderConfiguration;
37      private final ProcessCheckerType processChecker;
38      private final List<String[]> jpmsArguments;
39  
40      public StartupConfiguration(
41              @Nonnull String providerClassName,
42              @Nonnull AbstractPathConfiguration classpathConfiguration,
43              @Nonnull ClassLoaderConfiguration classLoaderConfiguration,
44              ProcessCheckerType processChecker,
45              @Nonnull List<String[]> jpmsArguments) {
46          this.classpathConfiguration = classpathConfiguration;
47          this.classLoaderConfiguration = classLoaderConfiguration;
48          this.providerClassName = providerClassName;
49          this.processChecker = processChecker;
50          this.jpmsArguments = jpmsArguments;
51      }
52  
53      public boolean isProviderMainClass() {
54          return providerClassName.endsWith("#main");
55      }
56  
57      public static StartupConfiguration inForkedVm(
58              String providerClassName,
59              ClasspathConfiguration classpathConfig,
60              ClassLoaderConfiguration classLoaderConfig,
61              ProcessCheckerType processChecker) {
62          return new StartupConfiguration(
63                  providerClassName,
64                  classpathConfig,
65                  classLoaderConfig,
66                  processChecker,
67                  Collections.<String[]>emptyList());
68      }
69  
70      public AbstractPathConfiguration getClasspathConfiguration() {
71          return classpathConfiguration;
72      }
73  
74      public boolean isManifestOnlyJarRequestedAndUsable() {
75          return classLoaderConfiguration.isManifestOnlyJarRequestedAndUsable();
76      }
77  
78      public String getProviderClassName() {
79          return providerClassName;
80      }
81  
82      public String getActualClassName() {
83          return isProviderMainClass() ? stripEnd(providerClassName, "#main") : providerClassName;
84      }
85  
86      /**
87       * <p>Strip any of a supplied String from the end of a String.</p>
88       * <br>
89       * <p>If the strip String is {@code null}, whitespace is
90       * stripped.</p>
91       *
92       * @param str   the String to remove characters from
93       * @param strip the String to remove
94       * @return the stripped String
95       */
96      private static String stripEnd(String str, String strip) {
97          if (str == null) {
98              return null;
99          }
100         int end = str.length();
101 
102         if (strip == null) {
103             while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
104                 end--;
105             }
106         } else {
107             while (end != 0 && strip.indexOf(str.charAt(end - 1)) != -1) {
108                 end--;
109             }
110         }
111         return str.substring(0, end);
112     }
113 
114     public ClassLoaderConfiguration getClassLoaderConfiguration() {
115         return classLoaderConfiguration;
116     }
117 
118     public boolean isShadefire() {
119         return providerClassName.startsWith("org.apache.maven.shadefire.surefire");
120     }
121 
122     public void writeSurefireTestClasspathProperty() {
123         getClasspathConfiguration().getTestClasspath().writeToSystemProperty(SUREFIRE_TEST_CLASSPATH);
124     }
125 
126     public ProcessCheckerType getProcessChecker() {
127         return processChecker;
128     }
129 
130     public List<String[]> getJpmsArguments() {
131         return jpmsArguments;
132     }
133 }