From 3dd7e2c4da9c80b8182a2d04dc129a67aa7910df Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:06:58 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835121 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/java/supplychain/OSGiBundleImpl.java | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-0.99/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java (limited to 'sca-java-1.x/branches/sca-java-0.99/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java') diff --git a/sca-java-1.x/branches/sca-java-0.99/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java b/sca-java-1.x/branches/sca-java-0.99/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java new file mode 100644 index 0000000000..25d7bcf2ff --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.99/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java @@ -0,0 +1,163 @@ +/* + * 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 supplychain; + + + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Hashtable; + +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; +import org.osgi.framework.ServiceReference; + + +/** + * This class implements the Customer service component. + */ +public class OSGiBundleImpl implements ServiceListener, BundleActivator { + + + String name; + + String[] references; + Class[] referenceClasses; + Field[] referenceFields; + String[] referenceFilters; + + Class myClass; + ArrayList serviceNames = new ArrayList(); + ArrayList serviceObjs = new ArrayList(); + ArrayList> serviceProperties = new ArrayList>(); + + + private BundleContext bundleContext; + + public OSGiBundleImpl() {} + + public OSGiBundleImpl(String[] references, String[] filters) { + + myClass = this.getClass(); + this.name = this.getClass().getSimpleName(); + this.references = references == null?new String[0] : references; + + + try { + referenceClasses = new Class[references.length]; + referenceFields = new Field[references.length]; + referenceFilters = new String[references.length]; + for (int i = 0; i < references.length; i++) { + referenceFields[i] = this.getClass().getDeclaredField(references[i]); + referenceFields[i].setAccessible(true); + referenceClasses[i] = referenceFields[i].getType(); + + if (filters != null && filters.length > i) + referenceFilters[i] = filters[i]; + } + + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + public void start(BundleContext bc) { + + System.out.println("Started bundle " + name); + + this.bundleContext = bc; + + boolean useSingleRegisterService = serviceNames.size() > 1; + for (int i = 1; i < serviceNames.size(); i++) { + if (serviceObjs.get(i) != serviceObjs.get(0) || + serviceProperties.get(i) != serviceProperties.get(0)) { + useSingleRegisterService = false; + break; + } + } + if (useSingleRegisterService) { + bundleContext.registerService(serviceNames.toArray(new String[serviceNames.size()]), + serviceObjs.get(0), serviceProperties.get(0)); + } + else { + for (int i = 0; i < serviceNames.size(); i++) { + bundleContext.registerService(serviceNames.get(i), serviceObjs.get(i), serviceProperties.get(i)); + } + } + + started(bc); + + for (int i = 0; i < references.length; i++) { + + try { + if (referenceFields[i].get(this) != null) + continue; + + ServiceReference[] refs = bundleContext.getServiceReferences(referenceClasses[i].getName(), referenceFilters[i]); + if (refs != null && refs.length > 0) { + Object obj = bundleContext.getService(refs[0]); + referenceFields[i].set(this, referenceClasses[i].cast(obj)); + } else { + String filter = "(objectclass=" + referenceClasses[i].getName() + ")"; + this.bundleContext.addServiceListener(this, filter); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + public void stop(BundleContext bc) { + System.out.println("Stop bundle " + name); + + } + + protected void started(BundleContext bc) { + + } + + public void serviceChanged(ServiceEvent event) { + try { + if (event.getType() == ServiceEvent.REGISTERED) { + + ServiceReference ref = event.getServiceReference(); + Object obj = bundleContext.getService(ref); + for (int i = 0; i < references.length; i++) { + if (referenceClasses[i].isAssignableFrom(obj.getClass())) { + referenceFields[i].set(this, referenceClasses[i].cast(obj)); + } + } + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + + + public void registerService(Object serviceObject, String serviceName, Hashtable props) { + serviceObjs.add(serviceObject); + serviceNames.add(serviceName); + serviceProperties.add(props == null? new Hashtable() : props); + } +} + \ No newline at end of file -- cgit v1.2.3