ServiceContentHandlerFactory.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.net.*;
00038 import org.osgi.service.url.*;
00039 import java.util.Map;
00040 import java.util.HashMap;
00041 import org.osgi.framework.*;
00042 
00047 public class ServiceContentHandlerFactory 
00048   implements ContentHandlerFactory 
00049 {
00050   Framework framework;
00051 
00052   // JVM classpath handlers. Initialized once at startup
00053   String[] jvmPkgs = null;
00054 
00055   // String (mimetype) -> ContentHandlerWrapper
00056   Map wrapMap   = new HashMap();
00057 
00058   ServiceContentHandlerFactory(Framework fw) {
00059     this.framework = fw;
00060 
00061     // Initialize JVM classpath handlers
00062     String s = System.getProperty("java.content.handler.pkgs", "");
00063     
00064     jvmPkgs = Util.splitwords(s, "|");
00065     for(int i = 0; i < jvmPkgs.length; i++) {
00066       jvmPkgs[i] = jvmPkgs[i].trim();
00067       if(Debug.url) {
00068         Debug.println("JVMClassPathCH - jvmPkgs[" + i + "]=" + jvmPkgs[i]);
00069       }
00070     }
00071   }
00072   
00073   public ContentHandler createContentHandler(String mimetype) {
00074     
00075     if(Debug.url) {
00076       Debug.println("createContentHandler protocol=" + mimetype);
00077     }
00078     
00079     ContentHandler handler = getJVMClassPathHandler(mimetype);
00080     
00081     if(handler != null) {
00082       if(Debug.url) {
00083         Debug.println("using JVMClassPath handler for " + mimetype);
00084       }
00085       return handler;
00086     }
00087     
00088         
00089     handler = getServiceHandler(mimetype);
00090 
00091     if(handler != null) {
00092       if(Debug.url) {
00093         Debug.println("Using service ContentHandler for " + mimetype + ", handler=" + handler);
00094       }
00095       return handler;
00096     }
00097     
00098     if(Debug.url) {
00099       Debug.println("Using default ContentHandler for " + mimetype);
00100     }
00101 
00102     // delegate to system handler
00103     return null;
00104   }
00105 
00106   ContentHandler getServiceHandler(String mimetype) {
00107     try {
00108       String filter = "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype + ")";
00109       //TODO true or false?
00110       ServiceReference[] srl = framework.services
00111         .get(ContentHandler.class.getName(), filter, null, false);
00112       
00113       if(srl != null && srl.length > 0) {
00114         ContentHandlerWrapper wrapper = 
00115           (ContentHandlerWrapper)wrapMap.get(mimetype);
00116         
00117         if(wrapper == null) {
00118           wrapper =  new ContentHandlerWrapper(framework, mimetype);
00119           wrapMap.put(mimetype, wrapper);
00120         }
00121         return wrapper;
00122       }
00123     } catch (InvalidSyntaxException e) {
00124       throw new RuntimeException("Failed to get service: " + e);
00125     }
00126 
00127     return null;
00128   }
00129 
00130   
00131 
00132   ContentHandler getJVMClassPathHandler(String mimetype) {
00133     for(int i = 0; i < jvmPkgs.length; i++) {
00134       String converted = convertMimetype(mimetype);
00135 
00136       String className = jvmPkgs[i] + "." + converted + ".Handler";
00137       try { 
00138         if(Debug.url) {
00139           Debug.println("JVMClassPathCH - trying ContentHandler class=" + className);
00140         }
00141         Class clazz = Class.forName(className);
00142         ContentHandler handler = (ContentHandler)clazz.newInstance();
00143         
00144         if(Debug.url) {
00145           Debug.println("JVMClassPathCH - created ContentHandler class=" + className);
00146         }
00147 
00148         return handler;
00149       } catch (Throwable t) {
00150         if(Debug.url) {
00151           Debug.println("JVMClassPathCH - no ContentHandler class " + className);
00152         }
00153       }
00154     }
00155     
00156     if(Debug.url) {
00157       Debug.println("JVMClassPath - no ContentHandler for " + mimetype);
00158     }
00159     
00160     return null;
00161   }
00162 
00163   // please check this one for correctness
00164   static String convertMimetype(String s) {
00165 
00166     String bad = ".,:;*-";
00167     for(int i = 0; i < bad.length(); i++) {
00168       s = s.replace(bad.charAt(i), '_');
00169     }
00170 
00171     s = s.replace('/', '.');
00172 
00173     return s;
00174   }
00175 }
00176 

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