From b55c256f7dbe53164505fe9906faf54f7ab4d301 Mon Sep 17 00:00:00 2001 From: Scott England-Sullivan Date: Thu, 28 Mar 2013 13:34:43 -0500 Subject: [PATCH 3/4] KARAF-2257 Improvements: Updating examples. --- scr/command/pom.xml | 5 +- scr/examples/NOTICE | 37 +---- scr/examples/README | 21 +-- scr/examples/component-factory/NOTICE | 21 +++ scr/examples/component-factory/pom.xml | 88 ++++++++++ .../factories/GreeterServiceComponentFactory.java | 25 +++ .../component/GreeterServiceFactoryManager.java | 104 +++++++++++ .../impl/GreeterServiceComponentFactoryImpl.java | 151 ++++++++++++++++ .../src/main/resources/OSGI-INF/bundle.info | 10 + scr/examples/managed-service/NOTICE | 21 +++ scr/examples/managed-service/pom.xml | 84 +++++++++ .../managed/service/ManagedGreeterService.java | 25 +++ .../service/component/ManagedGreeterComponent.java | 94 ++++++++++ .../service/impl/ManagedGreeterServiceImpl.java | 182 ++++++++++++++++++++ .../src/main/resources/OSGI-INF/bundle.info | 10 + scr/examples/pom.xml | 92 ++++------ scr/examples/service/NOTICE | 21 +++ scr/examples/service/pom.xml | 87 ++++++++++ .../karaf/scr/examples/service/GreeterService.java | 28 +++ .../service/component/GreeterComponent.java | 65 +++++++ .../examples/service/impl/GreeterServiceImpl.java | 48 +++++ .../src/main/resources/OSGI-INF/bundle.info | 10 + 22 files changed, 1128 insertions(+), 101 deletions(-) create mode 100755 scr/examples/component-factory/NOTICE create mode 100644 scr/examples/component-factory/pom.xml create mode 100644 scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/GreeterServiceComponentFactory.java create mode 100644 scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/component/GreeterServiceFactoryManager.java create mode 100644 scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/impl/GreeterServiceComponentFactoryImpl.java create mode 100644 scr/examples/component-factory/src/main/resources/OSGI-INF/bundle.info create mode 100755 scr/examples/managed-service/NOTICE create mode 100644 scr/examples/managed-service/pom.xml create mode 100644 scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/ManagedGreeterService.java create mode 100644 scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/component/ManagedGreeterComponent.java create mode 100644 scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/impl/ManagedGreeterServiceImpl.java create mode 100644 scr/examples/managed-service/src/main/resources/OSGI-INF/bundle.info create mode 100644 scr/examples/service/NOTICE create mode 100644 scr/examples/service/pom.xml create mode 100644 scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/GreeterService.java create mode 100644 scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/component/GreeterComponent.java create mode 100644 scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/impl/GreeterServiceImpl.java create mode 100644 scr/examples/service/src/main/resources/OSGI-INF/bundle.info diff --git a/scr/command/pom.xml b/scr/command/pom.xml index b9a7ae6..2b9424a 100644 --- a/scr/command/pom.xml +++ b/scr/command/pom.xml @@ -1,5 +1,8 @@ - + + + 4.0.0 + + + org.apache.karaf.scr + org.apache.karaf.scr.examples + 3.0.0-SNAPSHOT + + + org.apache.karaf.scr.examples.component.factories + bundle + Apache Karaf :: SCR :: Examples :: Component Factories + Building SCR Components using the BND Annotation Libraries + + + + org.apache.felix + org.apache.felix.scr + + + biz.aQute + bndlib + + + org.osgi + org.osgi.core + + + org.osgi + org.osgi.compendium + + + org.slf4j + slf4j-api + + + + + + + org.apache.felix + maven-bundle-plugin + true + true + + + + ${project.artifactId} + + + !${project.artifactId}, + * + + + ${project.artifactId}.component, + ${project.artifactId}.impl + + * + + + + + + + \ No newline at end of file diff --git a/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/GreeterServiceComponentFactory.java b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/GreeterServiceComponentFactory.java new file mode 100644 index 0000000..a6600b0 --- /dev/null +++ b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/GreeterServiceComponentFactory.java @@ -0,0 +1,25 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.component.factories; + +/** + * The SCR Service interface we are going to publish + */ +public interface GreeterServiceComponentFactory { + void startGreeter(); + void stopGreeter(); +} diff --git a/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/component/GreeterServiceFactoryManager.java b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/component/GreeterServiceFactoryManager.java new file mode 100644 index 0000000..a7ed40f --- /dev/null +++ b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/component/GreeterServiceFactoryManager.java @@ -0,0 +1,104 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.component.factories.component; + +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Component; +import aQute.bnd.annotation.component.Deactivate; +import aQute.bnd.annotation.component.Reference; + +import java.util.Properties; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.karaf.scr.examples.component.factories.GreeterServiceComponentFactory; +import org.osgi.service.component.ComponentFactory; +import org.osgi.service.component.ComponentInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(name = GreeterServiceFactoryManager.COMPONENT_NAME) +public class GreeterServiceFactoryManager { + + public static final String COMPONENT_NAME = "GreeterServiceFactoryManager"; + + public static final String COMPONENT_LABEL = "Greeter Service Factory Manager"; + + private static final Logger LOG = LoggerFactory.getLogger(GreeterServiceFactoryManager.class); + + private ComponentFactory factory; + private ComponentInstance instance; + private GreeterServiceComponentFactory greeterService; + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** + * Called when all of the SCR Components required dependencies have been + * satisfied. + */ + @Activate + public void activate() { + LOG.info("Activating the " + COMPONENT_LABEL); + try { + lock.readLock().lock(); + if (factory != null) { + final Properties props = new Properties(); + props.setProperty("salutation", "Hello"); + props.setProperty("name", "Scott"); + instance = factory.newInstance(props); + greeterService = (GreeterServiceComponentFactory)instance.getInstance(); + greeterService.startGreeter(); + } + } finally { + lock.readLock().unlock(); + } + } + + /** + * Called when any of the SCR Components required dependencies become + * unsatisfied. + */ + @Deactivate + public void deactivate() { + LOG.info("Deactivating the " + COMPONENT_LABEL); + try { + lock.readLock().lock(); + greeterService.stopGreeter(); + instance.dispose(); + } finally { + lock.readLock().unlock(); + } + } + + @Reference(target = "(component.factory=greeter.factory.provider)") + public void setFactory(final ComponentFactory factory) { + try { + lock.writeLock().lock(); + this.factory = factory; + } finally { + lock.writeLock().unlock(); + } + } + + public void unsetFactory(final ComponentFactory factory) { + try { + lock.writeLock().lock(); + this.factory = null; + } finally { + lock.writeLock().unlock(); + } + } +} diff --git a/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/impl/GreeterServiceComponentFactoryImpl.java b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/impl/GreeterServiceComponentFactoryImpl.java new file mode 100644 index 0000000..00f8b6d --- /dev/null +++ b/scr/examples/component-factory/src/main/java/org/apache/karaf/scr/examples/component/factories/impl/GreeterServiceComponentFactoryImpl.java @@ -0,0 +1,151 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.component.factories.impl; + +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Component; +import aQute.bnd.annotation.component.Deactivate; + +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.karaf.scr.examples.component.factories.GreeterServiceComponentFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An implementation of the GreeterServiceComponentFactory interface. Component + * configuration includes setting the name attribute and setting the + * configuration policy to required. The default is optional and when the + * component attempts to activate it will throw a RuntimeException. + * + * @author sully6768 + */ +// The ConfigAdmin PID of our component +@Component(name = GreeterServiceComponentFactoryImpl.COMPONENT_NAME, +// The Factory ID of the Component Factory +factory = "greeter.factory.provider") +public class GreeterServiceComponentFactoryImpl implements GreeterServiceComponentFactory { + + public static final String COMPONENT_NAME = "GreeterServiceComponentFactory"; + public static final String COMPONENT_LABEL = "Greeter Service Component Factory"; + + private static final Logger LOG = LoggerFactory.getLogger(GreeterServiceComponentFactoryImpl.class); + + private ExecutorService executor = Executors.newCachedThreadPool(); + private Worker worker = new Worker(); + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** + * Called when all of the SCR Components required dependencies have been + * satisfied. + */ + @Activate + public void activate(final Map properties) { + LOG.info("Activating the " + COMPONENT_LABEL); + + // Just because our component has a policy of required doesn't help to + // ensure our properties are set. + // + // First check that salutation is set + if (properties.containsKey("salutation")) { + try { + lock.writeLock().lock(); + worker.setSalutation((String)properties.get("salutation")); + } finally { + lock.writeLock().unlock(); + } + } else { + throw new IllegalArgumentException("The salutation property may not be null or empty: " + properties.get("salutation")); + } + + // Now verify that name is set + if (properties.containsKey("name")) { + try { + lock.writeLock().lock(); + worker.setName((String)properties.get("name")); + } finally { + lock.writeLock().unlock(); + } + } else { + throw new IllegalArgumentException("The name property may not be null or empty: " + properties.get("name")); + } + } + + /** + * Called when any of the SCR Components required dependencies become + * unsatisfied. + */ + @Deactivate + public void deactivate() { + LOG.info("Deactivating the " + COMPONENT_LABEL); + } + + public void startGreeter() { + try { + lock.writeLock().lock(); + executor.execute(worker); + } finally { + lock.writeLock().unlock(); + } + } + + public void stopGreeter() { + try { + lock.writeLock().lock(); + if (!executor.isTerminated()) { + executor.shutdownNow(); + } + } finally { + lock.writeLock().unlock(); + } + } + + /** + * Thread worker that continuously prints a message. + */ + private class Worker implements Runnable { + + private String name; + private String salutation; + + public void run() { + boolean running = true; + int messageCount = 0; + while (running) { + try { + LOG.info("Message " + (++messageCount) + ": " + salutation + " " + name); + Thread.sleep(1000); + } catch (InterruptedException e) { + running = false; + LOG.info("Thread shutting down"); + } + } + } + + public void setName(String userName) { + this.name = userName; + } + + public void setSalutation(String salutation) { + this.salutation = salutation; + } + } +} diff --git a/scr/examples/component-factory/src/main/resources/OSGI-INF/bundle.info b/scr/examples/component-factory/src/main/resources/OSGI-INF/bundle.info new file mode 100644 index 0000000..655a64e --- /dev/null +++ b/scr/examples/component-factory/src/main/resources/OSGI-INF/bundle.info @@ -0,0 +1,10 @@ +\u001B[1mSYNOPSIS\u001B[0m + ${project.name} + + ${project.description} + + Maven URL: + \u001B[33mmvn:${project.groupId}/${project.artifactId}/${project.version}\u001B[0m + +\u001B[1mDESCRIPTION\u001B[0m + An SCR example project for Component Factories. \ No newline at end of file diff --git a/scr/examples/managed-service/NOTICE b/scr/examples/managed-service/NOTICE new file mode 100755 index 0000000..fb04a14 --- /dev/null +++ b/scr/examples/managed-service/NOTICE @@ -0,0 +1,21 @@ +Apache Karaf +Copyright 2011 The Apache Software Foundation + + +I. Included Software + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). +Licensed under the Apache License 2.0. + + +II. Used Software + +This product uses software developed at +The OSGi Alliance (http://www.osgi.org/). +Copyright (c) OSGi Alliance (2000, 2010). +Licensed under the Apache License 2.0. + + +III. License Summary +- Apache License 2.0 diff --git a/scr/examples/managed-service/pom.xml b/scr/examples/managed-service/pom.xml new file mode 100644 index 0000000..8dc1098 --- /dev/null +++ b/scr/examples/managed-service/pom.xml @@ -0,0 +1,84 @@ + + + + + + 4.0.0 + + + org.apache.karaf.scr + org.apache.karaf.scr.examples + 3.0.0-SNAPSHOT + + + org.apache.karaf.scr.examples.managed.service + bundle + Apache Karaf :: SCR :: Examples :: Managed Services + Building SCR Components using the BND Annotation Libraries + + + + biz.aQute + bndlib + + + org.osgi + org.osgi.core + + + org.osgi + org.osgi.compendium + + + org.slf4j + slf4j-api + + + + + + + org.apache.felix + maven-bundle-plugin + true + true + + + + ${project.artifactId} + + + !${project.artifactId}, + * + + + ${project.artifactId}.component, + ${project.artifactId}.impl + + * + + + + + + + \ No newline at end of file diff --git a/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/ManagedGreeterService.java b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/ManagedGreeterService.java new file mode 100644 index 0000000..dcd3f53 --- /dev/null +++ b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/ManagedGreeterService.java @@ -0,0 +1,25 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.managed.service; + +/** + * The SCR Service interface we are going to publish + */ +public interface ManagedGreeterService { + void startGreeter(); + void stopGreeter(); +} diff --git a/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/component/ManagedGreeterComponent.java b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/component/ManagedGreeterComponent.java new file mode 100644 index 0000000..a8a85e7 --- /dev/null +++ b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/component/ManagedGreeterComponent.java @@ -0,0 +1,94 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.managed.service.component; + +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Component; +import aQute.bnd.annotation.component.Deactivate; +import aQute.bnd.annotation.component.Reference; +import org.apache.karaf.scr.examples.managed.service.ManagedGreeterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(name = ManagedGreeterComponent.COMPONENT_NAME) +public class ManagedGreeterComponent { + + public static final String COMPONENT_NAME = "ManagedGreeterComponent"; + + public static final String COMPONENT_LABEL = "Managed Greeter Component"; + + private static final Logger LOG = LoggerFactory.getLogger(ManagedGreeterComponent.class); + + private ManagedGreeterService greeterService; + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** + * Called when all of the SCR Components required dependencies have been + * satisfied. + */ + @Activate + public void activate() { + LOG.info("Activating the " + COMPONENT_LABEL); + try { + lock.readLock().lock(); + if (greeterService != null) { + greeterService.startGreeter(); + } + } finally { + lock.readLock().unlock(); + } + } + + /** + * Called when any of the SCR Components required dependencies become + * unsatisfied. + */ + @Deactivate + public void deactivate() { + LOG.info("Deactivating the " + COMPONENT_LABEL); + try { + lock.readLock().lock(); + if (greeterService != null) { + greeterService.stopGreeter(); + } + } finally { + lock.readLock().unlock(); + } + } + + @Reference + public void setGreeterService(final ManagedGreeterService greeterService) { + try { + lock.writeLock().lock(); + this.greeterService = greeterService; + } finally { + lock.writeLock().unlock(); + } + } + + public void unsetGreeterService(final ManagedGreeterService greeterService) { + try { + lock.writeLock().lock(); + this.greeterService = null; + } finally { + lock.writeLock().unlock(); + } + } +} diff --git a/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/impl/ManagedGreeterServiceImpl.java b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/impl/ManagedGreeterServiceImpl.java new file mode 100644 index 0000000..52c1fa0 --- /dev/null +++ b/scr/examples/managed-service/src/main/java/org/apache/karaf/scr/examples/managed/service/impl/ManagedGreeterServiceImpl.java @@ -0,0 +1,182 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.managed.service.impl; + +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Component; +import aQute.bnd.annotation.component.ConfigurationPolicy; +import aQute.bnd.annotation.component.Deactivate; + +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.karaf.scr.examples.managed.service.ManagedGreeterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An implementation of the ManagedGreeterService interface. Component + * configuration includes setting the name attribute and setting the + * configuration policy to required. The default is optional and when the + * component attempts to activate it will throw a RuntimeException. + * + * @author sully6768 + */ +// The ConfigAdmin PID of our component +@Component(name = ManagedGreeterServiceImpl.COMPONENT_NAME, +// The policy that makes our configuration a required dependency +configurationPolicy = ConfigurationPolicy.require) +public class ManagedGreeterServiceImpl implements ManagedGreeterService { + + public static final String COMPONENT_NAME = "ManagedGreeterService"; + public static final String COMPONENT_LABEL = "Managed Greeeter Service"; + + private static final Logger LOG = LoggerFactory.getLogger(ManagedGreeterServiceImpl.class); + + private ExecutorService executor = Executors.newCachedThreadPool(); + private Worker worker = new Worker(); + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** + * Called when all of the SCR Components required dependencies have been + * satisfied. + */ + @Activate + public void activate(final Map properties) { + LOG.info("Activating the " + COMPONENT_LABEL); + + // Just because our component has a policy of required doesn't help to + // ensure our properties are set. + // + // First check that salutation is set + if (properties.containsKey("salutation")) { + try { + lock.writeLock().lock(); + worker.setSalutation((String)properties.get("salutation")); + } finally { + lock.writeLock().unlock(); + } + } else { + throw new IllegalArgumentException("The salutation property may not be null or empty: " + properties.get("salutation")); + } + + // Now verify that name is set + if (properties.containsKey("name")) { + try { + lock.writeLock().lock(); + worker.setName((String)properties.get("name")); + } finally { + lock.writeLock().unlock(); + } + } else { + throw new IllegalArgumentException("The name property may not be null or empty: " + properties.get("name")); + } + } + + /** + * Called when any of the SCR Components required dependencies become + * unsatisfied. + */ + @Deactivate + public void deactivate() { + LOG.info("Deactivating the " + COMPONENT_LABEL); + } + +// /** +// * Called when the configuration associated with this component has been +// * updated. +// */ +// @Modified +// public void modified(final Map properties) { +// LOG.info("Modifying the " + COMPONENT_LABEL); +// +// // This time we really only need to make sure if it changed it isn't +// // empty +// if (properties.containsKey("salutation") && !properties.get("salutation").equals("")) { +// try { +// lock.writeLock().lock(); +// worker.setSalutation((String)properties.get("salutation")); +// } finally { +// lock.writeLock().unlock(); +// } +// } +// +// // Same for name +// if (properties.containsKey("name") && !properties.get("name").equals("")) { +// try { +// lock.writeLock().lock(); +// worker.setName((String)properties.get("name")); +// } finally { +// lock.writeLock().unlock(); +// } +// } +// } + + public void startGreeter() { + try { + lock.writeLock().lock(); + executor.execute(worker); + } finally { + lock.writeLock().unlock(); + } + } + + public void stopGreeter() { + try { + lock.writeLock().lock(); + if (!executor.isTerminated()) { + executor.shutdownNow(); + } + } finally { + lock.writeLock().unlock(); + } + } + + /** + * Thread worker that continuously prints a message. + */ + private class Worker implements Runnable { + + private String name; + private String salutation; + + public void run() { + boolean running = true; + int messageCount = 0; + while (running) { + try { + LOG.info("Message " + (++messageCount) + ": " + salutation + " " + name); + Thread.sleep(1000); + } catch (InterruptedException e) { + running = false; + LOG.info("Thread shutting down"); + } + } + } + + public void setName(String userName) { + this.name = userName; + } + + public void setSalutation(String salutation) { + this.salutation = salutation; + } + } +} diff --git a/scr/examples/managed-service/src/main/resources/OSGI-INF/bundle.info b/scr/examples/managed-service/src/main/resources/OSGI-INF/bundle.info new file mode 100644 index 0000000..6d72846 --- /dev/null +++ b/scr/examples/managed-service/src/main/resources/OSGI-INF/bundle.info @@ -0,0 +1,10 @@ +\u001B[1mSYNOPSIS\u001B[0m + ${project.name} + + ${project.description} + + Maven URL: + \u001B[33mmvn:${project.groupId}/${project.artifactId}/${project.version}\u001B[0m + +\u001B[1mDESCRIPTION\u001B[0m + An SCR example project for Managed Services. \ No newline at end of file diff --git a/scr/examples/pom.xml b/scr/examples/pom.xml index f71d63d..8cbfbf1 100644 --- a/scr/examples/pom.xml +++ b/scr/examples/pom.xml @@ -1,5 +1,8 @@ - + + + + + + - + + service + managed-service + component-factory + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/scr/examples/service/NOTICE b/scr/examples/service/NOTICE new file mode 100644 index 0000000..23de8f8 --- /dev/null +++ b/scr/examples/service/NOTICE @@ -0,0 +1,21 @@ +Apache Felix Karaf +Copyright 2011 The Apache Software Foundation + + +I. Included Software + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). +Licensed under the Apache License 2.0. + + +II. Used Software + +This product uses software developed at +The OSGi Alliance (http://www.osgi.org/). +Copyright (c) OSGi Alliance (2000, 2010). +Licensed under the Apache License 2.0. + + +III. License Summary +- Apache License 2.0 diff --git a/scr/examples/service/pom.xml b/scr/examples/service/pom.xml new file mode 100644 index 0000000..dd56b49 --- /dev/null +++ b/scr/examples/service/pom.xml @@ -0,0 +1,87 @@ + + + + + + 4.0.0 + + + org.apache.karaf.scr + org.apache.karaf.scr.examples + 3.0.0-SNAPSHOT + + + org.apache.karaf.scr.examples.service + bundle + Apache Karaf :: SCR :: Examples :: Basic Service + Building SCR Components using the BND Annotation Libraries + + + + + biz.aQute + bndlib + + + + org.slf4j + slf4j-api + + + + + + + org.apache.felix + maven-bundle-plugin + true + true + + + + ${project.artifactId} + + + !${project.artifactId}, + * + + + ${project.artifactId}.component, + ${project.artifactId}.impl + + * + + + + + + + \ No newline at end of file diff --git a/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/GreeterService.java b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/GreeterService.java new file mode 100644 index 0000000..9328ea8 --- /dev/null +++ b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/GreeterService.java @@ -0,0 +1,28 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.service; + +/** + * The SCR Service interface we are going to publish + * + * @author sully6768 + */ +public interface GreeterService { + + void printGreetings(); + +} diff --git a/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/component/GreeterComponent.java b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/component/GreeterComponent.java new file mode 100644 index 0000000..e550847 --- /dev/null +++ b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/component/GreeterComponent.java @@ -0,0 +1,65 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.service.component; + +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Component; +import aQute.bnd.annotation.component.Deactivate; +import aQute.bnd.annotation.component.Reference; +import org.apache.karaf.scr.examples.service.GreeterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(name = GreeterComponent.COMPONENT_NAME) +public class GreeterComponent { + + public static final String COMPONENT_NAME = "GreeterComponent"; + + public static final String COMPONENT_LABEL = "GreeterService Component"; + + private static final Logger LOG = LoggerFactory.getLogger(GreeterComponent.class); + + private GreeterService greeterService; + + /** + * Called when all of the SCR Components required dependencies have been + * satisfied. + */ + @Activate + public void activate() { + LOG.info("Activating the " + COMPONENT_LABEL); + greeterService.printGreetings(); + } + + /** + * Called when any of the SCR Components required dependencies become + * unsatisfied. + */ + @Deactivate + public void deactivate() { + LOG.info("Deactivating the " + COMPONENT_LABEL); + } + + @Reference + public void setGreeterService(final GreeterService greeterService) { + this.greeterService = greeterService; + } + + public void unsetGreeterService(final GreeterService greeterService) { + this.greeterService = null; + } +} diff --git a/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/impl/GreeterServiceImpl.java b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/impl/GreeterServiceImpl.java new file mode 100644 index 0000000..7b1c5e9 --- /dev/null +++ b/scr/examples/service/src/main/java/org/apache/karaf/scr/examples/service/impl/GreeterServiceImpl.java @@ -0,0 +1,48 @@ +/* + * 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. + */ +package org.apache.karaf.scr.examples.service.impl; + +import aQute.bnd.annotation.component.Component; +import org.apache.karaf.scr.examples.service.GreeterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *

+ * The service implementation for for our {@link GreeterService}. + *

+ *

+ * With the {@link Component} annotation, any declared inteface on this class will + * automatically be exported as a service. The DS annotations are build time + * only though and do not inherit. + *

+ * + * @author sully6768 + */ +@Component +public class GreeterServiceImpl implements GreeterService { + + private static final Logger LOG = LoggerFactory.getLogger(GreeterServiceImpl.class); + + private String name = System.getProperty("user.name", "Scott ES"); + + private String salutation = "Hello"; + + public void printGreetings() { + LOG.info(salutation + " " + name); + } +} diff --git a/scr/examples/service/src/main/resources/OSGI-INF/bundle.info b/scr/examples/service/src/main/resources/OSGI-INF/bundle.info new file mode 100644 index 0000000..2d76175 --- /dev/null +++ b/scr/examples/service/src/main/resources/OSGI-INF/bundle.info @@ -0,0 +1,10 @@ +\u001B[1mSYNOPSIS\u001B[0m + ${project.name} + + ${project.description} + + Maven URL: + \u001B[33mmvn:${project.groupId}/${project.artifactId}/${project.version}\u001B[0m + +\u001B[1mDESCRIPTION\u001B[0m + An SCR example project for basic Services and Components. \ No newline at end of file -- 1.7.7.3