View Javadoc
1   package org.eclipse.aether.internal.impl;
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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import java.util.ArrayList;
27  import java.util.concurrent.CopyOnWriteArrayList;
28  import java.util.concurrent.atomic.AtomicBoolean;
29  
30  import org.eclipse.aether.MultiRuntimeException;
31  import org.eclipse.aether.impl.RepositorySystemLifecycle;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   *
37   */
38  @Singleton
39  @Named
40  public class DefaultRepositorySystemLifecycle
41          implements RepositorySystemLifecycle
42  {
43      private final AtomicBoolean shutdown;
44  
45      private final CopyOnWriteArrayList<Runnable> onSystemEndedHandlers;
46  
47      @Inject
48      public DefaultRepositorySystemLifecycle()
49      {
50          this.shutdown = new AtomicBoolean( false );
51          this.onSystemEndedHandlers = new CopyOnWriteArrayList<>();
52      }
53  
54      @Override
55      public void systemEnded()
56      {
57          if ( shutdown.compareAndSet( false, true ) )
58          {
59              final ArrayList<Exception> exceptions = new ArrayList<>();
60              for ( Runnable onCloseHandler : onSystemEndedHandlers )
61              {
62                  try
63                  {
64                      onCloseHandler.run();
65                  }
66                  catch ( Exception e )
67                  {
68                      exceptions.add( e );
69                  }
70              }
71              MultiRuntimeException.mayThrow( "system on-close handler failures", exceptions );
72          }
73      }
74  
75      @Override
76      public void addOnSystemEndedHandler( Runnable handler )
77      {
78          requireNonNull( handler, "handler cannot be null" );
79          requireNotShutdown();
80          onSystemEndedHandlers.add( 0, handler );
81      }
82  
83      private void requireNotShutdown()
84      {
85          if ( shutdown.get() )
86          {
87              throw new IllegalStateException( "repository system is already shut down" );
88          }
89      }
90  }