1 16 package org.outerj.daisy.books.store.source; 17 18 import org.apache.excalibur.source.SourceFactory; 19 import org.apache.excalibur.source.Source; 20 import org.apache.avalon.framework.service.Serviceable; 21 import org.apache.avalon.framework.service.ServiceManager; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.context.Context; 26 import org.apache.avalon.framework.context.ContextException; 27 import org.apache.cocoon.components.ContextHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.outerj.daisy.books.store.BookStore; 30 import org.outerj.daisy.books.store.BookInstance; 31 import org.outerj.daisy.repository.Repository; 32 import org.outerj.daisy.frontend.WikiHelper; 33 34 import java.util.Map ; 35 import java.io.IOException ; 36 import java.net.MalformedURLException ; 37 38 public class BookStoreSourceFactory implements SourceFactory, Serviceable, ThreadSafe, Contextualizable { 39 private ServiceManager serviceManager; 40 private Context context; 41 42 public void contextualize(Context context) throws ContextException { 43 this.context = context; 44 } 45 46 public void service(ServiceManager serviceManager) throws ServiceException { 47 this.serviceManager = serviceManager; 48 } 49 50 public Source getSource(String location, Map parameters) throws IOException , MalformedURLException { 51 if (!location.startsWith("bookstore:")) 52 throw new MalformedURLException ("The URL does not use the bookstore sheme, it cannot be handled by this source implementation."); 53 54 String spec = location.substring("bookstore:".length()); 55 int firstSlashPos = spec.indexOf('/'); 56 if (firstSlashPos == -1) 57 throw new MalformedURLException ("Invalid bookstore URL: " + location); 58 59 String bookInstanceName = spec.substring(0, firstSlashPos); 60 String resource = spec.substring(firstSlashPos + 1); 61 62 Request request = ContextHelper.getRequest(context); 63 Repository repository = null; 64 try { 65 repository = WikiHelper.getRepository(request, serviceManager); 66 } catch (Exception e) { 67 throw new RuntimeException ("Error getting repository from bookstore source.", e); 68 } 69 BookStore bookStore = (BookStore)repository.getExtension("BookStore"); 70 BookInstance bookInstance = bookStore.getBookInstance(bookInstanceName); 71 72 return new BookStoreSource(bookInstance, resource); 73 } 74 75 public void release(Source source) { 76 } 77 } 78 | Popular Tags |