]> git.argeo.org Git - lgpl/argeo-commons.git/blob - trunk/server/runtime/org.argeo.server.webextender/src/main/java/org/argeo/server/webextender/TomcatDeployer.java
[maven-release-plugin] copy for tag argeo-commons-2.1.11
[lgpl/argeo-commons.git] / trunk / server / runtime / org.argeo.server.webextender / src / main / java / org / argeo / server / webextender / TomcatDeployer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 = "/ui";
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 log.info("Enhanced Argeo Tomcat webapp deployer being used");
43 }
44
45 @Override
46 protected String getServerInfo() {
47 return serverInfo;
48 }
49
50 @Override
51 protected void startDeployment(WarDeployment deployment) throws Exception {
52 // Context context = ((TomcatWarDeployment)
53 // deployment).getCatalinaContext();
54 // context.setCookies(false);
55 super.startDeployment(deployment);
56
57 // Required for multiple RAP sessions to work with Tomcat
58 // see
59 // http://wiki.eclipse.org/RAP/FAQ#How_to_run_a_RAP_application_in_multiple_browser_tabs.2Fwindows.3F
60 Context context = getContext(contextPath);
61 if (context != null)
62 context.setCookies(false);
63 }
64
65 /** @return null if not found */
66 private Context getContext(String path) {
67 for (Container container : getHost().findChildren()) {
68 if (log.isTraceEnabled())
69 log.trace(container.getClass() + ": " + container.getName());
70 if (container instanceof Context) {
71 Context context = (Context) container;
72 if (path.equals(context.getPath()))
73 return context;
74 }
75 }
76 return null;
77 }
78
79 private Container getHost() {
80 // get engine
81 Container container = service.getContainer();
82
83 if (container == null)
84 throw new IllegalStateException(
85 "The Tomcat server doesn't have any Engines defined");
86 // now get host
87 Container[] children = container.findChildren();
88 if (ObjectUtils.isEmpty(children))
89 throw new IllegalStateException(
90 "The Tomcat server doesn't have any Hosts defined");
91
92 // pick the first one and associate the context with it
93 return children[0];
94 }
95
96 public void setContextPath(String contextPath) {
97 this.contextPath = contextPath;
98 }
99
100 }