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.actions.impl;
36
37 import org.apache.log4j.Logger;
38 import org.ogf.graap.wsag.api.logging.LogMessage;
39 import org.ogf.graap.wsag.server.actions.IActionBuilder;
40 import org.ogf.graap.wsag.server.actions.IActionHandler;
41 import org.ogf.graap.wsag.server.actions.ICreateAgreementAction;
42 import org.ogf.graap.wsag.server.actions.IGetTemplateAction;
43 import org.ogf.graap.wsag.server.actions.INegotiationAction;
44 import org.ogf.graap.wsag.server.api.IAgreementFactoryContext;
45 import org.ogf.graap.wsag4j.types.configuration.ActionType;
46 import org.ogf.graap.wsag4j.types.configuration.ImplementationConfigurationType;
47
48
49
50
51
52
53
54 public class ActionBuilder
55 implements IActionBuilder
56 {
57
58 private static final Logger LOG = Logger.getLogger( ActionBuilder.class );
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public AgreementFactoryAction
78 createAgreementFactoryAction( ActionType configuration,
79 IAgreementFactoryContext factoryContext ) throws Exception
80 {
81
82 LOG.debug( LogMessage.getMessage( "Instantiate factory action {0}", configuration.getName() ) );
83
84
85
86
87 ImplementationConfigurationType templateActionCfg =
88 configuration.getGetTemplateConfiguration();
89 IGetTemplateAction templateAction =
90 (IGetTemplateAction) loadAction( templateActionCfg.getImplementationClass(),
91 IGetTemplateAction.class );
92
93 HandlerContext context = new HandlerContext();
94 context.setFactoryContext( factoryContext );
95 context.setHandlerConfiguration( templateActionCfg );
96 templateAction.setHandlerContext( context );
97
98 LOG.debug( "Get template action instantiated." );
99
100
101
102
103 ImplementationConfigurationType createActionCfg =
104 configuration.getCreateAgreementConfiguration();
105 ICreateAgreementAction agreementAction =
106 (ICreateAgreementAction) loadAction( createActionCfg.getImplementationClass(),
107 ICreateAgreementAction.class );
108
109 context = new HandlerContext();
110 context.setFactoryContext( factoryContext );
111 context.setHandlerConfiguration( createActionCfg );
112
113 agreementAction.setHandlerContext( context );
114
115 LOG.debug( "Create agreement action instantiated." );
116
117
118
119
120
121
122 ImplementationConfigurationType negotiateActionCfg =
123 configuration.getNegotiationConfiguration();
124 INegotiationAction negotiateAction = null;
125
126 context = new HandlerContext();
127 context.setFactoryContext( factoryContext );
128 context.setHandlerConfiguration( negotiateActionCfg );
129
130 if ( negotiateActionCfg == null )
131 {
132 negotiateAction = new NegotiationUnsupportedAction();
133
134 LOG.debug( "No negotiation action configured." );
135 }
136 else
137 {
138 negotiateAction =
139 (INegotiationAction) loadAction( negotiateActionCfg.getImplementationClass(),
140 INegotiationAction.class );
141
142 LOG.debug( "Negotiation action instantiated." );
143 }
144 negotiateAction.setHandlerContext( context );
145
146
147
148
149 AgreementFactoryAction action =
150 new AgreementFactoryAction( templateAction, agreementAction, negotiateAction );
151
152
153
154
155 action.setName( configuration.getName() );
156
157
158
159
160
161 action.setUseSession( configuration.getUseSession() );
162
163 LOG.debug( LogMessage.getMessage( "Factory action supports wsag4j session: {0}",
164 configuration.getUseSession() ) );
165
166 LOG.debug( LogMessage.getMessage( "Factory action {0} successfully instantiated.",
167 configuration.getName() ) );
168
169 return action;
170
171 }
172
173 private IActionHandler loadAction( String className, Class<?> interfaceClass ) throws Exception
174 {
175 try
176 {
177 Object instance = Class.forName( className ).newInstance();
178
179 if ( !interfaceClass.isInstance( instance ) )
180 {
181 String msgText = "Class {0} is not an instance of {1}.";
182 String message =
183 LogMessage.format( msgText, className, interfaceClass.getName() );
184
185 throw new Exception( message );
186 }
187
188 if ( instance instanceof IActionHandler )
189 {
190 return (IActionHandler) instance;
191 }
192
193 String msgText = "Action {0} is not an instance of {1}.";
194 String message =
195 LogMessage.format( msgText, className, IActionHandler.class.getName() );
196
197 throw new Exception( message );
198 }
199 catch ( ClassNotFoundException ex )
200 {
201 String msgText =
202 "Action {0} could not be loaded. Reason: Class {1} could not be found.";
203 String message = LogMessage.format( msgText, className, ex.getMessage() );
204
205 throw new Exception( message );
206 }
207 catch ( Exception ex )
208 {
209 String msgText = "Action {0} could not be loaded. Reason: {1}";
210 String message = LogMessage.format( msgText, className, ex.getMessage() );
211
212 throw new Exception( message );
213 }
214 }
215
216 }