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.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.archiva.redback.components.scheduler.DefaultScheduler;
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.apache.archiva.redback.components.taskqueue.Task;
30 import org.apache.archiva.redback.components.taskqueue.execution.ThreadedTaskQueueExecutor;
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.Properties;
39 import java.util.concurrent.ExecutorService;
42 * ArchivaStartup - the startup of all archiva features in a deterministic order.
46 public class ArchivaStartup
47 implements ServletContextListener
49 private ThreadedTaskQueueExecutor tqeDbScanning;
51 private ThreadedTaskQueueExecutor tqeRepoScanning;
53 private ThreadedTaskQueueExecutor tqeIndexing;
55 private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
57 private PlexusSisuBridge plexusSisuBridge;
59 private NexusIndexer nexusIndexer;
61 public void contextInitialized( ServletContextEvent contextEvent )
63 WebApplicationContext wac =
64 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
66 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
68 repositoryTaskScheduler =
69 wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
71 Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
73 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
75 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
77 plexusSisuBridge = wac.getBean( PlexusSisuBridge.class );
81 nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
83 catch ( PlexusSisuBridgeException e )
85 throw new RuntimeException( "Unable to get NexusIndexer: " + e.getMessage(), e );
89 securitySync.startup();
90 repositoryTaskScheduler.startup();
91 Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
93 catch ( ArchivaException e )
95 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
99 public void contextDestroyed( ServletContextEvent contextEvent )
101 WebApplicationContext applicationContext =
102 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
104 // TODO check this stop
107 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
109 ( (ClassPathXmlApplicationContext) applicationContext ).close();
112 if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
114 // stop task queue executors
115 stopTaskQueueExecutor( tqeDbScanning );
116 stopTaskQueueExecutor( tqeRepoScanning );
117 stopTaskQueueExecutor( tqeIndexing );
119 // stop the DefaultArchivaTaskScheduler and its scheduler
120 if ( repositoryTaskScheduler != null )
124 repositoryTaskScheduler.stop();
126 catch ( SchedulerException e )
133 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
134 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
135 schedulerField.setAccessible( true );
137 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
140 catch ( Exception e )
146 // close the application context
147 //applicationContext.close();
148 // TODO fix close call
149 //applicationContext.
152 // closing correctly indexer to close correctly lock and file
153 for ( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
157 indexingContext.close( false );
159 catch ( Exception e )
161 contextEvent.getServletContext().log( "skip error closing indexingContext " + e.getMessage() );
167 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
169 if ( taskQueueExecutor != null )
171 Task currentTask = taskQueueExecutor.getCurrentTask();
172 if ( currentTask != null )
174 taskQueueExecutor.cancelTask( currentTask );
179 taskQueueExecutor.stop();
180 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
181 if ( service != null )
186 catch ( Exception e )
193 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
195 ExecutorService service = null;
198 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
199 executorServiceField.setAccessible( true );
200 service = (ExecutorService) executorServiceField.get( ttqe );
202 catch ( Exception e )