ServiceRegistrationImpl.java

00001 /*
00002  * Copyright (c) 2003-2005, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 package org.knopflerfish.framework;
00036 
00037 import java.util.HashMap;
00038 import java.util.Dictionary;
00039 import java.util.Iterator;
00040 import java.util.Map;
00041 
00042 import org.osgi.framework.*;
00043 
00044 
00051 public class ServiceRegistrationImpl implements ServiceRegistration
00052 {
00056   BundleImpl bundle;
00057 
00061   Object service;
00062 
00066   ServiceReferenceImpl reference;
00067 
00071   PropertiesDictionary properties;
00072 
00077   HashMap /*Bundle->Integer*/ dependents = new HashMap();
00078 
00082   HashMap /*Bundle->Object*/ serviceInstances = new HashMap();
00083 
00087   boolean available;
00088 
00092   private Object eventLock = new Object();
00093 
00097   //private boolean unregistering = false;
00098 
00099 
00107   ServiceRegistrationImpl(BundleImpl b, Object s, PropertiesDictionary props) {
00108     bundle = b;
00109     service = s;
00110     properties = props;
00111     reference = new ServiceReferenceImpl(this);
00112     available = true;
00113   }
00114 
00115   //
00116   // ServiceRegistration interface
00117   //
00118 
00124   public ServiceReference getReference() {
00125     ServiceReference res = reference;
00126     if (res != null) {
00127       return res;
00128     } else {
00129       throw new IllegalStateException("Service is unregistered");
00130     }
00131   }
00132 
00133 
00139   public void setProperties(Dictionary props) {
00140     synchronized (eventLock) {
00141       synchronized (properties) {
00142         if (available) {
00143           String[] classes = (String[])properties.get(Constants.OBJECTCLASS);
00144           Long sid = (Long)properties.get(Constants.SERVICE_ID);
00145           properties = new PropertiesDictionary(props, classes, sid);
00146         } else {
00147           throw new IllegalStateException("Service is unregistered");
00148         }
00149       }
00150       bundle.framework.listeners.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, reference));
00151     }
00152   }
00153 
00159   public void unregister() {
00160     synchronized (eventLock) {
00161 
00162             if(!Framework.UNREGISTERSERVICE_VALID_DURING_UNREGISTERING)
00163             {
00164                     unregister_removeService();
00165             }
00166 
00167             bundle.framework.listeners.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, reference));
00168 
00169             if(Framework.UNREGISTERSERVICE_VALID_DURING_UNREGISTERING)
00170             {
00171                     unregister_removeService();
00172             }
00173     }
00174     bundle.framework.perm.callUnregister0(this);
00175     synchronized (properties) {
00176       bundle = null;
00177       dependents = null;
00178       reference = null;
00179       service = null;
00180       serviceInstances = null;
00181     }
00182   }
00183 
00184   void unregister0() {
00185     for (Iterator i = serviceInstances.entrySet().iterator(); i.hasNext();) {
00186       Map.Entry e = (Map.Entry)i.next();
00187       try {
00188         ((ServiceFactory)service).ungetService((Bundle)e.getKey(), this, e.getValue());
00189       } catch (Throwable ue) {
00190         bundle.framework.listeners.frameworkEvent(new FrameworkEvent(FrameworkEvent.ERROR, bundle, ue));
00191       }
00192     }
00193   }
00194 
00195   //
00196   // Framework internal
00197   //
00198 
00205   boolean isUsedByBundle(Bundle b) {
00206     Map deps = dependents;
00207     if (deps != null) {
00208       return deps.containsKey(b);
00209     } else {
00210       return false;
00211     }
00212   }
00213 
00217   private void unregister_removeService()
00218   {
00219       synchronized (properties) {
00220         if (available) {
00221           bundle.framework.services.removeServiceRegistration(this);
00222           available = false;
00223         } else {
00224           throw new IllegalStateException("Service is unregistered");
00225         }
00226       }
00227   }
00228 
00229 }

Generated on Mon Jan 11 21:19:16 2010 for OpenMobileIS by  doxygen 1.5.4