001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.osgi.services;
020
021import java.util.Dictionary;
022import java.util.Enumeration;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.osgi.framework.Bundle;
027import org.osgi.framework.ServiceFactory;
028import org.osgi.framework.ServiceReference;
029
030/**
031 * Dummy service reference. servicefactory not supported.
032 *
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class ServiceReferenceImpl implements ServiceReference {
036
037    protected Bundle bundle;
038
039    protected Object service;
040
041    protected ServiceFactory factory;
042
043    protected Map<String, Object> props;
044
045    public ServiceReferenceImpl(Bundle bundle, Object service) {
046        this.bundle = bundle;
047        if (service instanceof ServiceFactory) {
048            factory = (ServiceFactory) service;
049        } else {
050            this.service = service;
051        }
052    }
053
054    @Override
055    public synchronized Object getProperty(String key) {
056        return props != null ? props.get(key) : null;
057    }
058
059    @Override
060    public synchronized String[] getPropertyKeys() {
061        return props != null ? props.keySet().toArray(new String[props.size()]) : null;
062    }
063
064    @Override
065    public Bundle getBundle() {
066        return bundle;
067    }
068
069    @Override
070    public Bundle[] getUsingBundles() {
071        // not impl.
072        return new Bundle[] {};
073    }
074
075    @Override
076    public boolean isAssignableTo(Bundle bundle, String className) {
077        if (service == null) {
078            return true;
079        }
080        try {
081            return service.getClass() == bundle.loadClass(className);
082        } catch (ClassNotFoundException e) {
083            return false;
084        }
085    }
086
087    @Override
088    public int compareTo(Object reference) {
089        throw new UnsupportedOperationException("Not implemented");
090    }
091
092    public Object getService() {
093        return service == null ? factory.getService(bundle, null) : service;
094    }
095
096    public synchronized void setProperties(Dictionary<String, ?> dict) {
097        if (props == null) {
098            props = new HashMap<String, Object>();
099        }
100        Enumeration<String> en = dict.keys();
101        while (en.hasMoreElements()) {
102            String key = en.nextElement();
103            props.put(key, dict.get(key));
104        }
105    }
106}