1 package org.apache.maven.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 java.lang.reflect.Field;
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
27 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
28 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
29 import org.apache.maven.archiva.common.ArchivaException;
30 import org.codehaus.plexus.personality.plexus.lifecycle.phase.StoppingException;
31 import org.codehaus.plexus.scheduler.DefaultScheduler;
32 import org.codehaus.plexus.spring.PlexusToSpringUtils;
33 import org.codehaus.plexus.spring.PlexusWebApplicationContext;
34 import org.codehaus.plexus.taskqueue.Task;
35 import org.codehaus.plexus.taskqueue.execution.TaskQueueExecutor;
36 import org.codehaus.plexus.taskqueue.execution.ThreadedTaskQueueExecutor;
37 import org.springframework.context.support.ClassPathXmlApplicationContext;
38 import org.springframework.web.context.WebApplicationContext;
39 import org.springframework.web.context.support.WebApplicationContextUtils;
41 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
44 * ArchivaStartup - the startup of all archiva features in a deterministic order.
48 public class ArchivaStartup
49 implements ServletContextListener
51 private ThreadedTaskQueueExecutor tqeDbScanning;
53 private ThreadedTaskQueueExecutor tqeRepoScanning;
55 private ThreadedTaskQueueExecutor tqeIndexing;
57 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
59 public void contextInitialized( ServletContextEvent contextEvent )
61 WebApplicationContext wac =
62 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
64 SecuritySynchronization securitySync =
65 (SecuritySynchronization) wac.getBean( PlexusToSpringUtils.buildSpringId( SecuritySynchronization.class ) );
67 repositoryTaskScheduler =
68 (RepositoryArchivaTaskScheduler) wac.getBean( PlexusToSpringUtils.buildSpringId(
69 ArchivaTaskScheduler.class,
73 (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
74 "repository-scanning" ) );
77 (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
82 securitySync.startup();
83 repositoryTaskScheduler.startup();
86 catch ( ArchivaException e )
88 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
92 public void contextDestroyed( ServletContextEvent contextEvent )
94 WebApplicationContext applicationContext =
95 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
96 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
98 ( (ClassPathXmlApplicationContext) applicationContext ).close();
101 if ( applicationContext != null && applicationContext instanceof PlexusWebApplicationContext )
103 // stop task queue executors
104 stopTaskQueueExecutor( tqeDbScanning );
105 stopTaskQueueExecutor( tqeRepoScanning );
106 stopTaskQueueExecutor( tqeIndexing );
108 // stop the DefaultArchivaTaskScheduler and its scheduler
109 if ( repositoryTaskScheduler != null )
113 repositoryTaskScheduler.stop();
115 catch ( StoppingException e )
123 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
124 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
125 schedulerField.setAccessible( true );
127 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
130 catch ( Exception e )
135 // close the application context
136 ( (PlexusWebApplicationContext) applicationContext ).close();
140 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
142 if ( taskQueueExecutor != null )
144 Task currentTask = taskQueueExecutor.getCurrentTask();
145 if ( currentTask != null )
147 taskQueueExecutor.cancelTask( currentTask );
152 taskQueueExecutor.stop();
153 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
154 if ( service != null )
159 catch ( Exception e )
166 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
168 ExecutorService service = null;
171 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
172 executorServiceField.setAccessible( true );
173 service = (ExecutorService) executorServiceField.get( ttqe );
175 catch ( Exception e )