This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
WebappBootstrap.java
209 lines (172 loc) · 6.92 KB
/
WebappBootstrap.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2013 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.webapp;
import java.io.File;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.sonatype.nexus.NxApplication;
import org.sonatype.nexus.bootstrap.ConfigurationBuilder;
import org.sonatype.nexus.bootstrap.ConfigurationHolder;
import org.sonatype.nexus.bootstrap.EnvironmentVariables;
import org.sonatype.nexus.guice.NexusModules.CoreModule;
import org.sonatype.nexus.log.LogManager;
import org.sonatype.nexus.util.LockFile;
import org.sonatype.nexus.util.file.DirSupport;
import com.google.common.base.Throwables;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.context.Context;
import org.eclipse.sisu.plexus.PlexusSpaceModule;
import org.eclipse.sisu.space.BeanScanning;
import org.eclipse.sisu.space.ClassSpace;
import org.eclipse.sisu.space.URLClassSpace;
import org.eclipse.sisu.wire.WireModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkState;
/**
* Web application bootstrap {@link ServletContextListener}.
*
* @since 2.8
*/
public class WebappBootstrap
extends GuiceServletContextListener
{
private static final Logger log = LoggerFactory.getLogger(WebappBootstrap.class);
private LockFile lockFile;
private PlexusContainer container;
private Injector injector;
private NxApplication application;
private LogManager logManager;
@Override
public void contextInitialized(final ServletContextEvent event) {
log.info("Initializing");
ServletContext context = event.getServletContext();
try {
// Use bootstrap configuration if it exists, else load it
Map<String, String> properties = ConfigurationHolder.get();
if (properties != null) {
log.info("Using bootstrap launcher configuration");
}
else {
log.info("Loading configuration for WAR deployment environment");
// FIXME: This is what was done before, it seems completely wrong in WAR deployment since there is no bundle
String baseDir = System.getProperty("bundleBasedir", context.getRealPath("/WEB-INF"));
properties = new ConfigurationBuilder()
.defaults()
.set("bundleBasedir", new File(baseDir).getCanonicalPath())
.properties("/nexus.properties", true)
.properties("/nexus-test.properties", false)
.custom(new EnvironmentVariables())
.build();
System.getProperties().putAll(properties);
ConfigurationHolder.set(properties);
}
// Ensure required properties exist
requireProperty(properties, "bundleBasedir");
requireProperty(properties, "nexus-work");
requireProperty(properties, "nexus-app");
requireProperty(properties, "application-conf");
requireProperty(properties, "security-xml-file");
// lock the work directory
File workDir = new File(properties.get("nexus-work")).getCanonicalFile();
DirSupport.mkdir(workDir);
lockFile = new LockFile(new File(workDir, "nexus.lock"));
checkState(lockFile.lock(), "Nexus work directory already in use: %s", workDir);
final ClassWorld world = new ClassWorld("nexus", Thread.currentThread().getContextClassLoader());
final ClassRealm realm = world.getRealm("nexus");
// create the injector
ClassSpace space = new URLClassSpace(realm);
injector = Guice.createInjector(new WireModule(new AbstractModule()
{
@Override
protected void configure() {
bind(ClassRealm.class).toInstance(realm); // TEMP: won't be required in next step
}
}, new CoreModule(context, properties), new PlexusSpaceModule(space, BeanScanning.INDEX)));
log.debug("Injector: {}", injector);
container = injector.getInstance(PlexusContainer.class);
context.setAttribute(PlexusConstants.PLEXUS_KEY, container);
injector.getInstance(Context.class).put(PlexusConstants.PLEXUS_KEY, container);
log.debug("Container: {}", container);
// configure guice servlet (add injector to servlet context)
super.contextInitialized(event);
// configure logging
logManager = container.lookup(LogManager.class);
log.debug("Log manager: {}", logManager);
logManager.configure();
// start the application
application = container.lookup(NxApplication.class);
log.debug("Application: {}", application);
application.start();
}
catch (Exception e) {
log.error("Failed to initialize", e);
throw Throwables.propagate(e);
}
log.info("Initialized");
}
private static void requireProperty(final Map<String, String> properties, final String name) {
if (!properties.containsKey(name)) {
throw new IllegalStateException("Missing required property: " + name);
}
}
@Override
public void contextDestroyed(final ServletContextEvent event) {
log.info("Destroying");
ServletContext context = event.getServletContext();
// stop application
if (application != null) {
try {
application.stop();
}
catch (Exception e) {
log.error("Failed to stop application", e);
}
application = null;
}
// shutdown logging
if (logManager != null) {
logManager.shutdown();
logManager = null;
}
// unset injector from context
super.contextDestroyed(event);
injector = null;
// cleanup the container
if (container != null) {
container.dispose();
context.removeAttribute(PlexusConstants.PLEXUS_KEY);
container = null;
}
// release lock
if (lockFile != null) {
lockFile.release();
lockFile = null;
}
log.info("Destroyed");
}
@Override
protected Injector getInjector() {
checkState(injector != null, "Missing injector reference");
return injector;
}
}