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/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