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 java.util.Map;
022import java.util.Properties;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.Endpoint;
026import org.apache.camel.RuntimeCamelException;
027import org.apache.camel.impl.DefaultComponent;
028import org.apache.camel.util.CamelContextHelper;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Represents the component that manages {@link SapJcoEndpoint}.
034 */
035public class SapJcoComponent extends DefaultComponent
036{
037    private static final Logger                  LOG        = LoggerFactory.getLogger(SapJcoComponent.class);
038    
039    public SapJcoComponent()
040    {
041        super();
042        LOG.trace("SapJcoComponent instanced successfully");
043    }
044    
045    public SapJcoComponent(CamelContext context)
046    {
047        super(context);
048        LOG.trace("SapJcoComponent instanced successfully");
049    }
050    
051    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception
052    {
053        SapJcoEndpoint endpoint = new SapJcoEndpoint(uri, this);
054        Properties prop = lookup(remaining);
055        setProperties(endpoint, parameters);
056        endpoint.setPropSapConnection(prop);
057        SapJcoRegistry.register();
058        SapJcoRegistry.setProperty(endpoint.getSapDestName(), prop);
059        LOG.trace("SapJcoComponent create Endpoint successfully");
060        return endpoint;
061    }
062    
063    @Override
064    protected void doShutdown() throws Exception
065    {
066        SapJcoRegistry.unregister();
067        super.doShutdown();
068    }
069    
070    @Override
071    protected void doStop() throws Exception
072    {
073        SapJcoRegistry.unregister();
074        super.doStop();
075    }
076    
077    private Properties lookup(String remaining)
078    {
079        Properties prop = null;
080        Object resource = JndiResources.lookup(remaining);
081        if (resource != null)
082        {
083            if (resource instanceof Properties)
084                prop = (Properties) resource;
085            else
086                throw new RuntimeCamelException("Resource with name [" + remaining
087                        + "] must be an instance of java.util.Properties to connect with Sap JCo");
088        }
089        else
090        {
091            prop = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining, Properties.class);
092            LOG.info("lookup successfully properties in camel context [" + remaining + "]");
093        }
094        return prop;
095    }
096    
097    /*
098    static void setProperty(String destName, Properties props)
099    {
100        if (MY_PROVIDER instanceof SapJcoDestinationDataProvider)
101            ((SapJcoDestinationDataProvider) MY_PROVIDER).changeProperties(destName, props);
102        else if (MY_PROVIDER != null
103                && SapDataProviderFactory.SHARED_DATA_PROVIDER.equals(MY_PROVIDER.getClass().getName()))
104        {
105            CHANGE_PROPERTIES.invoke(MY_PROVIDER, new Object[] { destName, props });
106            //invoke("changeProperties", new Class[]{String.class, Properties.class}, new Object[]{destName, props});
107        }
108    }
109    */
110    
111    /*
112    private static void invoke(String methodName, Class<?>[] classes, Object[] params)
113    {
114        try
115        {
116            Method method = myProvider.getClass().getDeclaredMethod(methodName, classes);
117            method.invoke(myProvider, params);
118        }
119        catch (Exception e)
120        {
121            LOG.error("Cannot invoke " + methodName + "(...)", e);
122        }
123    }
124    */
125}