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