001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.source;
018:
019: import java.io.IOException;
020: import java.net.MalformedURLException;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.context.Context;
024: import org.apache.avalon.framework.context.ContextException;
025: import org.apache.avalon.framework.context.Contextualizable;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.avalon.framework.thread.ThreadSafe;
031: import org.apache.cocoon.portal.PortalService;
032: import org.apache.cocoon.portal.coplet.CopletInstanceData;
033: import org.apache.excalibur.source.Source;
034: import org.apache.excalibur.source.SourceException;
035: import org.apache.excalibur.source.SourceFactory;
036:
037: /**
038: * The source factory for the coplet sources
039: *
040: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
041: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
042: *
043: * @version CVS $Id: CopletSourceFactory.java 433543 2006-08-22 06:22:54Z crossley $
044: */
045: public class CopletSourceFactory extends AbstractLogEnabled implements
046: SourceFactory, Serviceable, ThreadSafe, Contextualizable {
047:
048: protected ServiceManager manager;
049: protected Context context;
050:
051: /* (non-Javadoc)
052: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
053: */
054: public void service(ServiceManager serviceManager)
055: throws ServiceException {
056: this .manager = serviceManager;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
061: */
062: public void contextualize(Context context) throws ContextException {
063: this .context = context;
064: }
065:
066: /**
067: * @see org.apache.excalibur.source.SourceFactory#getSource(String, Map)
068: */
069: public Source getSource(String location, Map parameters)
070: throws MalformedURLException, IOException {
071:
072: String uri = location;
073: String protocol = null;
074:
075: // remove the protocol
076: int position = location.indexOf(':') + 1;
077: if (position != 0) {
078: protocol = location.substring(0, position);
079: location = location.substring(position + 2);
080: }
081: PortalService service = null;
082: try {
083: service = (PortalService) this .manager
084: .lookup(PortalService.ROLE);
085: CopletInstanceData coplet = service.getComponentManager()
086: .getProfileManager()
087: .getCopletInstanceData(location);
088: if (coplet == null) {
089: throw new IOException("Unable to get coplet for "
090: + location);
091: }
092: CopletSource copletSource = new CopletSource(uri, protocol,
093: coplet);
094: copletSource.contextualize(this .context);
095: copletSource.service(this .manager);
096: return copletSource;
097: } catch (ContextException ce) {
098: throw new SourceException(
099: "Unable to lookup profile manager.", ce);
100: } catch (ServiceException ce) {
101: throw new SourceException(
102: "Unable to lookup profile manager.", ce);
103: } finally {
104: this .manager.release(service);
105: }
106: }
107:
108: /**
109: * @see org.apache.excalibur.source.SourceFactory#release(Source)
110: */
111: public void release(Source source) {
112: // nothing to do
113: }
114:
115: }
|