ContentHandlerWrapper.java

00001 /*
00002  * Copyright (c) 2003-2004, 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.io.*;
00038 import java.net.*;
00039 
00040 import org.osgi.service.url.*;
00041 import org.osgi.framework.*;
00042 
00043 public class ContentHandlerWrapper
00053   extends ContentHandler 
00054 {
00055 
00056   Framework              framework;
00057   String                 mimetype;
00058   String                 filter;
00059   ServiceReference       best;
00060 
00061   ContentHandlerWrapper(Framework              framework,
00062                         String                 mimetype) {
00063     
00064     this.framework = framework;
00065     this.mimetype  = mimetype;
00066 
00067     filter = 
00068       "(&" + 
00069       "(" + Constants.OBJECTCLASS + "=" + 
00070       ContentHandler.class.getName() + ")" + 
00071       "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype + 
00072       ")" + 
00073       ")";
00074 
00075     ServiceListener serviceListener = 
00076       new ServiceListener() {
00077         public void serviceChanged(ServiceEvent evt) {
00078           ServiceReference ref = 
00079             evt.getServiceReference();
00080             
00081           switch (evt.getType()) {
00082           case ServiceEvent.MODIFIED: {
00083             // fall through
00084           } 
00085           case ServiceEvent.REGISTERED: {
00086             if (best == null) {
00087               updateBest();
00088               return ;
00089             }
00090             
00091             if (compare(best, ref) > 0) {
00092               best = ref;
00093             }
00094             
00095           }; break;
00096           case ServiceEvent.UNREGISTERING: {
00097             if (best.equals(ref)) {
00098               best = null;
00099             }
00100           }
00101           }
00102         }
00103       };
00104     
00105     try {
00106       framework.systemBC.addServiceListener(serviceListener, filter);
00107       
00108     } catch (Exception e) {
00109       throw new IllegalArgumentException("Could not register service listener for content handler: " + e);
00110     }
00111     
00112     if(Debug.url) {
00113       Debug.println("created wrapper for " + mimetype + ", filter=" + filter);
00114     }
00115   }
00116   
00117   private int compare(ServiceReference ref1, ServiceReference ref2) {
00118     Object tmp1 = ref1.getProperty(Constants.SERVICE_RANKING);
00119     Object tmp2 = ref2.getProperty(Constants.SERVICE_RANKING);
00120     
00121     int r1 = (tmp1 instanceof Integer) ? ((Integer)tmp1).intValue() : 0;
00122     int r2 = (tmp2 instanceof Integer) ? ((Integer)tmp2).intValue() : 0;
00123 
00124     if (r2 == r1) {
00125       Long i1 = (Long)ref1.getProperty(Constants.SERVICE_ID);      
00126       Long i2 = (Long)ref2.getProperty(Constants.SERVICE_ID);
00127       return i1.compareTo(i2);
00128 
00129     } else {
00130       return r2 -r1;
00131     }
00132   }
00133 
00134   private void updateBest() {
00135     try {
00136       ServiceReference[] refs =
00137         framework.systemBC.getServiceReferences(ContentHandler.class.getName(), 
00138                                                 filter);
00139       if (refs != null) {
00140         best = refs[0];
00141       } 
00142 
00143       for (int i = 1; i < refs.length; i++) {
00144         if (compare(best, refs[i]) > 0) {
00145           best = refs[i];
00146         }
00147       }
00148 
00149     } catch (Exception e) {
00150       // this should not happen.
00151       throw new IllegalArgumentException("Could not register url handler: " + e);
00152     }
00153   }
00154   
00155 
00156   private ContentHandler getService() {
00157     ContentHandler obj;
00158 
00159     try {
00160       if (best == null) {
00161         updateBest();
00162       }
00163 
00164       if (best == null) {
00165         throw new IllegalStateException("null: Lost service for protocol="+ mimetype);
00166       }
00167 
00168       obj = (ContentHandler)framework.systemBC.getService(best);
00169 
00170       if (obj == null) {
00171         throw new IllegalStateException("null: Lost service for protocol=" + mimetype);
00172       }
00173       
00174     } catch (Exception e) {
00175       throw new IllegalStateException("null: Lost service for protocol=" + mimetype);
00176     }
00177 
00178     return obj;
00179   }
00180 
00181   public Object getContent(URLConnection urlc) throws IOException {
00182     return getService().getContent(urlc);
00183   }
00184 
00185   public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
00186     return getService().getContent(urlc, classes);
00187   }
00188 
00189   public String toString() {
00190     StringBuffer sb = new StringBuffer();
00191 
00192     sb.append("ContentHandlerWrapper[");
00193 
00194     ServiceReference ref = best; 
00195     sb.append("mimetype=" + mimetype);
00196     if(ref != null) {
00197       sb.append(", id=" + ref.getProperty(Constants.SERVICE_ID));
00198       sb.append(", rank=" + ref.getProperty(Constants.SERVICE_RANKING));
00199     } else {
00200       sb.append(" no service tracked");
00201     }
00202 
00203     sb.append("]");
00204 
00205     return sb.toString();
00206   }
00207 }
00208 
00209   

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