incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1467919 [2/2] - in /sling/trunk/contrib/extensions/muppet: ./ it/ it/src/ it/src/test/ it/src/test/java/ it/src/test/java/org/ it/src/test/java/org/apache/ it/src/test/java/org/apache/sling/ it/src/test/java/org/apache/sling/muppet/ it/src...
Date Mon, 15 Apr 2013 09:52:13 GMT
Added: sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/Activator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/Activator.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/Activator.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/Activator.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,47 @@
+/*
+ * 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 SF 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.muppet.rules.osgi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.rules.jmx.JmxBeansRuleBuilder;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+/** Register our {@link RuleBuilder} for OSGi */
+public class Activator implements BundleActivator {
+    private List<ServiceRegistration> regs;
+
+    public void start(BundleContext ctx) throws Exception {
+        regs = new ArrayList<ServiceRegistration>();
+        regs.add(ctx.registerService(RuleBuilder.class.getName(), new BundlesRuleBuilder(ctx),
null));
+        regs.add(ctx.registerService(RuleBuilder.class.getName(), new JmxBeansRuleBuilder(),
null));
+    }
+
+    public void stop(BundleContext ctx) throws Exception {
+        if(regs != null) {
+            for(ServiceRegistration reg : regs) {
+                reg.unregister();
+            }
+        }
+        regs = null;
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/BundlesRuleBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/BundlesRuleBuilder.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/BundlesRuleBuilder.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-rules/src/main/java/org/apache/sling/muppet/rules/osgi/BundlesRuleBuilder.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,113 @@
+/*
+ * 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 SF 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.muppet.rules.osgi;
+
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.api.SystemAttribute;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+/** RuleBuilder about OSGi bundles */
+public class BundlesRuleBuilder implements RuleBuilder {
+
+    public static final String NAMESPACE = "osgi";
+    public static final String BUNDLE_STATE_RULE = "bundle.state";
+    public static final String BUNDLE_NOT_FOUND = "BUNDLE_NOT_FOUND";
+    private final BundleContext bundleContext;
+    
+    static class BundleAttribute implements SystemAttribute {
+        private final SystemAttribute attr;
+        private final String name;
+        
+        BundleAttribute(String name, SystemAttribute attr) {
+            this.name = name;
+            this.attr = attr;
+        }
+        
+        @Override
+        public String toString() {
+            return name;
+        }
+        
+        @Override
+        public Object getValue() {
+            return attr.getValue();
+        }
+    }
+    
+    BundlesRuleBuilder(BundleContext ctx) {
+        bundleContext = ctx;
+    }
+    
+    private Bundle findBundle(String symbolicName) {
+        for(Bundle b : bundleContext.getBundles()) {
+            if(symbolicName.equals(b.getSymbolicName())) {
+                return b;
+            }
+        }
+        return null;
+    }
+    
+    private String bundleStateToString(int state) {
+        // TODO this must exist somewhere already...
+        if(state == Bundle.ACTIVE) {
+            return "active";
+        } else if(state == Bundle.RESOLVED) {
+            return "resolved";
+        } else if(state == Bundle.INSTALLED) {
+            return "installed";
+        } else if(state == Bundle.STOPPING) {
+            return "stopping";
+        } else if(state == Bundle.UNINSTALLED) {
+            return "uninstalled";
+        } else {
+            return String.valueOf(state);
+        }
+    }
+    
+    @Override
+    public Rule buildRule(String namespace, String ruleName, final String qualifier, String
expression) {
+        if(!NAMESPACE.equals(namespace)) {
+            return null;
+        }
+        
+        SystemAttribute attr = null;
+        
+        if(BUNDLE_STATE_RULE.equals(ruleName) && qualifier != null) {
+            // Get the state of a bundle
+            attr = new BundleAttribute(ruleName + ":" + qualifier, new SystemAttribute()
{
+                @Override
+                public Object getValue() {
+                    Bundle b = findBundle(qualifier);
+                    if(b == null) {
+                        return BUNDLE_NOT_FOUND;
+                    } else {
+                        return bundleStateToString(b.getState());
+                    }
+                }
+            });
+        }
+        
+        if(attr != null) {
+            return new Rule(attr, expression);
+        }
+        
+        return null;
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-rules/src/test/java/org/apache/sling/muppet/rules/impl/JmxBeansRuleBuilderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-rules/src/test/java/org/apache/sling/muppet/rules/impl/JmxBeansRuleBuilderTest.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-rules/src/test/java/org/apache/sling/muppet/rules/impl/JmxBeansRuleBuilderTest.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-rules/src/test/java/org/apache/sling/muppet/rules/impl/JmxBeansRuleBuilderTest.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,52 @@
+/*
+ * 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 SF 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.muppet.rules.impl;
+
+import org.apache.sling.muppet.api.EvaluationResult;
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.rules.jmx.JmxBeansRuleBuilder;
+import org.junit.Test;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+
+public class JmxBeansRuleBuilderTest {
+    private final RuleBuilder jmxRuleBuilder = new JmxBeansRuleBuilder();
+    
+    @Test
+    public void testBasicJvmBean() {
+        // Assuming this attribute is present in all JVMs that we use to run tests...
+        final Rule r = jmxRuleBuilder.buildRule("jmxbeans", "java.lang:type=ClassLoading",
"LoadedClassCount", "> 100");
+        assertNotNull("Expecting to get a jmxbean Rule", r);
+        assertEquals(EvaluationResult.Status.OK, r.execute());
+    }
+    
+    @Test
+    public void testHashSeparatorInBeanName() {
+        final Rule r = jmxRuleBuilder.buildRule("jmxbeans", "java.lang#type=ClassLoading",
"LoadedClassCount", "> 100");
+        assertNotNull("Expecting to get a jmxbean Rule", r);
+        assertEquals(EvaluationResult.Status.OK, r.execute());
+    }
+    
+    @Test
+    public void testNonExistentBean() {
+        final Rule r = jmxRuleBuilder.buildRule("jmxbeans", "java.lang:type=DoesNotExist",
"LoadedClassCount", "5");
+        assertNotNull("Expecting to get a jmxbean Rule", r);
+        assertEquals(EvaluationResult.Status.ERROR, r.execute());
+    }
+}

Propchange: sling/trunk/contrib/extensions/muppet/muppet-sling/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Apr 15 09:52:11 2013
@@ -0,0 +1,13 @@
+target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+
+
+

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml (added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/pom.xml Mon Apr 15 09:52:11 2013
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>15</version>
+        <relativePath>15</relativePath>
+    </parent>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.muppet.sling</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <name>Sling-specific Muppet Components</name>
+    <inceptionYear>2013</inceptionYear>
+    
+    <description>
+        Muppet servlet and rules for Sling
+    </description>
+
+    <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.muppet.sling.api
+                        </Export-Package>
+                        <Private-Package>
+                            org.apache.sling.muppet.sling.impl.*
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.6</source>
+                    <target>1.6</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.muppet.core</artifactId>
+            <version>${project.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.api</artifactId>
+            <version>2.0.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.jcr</groupId>
+            <artifactId>jcr</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.json</artifactId>
+            <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.6.2</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.6.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.1</version>
+            <scope>test</scope>
+        </dependency>
+     </dependencies>
+</project>

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/JsonResultRenderer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/JsonResultRenderer.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/JsonResultRenderer.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/JsonResultRenderer.java
Mon Apr 15 09:52:11 2013
@@ -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 SF 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.muppet.sling.api;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import org.apache.sling.muppet.api.EvaluationResult;
+
+/** Renders a List of EvaluationResult in JSON */
+public interface JsonResultRenderer {
+    void render(List<EvaluationResult> results, Writer output) throws IOException;
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/RulesResourceParser.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/RulesResourceParser.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/RulesResourceParser.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/api/RulesResourceParser.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,33 @@
+/*
+ * 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 SF 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.muppet.sling.api;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.muppet.api.Rule;
+
+/** Parses a Sling Resource into Muppet Rules */
+public interface RulesResourceParser {
+    String NAMESPACE = "namespace";
+    String RULE_NAME = "ruleName";
+    String QUALIFIER = "qualifier";
+    String EXPRESSION = "expression";
+    
+    List<Rule> parseResource(Resource r);
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/JsonResultRendererImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/JsonResultRendererImpl.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/JsonResultRendererImpl.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/JsonResultRendererImpl.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,55 @@
+/*
+ * 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 SF 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.muppet.sling.impl;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.muppet.api.EvaluationResult;
+import org.apache.sling.muppet.sling.api.JsonResultRenderer;
+
+/** Renders a List of EvaluationResult in JSON. See unit tests
+ *  for details.
+ */
+@Component
+@Service(value=JsonResultRenderer.class)
+public class JsonResultRendererImpl implements JsonResultRenderer {
+    
+    @Override
+    public void render(List<EvaluationResult> results, Writer output) throws IOException
{
+        // TODO not JSON yet...trouble with animalsniffer and JSON dependency
+        for(EvaluationResult r : results) {
+            output.write(r.getStatus().toString());
+            output.write(" ");
+            output.write(r.getRule().toString());
+            output.write("\n");
+        }
+        /*
+        final JSONWriter w = new JSONWriter(output);
+        try {
+            w.object();
+            w.endObject();
+        } catch (JSONException e) {
+            throw new IOException(e.getClass().getSimpleName(), e);
+        }
+        */
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/MuppetSlingServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/MuppetSlingServlet.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/MuppetSlingServlet.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/MuppetSlingServlet.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,66 @@
+/*
+ * 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 SF 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.muppet.sling.impl;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.sling.muppet.api.MuppetFacade;
+import org.apache.sling.muppet.api.RulesEngine;
+import org.apache.sling.muppet.sling.api.JsonResultRenderer;
+import org.apache.sling.muppet.sling.api.RulesResourceParser;
+
+/** Sling Servlet that renders a Resource that contains Muppet rules definitions,
+ *  after evaluating the rules.
+ *  {@link RulesResourceParser} defines the resource format, and {@link JsonResultRenderer}
+ *  defines the output format. 
+ */
+@SuppressWarnings("serial")
+@SlingServlet(extensions="json",resourceTypes="sling/muppet.rules",methods="GET",selectors="muppet")
+public class MuppetSlingServlet extends SlingSafeMethodsServlet {
+
+    @Reference
+    private MuppetFacade muppet;
+    
+    @Reference
+    private RulesResourceParser parser;
+    
+    @Reference
+    private JsonResultRenderer renderer;
+    
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

+            throws ServletException,IOException {
+        
+        // TODO restrict execution to admin?
+        
+        // TODO we could cache the engine + rules, not sure if it's worth it...
+        final RulesEngine engine = muppet.getNewRulesEngine();
+        engine.addRules(parser.parseResource(request.getResource()));
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+        renderer.render(engine.evaluateRules(), response.getWriter());
+        response.getWriter().flush();
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/RulesResourceParserImpl.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,71 @@
+/*
+ * 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 SF 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.muppet.sling.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.muppet.api.MuppetFacade;
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.sling.api.RulesResourceParser;
+
+/** Parses a Resource into a list of Rule. See unit tests for details */
+@Component
+@Service(value=RulesResourceParser.class)
+public class RulesResourceParserImpl implements RulesResourceParser {
+    
+    @Reference
+    private MuppetFacade muppet;
+    
+    @Override
+    public List<Rule> parseResource(Resource r) {
+        final List<Rule> result = new ArrayList<Rule>();
+        recursivelyParseResource(result, r);
+        return result;
+    }
+    
+    private void recursivelyParseResource(List<Rule> list, Resource r) {
+        final ValueMap props = r.adaptTo(ValueMap.class);
+        if(props.containsKey(NAMESPACE) && props.containsKey(RULE_NAME)) {
+            for(RuleBuilder b : muppet.getRuleBuilders()) {
+                final Rule rule = b.buildRule(
+                    props.get(NAMESPACE, String.class), 
+                    props.get(RULE_NAME, String.class), 
+                    props.get(QUALIFIER, String.class), 
+                    props.get(EXPRESSION, String.class)
+                );
+                if(rule != null) {
+                    list.add(rule);
+                }
+            }
+        }
+        
+        final Iterator<Resource> it = r.getResourceResolver().listChildren(r);
+        while(it.hasNext()) {
+            recursivelyParseResource(list, it.next());
+        }
+        
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/rules/LoginRuleBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/rules/LoginRuleBuilder.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/rules/LoginRuleBuilder.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/main/java/org/apache/sling/muppet/sling/impl/rules/LoginRuleBuilder.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,98 @@
+/*
+ * 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 SF 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.muppet.sling.impl.rules;
+
+import javax.jcr.Credentials;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.api.SystemAttribute;
+
+/** Creates {@link Rule} to check if specific credentials allow for
+ *  logging in to a {@link SlingRepository}. Can be used to verify 
+ *  that default passwords have been disabled on production systems.
+ *  Checking for failed logins is the only realistic use case, as
+ *  by the credentials will be exposed in plain text in the repository,
+ *  which is only ok for default demo passwords of course.   
+ */
+@Component
+@Service(value=RuleBuilder.class)
+public class LoginRuleBuilder implements RuleBuilder {
+
+    public static final String NAMESPACE = "sling";
+    public static final String RULE_NAME = "login";
+    
+    @Reference
+    private SlingRepository repository;
+    
+    private class LoginResultSystemAttribute implements SystemAttribute {
+
+        private final String username;
+        private final String password;
+        
+        LoginResultSystemAttribute(String username, String password) {
+            this.username= username;
+            this.password = password;
+        }
+        
+        @Override
+        public Object getValue() {
+            String result = "???";
+            final Credentials creds = new SimpleCredentials(username, password.toCharArray());
+            Session s = null;
+            try {
+                s = repository.login(creds);
+                result = "LOGIN_OK";
+            } catch(RepositoryException rex) {
+                result = "LOGIN_FAILED";
+            } finally {
+                if(s != null) {
+                    s.logout();
+                }
+            }
+            return result;
+        }
+        
+        @Override
+        public String toString() {
+            return "Attempt to login as user " + username;
+        }
+    }
+    
+    @Override
+    public Rule buildRule(String namespace, String ruleName, String qualifier, String expression)
{
+        if(!NAMESPACE.equals(namespace) || !RULE_NAME.equals(ruleName) || qualifier == null)
{
+            return null;
+        }
+        
+        // Qualifier must be username#password
+        final String [] creds = qualifier.split("#");
+        if(creds.length != 2) {
+            return null;
+        }
+        
+        return new Rule(new LoginResultSystemAttribute(creds[0], creds[1]), expression);
+    }
+}
\ No newline at end of file

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResolver.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResolver.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResolver.java
Mon Apr 15 09:52:11 2013
@@ -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 SF 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.muppet.sling;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+
+class MockResolver implements ResourceResolver {
+
+    private final List<MockResource> resources = new ArrayList<MockResource>();
+
+    void addResource(MockResource r) {
+        resources.add(r);
+    }
+    
+    @Override
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> arg0) {
+        return null;
+    }
+
+    @Override
+    public Iterator<Resource> findResources(String arg0, String arg1) {
+        return null;
+    }
+
+    @Override
+    public Resource getResource(Resource arg0, String arg1) {
+        return null;
+    }
+
+    @Override
+    public Resource getResource(String arg0) {
+        return null;
+    }
+
+    @Override
+    public String[] getSearchPath() {
+        return null;
+    }
+
+    @Override
+    public Iterator<Resource> listChildren(Resource r) {
+        final List<Resource> kids = new ArrayList<Resource>();
+        for(Resource kid : resources) {
+            if(kid.getPath().startsWith(r.getPath()) && kid.getPath().length() >
r.getPath().length()) {
+                kids.add(kid);
+            }
+        }
+        return kids.iterator();
+    }
+
+    @Override
+    public String map(HttpServletRequest arg0, String arg1) {
+        return null;
+    }
+
+    @Override
+    public String map(String arg0) {
+        return null;
+    }
+
+    @Override
+    public Iterator<Map<String, Object>> queryResources(String arg0, String arg1)
{
+        return null;
+    }
+
+    @Override
+    public Resource resolve(HttpServletRequest arg0, String arg1) {
+        return null;
+    }
+
+    @Override
+    @Deprecated
+    public Resource resolve(HttpServletRequest arg0) {
+        return null;
+    }
+
+    @Override
+    public Resource resolve(String arg0) {
+        return null;
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/MockResource.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,86 @@
+package org.apache.sling.muppet.sling;
+
+import java.util.HashMap;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import static org.apache.sling.muppet.sling.api.RulesResourceParser.NAMESPACE;
+import static org.apache.sling.muppet.sling.api.RulesResourceParser.RULE_NAME;
+import static org.apache.sling.muppet.sling.api.RulesResourceParser.QUALIFIER;
+import static org.apache.sling.muppet.sling.api.RulesResourceParser.EXPRESSION;
+
+class MockResource implements Resource {
+    private final ResourceResolver resolver;
+    private final ValueMap valueMap;
+    private final String path;
+    
+    @SuppressWarnings("serial")
+    static class PropertiesMap extends HashMap<String, Object> implements ValueMap
{
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T get(String key, Class<T> type) {
+            // we only need Strings in our tests
+            final Object value = get(key);
+            return value == null ? null : (T)value.toString();
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T get(String key, T defaultValue) {
+            final Object value = get(key);
+            if(value == null) {
+                return defaultValue;
+            }
+            return (T)value;
+        }
+        
+    };
+    
+    MockResource(MockResolver resolver, String path, String namespace, String ruleName, String
qualifier, String expression) {
+        this.resolver = resolver;
+        this.path = path;
+        this.valueMap = new PropertiesMap();
+        valueMap.put(NAMESPACE, namespace);
+        valueMap.put(RULE_NAME, ruleName);
+        valueMap.put(QUALIFIER, qualifier);
+        valueMap.put(EXPRESSION, expression);
+        resolver.addResource(this);
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Override
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> target) {
+        if(target == ValueMap.class) {
+            return (AdapterType)valueMap;
+        }
+        return null;
+    }
+
+    @Override
+    public String getPath() {
+        return path;
+    }
+
+    @Override
+    public ResourceMetadata getResourceMetadata() {
+        return null;
+    }
+
+    @Override
+    public ResourceResolver getResourceResolver() {
+        return resolver;
+    }
+
+    @Override
+    public String getResourceSuperType() {
+        return null;
+    }
+
+    @Override
+    public String getResourceType() {
+        return null;
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
(added)
+++ sling/trunk/contrib/extensions/muppet/muppet-sling/src/test/java/org/apache/sling/muppet/sling/RulesResourceParserTest.java
Mon Apr 15 09:52:11 2013
@@ -0,0 +1,116 @@
+/*
+ * 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 SF 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.muppet.sling;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.muppet.api.MuppetFacade;
+import org.apache.sling.muppet.api.Rule;
+import org.apache.sling.muppet.api.RuleBuilder;
+import org.apache.sling.muppet.api.RulesEngine;
+import org.apache.sling.muppet.api.SystemAttribute;
+import org.apache.sling.muppet.sling.impl.RulesResourceParserImpl;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RulesResourceParserTest {
+    private final RulesResourceParserImpl parser = new RulesResourceParserImpl();
+    private MockResolver resolver;
+    private final List<RuleBuilder> builders = new ArrayList<RuleBuilder>();
+    
+    private final MuppetFacade facade = new MuppetFacade() {
+        public RulesEngine getNewRulesEngine() { return null; }
+        public List<Rule> parseSimpleTextRules(Reader textRules) throws IOException
{ return null; }
+        public List<RuleBuilder> getRuleBuilders() { return builders; }
+    };
+    
+    @Before
+    public void setup() throws Exception {
+        resolver = new MockResolver();
+        
+        builders.add(new RuleBuilder() {
+            @Override
+            public Rule buildRule(final String namespace, final String ruleName, final String
qualifier, final String expression) {
+                if("test".equals(namespace)) {
+                    final SystemAttribute a = new SystemAttribute() {
+                        @Override
+                        public String toString() {
+                            return namespace + ":" + ruleName + ":" + qualifier;
+                        }
+                        @Override
+                        public Object getValue() {
+                            return toString();
+                        }
+                    };
+                    return new Rule(a, expression);
+                }
+                return null;
+            }
+        });
+        
+        final Field f = parser.getClass().getDeclaredField("muppet");
+        f.setAccessible(true);
+        f.set(parser, facade);
+    }
+    
+    @Test
+    public void testEmptyResource() {
+        final Resource r = new MockResource(resolver, "/", null, null, null, null);
+        assertEquals(0, parser.parseResource(r).size());
+    }
+    
+    @Test
+    public void testSingleResource() {
+        final Resource r = new MockResource(resolver, "/", "test", "constant", "5", ">
3");
+        final List<Rule> rules = parser.parseResource(r); 
+        assertEquals(1, rules.size());
+        assertEquals("Rule: test:constant:5 > 3", rules.get(0).toString());
+    }
+    
+    @Test
+    public void testResourceTree() {
+        final Resource root = new MockResource(resolver, "/foo", "test", "constant", "5",
"> 3");
+        new MockResource(resolver, "/foo/1", "test", "constant", "12", "A");
+        new MockResource(resolver, "/foo/2", "test", "constant", "12", "B");
+        new MockResource(resolver, "/foo/3", "SHOULD_BE_IGNORED", "constant", "12", "A");
+        new MockResource(resolver, "/foo/4", "null", "ignored as well", "12", "A");
+        new MockResource(resolver, "/foo/some/path/to/me", "test", "deep", "43", "C");
+        
+        final List<Rule> rules = parser.parseResource(root); 
+        assertEquals(4, rules.size());
+
+        final String [] expect = {
+            "Rule: test:constant:5 > 3",
+            "Rule: test:constant:12 A",
+            "Rule: test:constant:12 B",
+            "Rule: test:deep:43 C"
+        };
+        final String allText = rules.toString();
+        for(String resText : expect) {
+            assertTrue("Expecting rules list (" + allText + ") to contain " + resText, allText.indexOf(resText)
>= 0);
+        }
+    }
+}

Added: sling/trunk/contrib/extensions/muppet/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/muppet/pom.xml?rev=1467919&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/muppet/pom.xml (added)
+++ sling/trunk/contrib/extensions/muppet/pom.xml Mon Apr 15 09:52:11 2013
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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</groupId>
+        <artifactId>apache</artifactId>
+        <version>10</version>
+        <relativePath />
+    </parent>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>org.apache.sling.muppet.reactor</artifactId>
+    <packaging>pom</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <name>Muppet Reactor POM</name>
+    <inceptionYear>2011</inceptionYear>
+
+    <prerequisites>
+        <maven>3.0.4</maven>
+    </prerequisites>
+    
+    <modules>
+        <module>muppet-core</module>
+        <module>muppet-rules</module>
+        <module>muppet-sling</module>
+        <module>it</module>
+    </modules>
+</project>



Mime
View raw message