From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/main/java/supplychain/OSGiBundleImpl.java | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 sandbox/axis2-1.4/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java (limited to 'sandbox/axis2-1.4/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java') diff --git a/sandbox/axis2-1.4/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java b/sandbox/axis2-1.4/itest/osgi-implementation/src/main/java/supplychain/OSGiBundleImpl.java new file mode 100644 index 0000000000..25d7bcf2ff --- /dev/null +++ b/sandbox/axis2-1.4/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