View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.setup.for5_2;
35  
36  import info.magnolia.init.MagnoliaConfigurationProperties;
37  import info.magnolia.module.InstallContext;
38  import info.magnolia.module.delta.AbstractCondition;
39  import info.magnolia.objectfactory.Components;
40  
41  import java.lang.reflect.Method;
42  
43  import javax.inject.Inject;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * A {@link info.magnolia.module.delta.Condition} prohibiting to install in problematic environments - unless explicitly allowed.
51   */
52  public class IsNotAProblematicEnvironmentCondition extends AbstractCondition {
53      private static final Logger log = LoggerFactory.getLogger(IsNotAProblematicEnvironmentCondition.class);
54  
55      public static final String MAGNOLIA_ALLOW_PROBLEMATIC_ENVIRONMENT = "magnolia.allow.problematic.environment";
56  
57      static final String SYSTEM_PROPERTY_OS_NAME = "os.name";
58      static final String SYSTEM_PROPERTY_JAVA_VERSION = "java.version";
59  
60      final MagnoliaConfigurationProperties magnoliaConfigurationProperties;
61  
62      public IsNotAProblematicEnvironmentCondition() {
63          this(Components.getComponent(MagnoliaConfigurationProperties.class));
64      }
65  
66      @Inject
67      public IsNotAProblematicEnvironmentCondition(final MagnoliaConfigurationProperties magnoliaConfigurationProperties) {
68          super("Environment check", "See http://documentation.magnolia-cms.com/display/DOCS/Problematic+environments for more details.");
69          this.magnoliaConfigurationProperties = magnoliaConfigurationProperties;
70      }
71  
72      @Override
73      public boolean check(InstallContext installContext) {
74          if (!magnoliaConfigurationProperties.getBooleanProperty(MAGNOLIA_ALLOW_PROBLEMATIC_ENVIRONMENT)) {
75              final String osName = System.getProperty(SYSTEM_PROPERTY_OS_NAME);
76              final String javaVersion = System.getProperty(SYSTEM_PROPERTY_JAVA_VERSION);
77  
78              if (osName.endsWith("OS X") && (javaVersion.startsWith("1.7") || javaVersion.startsWith("1.8"))) {
79  
80                  String updateNumberString = StringUtils.substringAfter(javaVersion, "_");
81                  if (StringUtils.isNotBlank(updateNumberString)) {
82                      try {
83                          Short updateNumber = Short.parseShort(updateNumberString);
84                          if ((javaVersion.startsWith("1.7") && updateNumber >= 65) || (javaVersion.startsWith("1.8") && updateNumber >= 11)) {
85                              return true;
86                          }
87                      } catch (NumberFormatException e) {
88                          log.error("Invalid update number [{}] continue with determination of AS to be on the safe side.", updateNumberString);
89                      }
90                  }
91  
92                  // check for Tomcat
93                  Class serverInfo;
94                  try {
95                      serverInfo = Class.forName("org.apache.catalina.util.ServerInfo");
96                  } catch (ClassNotFoundException e) {
97                      // it's not tomcat - we're fine!
98                      return true;
99                  }
100 
101                 // version check on Tomcat
102                 try {
103                     final Method method = serverInfo.getMethod("getServerNumber");
104                     final Object versionObject = method.invoke(null);
105                     if (versionObject != null) {
106                         String specificationVersion = versionObject.toString();
107                         if (StringUtils.isNotEmpty(specificationVersion) && (specificationVersion.startsWith("6.") || specificationVersion.startsWith("7."))) {
108                             installContext.warn("Running " + osName + " with Java " + javaVersion + " and " + "Apache Tomcat" + " " + specificationVersion + " has known issues. Use property '" + MAGNOLIA_ALLOW_PROBLEMATIC_ENVIRONMENT + "=true' to allow this combination at your own risk.");
109                             return false;
110                         }
111                     }
112                 } catch (Exception e) {
113                     log.error("Could not determine version of Tomcat server - stopping installation to be on the safe side.", e);
114                     return false;
115                 }
116             }
117         }
118         return true;
119     }
120 }