1 package org.apache.archiva.web.startup;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.ArchivaException;
23 import org.apache.archiva.redback.components.scheduler.DefaultScheduler;
24 import org.apache.archiva.redback.components.taskqueue.Task;
25 import org.apache.archiva.redback.components.taskqueue.execution.ThreadedTaskQueueExecutor;
26 import org.apache.archiva.scheduler.repository.DefaultRepositoryArchivaTaskScheduler;
27 import org.apache.maven.index.NexusIndexer;
28 import org.apache.maven.index.context.IndexingContext;
29 import org.quartz.SchedulerException;
30 import org.springframework.web.context.WebApplicationContext;
31 import org.springframework.web.context.support.WebApplicationContextUtils;
33 import javax.servlet.ServletContext;
34 import javax.servlet.ServletContextEvent;
35 import javax.servlet.ServletContextListener;
36 import java.lang.reflect.Field;
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
41 * ArchivaStartup - the startup of all archiva features in a deterministic order.
43 public class ArchivaStartup
44 implements ServletContextListener
46 private ThreadedTaskQueueExecutor tqeDbScanning;
48 private ThreadedTaskQueueExecutor tqeRepoScanning;
50 private ThreadedTaskQueueExecutor tqeIndexing;
52 private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
54 private NexusIndexer nexusIndexer;
57 public void contextInitialized( ServletContextEvent contextEvent )
59 WebApplicationContext wac =
60 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
62 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
64 repositoryTaskScheduler =
65 wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
67 Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
69 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
71 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
73 nexusIndexer = wac.getBean( NexusIndexer.class );
77 securitySync.startup();
78 repositoryTaskScheduler.startup();
79 Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
81 catch ( ArchivaException e )
83 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
88 public void contextDestroyed( ServletContextEvent contextEvent )
90 WebApplicationContext applicationContext =
91 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
93 // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
94 // so servletContext.log
95 ServletContext servletContext = contextEvent.getServletContext();
97 // TODO check this stop
100 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
102 ( (ClassPathXmlApplicationContext) applicationContext ).close();
105 if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
107 // stop task queue executors
108 stopTaskQueueExecutor( tqeDbScanning, servletContext );
109 stopTaskQueueExecutor( tqeRepoScanning, servletContext );
110 stopTaskQueueExecutor( tqeIndexing, servletContext );
112 // stop the DefaultArchivaTaskScheduler and its scheduler
113 if ( repositoryTaskScheduler != null )
117 repositoryTaskScheduler.stop();
119 catch ( SchedulerException e )
121 servletContext.log( e.getMessage(), e );
126 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
127 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
128 schedulerField.setAccessible( true );
130 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
133 catch ( Exception e )
135 servletContext.log( e.getMessage(), e );
139 // close the application context
140 //applicationContext.close();
141 // TODO fix close call
142 //applicationContext.
145 // closing correctly indexer to close correctly lock and file
146 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
150 indexingContext.close( false );
152 catch ( Exception e )
154 servletContext.log( "skip error closing indexingContext " + e.getMessage(), e );
160 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
162 if ( taskQueueExecutor != null )
164 Task currentTask = taskQueueExecutor.getCurrentTask();
165 if ( currentTask != null )
167 taskQueueExecutor.cancelTask( currentTask );
172 taskQueueExecutor.stop();
173 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor, servletContext );
174 if ( service != null )
179 catch ( Exception e )
181 servletContext.log( e.getMessage(), e );
186 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
188 ExecutorService service = null;
191 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
192 executorServiceField.setAccessible( true );
193 service = (ExecutorService) executorServiceField.get( ttqe );
195 catch ( Exception e )
197 servletContext.log( e.getMessage(), e );