cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r418231 - in /cocoon/trunk/core/cocoon-core/src/main: java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf
Date Fri, 30 Jun 2006 10:52:57 GMT
Author: reinhard
Date: Fri Jun 30 03:52:56 2006
New Revision: 418231

URL: http://svn.apache.org/viewvc?rev=418231&view=rev
Log:
add a simple implementation of the block protocol (no polymorphism)

Added:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
  (with props)
Modified:
    cocoon/trunk/core/cocoon-core/src/main/resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf

Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java?rev=418231&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
(added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
Fri Jun 30 03:52:56 2006
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.components.source.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.environment.Context;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.SourceResolver;
+
+/**
+ * A factory for the block protocol. This is a very simple implementation of the block
+ * protocol as it doesn't support polymorphism. There is only a mapping from e.g.
+ * block:myblock:abc/x.xml to cocoon://blocks/myblock/abc/x.xml.
+ * 
+ * @version $Id$
+ */
+public class BlockSourceFactory extends AbstractLogEnabled implements SourceFactory, Serviceable,
+        Contextualizable, ThreadSafe {
+
+    /** The context */
+    protected Context envContext;
+
+    /** The ServiceManager */
+    protected ServiceManager manager;
+
+    /**
+     * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
+     */
+    public void service(ServiceManager manager) throws ServiceException {
+        this.manager = manager;
+    }
+
+    /**
+     * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
+     */
+    public void contextualize(org.apache.avalon.framework.context.Context context) throws
ContextException {
+        this.envContext = (Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
+    }
+
+    /**
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String,
+     *      java.util.Map)
+     */
+    public Source getSource(String location, Map parameters) throws SourceException, MalformedURLException,
+            IOException {
+
+        // parse the URL
+        String[] locationDetails = getLocationDetails(location);
+
+        // Lookup resolver and resolve URI
+        SourceResolver resolver = null;
+        Source source = null;
+        try {
+            resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+            source = resolver.resolveURI("cocoon://blocks/" + locationDetails[0] + "/" +
locationDetails[1]);
+        } catch (ServiceException se) {
+            throw new SourceException("Unable to lookup source resolver.", se);
+        } finally {
+            this.manager.release(resolver);
+        }
+        return source;
+    }
+
+    protected static String[] getLocationDetails(final String location) throws MalformedURLException
{
+        String[] blockDetails = new String[2];
+        int pos1 = location.indexOf(":");
+        String idAndLocation = location.substring(pos1 + 1);
+        int pos2 = idAndLocation.indexOf(":") + pos1 + 1;
+        blockDetails[0] = location.substring(pos1 + 1, pos2);
+        blockDetails[1] = location.substring(pos2 + 1);
+        if (blockDetails[1].indexOf(":") > -1) {
+            throw new MalformedURLException(
+                    "The block protocol doesn't support other protocols within their location.");
+        }
+        return blockDetails;
+    }
+
+    /**
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release(Source source) {
+        if (null != source) {
+            if (this.getLogger().isDebugEnabled()) {
+                this.getLogger().debug("Releasing block source " + source.getURI());
+            }
+            SourceResolver resolver = null;
+            try {
+                resolver = (SourceResolver) this.manager.lookup(SourceResolver.ROLE);
+                resolver.release(source);
+            } catch (ServiceException ignore) {
+                // we ignore this
+            } finally {
+                this.manager.release(resolver);
+            }
+        }
+    }
+
+}

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/core/cocoon-core/src/main/resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf?rev=418231&r1=418230&r2=418231&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/resources/META-INF/legacy/xconf/cocoon-core-source-factories.xconf
Fri Jun 30 03:52:56 2006
@@ -26,7 +26,8 @@
     <component-instance name="resource" class="org.apache.excalibur.source.impl.ResourceSourceFactory"/>
     <component-instance name="context" class="org.apache.cocoon.components.source.impl.ContextSourceFactory"/>
     <component-instance name="cocoon" class="org.apache.cocoon.components.source.impl.SitemapSourceFactory"/>
-
+    <component-instance name="block" class="org.apache.cocoon.components.source.impl.BlockSourceFactory"/>
+    
     <!--+
         | The "file:" source protocol is modifiable (can be written to) and
         | traversable (directory structures can be crawled).



Mime
View raw message