View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, 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.client;
36  
37  import java.text.MessageFormat;
38  
39  import javax.security.auth.login.LoginContext;
40  
41  import org.apache.log4j.Logger;
42  import org.ogf.graap.wsag.api.WsagConstants;
43  import org.ogf.graap.wsag.api.client.AgreementFactoryRegistryClient;
44  import org.ogf.graap.wsag.api.configuration.WSAG4JConfiguration;
45  import org.ogf.graap.wsag.client.impl.AgreementFactoryRegistryBuilder;
46  import org.ogf.graap.wsag.client.impl.DefaultAgreementFactoryRegistryBuilder;
47  import org.w3.x2005.x08.addressing.EndpointReferenceType;
48  
49  /*
50   * AgreementFactoryLocator
51   */
52  
53  /**
54   * The AgreementFactoryLocator returns a new agreement factory instance.
55   * 
56   * @author Oliver Waeldrich
57   * 
58   */
59  public class AgreementFactoryRegistryLocator
60  {
61  
62      private static AgreementFactoryRegistryBuilder creator = null;
63  
64      private static final Logger LOG = Logger.getLogger( AgreementFactoryRegistryLocator.class );
65  
66      /**
67       * System property key to lookup {@link AgreementFactoryRegistryBuilder} implementation
68       */
69      public static final String REGISTRY_LOCATOR = "org.ogf.graap.wsag.client.AgreementFactoryRegistryLocator";
70  
71      /**
72       * Creates a new {@link AgreementFactoryRegistryClient} for the given endpoint. This method is only used
73       * if server side security is disabled.
74       * 
75       * @param epr
76       *            the agreement factory registry endpoint
77       * 
78       * @return the new {@link AgreementFactoryRegistryClient}
79       * 
80       * @throws Exception
81       *             indicates an error while querying the factory clients from the registry
82       */
83      public static final synchronized AgreementFactoryRegistryClient getFactoryRegistry( EndpointReferenceType epr )
84          throws Exception
85      {
86          return getFactoryRegistry( epr, (LoginContext) null );
87      }
88  
89      /**
90       * Creates a new {@link AgreementFactoryRegistryClient} for the given endpoint and with the given
91       * {@link LoginContext}. This method is used if server side security is enabled (default).
92       * 
93       * @param epr
94       *            the agreement factory registry endpoint
95       * 
96       * @param context
97       *            the login context to use
98       * 
99       * @return the new {@link AgreementFactoryRegistryClient}
100      * 
101      * @throws Exception
102      *             indicates an error while querying the factory clients from the registry
103      */
104     public static final synchronized AgreementFactoryRegistryClient getFactoryRegistry( EndpointReferenceType epr,
105                                                                                         LoginContext context )
106         throws Exception
107     {
108         if ( creator == null )
109         {
110 
111             //
112             // First check if the registry locator implementation is provided via system properties.
113             //
114             if ( System.getProperties().containsKey( REGISTRY_LOCATOR ) )
115             {
116                 String implClass = System.getProperties().getProperty( REGISTRY_LOCATOR );
117                 try
118                 {
119                     if ( LOG.isDebugEnabled() )
120                     {
121                         LOG.debug( "AgreementFactoryRegistryLocator implementation (" + implClass
122                             + ") specified in system properties." );
123                     }
124 
125                     @SuppressWarnings( "unchecked" )
126                     Class<AgreementFactoryRegistryBuilder> clazz =
127                         (Class<AgreementFactoryRegistryBuilder>) Class.forName( implClass );
128                     creator = (AgreementFactoryRegistryBuilder) clazz.newInstance();
129                 }
130                 catch ( ClassNotFoundException e )
131                 {
132                     String message =
133                         "AgreementFactoryRegistryBuilder implementation ({0}) not found. "
134                             + "Load implementation specified in {1}.";
135                     LOG.warn( MessageFormat.format( message, new Object[] { implClass,
136                         WsagConstants.WSAG4J_CLIENT_CONFIG_FILE } ) );
137                 }
138                 catch ( ClassCastException e )
139                 {
140                     String message =
141                         "AgreementFactoryRegistryBuilder implementation ({0}) does not implement the interface {1}. "
142                             + "Implementation was specified in {2}.";
143                     LOG.warn( MessageFormat.format( message, new Object[] { implClass,
144                         AgreementFactoryRegistryBuilder.class.getName(),
145                         WsagConstants.WSAG4J_CLIENT_CONFIG_FILE } ) );
146                 }
147 
148             }
149 
150             //
151             // if no implementation was loaded, try the config file
152             //
153             if ( creator == null )
154             {
155                 Class<AgreementFactoryRegistryBuilder> interfaceDef = AgreementFactoryRegistryBuilder.class;
156                 String wsag4jClientConfigFile = WsagConstants.WSAG4J_CLIENT_CONFIG_FILE;
157                 String defaultImpl = DefaultAgreementFactoryRegistryBuilder.class.getName();
158 
159                 creator =
160                     (AgreementFactoryRegistryBuilder) WSAG4JConfiguration.findImplementation( interfaceDef,
161                                                                                               wsag4jClientConfigFile,
162                                                                                               defaultImpl );
163             }
164         }
165         return creator.newInstance( epr, context );
166     }
167 }