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.plexusbridge.PlexusSisuBridge;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
24 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
25 import org.apache.archiva.common.ArchivaException;
26 import org.apache.maven.index.NexusIndexer;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.codehaus.plexus.taskqueue.Task;
29 import org.codehaus.plexus.taskqueue.execution.ThreadedTaskQueueExecutor;
30 import org.codehaus.redback.components.scheduler.DefaultScheduler;
31 import org.quartz.SchedulerException;
32 import org.springframework.web.context.WebApplicationContext;
33 import org.springframework.web.context.support.WebApplicationContextUtils;
35 import javax.servlet.ServletContextEvent;
36 import javax.servlet.ServletContextListener;
37 import java.lang.reflect.Field;
38 import java.util.concurrent.ExecutorService;
41 * ArchivaStartup - the startup of all archiva features in a deterministic order.
45 public class ArchivaStartup
46 implements ServletContextListener
48 private ThreadedTaskQueueExecutor tqeDbScanning;
50 private ThreadedTaskQueueExecutor tqeRepoScanning;
52 private ThreadedTaskQueueExecutor tqeIndexing;
54 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
56 private PlexusSisuBridge plexusSisuBridge;
58 private NexusIndexer nexusIndexer;
60 public void contextInitialized( ServletContextEvent contextEvent )
62 WebApplicationContext wac =
63 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
65 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
67 repositoryTaskScheduler =
68 wac.getBean( "archivaTaskScheduler#repository", RepositoryArchivaTaskScheduler.class );
70 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
72 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
74 plexusSisuBridge = wac.getBean( PlexusSisuBridge.class );
78 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
80 catch ( PlexusSisuBridgeException e )
82 throw new RuntimeException( "Unable to get NexusIndexer: " + e.getMessage(), e );
86 securitySync.startup();
87 repositoryTaskScheduler.startup();
90 catch ( ArchivaException e )
92 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
96 public void contextDestroyed( ServletContextEvent contextEvent )
98 WebApplicationContext applicationContext =
99 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
101 // TODO check this stop
104 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
106 ( (ClassPathXmlApplicationContext) applicationContext ).close();
109 if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
111 // stop task queue executors
112 stopTaskQueueExecutor( tqeDbScanning );
113 stopTaskQueueExecutor( tqeRepoScanning );
114 stopTaskQueueExecutor( tqeIndexing );
116 // stop the DefaultArchivaTaskScheduler and its scheduler
117 if ( repositoryTaskScheduler != null )
121 repositoryTaskScheduler.stop();
123 catch ( SchedulerException e )
130 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
131 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
132 schedulerField.setAccessible( true );
134 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
137 catch ( Exception e )
143 // close the application context
144 //applicationContext.close();
145 // TODO fix close call
146 //applicationContext.
149 // closing correctly indexer to close correctly lock and file
150 for( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
154 indexingContext.close( false );
155 } catch ( Exception e )
157 contextEvent.getServletContext().log( "skip error closing indexingContext " + e.getMessage() );
163 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
165 if ( taskQueueExecutor != null )
167 Task currentTask = taskQueueExecutor.getCurrentTask();
168 if ( currentTask != null )
170 taskQueueExecutor.cancelTask( currentTask );
175 taskQueueExecutor.stop();
176 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
177 if ( service != null )
182 catch ( Exception e )
189 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
191 ExecutorService service = null;
194 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
195 executorServiceField.setAccessible( true );
196 service = (ExecutorService) executorServiceField.get( ttqe );
198 catch ( Exception e )