1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* 
18  
19   */
20  
21  package org.apache.wsrp4j.producer;
22  
23  import java.util.Properties;
24  
25  import org.apache.wsrp4j.exception.ErrorCodes;
26  import org.apache.wsrp4j.exception.WSRPException;
27  import org.apache.wsrp4j.exception.WSRPXHelper;
28  import org.apache.wsrp4j.log.LogManager;
29  import org.apache.wsrp4j.log.Logger;
30  import org.apache.wsrp4j.producer.provider.Provider;
31  import org.apache.wsrp4j.producer.provider.ProviderFactory;
32  import org.apache.wsrp4j.util.Utility;
33  
34  /**
35   * This class provides a static method to access the Provider component.
36   * Reads in the file "wsrp-services.properties".
37   *
38   * @author <a href="mailto:stefan.behl@de.ibm.com">Stefan Behl</a>
39   */
40  public class ProviderAccess {
41  
42      // the name of the .properties file
43      private static String WSRP_SERVICES = "wsrp-services.properties";
44  
45      // property name of the provider factory
46      private static String PROVIDER_FACTORY = "provider.factory";
47  
48      // the content of the properties file
49      private static Properties pFactories = null;
50  
51      // holds the instance of the provider after initializing
52      private static Provider provider = null;
53  
54      // log and trace support
55      private static Logger logger = LogManager.getLogManager().getLogger(
56              ProviderAccess.class);
57  
58      /**
59       * Fetches a Provider-instance utilizing the read ProviderFactory and returns it.
60       *
61       * @return A Provider-interface.
62       */
63      public static Provider getProvider() throws WSRPException {
64          String MN = "getProvider";
65          if (logger.isLogging(Logger.TRACE_HIGH)) {
66              logger.entry(Logger.TRACE_HIGH, MN);
67          }
68  
69          if (provider == null) {
70              // get provider
71              ProviderFactory factory = (ProviderFactory) getFactory(PROVIDER_FACTORY);
72              provider = factory.getProvider();
73          }
74  
75          if (logger.isLogging(Logger.TRACE_HIGH)) {
76              logger.exit(Logger.TRACE_HIGH, MN);
77          }
78  
79          return provider;
80      }
81  
82      /**
83       Internal mehtod.
84       Returns a configured Factory class.
85       @param type java.lang.String representing the factory type.
86       @return java.lang.Object
87       */
88      private static Object getFactory(String type) throws WSRPException {
89          String MN = "getFactory";
90          if (logger.isLogging(Logger.TRACE_HIGH)) {
91              logger.entry(Logger.TRACE_HIGH, MN);
92          }
93  
94          Object obj = null;
95  
96          try {
97              pFactories = Utility
98                      .loadPropertiesFromFile(ProviderAccess.WSRP_SERVICES);
99  
100             String factoryName = (String) pFactories.get(type);
101             Class cl = Class.forName(factoryName);
102 
103             if (logger.isLogging(Logger.TRACE_HIGH)) {
104                 logger.exit(Logger.TRACE_HIGH, MN);
105             }
106 
107             obj = cl.newInstance();
108 
109         }
110         catch (Exception e) {
111 
112             WSRPXHelper.throwX(logger, Logger.ERROR, MN,
113                     ErrorCodes.PROVIDER_FACTORY_NOT_FOUND);
114 
115         }
116 
117         return obj;
118     }
119 
120 }