FELIX-3981 Create a sample project for demonstrating Felix JAAS main features

Adding a LoginModuleFactory based example which uses an embedded DataBase (H2) to perform user authentication

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1461132 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/examples/jaas/jdbc-h2/pom.xml b/examples/jaas/jdbc-h2/pom.xml
new file mode 100644
index 0000000..0444809
--- /dev/null
+++ b/examples/jaas/jdbc-h2/pom.xml
@@ -0,0 +1,124 @@
+<?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/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix-parent</artifactId>
+    <version>2.1</version>
+    <relativePath>../../../pom/pom.xml</relativePath>
+  </parent>
+
+  <groupId>org.apache.felix.example</groupId>
+  <artifactId>org.apache.felix.example.jaas.jdbc-h2</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+
+  <name>JAAS Example - H2 Db</name>
+
+  <prerequisites>
+    <maven>3.0.3</maven>
+  </prerequisites>
+
+  <build>
+   <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <version>2.3.5</version>
+        <extensions>true</extensions>
+        <configuration>
+          <obrRepository>NONE</obrRepository>
+          <instructions>
+            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+            <Bundle-Activator>org.apache.felix.example.jaas.jdbc.internal.H2Activator</Bundle-Activator>
+            <Embed-Dependency>
+              h2
+            </Embed-Dependency>
+            <Import-Package>
+              org.osgi.service.jdbc.*;
+              org.apache.lucene.*;
+              javax.transaction.*;resolution:=optional,
+              *
+            </Import-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-scr-plugin</artifactId>
+        <version>1.11.0</version>
+        <executions>
+          <execution>
+            <id>generate-scr-scrdescriptor</id>
+            <goals>
+              <goal>scr</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.rat</groupId>
+        <artifactId>apache-rat-plugin</artifactId>
+        <configuration>
+          <includes>
+            <include>src/**</include>
+          </includes>
+          <excludes>
+            <exclude>src/main/resources/*.csv</exclude>
+          </excludes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>4.2.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>4.2.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.scr.annotations</artifactId>
+      <version>1.9.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.4</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.h2database</groupId>
+      <artifactId>h2</artifactId>
+      <version>1.3.171</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <version>1.6.4</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/examples/jaas/jdbc-h2/src/main/java/org/apache/felix/example/jaas/jdbc/internal/H2Activator.java b/examples/jaas/jdbc-h2/src/main/java/org/apache/felix/example/jaas/jdbc/internal/H2Activator.java
new file mode 100644
index 0000000..321b16d
--- /dev/null
+++ b/examples/jaas/jdbc-h2/src/main/java/org/apache/felix/example/jaas/jdbc/internal/H2Activator.java
@@ -0,0 +1,96 @@
+/*
+ * 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.felix.example.jaas.jdbc.internal;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.servlet.Servlet;
+import javax.sql.DataSource;
+
+import org.h2.Driver;
+import org.h2.jdbcx.JdbcDataSource;
+import org.h2.server.web.WebServlet;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class H2Activator implements BundleActivator
+{
+    private Logger log = LoggerFactory.getLogger(getClass());
+    private JdbcDataSource ds;
+    private Connection connection;
+
+    @Override
+    public void start(BundleContext context) throws Exception
+    {
+        ds = new JdbcDataSource();
+        ds.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
+
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put("dataSourceName", "test");
+        context.registerService(DataSource.class.getName(), ds, props);
+
+        loadData(ds);
+
+        //Register the H2 console servlet
+        Dictionary<String, String> servletProps = new Hashtable<String, String>();
+        servletProps.put("alias", "/h2");
+        servletProps.put("init.webAllowOthers", "true");
+
+        context.registerService(Servlet.class.getName(), new WebServlet(), servletProps);
+    }
+
+    private void loadData(JdbcDataSource ds) throws SQLException
+    {
+        //Load the default data of user and roles
+        connection = ds.getConnection();
+        Statement stmt = connection.createStatement();
+        stmt.execute("CREATE TABLE USERS AS SELECT * FROM CSVREAD('classpath:users.csv',null,'lineComment=#')");
+        stmt.execute("CREATE TABLE ROLES AS SELECT * FROM CSVREAD('classpath:roles.csv',null,'lineComment=#')");
+        stmt.close();
+        log.info("Successfully imported default user and roles");
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception
+    {
+        if (connection != null)
+        {
+            Statement stat = connection.createStatement();
+            stat.execute("SHUTDOWN");
+            stat.close();
+
+            try
+            {
+                connection.close();
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+        }
+        Driver.unload();
+    }
+}
diff --git a/examples/jaas/jdbc-h2/src/main/resources/roles.csv b/examples/jaas/jdbc-h2/src/main/resources/roles.csv
new file mode 100644
index 0000000..b073c09
--- /dev/null
+++ b/examples/jaas/jdbc-h2/src/main/resources/roles.csv
@@ -0,0 +1,5 @@
+#Default role to user mapping
+ROLE,USERNAME
+ADMIN,foo
+ADMIN,admin
+USER,bob
\ No newline at end of file
diff --git a/examples/jaas/jdbc-h2/src/main/resources/users.csv b/examples/jaas/jdbc-h2/src/main/resources/users.csv
new file mode 100644
index 0000000..83f1676
--- /dev/null
+++ b/examples/jaas/jdbc-h2/src/main/resources/users.csv
@@ -0,0 +1,4 @@
+USERNAME,PASSWORD
+foo,bar
+bob,password
+admin,password
\ No newline at end of file