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.quartz.SchedulerException;
28 import org.springframework.web.context.WebApplicationContext;
29 import org.springframework.web.context.support.WebApplicationContextUtils;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34 import java.lang.reflect.Field;
35 import java.util.Properties;
36 import java.util.concurrent.ExecutorService;
39 * ArchivaStartup - the startup of all archiva features in a deterministic order.
41 public class ArchivaStartup
42 implements ServletContextListener
44 private ThreadedTaskQueueExecutor tqeDbScanning;
46 private ThreadedTaskQueueExecutor tqeRepoScanning;
48 private ThreadedTaskQueueExecutor tqeIndexing;
50 private DefaultRepositoryArchivaTaskScheduler repositoryTaskScheduler;
53 public void contextInitialized( ServletContextEvent contextEvent )
55 WebApplicationContext wac =
56 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
58 SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
60 repositoryTaskScheduler =
61 wac.getBean( "archivaTaskScheduler#repository", DefaultRepositoryArchivaTaskScheduler.class );
63 Properties archivaRuntimeProperties = wac.getBean( "archivaRuntimeProperties", Properties.class );
65 tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
67 tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
72 securitySync.startup();
73 repositoryTaskScheduler.startup();
74 Banner.display( (String) archivaRuntimeProperties.get( "archiva.version" ) );
76 catch ( ArchivaException e )
78 throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
83 public void contextDestroyed( ServletContextEvent contextEvent )
85 WebApplicationContext applicationContext =
86 WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
88 // we log using servlet mechanism as due to some possible problem with slf4j when container shutdown
89 // so servletContext.log
90 ServletContext servletContext = contextEvent.getServletContext();
92 // TODO check this stop
95 if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
97 ( (ClassPathXmlApplicationContext) applicationContext ).close();
100 if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
102 // stop task queue executors
103 stopTaskQueueExecutor( tqeDbScanning, servletContext );
104 stopTaskQueueExecutor( tqeRepoScanning, servletContext );
105 stopTaskQueueExecutor( tqeIndexing, servletContext );
107 // stop the DefaultArchivaTaskScheduler and its scheduler
108 if ( repositoryTaskScheduler != null )
112 repositoryTaskScheduler.stop();
114 catch ( SchedulerException e )
116 servletContext.log( e.getMessage(), e );
121 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
122 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
123 schedulerField.setAccessible( true );
125 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
128 catch ( Exception e )
130 servletContext.log( e.getMessage(), e );
134 // close the application context
135 //applicationContext.close();
136 // TODO fix close call
137 //applicationContext.
143 private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor, ServletContext servletContext )
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, servletContext );
157 if ( service != null )
162 catch ( Exception e )
164 servletContext.log( e.getMessage(), e );
169 private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe, ServletContext servletContext )
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 )
180 servletContext.log( e.getMessage(), e );