001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.connect.client;
021
022import org.nuxeo.connect.NuxeoConnectClient;
023import org.nuxeo.connect.connector.ConnectConnector;
024import org.nuxeo.connect.downloads.ConnectDownloadManager;
025import org.nuxeo.connect.packages.PackageManager;
026import org.nuxeo.connect.registration.ConnectRegistrationService;
027import org.nuxeo.connect.update.PackageUpdateService;
028import org.nuxeo.runtime.model.ComponentContext;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * Nuxeo Runtime Component used to wrap nuxeo-connect-client services as Nuxeo Services.
033 * <p>
034 * This is required because nuxeo-connect-client can not depend on Nuxeo Runtime, so this wrapper manages the
035 * integration and the callbacks needed.
036 *
037 * @author tiry
038 */
039public class ConnectClientComponent extends DefaultComponent {
040
041    @Override
042    public void activate(ComponentContext context) {
043        NuxeoConnectClient.setCallBackHolder(new NuxeoCallbackHolder());
044    }
045
046    // Wrap connect client services as Nuxeo Services
047    public <T> T getAdapter(Class<T> adapter) {
048
049        if (adapter.getCanonicalName().equals(ConnectConnector.class.getCanonicalName())) {
050            return adapter.cast(NuxeoConnectClient.getConnectConnector());
051        }
052
053        if (adapter.getCanonicalName().equals(ConnectRegistrationService.class.getCanonicalName())) {
054            return adapter.cast(NuxeoConnectClient.getConnectRegistrationService());
055        }
056
057        if (adapter.getCanonicalName().equals(ConnectDownloadManager.class.getCanonicalName())) {
058            return adapter.cast(NuxeoConnectClient.getDownloadManager());
059        }
060
061        if (adapter.getCanonicalName().equals(PackageManager.class.getCanonicalName())) {
062            return adapter.cast(NuxeoConnectClient.getPackageManager());
063        }
064
065        if (adapter.getCanonicalName().equals(PackageUpdateService.class.getCanonicalName())) {
066            return adapter.cast(NuxeoConnectClient.getPackageUpdateService());
067        }
068
069        return adapter.cast(this);
070    }
071
072}