001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     matic
016 */
017package org.nuxeo.runtime.tomcat;
018
019import org.apache.catalina.Lifecycle;
020import org.apache.catalina.LifecycleEvent;
021import org.apache.catalina.LifecycleListener;
022import org.apache.catalina.core.NamingContextListener;
023import org.apache.catalina.core.StandardContext;
024import org.apache.naming.ContextAccessController;
025
026/**
027 * Grab security token and source context for setting write access onto naming context during container startup.
028 *
029 * @author matic
030 * @since 5.5
031 */
032public class ContextSecurityGrabber implements LifecycleListener {
033
034    final NamingContextListener namingContextListener = new NamingContextListener();
035
036    @Override
037    public void lifecycleEvent(LifecycleEvent event) {
038        final String type = event.getType();
039        final StandardContext source = (StandardContext) event.getSource();
040        if (source.getNamingContextListener() == null) {
041            namingContextListener.setName(source.getName());
042            source.setNamingContextListener(namingContextListener);
043        }
044        namingContextListener.lifecycleEvent(event);
045        if (Lifecycle.CONFIGURE_START_EVENT.equals(type)) {
046            final Object token = event.getLifecycle();
047            ContextAccessController.setWritable(namingContextListener.getName(), token);
048        }
049    }
050
051}