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.wsrf.bootstrap;
36  
37  import javax.security.auth.login.LoginContext;
38  
39  import org.apache.log4j.Logger;
40  import org.apache.muse.core.Environment;
41  import org.apache.muse.core.descriptor.DeploymentDescriptor;
42  import org.apache.muse.core.platform.AbstractIsolationLayer;
43  import org.ogf.graap.wsag.security.core.keystore.KeystoreLoginContext;
44  import org.ogf.graap.wsag.server.engine.WsagEngine;
45  
46  /**
47   * The bootstrap isolation layer implements the {@link AbstractIsolationLayer} in order to start up the WSRF
48   * layer before the initial invocation of the web service interface.
49   * 
50   * @author Oliver Waeldrich
51   * 
52   */
53  public class BootstrapIsolationLayer extends AbstractIsolationLayer
54  {
55  
56      private static final Logger LOG = Logger.getLogger( BootstrapIsolationLayer.class );
57  
58      private String realPath = null;
59  
60      private String deplomentUri = null;
61  
62      private boolean testMode = false;
63  
64      private boolean overrideHasBeenInitializedValue = false;
65  
66      private boolean overrideHasBeenShutdownValue = false;
67  
68      private boolean overrideHasFailedToInitializeValue = false;
69  
70      /**
71       * Creates a {@link BootstrapIsolationLayer} with a given application path (path of the expanded web
72       * application) and the external URI of the application.
73       * 
74       * @param realPath
75       *            the real path at the file system
76       * 
77       * @param defaultDeploymentUri
78       *            the default deployment URL
79       */
80      public BootstrapIsolationLayer( String realPath, String defaultDeploymentUri )
81      {
82          this.realPath = realPath;
83          deplomentUri = defaultDeploymentUri;
84  
85          if ( LOG.isDebugEnabled() )
86          {
87              LOG.debug( "create bootstrap isolation layer [real path: " + realPath + ", deployment uri: "
88                  + defaultDeploymentUri + "]" );
89          }
90      }
91  
92      /**
93       * Creates a new environment instance.
94       * 
95       * @return the new environment instance
96       */
97      protected Environment createEnvironment()
98      {
99          return new BootstrapEnvironment( realPath, WsagEngine.getGatewayURL() + "/services/WSAG4JService" );
100     }
101 
102     @Override
103     protected DeploymentDescriptor createDeploymentDescriptor()
104     {
105         return new WSAG4JDeploymentDescriptor();
106     }
107 
108     /**
109      * Initializes the environment.
110      */
111     public void initialize()
112     {
113 
114         LOG.info( "BootstrapIsolationLayer -> initialize" );
115 
116         synchronized ( BootstrapIsolationLayer.class )
117         {
118             if ( !hasBeenInitialized() )
119             {
120                 try
121                 {
122                     if ( LOG.isDebugEnabled() )
123                     {
124                         LOG.debug( "Initialize WsagEngine..." );
125                     }
126 
127                     //
128                     // initialize the WSAG4J server configuration
129                     //
130                     WsagEngine.initializeEngine( deplomentUri );
131 
132                     if ( LOG.isDebugEnabled() )
133                     {
134                         LOG.debug( "Build LoginContext..." );
135                     }
136 
137                     //
138                     // load the server login context
139                     //
140                     LoginContext context = new KeystoreLoginContext( WsagEngine.getWSRFConfiguration() );
141                     context.login();
142 
143                     if ( LOG.isDebugEnabled() )
144                     {
145                         LOG.debug( "Set LoginContext..." );
146                     }
147 
148                     //
149                     // set the login context for the engine
150                     //
151                     WsagEngine.setLoginContext( context );
152 
153                     if ( LOG.isDebugEnabled() )
154                     {
155                         LOG.debug( "Build LoginContext..." );
156                         LOG.debug( "Delegate initialization to AbstractIsolationLayer..." );
157                     }
158 
159                     //
160                     // call hook for subclasses
161                     //
162                     beforeParentInitialize();
163 
164                     //
165                     // initialize the isolation layer
166                     //
167                     super.initialize();
168 
169                     // set initialize override value
170                     setOverrideHasBeenInitializedValue( true );
171                 }
172                 catch ( Exception ex )
173                 {
174                     throw new RuntimeException(
175                                                 "The initialization process of the WSAG4J engine failed. Reason: "
176                                                     + ex.getMessage(), ex );
177                 }
178             }
179         }
180     }
181 
182     /**
183      * Shuts down the isolation layer.
184      */
185     public void shutdown()
186     {
187 
188         LOG.info( "BootstrapIsolationLayer -> shutdown" );
189 
190         synchronized ( BootstrapIsolationLayer.class )
191         {
192             if ( !hasBeenShutdown() )
193             {
194                 if ( LOG.isDebugEnabled() )
195                 {
196                     LOG.debug( "Shutting down WSAG4JResourceRouter..." );
197                 }
198 
199                 try
200                 {
201                     if ( LOG.isDebugEnabled() )
202                     {
203                         LOG.debug( "Delegate shutdown call..." );
204                     }
205 
206                     // shutdown the isolation layer
207                     super.shutdown();
208 
209                     // set shutdown override value
210                     setOverrideHasBeenShutdownValue( true );
211 
212                     // shutdown the WSAG4J instance (the server)
213                     WsagEngine.shutdownEngine();
214 
215                 }
216                 catch ( Exception ex )
217                 {
218                     throw new RuntimeException( "The shutdown process of the WSAG4J engine failed. Reason: "
219                         + ex.getMessage(), ex );
220                 }
221             }
222         }
223     }
224 
225     /**
226      * Hook for sub classes that is called before initialization.
227      */
228     protected void beforeParentInitialize()
229     {
230     }
231 
232     /**
233      * Toggles test mode on/off.
234      * 
235      * @param testMode
236      *            sets the test mode
237      */
238     public void setTestMode( boolean testMode )
239     {
240         this.testMode = testMode;
241     }
242 
243     @Override
244     public boolean hasBeenInitialized()
245     {
246         if ( testMode )
247         {
248             return overrideHasBeenInitializedValue;
249         }
250         else
251         {
252             return super.hasBeenInitialized();
253         }
254     }
255 
256     @Override
257     public boolean hasBeenShutdown()
258     {
259         if ( testMode )
260         {
261             return overrideHasBeenShutdownValue;
262         }
263         else
264         {
265             return super.hasBeenShutdown();
266         }
267     }
268 
269     @Override
270     public boolean hasFailedToInitialize()
271     {
272         if ( testMode )
273         {
274             return overrideHasFailedToInitializeValue;
275         }
276         else
277         {
278             return super.hasFailedToInitialize();
279         }
280     }
281 
282     /**
283      * 
284      * @return true if has been initialized
285      */
286     public boolean isOverrideHasBeenInitializedValue()
287     {
288         return overrideHasBeenInitializedValue;
289     }
290 
291     /**
292      * 
293      * @param overrideHasBeenInitializedValue
294      *            sets the overrideHasBeenInitializedValue
295      */
296     public void setOverrideHasBeenInitializedValue( boolean overrideHasBeenInitializedValue )
297     {
298         this.overrideHasBeenInitializedValue = overrideHasBeenInitializedValue;
299     }
300 
301     /**
302      * 
303      * @return true if has been shutdown
304      */
305     public boolean isOverrideHasBeenShutdownValue()
306     {
307         return overrideHasBeenShutdownValue;
308     }
309 
310     /**
311      * @param overrideHasBeenShutdownValue
312      *            sets the overrideHasBeenShutdownValue
313      */
314     public void setOverrideHasBeenShutdownValue( boolean overrideHasBeenShutdownValue )
315     {
316         this.overrideHasBeenShutdownValue = overrideHasBeenShutdownValue;
317     }
318 
319     /**
320      * 
321      * @return true if has failed to initialize
322      */
323     public boolean isOverrideHasFailedToInitializeValue()
324     {
325         return overrideHasFailedToInitializeValue;
326     }
327 
328     /**
329      * @param overrideHasFailedToInitializeValue
330      *            sets the overrideHasFailedToInitializeValue
331      */
332     public void setOverrideHasFailedToInitializeValue( boolean overrideHasFailedToInitializeValue )
333     {
334         this.overrideHasFailedToInitializeValue = overrideHasFailedToInitializeValue;
335     }
336 }