View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * WSAG4JFactoryActionBuilder
50   * 
51   * @author Oliver Waeldrich
52   * 
53   */
54  public class ActionBuilder
55      implements IActionBuilder
56  {
57  
58      private static final Logger LOG = Logger.getLogger( ActionBuilder.class );
59  
60      /**
61       * <!-- begin-UML-doc --> Creates a new instance of an {@link AgreementFactoryAction} based on the given
62       * configuration. <!-- end-UML-doc -->
63       * 
64       * @param configuration
65       *            The configuration of the factory action.
66       * 
67       * @param factoryContext
68       *            The context of the agreement factory that this action is created for.
69       * 
70       * @return The instantiated {@link AgreementFactoryAction} based on the given configuration.
71       * 
72       * @throws Exception
73       *             An error occurred during the instantiation process.
74       * 
75       * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
76       */
77      public AgreementFactoryAction
78          createAgreementFactoryAction( ActionType configuration,
79                                        IAgreementFactoryContext factoryContext ) throws Exception
80      {
81          // begin-user-code
82          LOG.debug( LogMessage.getMessage( "Instantiate factory action {0}", configuration.getName() ) );
83  
84          //
85          // load the IGetTemplateAction.
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         // load the ICreateAgreementAction.
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         // The configuration of a negotiation action is optional.
119         // It is not required that an agreement factory supports
120         // negotiation for a specific template.
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         // create the generic agreement factory action.
148         //
149         AgreementFactoryAction action =
150             new AgreementFactoryAction( templateAction, agreementAction, negotiateAction );
151 
152         //
153         // set the name for this action
154         //
155         action.setName( configuration.getName() );
156 
157         //
158         // specify, whether or not sessions are supported by this
159         // action block
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         // end-user-code
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 }