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.maven.archiva.common.ArchivaException;
28 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
29 import org.apache.maven.archiva.scheduled.DefaultArchivaTaskScheduler;
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 ArchivaTaskScheduler taskScheduler;
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 ) );
66 ResolverFactoryInit resolverFactory =
67 (ResolverFactoryInit) wac.getBean( PlexusToSpringUtils.buildSpringId( ResolverFactoryInit.class ) );
70 (ArchivaTaskScheduler) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaTaskScheduler.class ) );
73 (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
74 "database-update" ) );
76 (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
77 "repository-scanning" ) );
79 (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
84 securitySync.startup();
85 resolverFactory.startup();
86 taskScheduler.startup();
89 catch ( ArchivaException e )
91 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
95 public void contextDestroyed( ServletContextEvent contextEvent )
97 WebApplicationContext applicationContext =
98 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
99 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
101 ( (ClassPathXmlApplicationContext) applicationContext ).close();
104 if ( applicationContext != null && applicationContext instanceof PlexusWebApplicationContext )
106 // stop task queue executors
107 stopTaskQueueExecutor( tqeDbScanning );
108 stopTaskQueueExecutor( tqeRepoScanning );
109 stopTaskQueueExecutor( tqeIndexing );
111 // stop the DefaultArchivaTaskScheduler and its scheduler
112 if ( taskScheduler != null && taskScheduler instanceof DefaultArchivaTaskScheduler )
116 ( (DefaultArchivaTaskScheduler) taskScheduler ).stop();
118 catch ( StoppingException e )
126 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
127 Field schedulerField = taskScheduler.getClass().getDeclaredField( "scheduler" );
128 schedulerField.setAccessible( true );
130 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( taskScheduler );
133 catch ( Exception e )
138 // close the application context
139 ( (PlexusWebApplicationContext) applicationContext ).close();
143 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
145 if ( taskQueueExecutor != null )
147 Task currentTask = taskQueueExecutor.getCurrentTask();
148 if ( currentTask != null )
150 taskQueueExecutor.cancelTask( currentTask );
155 taskQueueExecutor.stop();
156 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
157 if ( service != null )
162 catch ( Exception e )
169 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
171 ExecutorService service = null;
174 Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
175 executorServiceField.setAccessible( true );
176 service = (ExecutorService) executorServiceField.get( ttqe );
178 catch ( Exception e )