KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > store > source > BookStoreSourceFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
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 JavaDoc location, Map JavaDoc parameters) throws IOException JavaDoc, MalformedURLException JavaDoc {
51         if (!location.startsWith("bookstore:"))
52             throw new MalformedURLException JavaDoc("The URL does not use the bookstore sheme, it cannot be handled by this source implementation.");
53
54         String JavaDoc spec = location.substring("bookstore:".length());
55         int firstSlashPos = spec.indexOf('/');
56         if (firstSlashPos == -1)
57             throw new MalformedURLException JavaDoc("Invalid bookstore URL: " + location);
58
59         String JavaDoc bookInstanceName = spec.substring(0, firstSlashPos);
60         String JavaDoc 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 JavaDoc e) {
67             throw new RuntimeException JavaDoc("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