001/*
002 * Copyright 2017 the original author or authors.
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package net.sf.jkniv.camel.sap.jco3;
020
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024import com.sap.conn.jco.ext.DestinationDataProvider;
025
026public class SapDataProviderFactory
027{
028    private static final Logger LOG = LoggerFactory.getLogger(SapDataProviderFactory.class);
029    public static final String  SHARED_DATA_PROVIDER   = "net.sf.jkniv.sap.env.SharedDestinationDataProvider";
030    public static final String  SHARED_SAPJCO_REGISTRY = "net.sf.jkniv.sap.env.SharedSapJcoRegistry";
031    private static DestinationDataProvider sapDataProvider;
032    
033    public static DestinationDataProvider getInstance()
034    {
035        if (sapDataProvider == null)
036        {
037            setInstanceOfDataProvider();
038        }
039        if (sapDataProvider == null)
040            sapDataProvider = new SapJcoDestinationDataProvider();
041        
042        LOG.debug("DestinationDataProvider instanceof {}",
043                (sapDataProvider != null ? sapDataProvider.getClass().getName() : "null"));
044        return sapDataProvider;
045    }
046    
047    private static void setInstanceOfDataProvider()
048    {
049        try
050        {
051            Invoke GET_INSTANCE = new Invoke(SHARED_DATA_PROVIDER, "getInstance", null);
052            sapDataProvider = (DestinationDataProvider) GET_INSTANCE.invoke(null);
053        }
054        catch (Exception e)
055        {
056            LOG.warn(
057                    SHARED_DATA_PROVIDER
058                            + " doesn't in the classpath [{}]. Add jkniv-sap-provider.jar file as shared library.",
059                    e.getMessage());
060        }
061    }
062    
063    public static Invoke getSharedRegister()
064    {
065        try
066        {
067            return  new Invoke(SHARED_SAPJCO_REGISTRY, "register", new Class[]{DestinationDataProvider.class, String.class});
068        }
069        catch (Exception e)
070        {
071            LOG.warn(
072                    SHARED_SAPJCO_REGISTRY
073                    + " doesn't in the classpath [{}]. Add jkniv-sap-provider.jar file as shared library.",
074                    e.getMessage());
075        }
076        return null;
077    }
078
079    public static Invoke getSharedUnregister()
080    {
081        try
082        {
083            return  new Invoke(SHARED_SAPJCO_REGISTRY, "unregister", new Class[]{DestinationDataProvider.class, String.class});
084        }
085        catch (Exception e)
086        {
087            LOG.warn(
088                    SHARED_SAPJCO_REGISTRY
089                    + " doesn't in the classpath [{}]. Add jkniv-sap-provider.jar file as shared library.",
090                    e.getMessage());
091        }
092        return null;
093    }
094}