]> source.dussan.org Git - archiva.git/blob
b9cebd807dd69d92ed771a062a4e5bcf0a0d79e2
[archiva.git] /
1 package org.apache.maven.archiva.web.startup;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.lang.reflect.Field;
23
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
26
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;
40
41 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
42
43 /**
44  * ArchivaStartup - the startup of all archiva features in a deterministic order.
45  *
46  * @version $Id$
47  */
48 public class ArchivaStartup
49     implements ServletContextListener
50 {
51     private ThreadedTaskQueueExecutor tqeDbScanning;
52
53     private ThreadedTaskQueueExecutor tqeRepoScanning;
54
55     private ThreadedTaskQueueExecutor tqeIndexing;
56
57     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
58     
59     public void contextInitialized( ServletContextEvent contextEvent )
60     {
61         WebApplicationContext wac =
62             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
63
64         SecuritySynchronization securitySync =
65             (SecuritySynchronization) wac.getBean( PlexusToSpringUtils.buildSpringId( SecuritySynchronization.class ) );
66
67         repositoryTaskScheduler =
68             (RepositoryArchivaTaskScheduler) wac.getBean( PlexusToSpringUtils.buildSpringId(
69                                                                                              ArchivaTaskScheduler.class,
70                                                                                              "repository" ) );
71
72         tqeRepoScanning =
73             (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
74                                                                                         "repository-scanning" ) );
75
76         tqeIndexing =
77             (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
78                                                                                         "indexing" ) );
79
80         try
81         {
82             securitySync.startup();
83             repositoryTaskScheduler.startup();
84             Banner.display();
85         }
86         catch ( ArchivaException e )
87         {
88             throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
89         }
90     }
91
92     public void contextDestroyed( ServletContextEvent contextEvent )
93     {
94         WebApplicationContext applicationContext =
95             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
96         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
97         {
98             ( (ClassPathXmlApplicationContext) applicationContext ).close();
99         }
100
101         if ( applicationContext != null && applicationContext instanceof PlexusWebApplicationContext )
102         {
103             // stop task queue executors
104             stopTaskQueueExecutor( tqeDbScanning );
105             stopTaskQueueExecutor( tqeRepoScanning );
106             stopTaskQueueExecutor( tqeIndexing );
107
108             // stop the DefaultArchivaTaskScheduler and its scheduler
109             if ( repositoryTaskScheduler != null )
110             {
111                 try
112                 {
113                     repositoryTaskScheduler.stop();
114                 }
115                 catch ( StoppingException e )
116                 {
117                     e.printStackTrace();
118                 }
119             }
120
121             try
122             {
123                 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
124                 Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
125                 schedulerField.setAccessible( true );
126
127                 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
128                 scheduler.stop();
129             }
130             catch ( Exception e )
131             {   
132                 e.printStackTrace();
133             }
134
135             // close the application context
136             ( (PlexusWebApplicationContext) applicationContext ).close();
137         }
138     }
139
140     private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
141     {
142         if ( taskQueueExecutor != null )
143         {
144             Task currentTask = taskQueueExecutor.getCurrentTask();
145             if ( currentTask != null )
146             {
147                 taskQueueExecutor.cancelTask( currentTask );
148             }
149
150             try
151             {
152                 taskQueueExecutor.stop();
153                 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
154                 if ( service != null )
155                 {
156                     service.shutdown();
157                 }
158             }
159             catch ( Exception e )
160             {
161                 e.printStackTrace();
162             }
163         }
164     }
165
166     private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
167     {
168         ExecutorService service = null;
169         try
170         {
171             Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
172             executorServiceField.setAccessible( true );
173             service = (ExecutorService) executorServiceField.get( ttqe );
174         }
175         catch ( Exception e )
176         {
177             e.printStackTrace();
178         }
179         return service;
180     }
181 }