001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 * Vladimir Pasquier <vpasquier@nuxeo.com>
018 * Stephane Lacoin <slacoin@nuxeo.com>
019 */
020
021package org.nuxeo.ecm.automation.client.jaxrs.impl;
022
023import java.net.URISyntaxException;
024import java.net.URL;
025
026import org.nuxeo.ecm.automation.client.AutomationClient;
027import org.nuxeo.ecm.automation.client.AutomationClientFactory;
028import org.osgi.framework.BundleActivator;
029import org.osgi.framework.BundleContext;
030
031/**
032 * @since 5.7 Automation client osgi activator to use HttpAutomationClient service
033 */
034public class AutomationClientActivator implements AutomationClientFactory, BundleActivator {
035
036    protected static volatile AutomationClientActivator instance;
037
038    protected BundleContext context;
039
040    @Override
041    public void start(BundleContext bundleContext) {
042        bundleContext.registerService(AutomationClientFactory.class.getName(), this, null);
043        this.instance = this;
044        this.context = bundleContext;
045    }
046
047    @Override
048    public void stop(BundleContext bundleContext) {
049        this.instance = null;
050        this.context = null;
051    }
052
053    @Override
054    public AutomationClient getClient(URL url) throws URISyntaxException {
055        return new HttpAutomationClient(url.toURI().toASCIIString());
056    }
057
058    public BundleContext getContext() {
059        return context;
060    }
061
062    @Override
063    public AutomationClient getClient(URL url, int httpCxTimeout) throws URISyntaxException {
064        return new HttpAutomationClient(url.toURI().toASCIIString(), httpCxTimeout);
065    }
066
067    public static AutomationClientActivator getInstance() {
068        return instance;
069    }
070}