]> source.dussan.org Git - archiva.git/blob
bca5a5fd0c1cadea996c4cf494bbe059083cbfbb
[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.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;
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 ArchivaTaskScheduler taskScheduler;
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         ResolverFactoryInit resolverFactory =
67             (ResolverFactoryInit) wac.getBean( PlexusToSpringUtils.buildSpringId( ResolverFactoryInit.class ) );
68
69         taskScheduler =
70             (ArchivaTaskScheduler) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaTaskScheduler.class ) );
71
72         tqeDbScanning =
73             (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
74                                                                                         "database-update" ) );
75         tqeRepoScanning =
76             (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
77                                                                                         "repository-scanning" ) );
78         tqeIndexing =
79             (ThreadedTaskQueueExecutor) wac.getBean( PlexusToSpringUtils.buildSpringId( TaskQueueExecutor.class,
80                                                                                         "indexing" ) );
81
82         try
83         {
84             securitySync.startup();
85             resolverFactory.startup();
86             taskScheduler.startup();
87             Banner.display();
88         }
89         catch ( ArchivaException e )
90         {
91             throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
92         }
93     }
94
95     public void contextDestroyed( ServletContextEvent contextEvent )
96     {
97         WebApplicationContext applicationContext =
98             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
99         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
100         {
101             ( (ClassPathXmlApplicationContext) applicationContext ).close();
102         }
103
104         if ( applicationContext != null && applicationContext instanceof PlexusWebApplicationContext )
105         {
106             // stop task queue executors
107             stopTaskQueueExecutor( tqeDbScanning );
108             stopTaskQueueExecutor( tqeRepoScanning );
109             stopTaskQueueExecutor( tqeIndexing );
110
111             // stop the DefaultArchivaTaskScheduler and its scheduler
112             if ( taskScheduler != null && taskScheduler instanceof DefaultArchivaTaskScheduler )
113             {
114                 try
115                 {
116                     ( (DefaultArchivaTaskScheduler) taskScheduler ).stop();
117                 }
118                 catch ( StoppingException e )
119                 {
120                     e.printStackTrace();
121                 }
122             }
123
124             try
125             {
126                 // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
127                 Field schedulerField = taskScheduler.getClass().getDeclaredField( "scheduler" );
128                 schedulerField.setAccessible( true );
129
130                 DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( taskScheduler );
131                 scheduler.stop();
132             }
133             catch ( Exception e )
134             {   
135                 e.printStackTrace();
136             }
137
138             // close the application context
139             ( (PlexusWebApplicationContext) applicationContext ).close();
140         }
141     }
142
143     private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
144     {
145         if ( taskQueueExecutor != null )
146         {
147             Task currentTask = taskQueueExecutor.getCurrentTask();
148             if ( currentTask != null )
149             {
150                 taskQueueExecutor.cancelTask( currentTask );
151             }
152
153             try
154             {
155                 taskQueueExecutor.stop();
156                 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
157                 if ( service != null )
158                 {
159                     service.shutdown();
160                 }
161             }
162             catch ( Exception e )
163             {
164                 e.printStackTrace();
165             }
166         }
167     }
168
169     private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
170     {
171         ExecutorService service = null;
172         try
173         {
174             Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
175             executorServiceField.setAccessible( true );
176             service = (ExecutorService) executorServiceField.get( ttqe );
177         }
178         catch ( Exception e )
179         {
180             e.printStackTrace();
181         }
182         return service;
183     }
184 }