View Javadoc
1   package org.apache.onami.persist;
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 com.google.inject.Injector;
23  import com.google.inject.Key;
24  
25  import javax.inject.Inject;
26  import javax.inject.Singleton;
27  import java.util.ArrayList;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Set;
31  
32  import static org.apache.onami.persist.Preconditions.checkNotNull;
33  
34  /**
35   * All persistence units. This is a convenience wrapper for multiple persistence units.
36   */
37  @Singleton
38  class AllPersistenceUnits
39      implements AllPersistenceServices, AllUnitsOfWork
40  {
41  
42      /**
43       * Collection of all known persistence services.
44       */
45      private final List<PersistenceService> persistenceServices = new ArrayList<PersistenceService>();
46  
47      /**
48       * Collection of all known units of work.
49       */
50      private final List<UnitOfWork> unitsOfWork = new ArrayList<UnitOfWork>();
51  
52      /**
53       * Collection of the keys of all known persistence services.
54       */
55      private final Set<Key<PersistenceService>> persistenceServiceKeys = new HashSet<Key<PersistenceService>>();
56  
57      /**
58       * Collection of the keys of of all known units of work.
59       */
60      private final Set<Key<UnitOfWork>> unitOfWorkKeys = new HashSet<Key<UnitOfWork>>();
61  
62      /**
63       * Adds a persistence service and a unit of work to this collection.
64       *
65       * @param psKey  the persistence service to add. Must not be {@code null}.
66       * @param uowKey the unit of work to add. Must not be {@code null}.
67       */
68      void add( Key<PersistenceService> psKey, Key<UnitOfWork> uowKey )
69      {
70          persistenceServiceKeys.add( checkNotNull( psKey, "psKey is mandatory!" ) );
71          unitOfWorkKeys.add( checkNotNull( uowKey, "ouwKey is mandatory!" ) );
72      }
73  
74      @Inject
75      private void init( Injector injector )
76      {
77          for ( Key<PersistenceService> persistenceServiceKey : persistenceServiceKeys )
78          {
79              persistenceServices.add( injector.getInstance( persistenceServiceKey ) );
80          }
81          for ( Key<UnitOfWork> unitOfWorkKey : unitOfWorkKeys )
82          {
83              unitsOfWork.add( injector.getInstance( unitOfWorkKey ) );
84          }
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      // @Override
91      public void startAllStoppedPersistenceServices()
92      {
93          AggregatedException.Builder exceptionBuilder = new AggregatedException.Builder();
94          for ( PersistenceService ps : persistenceServices )
95          {
96              try
97              {
98                  if ( !ps.isRunning() )
99                  {
100                     ps.start();
101                 }
102             }
103             catch ( Exception e )
104             {
105                 exceptionBuilder.add( e );
106             }
107         }
108         exceptionBuilder.throwRuntimeExceptionIfHasCauses(
109             "multiple exception occurred while starting the persistence service" );
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     // @Override
116     public void stopAllPersistenceServices()
117     {
118         AggregatedException.Builder exceptionBuilder = new AggregatedException.Builder();
119         for ( PersistenceService ps : persistenceServices )
120         {
121             try
122             {
123                 ps.stop();
124             }
125             catch ( Exception e )
126             {
127                 exceptionBuilder.add( e );
128             }
129         }
130         exceptionBuilder.throwRuntimeExceptionIfHasCauses(
131             "multiple exception occurred while stopping the persistence service" );
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     // @Override
138     public void beginAllInactiveUnitsOfWork()
139     {
140         AggregatedException.Builder exceptionBuilder = new AggregatedException.Builder();
141         for ( UnitOfWork unitOfWork : unitsOfWork )
142         {
143             try
144             {
145                 if ( !unitOfWork.isActive() )
146                 {
147                     unitOfWork.begin();
148                 }
149             }
150             catch ( Exception e )
151             {
152                 exceptionBuilder.add( e );
153             }
154         }
155         exceptionBuilder.throwRuntimeExceptionIfHasCauses(
156             "multiple exception occurred while starting the unit of work" );
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     // @Override
163     public void endAllUnitsOfWork()
164     {
165         AggregatedException.Builder exceptionBuilder = new AggregatedException.Builder();
166         for ( UnitOfWork unitOfWork : unitsOfWork )
167         {
168             try
169             {
170                 unitOfWork.end();
171             }
172             catch ( Exception e )
173             {
174                 exceptionBuilder.add( e );
175             }
176         }
177         exceptionBuilder.throwRuntimeExceptionIfHasCauses(
178             "multiple exception occurred while ending the unit of work" );
179     }
180 
181 }