Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
Beginning of Keycloak Server fraction.
Browse files Browse the repository at this point in the history
Auto-config Datasources, Infinispan, and apply a keycloak-server.json
Still needs significant improvement, but doesn't fail the build.
  • Loading branch information
bobmcwhirter committed Dec 9, 2015
1 parent ab6af82 commit ebdc37f
Show file tree
Hide file tree
Showing 25 changed files with 951 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/** Used only for loading dependencies of org.wildfly.bootstrap:main from its own jar.
*
Expand All @@ -39,18 +40,24 @@ public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateL
final String path = "modules/" + identifier.getName().replace('.', '/') + "/" + identifier.getSlot() + "/module.xml";

ClassLoader cl = BootstrapClasspathModuleFinder.class.getClassLoader();
InputStream in = cl.getResourceAsStream(path);
URL url = cl.getResource(path);

if (in == null) {
if (url == null) {
return null;
}

//System.err.println( "BootstrapClasspathModuleFinder: " + identifier );

ModuleSpec moduleSpec = null;
InputStream in = null;
try {
final URL base = new URL( url, "./" );
in = url.openStream();
moduleSpec = ModuleXmlParserBridge.parseModuleXml(new ModuleXmlParserBridge.ResourceRootFactoryBridge() {
@Override
public ResourceLoader createResourceLoader(final String rootPath, final String loaderPath, final String loaderName) throws IOException {
return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
//return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
return NestedJarResourceLoader.loaderFor( base, rootPath, loaderPath, loaderName );
}
}, "/", in, path.toString(), delegateLoader, identifier);

Expand All @@ -62,7 +69,9 @@ public ResourceLoader createResourceLoader(final String rootPath, final String l
throw t;
} finally {
try {
in.close();
if ( in != null ) {
in.close();
}
} catch (IOException e) {
throw new ModuleLoadException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.jboss.modules.Environment;
import org.jboss.modules.ModuleFinder;
Expand All @@ -40,22 +42,31 @@ public ModuleSpec findModule(ModuleIdentifier identifier, ModuleLoader delegateL

try {
ClassLoader cl = Layout.getInstance().getBootstrapClassLoader();
InputStream in = cl.getResourceAsStream(path);
URL url = cl.getResource(path);
//InputStream in = cl.getResourceAsStream(path);

if (in == null && cl != ClasspathModuleFinder.class.getClassLoader()) {
in = ClasspathModuleFinder.class.getClassLoader().getResourceAsStream(path);
if (url == null && cl != ClasspathModuleFinder.class.getClassLoader()) {
url = ClasspathModuleFinder.class.getClassLoader().getResource(path);
}

if (in == null) {
if (url == null) {
return null;
}

//System.err.println( "ClasspathModuleFinder: " + identifier );

final URL base = new URL( url, "./" );

InputStream in = url.openStream();

ModuleSpec moduleSpec = null;
try {
moduleSpec = ModuleXmlParserBridge.parseModuleXml(new ModuleXmlParserBridge.ResourceRootFactoryBridge() {
@Override
public ResourceLoader createResourceLoader(final String rootPath, final String loaderPath, final String loaderName) throws IOException {
return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
//return Environment.getModuleResourceLoader(rootPath, loaderPath, loaderName);
//return new NestedJarResourceLoader( base );
return NestedJarResourceLoader.loaderFor( base, rootPath, loaderPath, loaderName );
}
}, "/", in, path.toString(), delegateLoader, identifier);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.wildfly.swarm.bootstrap.modules;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.jboss.modules.ResourceLoader;
import org.jboss.modules.ResourceLoaders;

/**
* @author Bob McWhirter
*/
public class NestedJarResourceLoader {

private static Map<String, File> exploded = new HashMap<>();

public static ResourceLoader loaderFor(URL base, String rootPath, String loaderPath, String loaderName) throws IOException {

//System.err.println( "** " + base + ", " + rootPath + ", " + loaderPath + ", " + loaderName );

if (base.toExternalForm().startsWith("jar:file:")) {
int endLoc = base.toExternalForm().indexOf(".jar!");
if (endLoc > 0) {
String jarPath = base.toExternalForm().substring(9, endLoc + 4);

File exp = exploded.get(jarPath);

if (exp == null) {
exp = File.createTempFile("module-jar", ".jar_d");
exp.delete();
exp.mkdirs();
exp.deleteOnExit();

JarFile jarFile = new JarFile(jarPath);

Enumeration<JarEntry> entries = jarFile.entries();

while (entries.hasMoreElements()) {
JarEntry each = entries.nextElement();

if (!each.isDirectory()) {
File out = new File(exp, each.getName());
out.getParentFile().mkdirs();
Files.copy(jarFile.getInputStream(each), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}

String relativeRoot = base.toExternalForm().substring(endLoc + 5);
File resourceRoot = new File( new File( exp, relativeRoot ), loaderPath );
/*
if ( resourceRoot.listFiles() != null ) {
System.err.println("@ " + resourceRoot + " --> " + Arrays.asList(resourceRoot.listFiles()));
}
*/
return ResourceLoaders.createFileResourceLoader(loaderName, resourceRoot );
}
}

return ResourceLoaders.createFileResourceLoader(loaderPath, new File(rootPath));
}

}
67 changes: 67 additions & 0 deletions keycloak-server/api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2015 Red Hat, Inc. and/or its affiliates.
~
~ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
-->
<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.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-keycloak-server-parent</artifactId>
<version>1.0.0.Alpha6-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-keycloak-server</artifactId>

<name>WildFly Swarm: Keycloak Server API</name>
<description>WildFly Swarm: Keycloak Server API</description>

<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-container</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-keycloak-server-modules</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Dependent APIs -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-jaxrs</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-infinispan</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-datasources</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-ee</artifactId>
<version>${project.version}</version>
</dependency>
<!--
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-security</artifactId>
<version>${project.version}</version>
</dependency>
-->

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* Copyright 2015 Red Hat, Inc, and individual contributors.
*
* Licensed 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.wildfly.swarm.keycloak.server;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;

import org.wildfly.swarm.config.datasources.DataSource;
import org.wildfly.swarm.config.infinispan.CacheContainer;
import org.wildfly.swarm.config.infinispan.cache_container.LocalCache;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.container.Fraction;
import org.wildfly.swarm.datasources.DatasourcesFraction;
import org.wildfly.swarm.ee.EEFraction;
import org.wildfly.swarm.infinispan.InfinispanFraction;

/**
* @author Bob McWhirter
*/
public class KeycloakServerFraction implements Fraction {

public KeycloakServerFraction() {
}

@Override
public void postInitialize(Container.PostInitContext initContext) {

if (System.getProperty("jboss.server.config.dir") == null) {
try {
Path dir = Files.createTempDirectory("swarm-keycloak-config");
System.setProperty("jboss.server.config.dir", dir.toString() );
Files.copy( getClass().getClassLoader().getResourceAsStream( "keycloak-server.json"),
dir.resolve( "keycloak-server.json" ),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}

InfinispanFraction infinispan = (InfinispanFraction) initContext.fraction("infinispan");

CacheContainer cache = infinispan.subresources().cacheContainer("keycloak");
if (cache == null) {
infinispan.cacheContainer("keycloak", (c) -> {
c.jndiName("infinispan/Keycloak");
c.localCache("realms", KeycloakServerFraction::configureCache);
c.localCache("users", KeycloakServerFraction::configureCache);
c.localCache("sessions", KeycloakServerFraction::configureCache);
c.localCache("loginFailures", KeycloakServerFraction::configureCache);
});
}

DatasourcesFraction datasources = (DatasourcesFraction) initContext.fraction("datasources");

if (datasources.subresources().dataSource("KeycloakDS") == null) {
if (datasources.subresources().jdbcDriver("h2") == null) {
datasources.jdbcDriver("h2", (driver) -> {
driver.driverModuleName("com.h2database.h2");
driver.moduleSlot("main");
driver.xaDatasourceClass("org.h2.jdbcx.JdbcDataSource");
});
}
datasources.dataSource("KeycloakDS", (ds) -> {
ds.jndiName("java:jboss/datasources/KeycloakDS");
ds.useJavaContext(true);
ds.connectionUrl("jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE");
ds.driverName("h2");
ds.userName("sa");
ds.password("sa");
});
}
}

private static void configureCache(LocalCache c) {
c.transactionComponent();
c.evictionComponent();
c.lockingComponent();
c.expirationComponent();
c.noneStore();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2015 Red Hat, Inc, and individual contributors.
*
* Licensed 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.wildfly.swarm.keycloak.server;

import org.wildfly.swarm.container.RuntimeModuleProvider;

/**
* @author Bob McWhirter
*/
public class KeycloakServerRuntimeModuleProvider implements RuntimeModuleProvider {
@Override
public String getModuleName() {
return "org.wildfly.swarm.keycloak.server";
}

@Override
public String getSlotName() {
return "runtime";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.wildfly.swarm.keycloak.server.KeycloakServerRuntimeModuleProvider
Loading

0 comments on commit ebdc37f

Please sign in to comment.