]> source.dussan.org Git - archiva.git/blob
da4bcce2558c239b71f401a9afe67288462c30c5
[archiva.git] /
1 package org.apache.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 org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
24 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
25 import org.apache.archiva.common.ArchivaException;
26 import org.apache.maven.index.NexusIndexer;
27 import org.apache.maven.index.context.IndexingContext;
28 import org.codehaus.plexus.taskqueue.Task;
29 import org.codehaus.plexus.taskqueue.execution.ThreadedTaskQueueExecutor;
30 import org.codehaus.redback.components.scheduler.DefaultScheduler;
31 import org.quartz.SchedulerException;
32 import org.springframework.web.context.WebApplicationContext;
33 import org.springframework.web.context.support.WebApplicationContextUtils;
34
35 import javax.servlet.ServletContextEvent;
36 import javax.servlet.ServletContextListener;
37 import java.lang.reflect.Field;
38 import java.util.concurrent.ExecutorService;
39
40 /**
41  * ArchivaStartup - the startup of all archiva features in a deterministic order.
42  *
43  * @version $Id$
44  */
45 public class ArchivaStartup
46     implements ServletContextListener
47 {
48     private ThreadedTaskQueueExecutor tqeDbScanning;
49
50     private ThreadedTaskQueueExecutor tqeRepoScanning;
51
52     private ThreadedTaskQueueExecutor tqeIndexing;
53
54     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
55
56     private PlexusSisuBridge plexusSisuBridge;
57
58     private NexusIndexer nexusIndexer;
59
60     public void contextInitialized( ServletContextEvent contextEvent )
61     {
62         WebApplicationContext wac =
63             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
64
65         SecuritySynchronization securitySync = wac.getBean( SecuritySynchronization.class );
66
67         repositoryTaskScheduler =
68             wac.getBean( "archivaTaskScheduler#repository", RepositoryArchivaTaskScheduler.class );
69
70         tqeRepoScanning = wac.getBean( "taskQueueExecutor#repository-scanning", ThreadedTaskQueueExecutor.class );
71
72         tqeIndexing = wac.getBean( "taskQueueExecutor#indexing", ThreadedTaskQueueExecutor.class );
73
74         plexusSisuBridge = wac.getBean( PlexusSisuBridge.class );
75
76         try
77         {
78             nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
79         }
80         catch ( PlexusSisuBridgeException e )
81         {
82             throw new RuntimeException( "Unable to get NexusIndexer: " + e.getMessage(), e );
83         }
84         try
85         {
86             securitySync.startup();
87             repositoryTaskScheduler.startup();
88             Banner.display();
89         }
90         catch ( ArchivaException e )
91         {
92             throw new RuntimeException( "Unable to properly startup archiva: " + e.getMessage(), e );
93         }
94     }
95
96     public void contextDestroyed( ServletContextEvent contextEvent )
97     {
98         WebApplicationContext applicationContext =
99             WebApplicationContextUtils.getRequiredWebApplicationContext( contextEvent.getServletContext() );
100
101         // TODO check this stop
102
103         /*
104         if ( applicationContext != null && applicationContext instanceof ClassPathXmlApplicationContext )
105         {
106             ( (ClassPathXmlApplicationContext) applicationContext ).close();
107         } */
108
109         if ( applicationContext != null ) //&& applicationContext instanceof PlexusWebApplicationContext )
110         {
111             // stop task queue executors
112             stopTaskQueueExecutor( tqeDbScanning );
113             stopTaskQueueExecutor( tqeRepoScanning );
114             stopTaskQueueExecutor( tqeIndexing );
115
116             // stop the DefaultArchivaTaskScheduler and its scheduler
117             if ( repositoryTaskScheduler != null )
118             {
119                 try
120                 {
121                     repositoryTaskScheduler.stop();
122                 }
123                 catch ( SchedulerException e )
124                 {
125                     e.printStackTrace();
126                 }
127
128                 try
129                 {
130                     // shutdown the scheduler, otherwise Quartz scheduler and Threads still exists
131                     Field schedulerField = repositoryTaskScheduler.getClass().getDeclaredField( "scheduler" );
132                     schedulerField.setAccessible( true );
133
134                     DefaultScheduler scheduler = (DefaultScheduler) schedulerField.get( repositoryTaskScheduler );
135                     scheduler.stop();
136                 }
137                 catch ( Exception e )
138                 {
139                     e.printStackTrace();
140                 }
141             }
142
143             // close the application context
144             //applicationContext.close();
145             // TODO fix close call
146             //applicationContext.
147         }
148
149         // closing correctly indexer to close correctly lock and file
150         for( IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values() )
151         {
152             try
153             {
154                 indexingContext.close( false );
155             } catch ( Exception e )
156             {
157                 contextEvent.getServletContext().log( "skip error closing indexingContext " + e.getMessage() );
158             }
159         }
160
161     }
162
163     private void stopTaskQueueExecutor( ThreadedTaskQueueExecutor taskQueueExecutor )
164     {
165         if ( taskQueueExecutor != null )
166         {
167             Task currentTask = taskQueueExecutor.getCurrentTask();
168             if ( currentTask != null )
169             {
170                 taskQueueExecutor.cancelTask( currentTask );
171             }
172
173             try
174             {
175                 taskQueueExecutor.stop();
176                 ExecutorService service = getExecutorServiceForTTQE( taskQueueExecutor );
177                 if ( service != null )
178                 {
179                     service.shutdown();
180                 }
181             }
182             catch ( Exception e )
183             {
184                 e.printStackTrace();
185             }
186         }
187     }
188
189     private ExecutorService getExecutorServiceForTTQE( ThreadedTaskQueueExecutor ttqe )
190     {
191         ExecutorService service = null;
192         try
193         {
194             Field executorServiceField = ttqe.getClass().getDeclaredField( "executorService" );
195             executorServiceField.setAccessible( true );
196             service = (ExecutorService) executorServiceField.get( ttqe );
197         }
198         catch ( Exception e )
199         {
200             e.printStackTrace();
201         }
202         return service;
203     }
204 }