1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37 @Singleton
38 class AllPersistenceUnits
39 implements AllPersistenceServices, AllUnitsOfWork
40 {
41
42
43
44
45 private final List<PersistenceService> persistenceServices = new ArrayList<PersistenceService>();
46
47
48
49
50 private final List<UnitOfWork> unitsOfWork = new ArrayList<UnitOfWork>();
51
52
53
54
55 private final Set<Key<PersistenceService>> persistenceServiceKeys = new HashSet<Key<PersistenceService>>();
56
57
58
59
60 private final Set<Key<UnitOfWork>> unitOfWorkKeys = new HashSet<Key<UnitOfWork>>();
61
62
63
64
65
66
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
89
90
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
114
115
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
136
137
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
161
162
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 }