Author: bdelacretaz
Date: Thu Sep 11 06:39:08 2008
New Revision: 694251
URL: http://svn.apache.org/viewvc?rev=694251&view=rev
Log:
SLING-654 - RunMode service added
Added:
incubator/sling/trunk/extensions/runmode/ (with props)
incubator/sling/trunk/extensions/runmode/pom.xml (with props)
incubator/sling/trunk/extensions/runmode/src/
incubator/sling/trunk/extensions/runmode/src/main/
incubator/sling/trunk/extensions/runmode/src/main/java/
incubator/sling/trunk/extensions/runmode/src/main/java/org/
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
(with props)
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
(with props)
incubator/sling/trunk/extensions/runmode/src/test/
incubator/sling/trunk/extensions/runmode/src/test/java/
incubator/sling/trunk/extensions/runmode/src/test/java/org/
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
(with props)
Propchange: incubator/sling/trunk/extensions/runmode/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Sep 11 06:39:08 2008
@@ -0,0 +1,12 @@
+target
+bin
+derby.log
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+
Added: incubator/sling/trunk/extensions/runmode/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/runmode/pom.xml?rev=694251&view=auto
==============================================================================
--- incubator/sling/trunk/extensions/runmode/pom.xml (added)
+++ incubator/sling/trunk/extensions/runmode/pom.xml Thu Sep 11 06:39:08 2008
@@ -0,0 +1,87 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>sling</artifactId>
+ <version>4-incubator-SNAPSHOT</version>
+ <relativePath> ../../sling/parent/</relativePath>
+ </parent>
+
+ <artifactId>org.apache.sling.runmode</artifactId>
+ <version>2.0.3-incubator-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <name>Sling - JCR RunMode service</name>
+ <description>
+ Used to enable/disable sets of components
+ based on a list of "run mode" strings.
+ </description>
+
+ <scm>
+ <connection>
scm:svn:http://svn.apache.org/repos/asf/incubator/sling/whiteboard/runmode</connection>
+ <developerConnection>
scm:svn:https://svn.apache.org/repos/asf/incubator/sling/whiteboard/runmode</developerConnection>
+ <url> http://svn.apache.org/viewvc/incubator/sling/whiteboard/runmode</url>
+ </scm>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>
+ org.apache.sling.runmode,
+ !org.apache.sling.runmode.impl
+ </Export-Package>
+ <Private-Package>org.apache.sling.runmode.impl.*</Private-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ </dependencies>
+</project>
Propchange: incubator/sling/trunk/extensions/runmode/pom.xml
------------------------------------------------------------------------------
svn:eol-style = native
Added:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java?rev=694251&view=auto
==============================================================================
---
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
(added)
+++
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
Thu Sep 11 06:39:08 2008
@@ -0,0 +1,45 @@
+/*
+ * 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.sling.runmode;
+
+/** Define "run modes" for Sling, and allow components to find
+ * out if they're active according to the current set of run modes.
+ *
+ * A "run mode" is simply a string like "author", "dmz", "development",...
+ * The service does not validate their values.
+ *
+ */
+public interface RunMode {
+ /** Suggested name for the System property used to set run modes.
+ * If that's used, the service should accept a comma-separated
+ * list of values, each defining one run mode.
+ */
+ String RUN_MODES_SYSTEM_PROPERTY = "sling.run.modes";
+
+ /** Wildcard for run modes, means "accept all modes" */
+ String RUN_MODE_WILDCARD = "*";
+
+ /** True if at least one of the given runModes is contained
+ * in the set of current active run modes.
+ */
+ boolean isActive(String [] runModes);
+
+ /** Return the current set of active run modes */
+ String [] getCurrentRunModes();
+}
Propchange:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/RunMode.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java?rev=694251&view=auto
==============================================================================
---
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
(added)
+++
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
Thu Sep 11 06:39:08 2008
@@ -0,0 +1,110 @@
+/*
+ * 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.sling.runmode.impl;
+
+import org.apache.sling.runmode.RunMode;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** RunMode service that sets the current run modes from a
+ * BundleContext property when activated.
+ *
+ * BundleContext properties can be set from system properties,
+ * that's one way of defining the current set of run modes.
+ *
+ * @scr.component
+ * label="Sling RunMode service"
+ * description="RunMode service configured from a BundleContext property"
+ * immediate="true"
+ *
+ * @scr.service
+ */
+public class RunModeImpl implements RunMode {
+
+ private String [] runModes;
+ protected final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ public RunModeImpl() {
+ }
+
+ RunModeImpl(String str) {
+ runModes = parseRunModes(str);
+ }
+
+ protected void activate(ComponentContext context) {
+ runModes =
parseRunModes(context.getBundleContext().getProperty(RUN_MODES_SYSTEM_PROPERTY));
+ log.info("{}, set from BundleContext property '{}'", toString(),
RUN_MODES_SYSTEM_PROPERTY);
+ }
+
+ @Override
+ public String toString() {
+ final StringBuffer sb = new StringBuffer();
+ sb.append(getClass().getSimpleName());
+ sb.append(": ");
+ for(int i=0; i < runModes.length; i++) {
+ if(i > 0) {
+ sb.append(", ");
+ }
+ sb.append(runModes[i]);
+ }
+ return sb.toString();
+ }
+
+ public String[] getCurrentRunModes() {
+ final String [] result = new String[runModes.length];
+ System.arraycopy(runModes, 0, result, 0, runModes.length);
+ return result;
+ }
+
+ public boolean isActive(String[] runModesToCheck) {
+ boolean result = false;
+
+ mainLoop:
+ for(String m : runModesToCheck) {
+ m = m.trim();
+ if(m.equals(RUN_MODE_WILDCARD)) {
+ result = true;
+ break mainLoop;
+ }
+ for(int i = 0; i < runModes.length; i++) {
+ if(m.equals(runModes[i])) {
+ result = true;
+ break mainLoop;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /** Parse str as a comma-separated list of run modes */
+ private String [] parseRunModes(String str) {
+ if(str == null || str.trim().length() == 0) {
+ return new String[0];
+ }
+
+ final String [] result = str.split(",");
+ for(int i=0; i < result.length; i++) {
+ result[i] = result[i].trim();
+ }
+ return result;
+ }
+
+}
Propchange:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/extensions/runmode/src/main/java/org/apache/sling/runmode/impl/RunModeImpl.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added:
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java?rev=694251&view=auto
==============================================================================
---
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
(added)
+++
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
Thu Sep 11 06:39:08 2008
@@ -0,0 +1,42 @@
+package org.apache.sling.runmode.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+
+import org.apache.sling.runmode.RunMode;
+
+public class RunModeImplTest {
+
+ private void assertParse(String str, String [] expected) {
+ final RunMode rm = new RunModeImpl(str);
+ final String [] actual = rm.getCurrentRunModes();
+ assertArrayEquals("Parsed runModes match for '" + str + "'", expected,
actual);
+ }
+
+ @org.junit.Test public void testParseRunModes() {
+ assertParse(null, new String[0]);
+ assertParse("", new String[0]);
+ assertParse(" foo \t", new String[] { "foo" });
+ assertParse(" foo \t, bar\n", new String[] { "foo", "bar" });
+ }
+
+ @org.junit.Test public void testToString() {
+ final RunMode rm = new RunModeImpl("\nfoo, bar\t");
+ assertEquals("RunModeImpl: foo, bar", rm.toString());
+ }
+
+ @org.junit.Test public void testMatchesNotEmpty() {
+ final RunMode rm = new RunModeImpl("foo,bar");
+
+ assertTrue("single foo should be active", rm.isActive(new String[] {
"foo" }));
+ assertTrue("foo wiz should be active", rm.isActive(new String[] {
"foo", "wiz" }));
+ assertTrue("star wiz should be active", rm.isActive(new String[] {
"*", "wiz" }));
+ assertTrue("star should be active", rm.isActive(new String[] { "*" }));
+
+ assertFalse("wiz should be not active", rm.isActive(new String[] {
"wiz" }));
+ assertFalse("wiz bah should be not active", rm.isActive(new String[] {
"wiz", "bah" }));
+ assertFalse("empty should be not active", rm.isActive(new String[0]));
+ }
+}
\ No newline at end of file
Propchange:
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/sling/trunk/extensions/runmode/src/test/java/org/apache/sling/runmode/impl/RunModeImplTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL