Author: tv
Date: Fri Sep  4 20:13:38 2009
New Revision: 811533

URL: http://svn.apache.org/viewvc?rev=811533&view=rev
Log:
A proposal for a Turbine OSGi service

Added:
    turbine/core/trunk/proposals/tv/
    turbine/core/trunk/proposals/tv/osgi/
    turbine/core/trunk/proposals/tv/osgi/bundle/
    turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/
    turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF   (with 
props)
    turbine/core/trunk/proposals/tv/osgi/bundle/build.properties   (with props)
    turbine/core/trunk/proposals/tv/osgi/bundle/src/
    turbine/core/trunk/proposals/tv/osgi/bundle/src/org/
    turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/
    turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/
    turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/
    
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/
    
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/
    
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java
   (with props)
    
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java
   (with props)
    turbine/core/trunk/proposals/tv/osgi/pom.xml   (with props)
    turbine/core/trunk/proposals/tv/osgi/service/
    turbine/core/trunk/proposals/tv/osgi/service/java/
    turbine/core/trunk/proposals/tv/osgi/service/java/org/
    turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/
    turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java
   (with props)
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java
   (with props)
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java
   (with props)
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java
   (with props)
    
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html
   (with props)
    turbine/core/trunk/proposals/tv/osgi/service/test/
    turbine/core/trunk/proposals/tv/osgi/service/test/org/
    turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/
    turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/
    
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/
    
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/
    
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java
   (with props)

Added: turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF?rev=811533&view=auto
==============================================================================
--- turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF (added)
+++ turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF Fri Sep  4 
20:13:38 2009
@@ -0,0 +1,10 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: HelloWorldBundle
+Bundle-SymbolicName: org.apache.turbine.osgi.test.HelloWorldBundle
+Bundle-Version: 1.0.0.dev
+Bundle-Activator: org.apache.turbine.osgi.test.helloworldbundle.Activator
+Bundle-Vendor: Apache
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Import-Package: org.osgi.framework;version="1.3.0"
+Export-Package: org.apache.turbine.osgi.test.helloworldbundle

Propchange: turbine/core/trunk/proposals/tv/osgi/bundle/META-INF/MANIFEST.MF
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: turbine/core/trunk/proposals/tv/osgi/bundle/build.properties
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/bundle/build.properties?rev=811533&view=auto
==============================================================================
--- turbine/core/trunk/proposals/tv/osgi/bundle/build.properties (added)
+++ turbine/core/trunk/proposals/tv/osgi/bundle/build.properties Fri Sep  4 
20:13:38 2009
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .

