1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.server.persistence.impl;
36
37 import java.text.MessageFormat;
38 import java.util.UUID;
39
40 import org.apache.log4j.Logger;
41 import org.ogf.graap.wsag.api.logging.LogMessage;
42 import org.ogf.graap.wsag.server.api.IAgreementFactory;
43 import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
44 import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
45 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
46 import org.ogf.graap.wsag4j.types.configuration.WSAG4JEngineConfigurationType;
47
48
49
50
51 public class DatabaseWSAG4JPersistence extends AbstractWSAG4JPersistence
52 {
53
54 private static final Logger LOG = Logger.getLogger( DatabaseWSAG4JPersistence.class );
55
56
57
58
59 protected WSAG4JEngineConfigurationType[] config;
60
61
62
63
64
65
66 @Override
67 protected PersistentAgreementFactory[] doLoad() throws PersistedResourceException
68 {
69
70 if ( LOG.isDebugEnabled() )
71 {
72 LOG.debug( "Loading DatabaseWSAG4JPersistence instance" );
73 }
74
75 AbstractPersistentAgreementFactory persistentFactory = null;
76
77
78
79
80
81
82 try
83 {
84
85
86
87 IAgreementFactory factory = getAgreementFactoryPrototype( wsag4jConfiguration );
88 factory.initialize();
89
90
91
92
93 persistentFactory = new DatabasePersistentAgreementFactory( factory );
94
95
96
97
98 if ( wsag4jConfiguration.isSetResourceId() )
99 {
100 persistentFactory.setResourceId( wsag4jConfiguration.getResourceId() );
101 }
102 else
103 {
104 persistentFactory.setResourceId( UUID.randomUUID().toString() );
105 LOG.error( "agreement factory id not set in configuration file" );
106 LOG.error( "generate random agreement factory id " );
107 LOG.error( "agreement persistence will be disabled" );
108 }
109
110
111
112
113 persistentFactory.load();
114
115 if ( LOG.isDebugEnabled() )
116 {
117 LOG.debug( "wsag4jConfiguration.getResourceId(): " + wsag4jConfiguration.getResourceId() );
118 }
119
120 }
121 catch ( Exception ex )
122 {
123 String message =
124 MessageFormat.format(
125 "Could not load WSAG4J factory instance ''{0}''. Ignoring this instance.",
126 new Object[] { wsag4jConfiguration.getResourceId() } );
127 throw new PersistedResourceException( message, ex );
128 }
129
130 if ( LOG.isDebugEnabled() )
131 {
132 LOG.debug( "DatabaseWSAG4JPersistence initialized." );
133 }
134
135 return new PersistentAgreementFactory[] { persistentFactory };
136 }
137
138
139
140
141 @Override
142 public boolean doRemove( PersistentAgreementFactory factory ) throws PersistedResourceException
143 {
144 throw new PersistedResourceException( "Operation not supported for WSAG4J database persistence." );
145 }
146
147
148
149
150
151
152 @Deprecated
153 public void saveAgreementFactories( PersistentAgreementFactory[] factories ) throws Exception
154 {
155 if ( LOG.isDebugEnabled() )
156 {
157 LOG.debug( LogMessage.getMessage(
158 "Try to save all agreements for the {0} specified agreement factories.", factories.length ) );
159 }
160
161
162
163
164
165
166 for ( PersistentAgreementFactory persistentAgreementFactory : factories )
167 {
168 for ( PersistentAgreement persistentAgreement : persistentAgreementFactory.list() )
169 {
170 try
171 {
172
173 persistentAgreement.save();
174 }
175 catch ( Exception ex )
176 {
177 LOG.error( LogMessage.getMessage(
178 "Could not save agreement ''{0}'' for agreement factory ''{1}''.",
179 persistentAgreement, persistentAgreementFactory.getResourceId() ), ex );
180 }
181 }
182 }
183
184 if ( LOG.isDebugEnabled() )
185 {
186 LOG.debug( "Saved all agreements." );
187 }
188 }
189
190 }