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 2006/04/18 00:26:27 UTC
svn commit: r394791 - in /cocoon/trunk/core/cocoon-core: META-INF/
src/main/java/org/apache/cocoon/core/osgi/
Author: danielf
Date: Mon Apr 17 15:26:21 2006
New Revision: 394791
URL: http://svn.apache.org/viewcvs?rev=394791&view=rev
Log:
Working on using beans from other bundles. Doesn't work yet.
Added:
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringConfigurableListableBeanRegistry.java
Modified:
cocoon/trunk/core/cocoon-core/META-INF/MANIFEST.MF
cocoon/trunk/core/cocoon-core/META-INF/components.xml
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringBeanRegistry.java
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringBridge.java
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
Modified: cocoon/trunk/core/cocoon-core/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/META-INF/MANIFEST.MF?rev=394791&r1=394790&r2=394791&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/META-INF/MANIFEST.MF (original)
+++ cocoon/trunk/core/cocoon-core/META-INF/MANIFEST.MF Mon Apr 17 15:26:21 2006
@@ -122,6 +122,7 @@
org.apache.cocoon.xml,
org.apache.cocoon.xml.dom,
org.apache.cocoon.xml.xlink,
+ org.apache.excalibur.source,
org.apache.excalibur.store,
org.springframework.beans,
org.springframework.beans.factory,
Modified: cocoon/trunk/core/cocoon-core/META-INF/components.xml
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/META-INF/components.xml?rev=394791&r1=394790&r2=394791&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/META-INF/components.xml (original)
+++ cocoon/trunk/core/cocoon-core/META-INF/components.xml Mon Apr 17 15:26:21 2006
@@ -22,7 +22,7 @@
bind="setLogger"/>
</scr:component>
- <scr:component name="org.apache.cocoon.core.osgi.SpringEcmFactory">
+ <scr:component name="org.apache.cocoon.core.osgi.SpringEcmFactory" immediate="true">
<scr:implementation class="org.apache.cocoon.core.osgi.OSGiSpringECMFactory"/>
<scr:property name="configFile" value="/META-INF/xconf/cocoon.xconf"/>
<scr:service>
@@ -44,7 +44,8 @@
<scr:property name="path" value="/sitemap-test"/>
<scr:property name="sitemapPath" value="/COB-INF/sitemap.xmap"/>
<scr:reference name="beanFactory"
- interface="org.apache.cocoon.core.osgi.CocoonSpringBeanRegistry"
+ interface="org.apache.cocoon.core.osgi.CocoonSpringBeanRegistry"
+ target="(component.name=org.apache.cocoon.core.osgi.SpringEcmFactory)"
bind="setBeanFactory"/>
</scr:component>
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringBeanRegistry.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringBeanRegistry.java?rev=394791&r1=394790&r2=394791&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringBeanRegistry.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringBeanRegistry.java Mon Apr 17 15:26:21 2006
@@ -15,15 +15,15 @@
*/
package org.apache.cocoon.core.osgi;
-import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.BeanFactory;
/**
* Another interface needed to be introduced because Equinox hat some problems with
- * the name @link {@link ConfigurableListableBeanFactory} because of some introspection
+ * the name @link {@link BeanFactory} because of some introspection
* done on all classes that end on "Factory".
*
* @version $Id$
*/
-public interface CocoonSpringBeanRegistry extends ConfigurableListableBeanFactory {
+public interface CocoonSpringBeanRegistry extends BeanFactory {
}
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringConfigurableListableBeanRegistry.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringConfigurableListableBeanRegistry.java?rev=394791&view=auto
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringConfigurableListableBeanRegistry.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/CocoonSpringConfigurableListableBeanRegistry.java Mon Apr 17 15:26:21 2006
@@ -0,0 +1,30 @@
+/*
+ * 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.core.osgi;
+
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+
+/**
+ * Another interface needed to be introduced because Equinox hat some problems with
+ * the name @link {@link ConfigurableListableBeanFactory} because of some introspection
+ * done on all classes that end on "Factory".
+ *
+ * @version $Id$
+ */
+public interface CocoonSpringConfigurableListableBeanRegistry
+extends CocoonSpringBeanRegistry, ConfigurableListableBeanFactory {
+
+}
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringBridge.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringBridge.java?rev=394791&r1=394790&r2=394791&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringBridge.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringBridge.java Mon Apr 17 15:26:21 2006
@@ -15,8 +15,13 @@
*/
package org.apache.cocoon.core.osgi;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.FatalBeanException;
+import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
/**
@@ -25,30 +30,104 @@
*
* @version $Id$
*/
-public class OSGiSpringBridge implements BeanFactory {
-
- public boolean containsBean(String arg0) {
- return false;
- }
-
- public String[] getAliases(String arg0) throws NoSuchBeanDefinitionException {
- return null;
- }
-
- public Object getBean(String arg0) throws BeansException {
- return null;
- }
-
- public Object getBean(String arg0, Class arg1) throws BeansException {
- return null;
- }
-
- public Class getType(String arg0) throws NoSuchBeanDefinitionException {
- return null;
- }
-
- public boolean isSingleton(String arg0) throws NoSuchBeanDefinitionException {
- return false;
- }
-
+public class OSGiSpringBridge implements CocoonSpringBeanRegistry {
+ private BundleContext bundleContext;
+
+ public static final String HINT_PROPERTY = "component.hint";
+
+ protected void activate(ComponentContext componentContext) {
+ System.out.println("OSGiSpringBridge: activate");
+ this.bundleContext = componentContext.getBundleContext();
+ }
+
+ protected void deactivate(ComponentContext componentContext) {
+
+ }
+
+ public boolean containsBean(String beanName) {
+ System.out.println("containsBean name=" + beanName);
+ try {
+ return getServiceReference(this.bundleContext, beanName) != null;
+ } catch (InvalidSyntaxException e) {
+ return false;
+ }
+ }
+
+ public String[] getAliases(String beanName) throws NoSuchBeanDefinitionException {
+ if (!containsBean(beanName))
+ throw new NoSuchBeanDefinitionException(beanName);
+ // FIXME aliases should probably be registred as services in some way
+ return new String[]{};
+ }
+
+ public Object getBean(String beanName) throws BeansException {
+ return getBean(beanName, null);
+ }
+
+ public Object getBean(String beanName, Class clazz) throws BeansException {
+ System.out.println("getBean name=" + beanName + " class=" +
+ (clazz != null ? clazz.getName() : ""));
+ ServiceReference reference = null;
+ try {
+ reference = getServiceReference(this.bundleContext, beanName);
+ } catch (InvalidSyntaxException e) {
+ throw new FatalBeanException("Cannot look up OSGi service " + beanName, e);
+ }
+ if (reference == null) {
+ throw new NoSuchBeanDefinitionException(beanName);
+ }
+ // TODO services are never returned to the context,
+ // don't know how to fix that with Spring
+ Object bean = this.bundleContext.getService(reference);
+
+ if (clazz != null && !clazz.isInstance(bean))
+ throw new BeanNotOfRequiredTypeException(beanName, clazz, bean.getClass());
+
+ return bean;
+ }
+
+ public Class getType(String beanName) throws NoSuchBeanDefinitionException {
+ return getBean(beanName).getClass();
+ }
+
+ public boolean isSingleton(String beanName) throws NoSuchBeanDefinitionException {
+ if (!containsBean(beanName))
+ throw new NoSuchBeanDefinitionException(beanName);
+ // all OSGi services are supposed to be singletons
+ return true;
+ }
+
+ public static ServiceReference getServiceReference(BundleContext ctx, String role)
+ throws InvalidSyntaxException {
+ ServiceReference result;
+ String itf = getServiceInterface(role);
+ String hint = getServiceHint(role);
+
+ if (hint == null) {
+ // Single interface role
+ result = ctx.getServiceReference(itf);
+ } else {
+ // '*' is used as wildcard character in LDAP filters, it is also used
+ // as hint for the URLSourceFactory and need to be escaped
+ if ("*".equals(hint))
+ hint = "\\*";
+ // Hinted role: create query
+ String query = "(" + HINT_PROPERTY + "=" + hint + ")";
+ ServiceReference[] results = ctx.getServiceReferences(itf, query);
+ result = (results != null && results.length > 1) ? results[0] : null;
+ }
+
+ return result;
+ }
+
+ public static String getServiceInterface(String role) {
+ int pos = role.indexOf('/');
+
+ return pos == -1 ? role : role.substring(0, pos);
+ }
+
+ public static String getServiceHint(String role) {
+ int pos = role.indexOf('/');
+ return pos == -1 ? null : role.substring(pos+1);
+ }
}
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java?rev=394791&r1=394790&r2=394791&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/osgi/OSGiSpringECMFactory.java Mon Apr 17 15:26:21 2006
@@ -16,15 +16,19 @@
package org.apache.cocoon.core.osgi;
import java.beans.PropertyEditor;
+import java.net.MalformedURLException;
import java.net.URL;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
+import javax.servlet.ServletException;
+
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceSelector;
+import org.apache.cocoon.ProcessingUtil;
import org.apache.cocoon.core.CoreUtil;
import org.apache.cocoon.core.Settings;
import org.apache.cocoon.core.container.spring.AvalonEnvironment;
@@ -49,13 +53,14 @@
/**
* The @link {@link OSGiSpringECMFactory} gives access to all Spring beans via the
- * {@link CocoonSpringBeanRegistry} interface which extends the @link {@link ConfigurableListableBeanFactory}.
+ * {@link CocoonSpringConfigurableListableBeanRegistry} interface which extends
+ * the @link {@link ConfigurableListableBeanFactory}.
*
* Additionally it exposes all beans as OSGi services.
*
* @version $Id$
*/
-public class OSGiSpringECMFactory implements CocoonSpringBeanRegistry {
+public class OSGiSpringECMFactory implements CocoonSpringConfigurableListableBeanRegistry {
private static final String MANIFEST_FILE = "/META-INF/MANIFEST.MF";
@@ -65,6 +70,9 @@
private Settings settings;
private ConfigurableListableBeanFactory beanFactory;
private ComponentContext componentContext;
+ // FIXME there should not be any need for a parentBeanFactory in this class as
+ // the wrapped class contains it
+ private BeanFactory parentBeanfactory;
protected Settings getSettings() {
return this.settings;
@@ -82,12 +90,54 @@
this.logger = logger;
}
- /*
+ // FIXME Added as there is a problem using methods with argument type ending with
+ // Factory in Equinox DS
+ public void setParentBeanFactory(CocoonSpringBeanRegistry factory) {
+ this.parentBeanfactory = factory;
+ }
+
+ /*
* TODO (DF/RP) move activation into a seperate thread
*/
protected void activate(ComponentContext componentContext) throws Exception {
this.componentContext = componentContext;
- URL manifestUrl = componentContext.getBundleContext().getBundle().getEntry(MANIFEST_FILE);
+
+ // if the logger and setter hasn't been injected look for them in the
+ // parent bean factory if there is one
+ if (this.logger == null && this.parentBeanfactory != null) {
+ this.logger = (Logger) this.parentBeanfactory.getBean(ProcessingUtil.LOGGER_ROLE);
+ }
+ if (this.settings == null && this.parentBeanfactory != null) {
+ this.settings = (Settings) this.parentBeanfactory.getBean(ProcessingUtil.SETTINGS_ROLE);
+ }
+
+ AvalonEnvironment avalonEnvironment = this.createAvalonEnvironment();
+
+ // get the configuration file property
+ String configFile= (String) this.componentContext.getProperties().get(CONFIG_FILE);
+ if(configFile == null) {
+ throw new ECMConfigurationFileNotSetException("You have to provide a ECM configurationf file!");
+ }
+ if (this.parentBeanfactory == null)
+ this.parentBeanfactory = BeanFactoryUtil.createRootBeanFactory(avalonEnvironment);
+ ConfigurationInfo springBeanConfiguration = ConfigReader.readConfiguration(configFile, avalonEnvironment);
+ this.beanFactory = BeanFactoryUtil.createBeanFactory(avalonEnvironment, springBeanConfiguration,
+ null, this.parentBeanfactory, false, false);
+ this.beanFactory.addBeanPostProcessor(new ServiceRegistrationPostProcessor());
+ this.beanFactory.preInstantiateSingletons();
+ // What is this for?
+ //Store store = (Store) beanFactory.getBean(Store.ROLE);
+ //this.logger.debug("Store: " + store);
+ }
+
+ /**
+ * create an Avalon environment (it's some kind of container for Avalon related information)
+ * @return the Avalon environment
+ * @throws ServletException
+ * @throws MalformedURLException
+ */
+ private AvalonEnvironment createAvalonEnvironment() throws ServletException, MalformedURLException {
+ URL manifestUrl = this.componentContext.getBundleContext().getBundle().getEntry(MANIFEST_FILE);
String contextPath = manifestUrl.toString();
contextPath = manifestUrl.toString().substring(0, contextPath.length() - (MANIFEST_FILE.length() - 1));
@@ -105,40 +155,14 @@
avalonEnvironment.logger = this.logger;
avalonEnvironment.settings = this.settings;
avalonEnvironment.servletContext = osgiServletContext;
-
- // get the configuration file property
- String configFile= (String) componentContext.getProperties().get(CONFIG_FILE);
- if(configFile == null) {
- throw new ECMConfigurationFileNotSetException("You have to provide a ECM configurationf file!");
- }
-
- ConfigurableListableBeanFactory rootBeanFactory = BeanFactoryUtil.createRootBeanFactory(avalonEnvironment);
- ConfigurationInfo springBeanConfiguration = ConfigReader.readConfiguration(configFile, avalonEnvironment);
- this.beanFactory = BeanFactoryUtil.createBeanFactory(avalonEnvironment, springBeanConfiguration,
- null, rootBeanFactory, false, false);
- this.beanFactory.addBeanPostProcessor(new ServiceRegistrationPostProcessor());
- this.beanFactory.preInstantiateSingletons();
- Store store = (Store) beanFactory.getBean(Store.ROLE);
- this.logger.debug("Store: " + store);
+ return avalonEnvironment;
}
protected void deactivate(ComponentContext componentContext) {
this.beanFactory.destroySingletons();
}
- public static String getServiceInterface(String role) {
- int pos = role.indexOf('/');
-
- return pos == -1 ? role : role.substring(0, pos);
- }
-
- public static String getServiceHint(String role) {
- int pos = role.indexOf('/');
- return pos == -1 ? null : role.substring(pos+1);
- }
-
private class ServiceRegistrationPostProcessor implements DestructionAwareBeanPostProcessor {
- private static final String HINT_PROPERTY = "component.hint";
private BundleContext bundleContext =
OSGiSpringECMFactory.this.componentContext.getBundleContext();
/** Mapping from service instance to ServiceReference */
@@ -169,12 +193,12 @@
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- String itfName = getServiceInterface(beanName);
- String hint = getServiceHint(beanName);
+ String itfName = OSGiSpringBridge.getServiceInterface(beanName);
+ String hint = OSGiSpringBridge.getServiceHint(beanName);
Dictionary properties = null;
if (hint != null) {
properties = new Hashtable();
- properties.put(HINT_PROPERTY, hint);
+ properties.put(OSGiSpringBridge.HINT_PROPERTY, hint);
}
if (isServiceSelector(bean)) {
// TODO implement
@@ -203,6 +227,14 @@
// are not singletons
info("========= initialization: ", bean, beanName);
}
+ String[] aliases = getAliases(beanName);
+ if (aliases.length > 0) {
+ System.out.print("ALIAS " + beanName + "[ ");
+ for (int i = 0; i < aliases.length; i++) {
+ System.out.print(aliases[i] + " ");
+ }
+ System.out.println("]");
+ }
return bean;
}
@@ -493,9 +525,4 @@
public void setParentBeanFactory(BeanFactory arg0) {
this.beanFactory.setParentBeanFactory(arg0);
}
-
- public Object getBeanFromReinhard(String name) {
- return this.getBean(name);
- }
-
}