1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
50
51
52
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
60
61
62 protected static final String MAGNOLIA_UNQUALIFIED_SERVER_NAME = "magnolia.unqualified.server.name";
63
64
65
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
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
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
121
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
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 }