All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jkniv.camel.sap.jco3.SapJcoDestinationDataProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 the original author or authors.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.jkniv.camel.sap.jco3;

import java.util.HashMap;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sap.conn.jco.ext.DataProviderException;
import com.sap.conn.jco.ext.DestinationDataEventListener;
import com.sap.conn.jco.ext.DestinationDataProvider;

/**
 * Each application using Java Connector 3 deals with destinations. A destination represents a logical address 
 * of an ABAP system and thus separates the destination configuration from application logic. JCo retrieves
 * the destination parameters required at runtime from DestinationDataProvider and ServerDataProvider registered
 * in the runtime environment. If no provider is registered, JCo uses the default implementation that reads the 
 * configuration from a properties file. It is expected that each environment provides a suitable 
 * implementation that meets security and other requirements. Furthermore, only one DestinationDataProvider 
 * and only one ServerDataProvider can be registered per process. The reason behind this design decision 
 * is the following: the provider implementations are part of the environment infrastructure. 
 * The implementation should not be application specific, and in particular must be separated from 
 * application logic. 
 */
class SapJcoDestinationDataProvider implements DestinationDataProvider
{
    private static Logger                LOG             = LoggerFactory.getLogger(SapJcoDestinationDataProvider.class);
    private DestinationDataEventListener eL;
    private HashMap  propsByDestName = new HashMap();
    //private static SapJcoDestinationDataProvider instance;
    
    public SapJcoDestinationDataProvider()
    {
    }
    
    //    public static SapJcoDestinationDataProvider getInstance()
    //    {
    //        if (instance == null)
    //            instance = new SapJcoDestinationDataProvider();
    //        return instance;
    //    }
    
    /**
     * The name of SAP JCoDestination name that identifies a physical destination of a function call.
     */
    public Properties getDestinationProperties(String destName)
    {
        try
        {
            //read the destination from DB
            Properties p = propsByDestName.get(destName);
            if (p != null)
            {
                //check if all is correct, for example
                if (p.isEmpty())
                    throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION,
                            "destination [" + destName + "] configuration is incorrect", null);
                
                return p;
            }
            return null;
        }
        catch (RuntimeException re)
        {
            throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR,
                    "Internal error to get config from destination [" + destName + "]", re);
        }
    }
    
    /**
     * An implementation supporting events has to retain the eventListener instance provided
     * by the JCo runtime. This listener instance shall be used to notify the JCo runtime
     * about all changes in destination configurations.
     * 
     * @param eventListener The DestinationDataEventListener interface reacts on the events 
     * that a DestinationDataProvider could fire if the destination configuration was changed
     */
    public void setDestinationDataEventListener(DestinationDataEventListener eventListener)
    {
        this.eL = eventListener;
    }
    
    private boolean hasDestName(String destName)
    {
        return propsByDestName.containsKey(destName);
    }
    
    public boolean supportsEvents()
    {
        return true;
    }
    
    /**
     * Update the connect properties from a specific destName
     * @param destName the name of JCoDestination
     * @param properties connection properties
     */
    public void changeProperties(String destName, Properties properties)
    {
        synchronized (propsByDestName)
        {
            if (!hasDestName(destName))
            {
                
                if (properties == null)
                {
                    //System.out.println("Jco Destination removing ["+destName+"]");
                    LOG.trace("Jco Destination removing [{}]", destName);
                    if (propsByDestName.remove(destName) != null)
                        eL.deleted(destName);
                }
                else
                {
                    //System.out.println("Update Jco Destination ["+destName+"] = " + properties);
                    LOG.debug("Update Jco Destination [{}] = {}", destName, properties);
                    propsByDestName.put(destName, properties);
                    eL.updated(destName); // create or updated
                }
            }
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy