You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by da...@apache.org on 2005/09/07 01:27:23 UTC

svn commit: r279176 - in /cocoon/trunk: src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java tools/targets/osgi-build.xml

Author: danielf
Date: Tue Sep  6 16:27:17 2005
New Revision: 279176

URL: http://svn.apache.org/viewcvs?rev=279176&view=rev
Log:
A BootstrapEnvironment for OSGi usage. Updated the build script to reflect some changes in block directory structure and reorganized the task somewhat to make it possible to compile a single bundle.

Added:
    cocoon/trunk/src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java
Modified:
    cocoon/trunk/tools/targets/osgi-build.xml

Added: cocoon/trunk/src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java?rev=279176&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java (added)
+++ cocoon/trunk/src/java/org/apache/cocoon/core/osgi/OSGiBootstrapEnvironment.java Tue Sep  6 16:27:17 2005
@@ -0,0 +1,246 @@
+/*
+ * Copyright 1999-2005 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.core.osgi;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import org.apache.avalon.excalibur.logger.LoggerManager;
+import org.apache.avalon.framework.context.DefaultContext;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.cocoon.core.BootstrapEnvironment;
+import org.apache.cocoon.core.MutableSettings;
+import org.apache.cocoon.core.Settings;
+import org.apache.cocoon.environment.Context;
+import org.apache.cocoon.environment.impl.AbstractContext;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.log.LogService;
+
+public class OSGiBootstrapEnvironment
+    implements BootstrapEnvironment {
+    
+    private final String configuration = "/WEB-INF/block.xconf";
+    private final ClassLoader classLoader;
+    public Logger logger = null;
+    private final String contextPath;
+    private final Context environmentContext;
+
+    public OSGiBootstrapEnvironment(ClassLoader classLoader, BundleContext bc)
+	throws IOException {
+        this.classLoader = classLoader;
+
+        // Create a logger manager that delegates to OSGi
+        // FIXME: have the maximum level as a property of the bundle
+        LoggerManager logManager = new OSGiLoggerManager(bc, LogService.LOG_DEBUG);
+        this.logger = logManager.getDefaultLogger();
+
+	Bundle bundle = getSitemapBundle(bc, this.logger); //bc.getBundle();
+	// Try to figure out the path of the root from that of /WEB-INF/block.xconf
+        String path = bundle.getResource(this.configuration).toString();
+	if (path == null)
+	    throw new FileNotFoundException("Unable to get resource '/WEB-INF/block.xconf'.");
+	path = path.substring(0, path.length() - this.configuration.length() - 1);
+        this.contextPath = path;
+
+        this.environmentContext = new OSGiContext(bundle);
+    }
+
+    /**
+     * @see org.apache.cocoon.core.BootstrapEnvironment#getBootstrapLogger(org.apache.cocoon.core.BootstrapEnvironment.LogLevel)
+     */
+    public Logger getBootstrapLogger(LogLevel logLevel) {
+        return this.logger;
+    }
+
+    /** Log a message during bootstrapping. This is used to log
+     * information before the logging system is setup.
+     * @param message A message.
+     */
+    public void log(String message) {
+        this.logger.info(message);
+    }
+
+    /** Log a message during bootstrapping. This is used to log
+     * information before the logging system is setup.
+     * @param message A message.
+     * @param error   An error.
+     */
+    public void log(String message, Throwable error) {
+        this.logger.info(message, error);
+    }
+
+    /**
+     * Pass the root logger back to the environment. As soon as the
+     * logging system is set up, this method is called.
+     * @param rootLogger The root logger.
+     */
+    public void setLogger(Logger rootLogger) {
+        this.logger = rootLogger;
+    }
+
+    /**
+     * Get the input stream from a resource at the given
+     * path. Only paths relative to the bootstrap context are
+     * supported. Returns null if no resource exists at the
+     * specified path
+     */
+    public InputStream getInputStream(String path) {
+        try {
+            return (new URL(this.contextPath + path)).openStream();
+        } catch (IOException e) {
+            this.log("Couldn't open " + this.contextPath + path);
+            return null;
+        }
+    }
+    
+    public void configure(MutableSettings settings) {
+        settings.setConfiguration(this.configuration);
+        settings.setWorkDirectory("work");
+        settings.setInitClassloader(true);
+    }
+
+    public void configureLoggingContext(DefaultContext context) {
+        // simply do nothing
+    }
+
+    public void configure(DefaultContext context) {
+    }
+
+    public ClassLoader getInitClassLoader() {
+        return this.classLoader;
+    }
+
+    public Context getEnvironmentContext() {
+        return this.environmentContext;
+    }
+    
+    /**
+     * Returns the URL to the application context.
+     */
+    public String getContextURL() {
+        return this.contextPath;
+    }
+
+    /**
+     * Returns a file to the application context.
+     * @return A file pointing to the context or null if the context is not
+     *         writeable.
+     */
+    public File getContextForWriting() {
+        return null;
+    }
+
+    /**
+     * Set the ConfigFile for the Cocoon object.
+     *
+     * @param configFileName The file location for the cocoon.xconf
+     *
+     * @throws Exception
+     */
+    public URL getConfigFile(String configFileName) throws Exception {
+        return new URL(this.contextPath + configFileName);
+    }
+
+    public String getClassPath(Settings settings) {
+        return null;
+    }
+
+    private Bundle getSitemapBundle(BundleContext bc, Logger log) {
+	String SITEMAP = "sitemap";
+        Bundle[] bundles = bc.getBundles();
+        for (int i = 0; i < bundles.length; i++) {
+            Bundle bundle = bundles[i];
+            try {
+                log.info("checking bundle " + bundle + " name=" + bundle.getHeaders().get(Constants.BUNDLE_NAME) + " category=" + bundle.getHeaders().get(Constants.BUNDLE_CATEGORY) + " state=" + bundle.getState());
+                if ((bundle.getState() == Bundle.INSTALLED ||
+                     bundle.getState() == Bundle.RESOLVED ||
+                     bundle.getState() == Bundle.ACTIVE)) {
+                    log.info("ok state");
+                    if (SITEMAP.equals(bundle.getHeaders().get(Constants.BUNDLE_CATEGORY))) {
+                        log.info("sitemap");
+                        return bundle;
+                    }
+                }
+            } catch (NullPointerException e) {
+                // BUNDLE_CATEGORY not present in the headers of the
+                // bundle, nothing to do.
+            }
+        }
+        return null;
+    }
+
+    public class OSGiContext extends AbstractContext {
+
+	Bundle bundle;
+	private Hashtable attributes = new Hashtable();
+	private Hashtable initparameters = new Hashtable();
+
+	public OSGiContext(Bundle bundle) {
+	    this.bundle = bundle;
+            this.initparameters.put("init-classloader", "true");
+            this.initparameters.put("work-directory", "work");
+	}
+
+	public Object getAttribute(String name) {
+	    return attributes.get(name);
+	}
+
+	public void setAttribute(String name, Object value) {
+	    attributes.put(name, value);
+	}
+
+	public void removeAttribute(String name) {
+	    attributes.remove(name);
+	}
+
+	public Enumeration getAttributeNames() {
+	    return attributes.keys();
+	}
+
+	public URL getResource(String path) throws MalformedURLException {
+	    return this.bundle.getResource(path);
+	}
+
+	public String getRealPath(String path) {
+	    try {
+		return getResource(path).toString();
+	    } catch (MalformedURLException me) {
+		// Will not happen, Bundle getResource does not throw MalformedURLException
+		return null;
+	    }
+	}
+
+	public String getMimeType(String file) {
+	    return null;
+	}
+
+	public String getInitParameter(String name) {
+	    return (String)initparameters.get(name);
+	}
+
+	public InputStream getResourceAsStream(String path) {
+	    return null;
+	}
+    }
+}

Modified: cocoon/trunk/tools/targets/osgi-build.xml
URL: http://svn.apache.org/viewcvs/cocoon/trunk/tools/targets/osgi-build.xml?rev=279176&r1=279175&r2=279176&view=diff
==============================================================================
--- cocoon/trunk/tools/targets/osgi-build.xml (original)
+++ cocoon/trunk/tools/targets/osgi-build.xml Tue Sep  6 16:27:17 2005
@@ -32,8 +32,6 @@
     <property name="build.osgi.bundles.template" value="org.apache.cocoon.template_1.0.0.jar"/>        
     <property name="build.osgi.knopflerfish.fwdir" value="fwdir"/>
 
-    <property name="lib.osgi" value="${lib}/osgi"/>
-    
     <property name="src.osgi.servlet" value="${src}/osgi-servlet"/>
     <property name="src.blocks.template" value="${blocks}/template/trunk/java"/>    
     
@@ -44,7 +42,7 @@
     <mkdir dir="${build.osgi.blocks.template}"/>      
   </target>
   
-  <target name="osgi" depends="osgi.bundling" 
+  <target name="osgi" depends="osgi.bundle.core, osgi.bundle.osgi-servlet, osgi.bundle.webapp, osgi.bundle.block.template" 
     description="Setup Cocoon to run within OSGi containers">
   </target>
   
@@ -53,7 +51,7 @@
     <delete dir="${build.osgi.knopflerfish.fwdir}"/>
   </target>
   
-  <target name="osgi.compile-osgi-servlet">
+  <target name="osgi.compile-osgi-servlet" depends="osgi.init, compile-core">
     <path id="osgi.osgi-servlet.classpath">
       <fileset dir="./lib/core">
         <include name="knopflerfish-*.jar"/>
@@ -76,12 +74,12 @@
       classpathref="osgi.osgi-servlet.classpath"/>
   </target>
   
-  <target name="osgi.prepare-webapp">
+  <target name="osgi.prepare-webapp" depends="osgi.init">
     <copy file="${webapp}/welcome.xml" tofile="${build.osgi.webapp}/welcome.xml" filtering="on"/>
     <copy file="${webapp}/not-found.xml" tofile="${build.osgi.webapp}/not-found.xml" filtering="on"/>
     <copy file="${webapp}/welcome.xslt" tofile="${build.osgi.webapp}/welcome.xslt" filtering="on"/>
     <copy file="${webapp}/sitemap.xmap" tofile="${build.osgi.webapp}/sitemap.xmap"/>
-    <copy file="${webapp}/sitemap-additions.xconf" tofile="${build.osgi.webapp}/sitemap-additions.xconf"/>
+    <copy file="${webapp}/WEB-INF/sitemap-additions/cocoon-core.xconf" tofile="${build.osgi.webapp}/WEB-INF/sitemap-additions/cocoon-core.xconf"/>
     <copy file="${webapp}/wiring.xml" tofile="${build.osgi.webapp}/wiring.xml" filtering="on"/>
       
     <copy todir="${build.osgi.webapp}/stylesheets" filtering="on">
@@ -113,6 +111,12 @@
       </fileset>
     </copy>
       
+    <replace file="${build.osgi.webapp}/sitemap.xmap">
+      <replacetoken><![CDATA[<map:include dir="context://WEB-INF/sitemap-additions" pattern="*.xconf"/>]]></replacetoken>
+      <replacevalue><![CDATA[<!-- excluded by the OSGi Ant task!!!!
+        map:include dir="context://WEB-INF/sitemap-additions" pattern="*.xconf"/-->]]></replacevalue>
+    </replace>  
+    
     <replace file="${build.osgi.webapp}/WEB-INF/cocoon.xconf">
       <replacetoken><![CDATA[<include dir="context://WEB-INF/xconf" pattern="*.xconf"/>]]></replacetoken>
       <replacevalue><![CDATA[<!-- replaced by the OSGi Ant task
@@ -150,7 +154,7 @@
     
   </target>
   
-  <target name="osgi.block.template">
+  <target name="osgi.block.template" depends="osgi.init, compile-core">
     <path id="osgi.blocks.template.classpath">
       <fileset dir="${lib.core}">
         <include name="*.jar"/>
@@ -158,9 +162,6 @@
       <fileset dir="${lib.endorsed}">
         <include name="*.jar"/>
       </fileset>         
-      <fileset dir="${lib.osgi}">
-        <include name="*/*.jar"/>
-      </fileset>          
       <!-- include Cocoon core -->
       <dirset dir="${build.dest}"/>
     </path>    
@@ -186,7 +187,7 @@
     
   </target>
   
-  <target name="osgi.bundling" depends="osgi.init, compile-core, osgi.compile-osgi-servlet, osgi.prepare-webapp, osgi.block.template"> 
+  <target name="osgi.bundle.core" depends="osgi.init, compile-core"> 
     
     <!-- note: packaging OSGI packages is only provisional; of course we will need a more generic build system in the future -->
   
@@ -204,15 +205,24 @@
         <include name="*.jar"/>
       </fileset>      
     </jar>
-    <!-- the Cocoon webapp bundle -->
-    <jar file="${build.osgi}/${build.osgi.bundles.cocoon_webapp}" manifest="${webapp}/Manifest.mf">
-      <fileset dir="${build.osgi.webapp}"/>
-    </jar>
-    <!-- the osgi-servlet bundle -->
+  </target>
+
+  <!-- the osgi-servlet bundle -->
+  <target name="osgi.bundle.osgi-servlet" depends="osgi.compile-osgi-servlet"> 
     <jar file="${build.osgi}/${build.osgi.bundles.cocoon_servlet}" manifest="${src.osgi.servlet}/Manifest.mf">
       <fileset dir="${build.osgi.servlet}"/>
     </jar>
-    <!-- the templating bundle -->
+  </target>
+
+  <!-- the Cocoon webapp bundle -->
+  <target name="osgi.bundle.webapp" depends="osgi.prepare-webapp"> 
+    <jar file="${build.osgi}/${build.osgi.bundles.cocoon_webapp}" manifest="${webapp}/Manifest.mf">
+      <fileset dir="${build.osgi.webapp}"/>
+    </jar>
+  </target>
+
+  <!-- the templating bundle -->
+  <target name="osgi.bundle.block.template" depends="osgi.block.template"> 
     <jar file="${build.osgi}/${build.osgi.bundles.template}" manifest="${src.blocks.template}/Manifest.mf">
       <fileset dir="${build.osgi.blocks.template}"/>
     </jar>