001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     matic
018 */
019package org.nuxeo.runtime.tomcat;
020
021import org.apache.catalina.Lifecycle;
022import org.apache.catalina.LifecycleEvent;
023import org.apache.catalina.LifecycleListener;
024import org.apache.catalina.core.NamingContextListener;
025import org.apache.catalina.core.StandardContext;
026import org.apache.naming.ContextAccessController;
027
028/**
029 * Grab security token and source context for setting write access onto naming context during container startup.
030 *
031 * @author matic
032 * @since 5.5
033 */
034public class ContextSecurityGrabber implements LifecycleListener {
035
036    final NamingContextListener namingContextListener = new NamingContextListener();
037
038    @Override
039    public void lifecycleEvent(LifecycleEvent event) {
040        final String type = event.getType();
041        final StandardContext source = (StandardContext) event.getSource();
042        if (source.getNamingContextListener() == null) {
043            namingContextListener.setName(source.getName());
044            source.setNamingContextListener(namingContextListener);
045        }
046        namingContextListener.lifecycleEvent(event);
047        if (Lifecycle.CONFIGURE_START_EVENT.equals(type)) {
048            final Object token = event.getLifecycle();
049            ContextAccessController.setWritable(namingContextListener.getName(), token);
050        }
051    }
052
053}