001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.test.util; 019 020import static org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer; 021import static org.slf4j.LoggerFactory.getLogger; 022 023import java.net.URI; 024import java.util.Collection; 025 026import javax.annotation.PostConstruct; 027import javax.annotation.PreDestroy; 028import javax.xml.bind.JAXBContext; 029import javax.xml.bind.Unmarshaller; 030 031import org.fcrepo.http.commons.webxml.WebAppConfig; 032import org.fcrepo.http.commons.webxml.bind.ContextParam; 033import org.fcrepo.http.commons.webxml.bind.Filter; 034import org.fcrepo.http.commons.webxml.bind.FilterMapping; 035import org.fcrepo.http.commons.webxml.bind.InitParam; 036import org.fcrepo.http.commons.webxml.bind.Listener; 037import org.fcrepo.http.commons.webxml.bind.Servlet; 038import org.fcrepo.http.commons.webxml.bind.ServletMapping; 039import org.glassfish.grizzly.http.server.HttpServer; 040import org.glassfish.grizzly.servlet.FilterRegistration; 041import org.glassfish.grizzly.servlet.ServletRegistration; 042import org.glassfish.grizzly.servlet.WebappContext; 043import org.slf4j.Logger; 044import org.springframework.beans.BeansException; 045import org.springframework.context.ApplicationContext; 046import org.springframework.context.ApplicationContextAware; 047 048/** 049 * <p>ContainerWrapper class.</p> 050 * 051 * @author awoods 052 */ 053public class ContainerWrapper implements ApplicationContextAware { 054 055 private static final Logger logger = getLogger(ContainerWrapper.class); 056 057 private int port; 058 059 private HttpServer server; 060 061 private WebappContext appContext; 062 063 private String configLocation; 064 065 public void setConfigLocation(final String configLocation) { 066 this.configLocation = configLocation.replaceFirst("^classpath:", "/"); 067 } 068 069 public void setPort(final int port) { 070 this.port = port; 071 } 072 073 @PostConstruct 074 public void start() throws Exception { 075 076 final JAXBContext context = JAXBContext.newInstance(WebAppConfig.class); 077 final Unmarshaller u = context.createUnmarshaller(); 078 final WebAppConfig o = 079 (WebAppConfig) u.unmarshal(getClass().getResource( 080 this.configLocation)); 081 082 final URI uri = URI.create("http://localhost:" + port); 083 084 server = createHttpServer(uri); 085 086 // create a "root" web application 087 appContext = new WebappContext(o.displayName(), "/"); 088 089 for (final ContextParam p : o.contextParams()) { 090 appContext.addContextInitParameter(p.name(), p.value()); 091 } 092 093 for (final Listener l : o.listeners()) { 094 appContext.addListener(l.className()); 095 } 096 097 for (final Servlet s : o.servlets()) { 098 final ServletRegistration servlet = 099 appContext.addServlet(s.servletName(), s.servletClass()); 100 101 final Collection<ServletMapping> mappings = 102 o.servletMappings(s.servletName()); 103 for (final ServletMapping sm : mappings) { 104 servlet.addMapping(sm.urlPattern()); 105 } 106 for (final InitParam p : s.initParams()) { 107 servlet.setInitParameter(p.name(), p.value()); 108 } 109 } 110 111 for (final Filter f : o.filters()) { 112 final FilterRegistration filter = 113 appContext.addFilter(f.filterName(), f.filterClass()); 114 115 final Collection<FilterMapping> mappings = 116 o.filterMappings(f.filterName()); 117 for (final FilterMapping sm : mappings) { 118 final String urlPattern = sm.urlPattern(); 119 final String servletName = sm.servletName(); 120 if (urlPattern != null) { 121 filter.addMappingForUrlPatterns(null, urlPattern); 122 } else { 123 filter.addMappingForServletNames(null, servletName); 124 } 125 126 } 127 for (final InitParam p : f.initParams()) { 128 filter.setInitParameter(p.name(), p.value()); 129 } 130 } 131 132 appContext.deploy(server); 133 134 logger.debug("started grizzly webserver endpoint at " + 135 server.getHttpHandler().getName()); 136 } 137 138 @PreDestroy 139 public void stop() { 140 try { 141 appContext.undeploy(); 142 } catch (final Exception e) { 143 logger.warn(e.getMessage(), e); 144 } finally { 145 server.shutdownNow(); 146 } 147 } 148 149 @Override 150 public void setApplicationContext(final ApplicationContext applicationContext) 151 throws BeansException { 152 // this.applicationContext = applicationContext; 153 154 } 155 156}