Propchange: turbine/core/trunk/proposals/tv/osgi/bundle/build.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,54 @@
+package org.apache.turbine.osgi.test.helloworldbundle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+public class Activator implements BundleActivator
+{
+    private ServiceRegistration m_registration = null;
+
+    /**
+     * @see
+     * 
org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
+     * )
+     */
+    public void start(BundleContext context) throws Exception
+    {
+        System.out.println("Hello World!!");
+        
+        // Register the property lookup service and save
+        // the service registration.
+        m_registration = 
context.registerService(HelloWorldService.class.getName(), new 
HelloWorldService(), null);
+    }
+
+    /**
+     * @see
+     * 
org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(BundleContext context) throws Exception
+    {
+        m_registration.unregister();
+        System.out.println("Goodbye World!!");
+    }
+
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/Activator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,28 @@
+package org.apache.turbine.osgi.test.helloworldbundle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+public class HelloWorldService
+{
+    public String sayHello(String toWhom)
+    {
+        return "Hello " + toWhom;
+    }
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/bundle/src/org/apache/turbine/osgi/test/helloworldbundle/HelloWorldService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: turbine/core/trunk/proposals/tv/osgi/pom.xml
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/pom.xml?rev=811533&view=auto
==============================================================================
--- turbine/core/trunk/proposals/tv/osgi/pom.xml (added)
+++ turbine/core/trunk/proposals/tv/osgi/pom.xml Fri Sep  4 20:13:38 2009
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+
+-->
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
+<!-- ... -->
+    <dependency>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>org.apache.felix.framework</artifactId>
+        <version>1.8.1</version>
+    </dependency>
+<!-- ... -->
+</project>
\ No newline at end of file

Propchange: turbine/core/trunk/proposals/tv/osgi/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,65 @@
+package org.apache.turbine.services.osgi;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.turbine.services.Service;
+import org.apache.turbine.services.TurbineServiceProvider;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+
+/**
+ * This service allows access to OSGi components.
+ *
+ * @author <a href="mailto:[email protected]";>Thomas Vandahl</a>
+ * @version $Id: OSGiComponentService.java 615328 2008-01-25 20:25:05Z tv $
+ */
+public interface OSGiComponentService
+        extends Service, TurbineServiceProvider
+{
+    /** The publically visible name of the service */
+    String SERVICE_NAME = "OSGiComponentService";
+
+    /** Where we write the OSGi Logger messages */
+    String OSGI_LOG_CATEGORY = "osgi";
+
+    /** Key used to define the root directory for the OSGi bundle cache */
+    String CONFIG_KEY_CACHE_DIRECTORY = "cacheDirectory";
+
+    /** Key used to install bundles by default */
+    String CONFIG_KEY_INSTALL_BUNDLE = "installBundle";
+    
+    /**
+     * Install a Bundle
+     * 
+     * @param bundleUrl the url pointing to the bundle
+     * 
+     * @return the Bundle object installed and activated
+     * @throws BundleException if the installation fails
+     */
+    Bundle installBundle(String bundleUrl) throws BundleException;
+    
+    /**
+     * Get all bundles
+     * 
+     * @return an array of bundles
+     * @throws BundleException if the operation fails
+     */
+    Bundle[] getBundles() throws BundleException;
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiComponentService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,136 @@
+package org.apache.turbine.services.osgi;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * This provides access to the system bundle context for OSGi components.
+ *
+ * @author <a href="mailto:[email protected]";>Thomas Vandahl</a>
+ * @version $Id: OSGiComponentService.java 615328 2008-01-25 20:25:05Z tv $
+ */
+public class OSGiHostActivator
+        implements BundleActivator
+{
+    /** a reference to the system bundle context */
+    private BundleContext context = null;
+    
+    /** 
+     * a map which stores the service names and their associated 
ServiceTracker 
+     * to allow for proper ungetService()
+     */
+    private Map<String, ServiceTracker> serviceTrackerHash = null;
+
+    /**
+     * Start the bundle (OSGi lifecycle)
+     */
+    public void start(BundleContext context)
+    {
+        this.context = context;
+        this.serviceTrackerHash = Collections.synchronizedMap(new 
HashMap<String, ServiceTracker>());
+    }
+
+    /**
+     * Stop the bundle (OSGi lifecycle)
+     */
+    public void stop(BundleContext context)
+    {
+        this.context = null;
+        
+        for (ServiceTracker tracker : this.serviceTrackerHash.values())
+        {
+            tracker.close();
+        }
+        
+        this.serviceTrackerHash.clear();
+    }
+    
+    /**
+     * Install a Bundle
+     * 
+     * @param bundleUrl the url pointing to the bundle
+     * 
+     * @return the Bundle object installed and activated
+     * @throws BundleException if the installation fails
+     */
+    public Bundle installBundle(String bundle) throws BundleException
+    {
+        return this.context.installBundle(bundle);
+    }
+
+    /**
+     * Get all bundles
+     * 
+     * @return an array of bundles
+     * @throws BundleException if the operation fails
+     */
+    public Bundle[] getBundles() throws BundleException
+    {
+        return this.context.getBundles();
+    }
+    
+    /**
+     * Get a (possibly cached) instance of a ServiceTracker for a given 
service class
+     * 
+     * @param clazzName the class name of the registered service
+     * 
+     * @return a ServiceTracker instance
+     */
+    private ServiceTracker getServiceTracker(String clazzName)
+    {
+        ServiceTracker tracker = null;
+        
+        if (this.serviceTrackerHash.containsKey(clazzName))
+        {
+            tracker = this.serviceTrackerHash.get(clazzName);
+        }
+        else
+        {
+            tracker = new ServiceTracker(this.context, clazzName, null);
+            tracker.open();
+            
+            this.serviceTrackerHash.put(clazzName, tracker);
+        }
+        
+        return tracker;
+    }
+
+    /**
+     * Get a service from the OSGi container
+     * 
+     * @param roleName the (interface-)name of the service.
+     * 
+     * @return the service object
+     */
+    public Object getService(String roleName)
+    {
+        ServiceTracker tracker = getServiceTracker(roleName);
+        return tracker.getService();
+    }
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiHostActivator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,77 @@
+package org.apache.turbine.services.osgi;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.commons.logging.Log;
+import org.apache.felix.framework.Logger;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * This encapsulates a logger for OSGi components.
+ *
+ * @author <a href="mailto:[email protected]";>Thomas Vandahl</a>
+ * @version $Id: OSGiComponentService.java 615328 2008-01-25 20:25:05Z tv $
+ */
+public class OSGiLogger
+        extends Logger
+{
+    private Log log;
+    
+    /**
+     * Constructor
+     * 
+     * @param log a Commons logger
+     */
+    public OSGiLogger(Log log)
+    {
+        super();
+        this.log = log;
+    }
+
+    /**
+     * @see 
org.apache.felix.framework.Logger#doLog(org.osgi.framework.ServiceReference, 
int, java.lang.String, java.lang.Throwable)
+     */
+    protected void doLog(ServiceReference sr, int level, String msg, Throwable 
throwable)
+    {
+        String prefix = (sr != null) ? sr.toString() : "OSGi-Framework";
+        
+        switch (level)
+        {
+            case Logger.LOG_DEBUG:
+                log.debug(prefix + ": " + msg, throwable);
+                break;
+
+            case Logger.LOG_INFO:
+                log.info(prefix + ": " + msg, throwable);
+                break;
+
+            case Logger.LOG_WARNING:
+                log.warn(prefix + ": " + msg, throwable);
+                break;
+
+            case Logger.LOG_ERROR:
+                log.error(prefix + ": " + msg, throwable);
+                break;
+
+            default:
+                log.fatal(prefix + ": " + msg, throwable);
+        }
+    }
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/OSGiLogger.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,287 @@
+package org.apache.turbine.services.osgi;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.felix.framework.Felix;
+import org.apache.felix.framework.Logger;
+import org.apache.felix.framework.util.FelixConstants;
+import org.apache.felix.framework.util.StringMap;
+import org.apache.turbine.Turbine;
+import org.apache.turbine.services.InitializationException;
+import org.apache.turbine.services.InstantiationException;
+import org.apache.turbine.services.TurbineBaseService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceException;
+
+/**
+ * An implementation of a Turbine service initializing the Apache Felix 
container
+ *
+ * @author <a href="mailto:[email protected]";>Thomas Vandahl</a>
+ */
+public class TurbineOSGiComponentService
+        extends TurbineBaseService
+        implements OSGiComponentService
+{
+    /** the logger to be used */
+    private static Log log = LogFactory.getLog(OSGI_LOG_CATEGORY);
+
+    /** OSGi container */
+    private Felix m_felix = null;
+
+    /** OSGi system bundle context accessor */
+    private OSGiHostActivator m_activator = null;
+
+    // -------------------------------------------------------------
+    // Service initialization
+    // -------------------------------------------------------------
+
+    public TurbineOSGiComponentService()
+    {
+        // nothing to do
+    }
+
+    /**
+     * Load all configured components and initialize them. This is a zero 
parameter variant which
+     * queries the Turbine Servlet for its config.
+     *
+     * @throws InitializationException Something went wrong in the init stage
+     */
+    public void init() throws InitializationException
+    {
+        try
+        {
+            log.info("Initializing TurbineOSGiComponentService ...");
+            initialize();
+            setInit(true);
+        }
+        catch (Exception e)
+        {
+            log.error("Exception caught initializing service: ", e);
+            throw new InitializationException("Initializing 
TurbineOSGiComponentService failed", e);
+        }
+    }
+
+    /**
+     * Shuts the Component Service down, calls dispose on the components that 
implement this
+     * interface
+     *
+     */
+    public void shutdown()
+    {
+        log.info("Disposing TurbineOSGiComponentService ...");
+        dispose();
+        setInit(false);
+    }
+
+    /**
+     * Initializes the container
+     *
+     * @throws Exception generic exception
+     */
+    public void initialize() throws Exception
+    {
+        // get the configuration from the baseclass
+        Configuration conf = this.getConfiguration();
+
+        // Create a case-insensitive configuration property map.
+        StringMap configMap = new StringMap(false);
+
+        // Add core OSGi packages to be exported from the class path
+        // via the system bundle.
+        configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
+            "org.osgi.framework; version=1.3.0," +
+            "org.osgi.service.packageadmin; version=1.2.0," +
+            "org.osgi.service.startlevel; version=1.0.0," +
+            "org.osgi.service.url; version=1.0.0");
+
+        // Provide a logger
+        configMap.put(FelixConstants.LOG_LOGGER_PROP, new OSGiLogger(log));
+        configMap.put(FelixConstants.LOG_LEVEL_PROP, 
String.valueOf(Logger.LOG_DEBUG));
+        
+        // determine the home directory
+        String cacheDir = conf.getString(CONFIG_KEY_CACHE_DIRECTORY, 
Turbine.getRealPath("/"));
+        File dir = new File(cacheDir);
+        
+        if (!dir.isAbsolute())
+        {
+            cacheDir = Turbine.getRealPath("/") + File.separator + cacheDir;
+        }
+        
+        log.info("Using cache directory " + cacheDir);
+
+        // Explicitly specify the directory to use for caching bundles.
+        configMap.put("felix.cache.rootdir", cacheDir);
+
+        // Create host activator;
+        m_activator = new OSGiHostActivator();
+        List<BundleActivator> list = new ArrayList<BundleActivator>();
+        list.add(m_activator);
+        configMap.put(FelixConstants.SYSTEMBUNDLE_ACTIVATORS_PROP, list);
+        
+        // Now create an instance of the framework with
+        // our configuration properties and activator.
+        m_felix = new Felix(configMap);
+
+        // Start Felix instance.
+        m_felix.start();
+        
+        // Install default bundles from the configuration
+        for (Iterator keys = conf.getKeys(); keys.hasNext();)
+        {
+            String key = (String) keys.next();
+            String[] keyParts = StringUtils.split(key, ".");
+            
+            if (keyParts.length == 2 && 
CONFIG_KEY_INSTALL_BUNDLE.equals(keyParts[0]))
+            {
+                Bundle bundle = installBundle(conf.getString(key));
+                log.info("Installed bundle " + bundle.getSymbolicName());
+                
+                if (bundle.getState() != Bundle.ACTIVE)
+                {
+                    bundle.start();
+                }
+            }
+        }
+    }
+
+    /**
+     * Disposes of the container and releases resources
+     */
+    public void dispose()
+    {
+        // Shut down the felix framework when stopping the
+        // Turbine service.
+        if (this.m_felix != null)
+        {
+            try
+            {
+                this.m_felix.stop();
+                this.m_felix.waitForStop(30000);
+            }
+            catch (Exception e)
+            {
+                // Swallow
+            }
+            
+            this.m_felix = null;
+        }
+    }
+
+    /**
+     * Returns an instance of the named component
+     *
+     * @param roleName Name of the role the component fills.
+     * @return an instance of the named component
+     * @throws Exception generic exception
+     */
+    public Object lookup(String roleName) throws ServiceException
+    {
+        return this.m_activator.getService(roleName);
+    }
+
+    /**
+     * Releases the component.
+     *
+     * @param component the component to release
+     */
+    public void release(Object component)
+    {
+        // do nothing
+    }
+
+    /**
+     * @see 
org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
+     */
+    public boolean hasService(String roleName)
+    {
+        return this.m_activator.getService(roleName) != null;
+    }
+
+    // -------------------------------------------------------------
+    // TurbineServiceProvider
+    // -------------------------------------------------------------
+
+    /**
+     * @see 
org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String)
+     */
+    public boolean exists(String roleName)
+    {
+        return this.hasService(roleName);
+    }
+
+    /**
+     * @see 
org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String)
+     */
+    public Object get(String roleName) throws InstantiationException
+    {
+        try
+        {
+            return this.lookup(roleName);
+        }
+        catch (ServiceException e)
+        {
+            String msg = "Unable to get the following service : " + roleName;
+            log.error(msg);
+            throw new InstantiationException(msg);
+        }
+        catch (Throwable t)
+        {
+            String msg = "Unable to get the following service : " + roleName;
+            log.error(msg,t);
+            throw new InstantiationException(msg,t);
+        }
+    }
+    
+    /**
+     * Install a Bundle
+     * 
+     * @param bundleUrl the url pointing to the bundle
+     * 
+     * @return the Bundle object installed and activated
+     * @throws BundleException if the installation fails
+     */
+    public Bundle installBundle(String bundleUrl) throws BundleException
+    {
+        return this.m_activator.installBundle(bundleUrl);
+    }
+
+    /**
+     * Get all bundles
+     * 
+     * @return an array of bundles
+     * @throws BundleException if the operation fails
+     */
+    public Bundle[] getBundles() throws BundleException
+    {
+        return this.m_activator.getBundles();
+    }
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/TurbineOSGiComponentService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,29 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you 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.
+-->
+<html>
+<head>
+<!-- head part is ignored -->
+</head>
+
+<body>
+OSGi based component service to provide access to OSGi bundles for Turbine 
applications.
+<br>
+<font size="-2">$Id: package.html 615328 2008-01-25 20:25:05Z tv $</font>
+</body>
+</html>

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/java/org/apache/turbine/services/osgi/package.html
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java
URL: 
http://svn.apache.org/viewvc/turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java?rev=811533&view=auto
==============================================================================
--- 
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java
 (added)
+++ 
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java
 Fri Sep  4 20:13:38 2009
@@ -0,0 +1,130 @@
+package org.apache.turbine.services.osgi;
+
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+
+import java.lang.reflect.Method;
+
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.turbine.services.ServiceManager;
+import org.apache.turbine.services.TurbineServices;
+import org.apache.turbine.test.BaseTestCase;
+import org.osgi.framework.Bundle;
+
+
+/**
+ * Simple test to make sure that the OSGiComponentService can be initialized.
+ *
+ * @author <a href="mailto:[email protected]";>Thomas Vandahl</a>
+ * @version $Id: TurbineOSGiComponentServiceTest.java 731294 2009-01-04 
16:39:38Z tv $
+ */
+public class TurbineOSGiComponentServiceTest
+        extends BaseTestCase
+{
+    private static final String PREFIX = "services." +
+            OSGiComponentService.SERVICE_NAME + '.';
+
+    /**
+     * Initialize the unit test.  The OSGiComponentService will be configured
+     * and initialized.
+
+     *
+     * @param name
+     */
+    public TurbineOSGiComponentServiceTest(String name)
+            throws Exception
+    {
+        super(name);
+        ServiceManager serviceManager = TurbineServices.getInstance();
+        serviceManager.setApplicationRoot(".");
+
+        Configuration cfg = new BaseConfiguration();
+
+        cfg.setProperty(PREFIX + "classname", 
TurbineOSGiComponentService.class.getName());
+        cfg.setProperty(PREFIX + "earlyInit", "true");
+
+        // install a bundle
+        cfg.setProperty(PREFIX + "installBundle.1",
+                
"file:src/test/org.apache.turbine.osgi.test.HelloWorldBundle_1.0.0.dev.jar");
+
+        serviceManager.setConfiguration(cfg);
+
+        try
+        {
+            serviceManager.init();
+        }
+        catch(Exception e)
+        {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
+    /**
+     * Use the service to get an instance of the TestComponent.  The test() 
method will be called to
+     * simply write a log message.  The component will then be released.
+     */
+    public void testGetAndUseTestComponent()
+    {
+        try
+        {
+            OSGiComponentService osgi = 
+                
(OSGiComponentService)TurbineServices.getInstance().getService(OSGiComponentService.SERVICE_NAME);
+            
+            assertNotNull("Service should be present", osgi);
+            
+            Bundle[] bundles = osgi.getBundles();
+            
+            String myBundleName = 
"org.apache.turbine.osgi.test.HelloWorldBundle";
+            boolean bundleFound = false;
+            int bundleState = 0;
+            
+            for (int i = 0; i < bundles.length; i++)
+            {
+                System.out.println("Found bundle " + 
bundles[i].getSymbolicName() + " state " + bundles[i].getState());
+                
+                if (myBundleName.equals(bundles[i].getSymbolicName()))
+                {
+                    bundleFound = true;
+                    bundleState = bundles[i].getState();
+                }
+            }
+            
+            assertTrue("Bundle should be installed", bundleFound);
+            assertEquals("Bundle should have been activated", bundleState, 
Bundle.ACTIVE);
+            
+            Object service = 
TurbineServices.getInstance().getService("org.apache.turbine.osgi.test.helloworldbundle.HelloWorldService");
+            
+            assertNotNull("OSGI-implemented HelloWorldService should be 
present", service);
+            
+            Method sayHello = service.getClass().getMethod("sayHello", 
String.class);
+            String response = (String)sayHello.invoke(service, "Joe");
+            
+            assertEquals("Service should return 'Hello Joe'", response, "Hello 
Joe");
+        }
+        catch(Exception e)
+        {
+            e.printStackTrace();
+            fail();
+        }
+    }
+}

Propchange: 
turbine/core/trunk/proposals/tv/osgi/service/test/org/apache/turbine/services/osgi/TurbineOSGiComponentServiceTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to