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
64 @Inject
65 public DefaultMagnoliaInitPaths(final ServletContext servletContext) {
66 this.serverName = determineServerName(servletContext);
67 this.rootPath = determineRootPath(servletContext);
68 this.webappFolderName = determineWebappFolderName(rootPath, servletContext);
69 this.contextPath = determineContextPath(servletContext);
70 log.info("servername is {}, rootPath is {}, webapp is {}, contextPath is {}", serverName, rootPath, webappFolderName, contextPath);
71 }
72
73
74
75
76 @Deprecated
77 public DefaultMagnoliaInitPaths(MagnoliaServletContextListener magnoliaServletContextListener, final ServletContext servletContext) {
78 this(servletContext);
79 }
80
81
82
83
84 protected String determineServerName(ServletContext context) {
85 final boolean unqualifiedServerName = BooleanUtils.toBoolean(context.getInitParameter(MAGNOLIA_UNQUALIFIED_SERVER_NAME));
86
87 try {
88 String serverName = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName());
89
90 if (unqualifiedServerName && StringUtils.contains(serverName, ".")) {
91 serverName = StringUtils.substringBefore(serverName, ".");
92 }
93 return serverName;
94 } catch (UnknownHostException e) {
95 log.warn("Failed to obtain server name, please check your configuration. Using 'default' as server name instead.");
96 return "default";
97 }
98 }
99
100
101
102
103
104 protected String determineRootPath(ServletContext context) {
105 String realPathFromContext = context.getRealPath(StringUtils.EMPTY);
106 if (realPathFromContext == null) {
107
108 realPathFromContext = context.getRealPath("/");
109 }
110
111 String realPath = StringUtils.replace(realPathFromContext, "\\", "/");
112 realPath = StringUtils.removeEnd(realPath, "/");
113 if (realPath == null) {
114
115
116 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.");
117 }
118 return realPath;
119 }
120
121 protected String determineWebappFolderName(String determinedRootPath, ServletContext context) {
122 return StringUtils.substringAfterLast(determinedRootPath, "/");
123 }
124
125 protected String determineContextPath(ServletContext context) {
126 return context.getContextPath();
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public String getServerName() {
144 return serverName;
145 }
146
147 @Override
148 public String getRootPath() {
149 return rootPath;
150 }
151
152 @Override
153 public String getWebappFolderName() {
154 return webappFolderName;
155 }
156
157 @Override
158 public String getContextPath() {
159 return contextPath;
160 }
161
162 }