001: /*
002: * Copyright 2000-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018:
019: */
020:
021: package org.apache.wsrp4j.producer;
022:
023: import java.util.Properties;
024:
025: import org.apache.wsrp4j.exception.ErrorCodes;
026: import org.apache.wsrp4j.exception.WSRPException;
027: import org.apache.wsrp4j.exception.WSRPXHelper;
028: import org.apache.wsrp4j.log.LogManager;
029: import org.apache.wsrp4j.log.Logger;
030: import org.apache.wsrp4j.producer.provider.Provider;
031: import org.apache.wsrp4j.producer.provider.ProviderFactory;
032: import org.apache.wsrp4j.util.Utility;
033:
034: /**
035: * This class provides a static method to access the Provider component.
036: * Reads in the file "wsrp-services.properties".
037: *
038: * @author <a href="mailto:stefan.behl@de.ibm.com">Stefan Behl</a>
039: */
040: public class ProviderAccess {
041:
042: // the name of the .properties file
043: private static String WSRP_SERVICES = "wsrp-services.properties";
044:
045: // property name of the provider factory
046: private static String PROVIDER_FACTORY = "provider.factory";
047:
048: // the content of the properties file
049: private static Properties pFactories = null;
050:
051: // holds the instance of the provider after initializing
052: private static Provider provider = null;
053:
054: // log and trace support
055: private static Logger logger = LogManager.getLogManager()
056: .getLogger(ProviderAccess.class);
057:
058: /**
059: * Fetches a Provider-instance utilizing the read ProviderFactory and returns it.
060: *
061: * @return A Provider-interface.
062: */
063: public static Provider getProvider() throws WSRPException {
064: String MN = "getProvider";
065: if (logger.isLogging(Logger.TRACE_HIGH)) {
066: logger.entry(Logger.TRACE_HIGH, MN);
067: }
068:
069: if (provider == null) {
070: // get provider
071: ProviderFactory factory = (ProviderFactory) getFactory(PROVIDER_FACTORY);
072: provider = factory.getProvider();
073: }
074:
075: if (logger.isLogging(Logger.TRACE_HIGH)) {
076: logger.exit(Logger.TRACE_HIGH, MN);
077: }
078:
079: return provider;
080: }
081:
082: /**
083: Internal mehtod.
084: Returns a configured Factory class.
085: @param type java.lang.String representing the factory type.
086: @return java.lang.Object
087: */
088: private static Object getFactory(String type) throws WSRPException {
089: String MN = "getFactory";
090: if (logger.isLogging(Logger.TRACE_HIGH)) {
091: logger.entry(Logger.TRACE_HIGH, MN);
092: }
093:
094: Object obj = null;
095:
096: try {
097: pFactories = Utility
098: .loadPropertiesFromFile(ProviderAccess.WSRP_SERVICES);
099:
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: } catch (Exception e) {
110:
111: WSRPXHelper.throwX(logger, Logger.ERROR, MN,
112: ErrorCodes.PROVIDER_FACTORY_NOT_FOUND);
113:
114: }
115:
116: return obj;
117: }
118:
119: }
|