KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > source > CopletSourceFactory


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
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.apache.cocoon.portal.source;
17
18 import java.io.IOException JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.context.ContextException;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.portal.PortalService;
31 import org.apache.cocoon.portal.coplet.CopletInstanceData;
32 import org.apache.excalibur.source.Source;
33 import org.apache.excalibur.source.SourceException;
34 import org.apache.excalibur.source.SourceFactory;
35
36 /**
37  * The source factory for the coplet sources
38  *
39  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
40  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
41  *
42  * @version CVS $Id: CopletSourceFactory.java 123407 2004-12-27 13:51:59Z cziegeler $
43  */

44 public class CopletSourceFactory
45     extends AbstractLogEnabled
46     implements SourceFactory, Serviceable, ThreadSafe, Contextualizable {
47
48     protected ServiceManager manager;
49     protected Context context;
50     
51     /* (non-Javadoc)
52      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
53      */

54     public void service(ServiceManager serviceManager) throws ServiceException {
55         this.manager = serviceManager;
56     }
57
58     /* (non-Javadoc)
59      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
60      */

61     public void contextualize(Context context) throws ContextException {
62         this.context = context;
63     }
64     
65     /**
66      * @see org.apache.excalibur.source.SourceFactory#getSource(String, Map)
67      */

68     public Source getSource(String JavaDoc location, Map JavaDoc parameters)
69         throws MalformedURLException JavaDoc, IOException JavaDoc {
70         
71         String JavaDoc uri = location;
72         String JavaDoc protocol = null;
73         
74         // remove the protocol
75
int position = location.indexOf(':') + 1;
76         if (position != 0) {
77             protocol = location.substring(0, position);
78             location = location.substring(position+2);
79         }
80         PortalService service = null;
81         try {
82             service = (PortalService)this.manager.lookup(PortalService.ROLE);
83             CopletInstanceData coplet = service.getComponentManager().getProfileManager().getCopletInstanceData(location);
84             if ( coplet == null ) {
85                 throw new IOException JavaDoc("Unable to get coplet for " + location);
86             }
87             CopletSource copletSource =
88                 new CopletSource(uri, protocol,
89                                  coplet);
90             copletSource.contextualize(this.context);
91             copletSource.service(this.manager);
92             return copletSource;
93         } catch (ContextException ce) {
94             throw new SourceException("Unable to lookup profile manager.", ce);
95         } catch (ServiceException ce) {
96             throw new SourceException("Unable to lookup profile manager.", ce);
97         } finally {
98             this.manager.release(service);
99         }
100     }
101
102     /**
103      * @see org.apache.excalibur.source.SourceFactory#release(Source)
104      */

105     public void release(Source source) {
106         // nothing to do
107
}
108
109 }
110
Popular Tags