View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.codehaus.mojo.cobertura.configuration;
21  
22  
23  import java.util.Map;
24  
25  /**
26   * Class for checking and retrieving the maven environment variable for the
27   * maximum heap size setting.
28   *
29   * @author Trampas Kirk
30   * @since 2.3
31   */
32  public class MaxHeapSizeUtil
33  {
34  
35      /**
36       * The maven options environment variable. Case sensitive.
37       */
38      public static final String MAVEN_OPTIONS = "MAVEN_OPTS";
39  
40      /**
41       * The max heap size JVM parameter. Case sensitive.
42       */
43      public static final String MAX_MEMORY_FLAG = "-Xmx";
44  
45      /**
46       * The number of characters from the flag char start to the actual setting.
47       */
48      private static final int NUMBER_OF_FLAG_CHARS = MAX_MEMORY_FLAG.length();
49  
50      /**
51       * The instance of the <code>MemSettingUtil</code> singleton.
52       */
53      private static MaxHeapSizeUtil maxHeapSizeUtil;
54  
55      /**
56       * Singleton getter.
57       *
58       * @return the <code>MemSettingUtil</code> instance.
59       */
60      public static MaxHeapSizeUtil getInstance()
61      {
62          if ( maxHeapSizeUtil == null )
63          {
64              maxHeapSizeUtil = new MaxHeapSizeUtil();
65          }
66          return maxHeapSizeUtil;
67      }
68  
69      /**
70       * Private constructor for singleton purposes.
71       */
72      private MaxHeapSizeUtil()
73      {
74          // do nothing
75      }
76  
77      /**
78       * The environment settings.
79       */
80      private Map<String, String> envSettings;
81  
82      /**
83       * Gets envSettings map, typically a reference to the <code>System.getEnv()</code> settings.
84       * A lazy loaded property is used to make unit tests easy.
85       *
86       * @return the <code>System.getEnv()</code> settings.
87       */
88      private Map<String, String> getEnvSettings()
89      {
90          if ( envSettings == null )
91          {
92              envSettings = System.getenv();
93          }
94          return envSettings;
95      }
96  
97      /**
98       * Sets the <code>System.getEnv()</code> settings.
99       *
100      * @param envSettings the settings map to use
101      */
102     public void setEnvSettings( Map<String, String> envSettings )
103     {
104         this.envSettings = envSettings;
105     }
106 
107     /**
108      * Gets the maximum heap size JVM argument from the maven options environment variable.
109      * Returns only the numeric and unit portion.  For example, given a maven options setting
110      * of "-Xmx64m" this method will return "64m". Returns <code>null</code> if the maven
111      * environment variable isn't set, the JVM heap size argument is not present, or the JVM
112      * heap size argument is  somehow invalid.
113      *
114      * @return the maximum heap size JVM argument from the maven options environment variable
115      * @see #envHasMavenMaxMemSetting()
116      */
117     public String getMavenMaxMemSetting()
118     {
119         boolean hasMavenOptions = getEnvSettings().containsKey( MAVEN_OPTIONS );
120         if ( !hasMavenOptions )
121         {
122             return null;
123         }
124 
125         String mavenOpts = (String) getEnvSettings().get( MAVEN_OPTIONS );
126         boolean hasMaxMemSetting = mavenOpts.contains( MAX_MEMORY_FLAG );
127 
128         if ( !hasMaxMemSetting )
129         {
130             return null;
131         }
132 
133         String mavenOptionsEnvironmentSetting = (String) getEnvSettings().get( MAVEN_OPTIONS );
134         int startIndex = mavenOptionsEnvironmentSetting.indexOf( MAX_MEMORY_FLAG ) + NUMBER_OF_FLAG_CHARS;
135         int endIndex = mavenOptionsEnvironmentSetting.indexOf( ' ', startIndex );
136 
137         if ( endIndex == -1 )
138         {
139             endIndex = mavenOptionsEnvironmentSetting.length();
140         }
141 
142         String maxMemSetting = mavenOptionsEnvironmentSetting.substring( startIndex, endIndex ).trim();
143 
144         if ( !maxMemSetting.matches( "\\d+[mgMG]" ) )
145         {
146             return null;
147         }
148         return maxMemSetting;
149     }
150 
151     /**
152      * Returns <code>true</code> if the current environment has the max heap size set in the maven
153      * options, otherwise <code>false</code>.
154      *
155      * @return <code>true</code> if the current environment has the max heap size set in the maven
156      * options, otherwise <code>false</code>.
157      * @see #getMavenMaxMemSetting()
158      */
159     public boolean envHasMavenMaxMemSetting()
160     {
161         if ( getMavenMaxMemSetting() != null )
162         {
163             return true;
164         }
165         return false;
166     }
167 }