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 java.net.InetAddress;
37 import java.net.UnknownHostException;
38
39 import javax.inject.Inject;
40 import javax.inject.Singleton;
41 import javax.servlet.ServletContext;
42
43 import org.apache.commons.lang3.BooleanUtils;
44 import org.apache.commons.lang3.StringUtils;
45
46
47
48
49 @Singleton
50 public class DefaultMagnoliaInitPaths implements MagnoliaInitPaths {
51 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMagnoliaInitPaths.class);
52
53
54
55
56
57 protected static final String MAGNOLIA_UNQUALIFIED_SERVER_NAME = "magnolia.unqualified.server.name";
58
59 private final String serverName;
60 private final String rootPath;
61 private final String webappFolderName;
62 private final String contextPath;
63 private final String serverInfo;
64
65 @Inject
66 public DefaultMagnoliaInitPaths(final ServletContext servletContext) {
67 this.serverName = determineServerName(servletContext);
68 this.rootPath = determineRootPath(servletContext);
69 this.webappFolderName = determineWebappFolderName(rootPath, servletContext);
70 this.contextPath = determineContextPath(servletContext);
71 this.serverInfo = servletContext.getServerInfo();
72 log.info("servername is {}, rootPath is {}, webapp is {}, contextPath is {}", serverName, rootPath, webappFolderName, contextPath);
73 }
74
75
76
77
78 @Deprecated
79 public DefaultMagnoliaInitPaths(MagnoliaServletContextListener magnoliaServletContextListener, final ServletContext servletContext) {
80 this(servletContext);
81 }
82
83
84
85
86 protected String determineServerName(ServletContext context) {
87 final boolean unqualifiedServerName = BooleanUtils.toBoolean(context.getInitParameter(MAGNOLIA_UNQUALIFIED_SERVER_NAME));
88
89 try {
90 String serverName = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName());
91
92 if (unqualifiedServerName && StringUtils.contains(serverName, ".")) {
93 serverName = StringUtils.substringBefore(serverName, ".");
94 }
95 return serverName;
96 } catch (UnknownHostException e) {
97 log.warn("Failed to obtain server name, please check your configuration. Using 'default' as server name instead.");
98 return "default";
99 }
100 }
101
102
103
104
105
106 protected String determineRootPath(ServletContext context) {
107 String realPathFromContext = context.getRealPath(StringUtils.EMPTY);
108 if (realPathFromContext == null) {
109
110 realPathFromContext = context.getRealPath("/");
111 }
112
113 String realPath = StringUtils.replace(realPathFromContext, "\\", "/");
114 realPath = StringUtils.removeEnd(realPath, "/");
115 if (realPath == null) {
116
117
118 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.");
119 }
120 return realPath;
121 }
122
123 protected String determineWebappFolderName(String determinedRootPath, ServletContext context) {
124 return StringUtils.substringAfterLast(determinedRootPath, "/");
125 }
126
127 protected String determineContextPath(ServletContext context) {
128 return context.getContextPath();
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 @Override
145 public String getServerName() {
146 return serverName;
147 }
148
149 @Override
150 public String getRootPath() {
151 return rootPath;
152 }
153
154 @Override
155 public String getWebappFolderName() {
156 return webappFolderName;
157 }
158
159 @Override
160 public String getContextPath() {
161 return contextPath;
162 }
163
164 @Override
165 public String getServerInfo() {
166 return serverInfo;
167 }
168 }