]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.webextender/src/main/java/org/argeo/server/webextender/TomcatDeployer.java
f20ceb13fe48096931a88d2218e0f8e04c36ed29
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.webextender / src / main / java / org / argeo / server / webextender / TomcatDeployer.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.server.webextender;
17
18 import org.apache.catalina.Container;
19 import org.apache.catalina.Context;
20 import org.apache.catalina.Service;
21 import org.springframework.osgi.web.deployer.WarDeployment;
22 import org.springframework.osgi.web.deployer.tomcat.TomcatWarDeployer;
23 import org.springframework.util.ObjectUtils;
24
25 /**
26 * Wraps the Spring DM Tomcat deployer in order to avoid issue with call to
27 * getServerInfo() when undeployed. We need to hack a lot here because Spring
28 * OSGi Web is really not extendable.
29 */
30 public class TomcatDeployer extends TomcatWarDeployer {
31 private String serverInfo;
32 private Service service;
33 private String contextPath = "/org.argeo.rap.webapp";
34
35 @Override
36 public void setService(Object service) {
37 this.service = (Service) service;
38 super.setService(service);
39 // TODO: listen to OSGi service so that we get notified in the
40 // (unlikely) case the underlying service is updated
41 serverInfo = ((Service) service).getInfo();
42 if (log.isTraceEnabled())
43 log.trace("Argeo modified Tomcat deployer used");
44 }
45
46 @Override
47 protected String getServerInfo() {
48 return serverInfo;
49 }
50
51 @Override
52 protected void startDeployment(WarDeployment deployment) throws Exception {
53 // Context context = ((TomcatWarDeployment)
54 // deployment).getCatalinaContext();
55 // context.setCookies(false);
56 super.startDeployment(deployment);
57
58 // Required for multiple RAP sessions to work with Tomcat
59 // see
60 // http://wiki.eclipse.org/RAP/FAQ#How_to_run_a_RAP_application_in_multiple_browser_tabs.2Fwindows.3F
61 Context context = getContext(contextPath);
62 if (context != null)
63 context.setCookies(false);
64 }
65
66 /** @return null if not found */
67 private Context getContext(String path) {
68 for (Container container : getHost().findChildren()) {
69 if (log.isTraceEnabled())
70 log.trace(container.getClass() + ": " + container.getName());
71 if (container instanceof Context) {
72 Context context = (Context) container;
73 if (path.equals(context.getPath()))
74 return context;
75 }
76 }
77 return null;
78 }
79
80 private Container getHost() {
81 // get engine
82 Container container = service.getContainer();
83
84 if (container == null)
85 throw new IllegalStateException(
86 "The Tomcat server doesn't have any Engines defined");
87 // now get host
88 Container[] children = container.findChildren();
89 if (ObjectUtils.isEmpty(children))
90 throw new IllegalStateException(
91 "The Tomcat server doesn't have any Hosts defined");
92
93 // pick the first one and associate the context with it
94 return children[0];
95 }
96
97 public void setContextPath(String contextPath) {
98 this.contextPath = contextPath;
99 }
100
101 }