View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.init;
35  
36  import info.magnolia.cms.util.DeprecationUtil;
37  import org.apache.commons.lang.BooleanUtils;
38  import org.apache.commons.lang.StringUtils;
39  
40  import javax.inject.Inject;
41  import javax.inject.Singleton;
42  import javax.servlet.ServletContext;
43  import java.lang.reflect.InvocationTargetException;
44  import java.lang.reflect.Method;
45  import java.net.InetAddress;
46  import java.net.UnknownHostException;
47  
48  /**
49   * A simple wrapper about the few initial variables used to resolve {@link PropertySource}.
50   *
51   * @author gjoseph
52   * @version $Revision: $ ($Author: $)
53   */
54  @Singleton
55  public class DefaultMagnoliaInitPaths implements MagnoliaInitPaths {
56      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMagnoliaInitPaths.class);
57  
58      /**
59       * Context parameter name. If set to true in web.xml the server name resolved by magnolia will never contain the
60       * domain (the server "server.domain.com" will be simply resolved as "server").
61       */
62      protected static final String MAGNOLIA_UNQUALIFIED_SERVER_NAME = "magnolia.unqualified.server.name";
63  
64      /**
65       * @deprecated since 4.5 - only used here for retro-compatibility with potential subclasses of MgnlServletContextListener
66       */
67      private final MagnoliaServletContextListener magnoliaServletContextListener;
68      private final String serverName;
69      private final String rootPath;
70      private final String webappFolderName;
71      private final String contextPath;
72  
73      @Inject
74      public DefaultMagnoliaInitPaths(MagnoliaServletContextListener magnoliaServletContextListener, final ServletContext servletContext) {
75          this.magnoliaServletContextListener = magnoliaServletContextListener;
76          this.serverName = determineServerName(servletContext);
77          this.rootPath = determineRootPath(servletContext);
78          this.webappFolderName = determineWebappFolderName(rootPath, servletContext);
79          this.contextPath = determineContextPath(servletContext);
80          log.debug("servername is {}, rootPath is {}, webapp is {}, contextPath is {}", new Object[]{serverName, rootPath, webappFolderName, contextPath});
81      }
82  
83      /**
84       * Figures out the local host name, makes sure it's lowercase, and use its unqualified name if the {@value #MAGNOLIA_UNQUALIFIED_SERVER_NAME} init parameter is set to true.
85       */
86      protected String determineServerName(ServletContext context) {
87          final boolean unqualifiedServerName = BooleanUtils.toBoolean(context.getInitParameter(MAGNOLIA_UNQUALIFIED_SERVER_NAME));
88          final String retroCompatMethodCall = magnoliaServletContextListener.initServername(unqualifiedServerName);
89          if (retroCompatMethodCall != null) {
90              DeprecationUtil.isDeprecated("You should update your code and override determineServerName(ServletContext) instead of initServername(String)");
91              return retroCompatMethodCall;
92          }
93  
94          try {
95              String serverName = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName());
96  
97              if (unqualifiedServerName && StringUtils.contains(serverName, ".")) {
98                  serverName = StringUtils.substringBefore(serverName, ".");
99              }
100             return serverName;
101         } catch (UnknownHostException e) {
102             log.error(e.getMessage());
103             return null;
104         }
105     }
106 
107     /**
108      * Figures out the root path where the webapp is deployed.
109      */
110     protected String determineRootPath(ServletContext context) {
111         final String retroCompatMethodCall = magnoliaServletContextListener.initRootPath(context);
112         if (retroCompatMethodCall != null) {
113             DeprecationUtil.isDeprecated("You should update your code and override determineRootPath(ServletContext) instead of initRootPath(ServletContext)");
114             return retroCompatMethodCall;
115         }
116 
117         String realPath = StringUtils.replace(context.getRealPath(StringUtils.EMPTY), "\\", "/");
118         realPath = StringUtils.removeEnd(realPath, "/");
119         if (realPath == null) {
120             // don't use new java.io.File("x").getParentFile().getAbsolutePath() to find out real directory, could throw
121             // a NPE for unexpanded war
122             throw new RuntimeException("Magnolia is not configured properly and therefore unable to start: real path can't be obtained [ctx real path:" + context.getRealPath(StringUtils.EMPTY) + "]. Please refer to the Magnolia documentation for installation instructions specific to your environment.");
123         }
124         return realPath;
125     }
126 
127     protected String determineWebappFolderName(String determinedRootPath, ServletContext context) {
128         final String retroCompatMethodCall = magnoliaServletContextListener.initWebappName(determinedRootPath);
129         if (retroCompatMethodCall != null) {
130             DeprecationUtil.isDeprecated("You should update your code and override determineWebappFolderName(String, ServletContext) instead of initWebappName(String)");
131             return retroCompatMethodCall;
132         }
133 
134         return StringUtils.substringAfterLast(determinedRootPath, "/");
135     }
136 
137     protected String determineContextPath(ServletContext context) {
138         // Getting the contextPath via reflection, until we can depend on servlet 2.5 : See MAGNOLIA-3094
139         try {
140             final Method getContextPath = context.getClass().getMethod("getContextPath", null);
141             return (String) getContextPath.invoke(context);
142         } catch (NoSuchMethodException e) {
143             log.info("Magnolia appears to be running on a server using a Servlet API version older than 2.5, so we can not know the contextPath at startup.");
144             return null;
145         } catch (InvocationTargetException e) {
146             throw new IllegalStateException(e);
147         } catch (IllegalAccessException e) {
148             throw new IllegalStateException(e);
149         }
150     }
151 
152     @Override
153     public String getServerName() {
154         return serverName;
155     }
156 
157     @Override
158     public String getRootPath() {
159         return rootPath;
160     }
161 
162     @Override
163     public String getWebappFolderName() {
164         return webappFolderName;
165     }
166 
167     @Override
168     public String getContextPath() {
169         return contextPath;
170     }
171 
172 }