View Javadoc

1   package org.apache.onami.autobind.install;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *  http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.concurrent.Callable;
25  
26  /**
27   * This class is internally used, to bind Installation Requests and process them
28   * sequentially in a predefined order.
29   */
30  public class InstallationContext {
31  
32      private final Map<BindingStage, List<Callable<?>>> context = new HashMap<BindingStage, List<Callable<?>>>();
33  
34      public void process()
35          throws Exception
36      {
37          for ( BindingStage stage : BindingStage.ORDERED )
38          {
39              List<Callable<?>> requests = context.get( stage );
40              if ( requests != null )
41              {
42                  for ( Callable<?> request : requests )
43                  {
44                      request.call();
45                  }
46              }
47          }
48      }
49  
50      public void add( BindingStage stage, Callable<?> request )
51      {
52          synchronized ( context )
53          {
54              List<Callable<?>> requests = context.get( stage );
55              if ( requests == null )
56              {
57                  requests = new ArrayList<Callable<?>>();
58                  context.put( stage, requests );
59              }
60              requests.add( request );
61          }
62      }
63  
64      public void add( StageableRequest request )
65      {
66          synchronized ( context )
67          {
68              List<Callable<?>> requests = context.get( request.getExecutionStage() );
69              if ( requests == null )
70              {
71                  requests = new ArrayList<Callable<?>>();
72                  context.put( request.getExecutionStage(), requests );
73              }
74              requests.add( request );
75          }
76      }
77  
78      public static interface StageableRequest
79          extends Callable<Void>
80      {
81  
82          BindingStage getExecutionStage();
83  
84      }
85  
86  }