Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-2474 Possibility to add custom SPI and extend the data model
- Loading branch information
Showing
15 changed files
with
380 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
model/jpa/src/main/java/org/keycloak/connections/jpa/entityprovider/JpaEntityProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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.keycloak.connections.jpa.entityprovider; | ||
|
||
import java.util.List; | ||
|
||
import org.keycloak.provider.Provider; | ||
|
||
/** | ||
* @author <a href="mailto:erik.mulder@docdatapayments.com">Erik Mulder</a> | ||
* | ||
* A JPA Entity Provider can supply extra JPA entities that the Keycloak system should include in it's entity manager. The | ||
* entities should be provided as a list of Class objects. | ||
*/ | ||
public interface JpaEntityProvider extends Provider { | ||
|
||
/** | ||
* Return the entities that should be added to the entity manager. | ||
* | ||
* @return list of class objects | ||
*/ | ||
List<Class<?>> getEntities(); | ||
|
||
/** | ||
* Return the location of the Liquibase changelog that facilitates the extra JPA entities. | ||
* This should be a location that can be found on the same classpath as the entity classes. | ||
* | ||
* @return a changelog location or null if not needed | ||
*/ | ||
String getChangelogLocation(); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...a/src/main/java/org/keycloak/connections/jpa/entityprovider/JpaEntityProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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.keycloak.connections.jpa.entityprovider; | ||
|
||
import org.keycloak.provider.ProviderFactory; | ||
|
||
/** | ||
* @author <a href="mailto:erik.mulder@docdatapayments.com">Erik Mulder</a> | ||
* | ||
* Extended interface for a provider factory for JpaEntityProvider's. | ||
*/ | ||
public interface JpaEntityProviderFactory extends ProviderFactory<JpaEntityProvider> { | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
model/jpa/src/main/java/org/keycloak/connections/jpa/entityprovider/JpaEntitySpi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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.keycloak.connections.jpa.entityprovider; | ||
|
||
import org.keycloak.provider.Provider; | ||
import org.keycloak.provider.ProviderFactory; | ||
import org.keycloak.provider.Spi; | ||
|
||
/** | ||
* @author <a href="mailto:erik.mulder@docdatapayments.com">Erik Mulder</a> | ||
* | ||
* Spi that allows for adding extra JPA entity's to the Keycloak entity manager. | ||
*/ | ||
public class JpaEntitySpi implements Spi { | ||
|
||
@Override | ||
public boolean isInternal() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "jpa-entity-provider"; | ||
} | ||
|
||
@Override | ||
public Class<? extends Provider> getProviderClass() { | ||
return JpaEntityProvider.class; | ||
} | ||
|
||
@Override | ||
public Class<? extends ProviderFactory> getProviderFactoryClass() { | ||
return JpaEntityProviderFactory.class; | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
model/jpa/src/main/java/org/keycloak/connections/jpa/entityprovider/ProxyClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2016 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* 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.keycloak.connections.jpa.entityprovider; | ||
|
||
import java.net.URL; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author <a href="mailto:erik.mulder@docdatapayments.com">Erik Mulder</a> | ||
* | ||
* Classloader implementation to facilitate loading classes and resources from a collection of other classloaders. | ||
* Effectively it forms a proxy to one or more other classloaders. | ||
* | ||
* The way it works: | ||
* - Get all (unique) classloaders from all provided classes | ||
* - For each class or resource that is 'requested': | ||
* - First try all provided classloaders and if we have a match, return that | ||
* - If no match was found: proceed with 'normal' classloading in 'current classpath' scope | ||
* | ||
* In this particular context: only loadClass and getResource overrides are needed, since those | ||
* are the methods that a classloading and resource loading process will need. | ||
*/ | ||
public class ProxyClassLoader extends ClassLoader { | ||
|
||
private Set<ClassLoader> classloaders; | ||
|
||
public ProxyClassLoader(Collection<Class<?>> classes, ClassLoader parentClassLoader) { | ||
super(parentClassLoader); | ||
init(classes); | ||
} | ||
|
||
public ProxyClassLoader(Collection<Class<?>> classes) { | ||
init(classes); | ||
} | ||
|
||
private void init(Collection<Class<?>> classes) { | ||
classloaders = new HashSet<>(); | ||
for (Class<?> clazz : classes) { | ||
classloaders.add(clazz.getClassLoader()); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<?> loadClass(String name) throws ClassNotFoundException { | ||
for (ClassLoader classloader : classloaders) { | ||
try { | ||
return classloader.loadClass(name); | ||
} catch (ClassNotFoundException e) { | ||
// This particular class loader did not find the class. It's expected behavior that | ||
// this can happen, so we'll just ignore the exception and let the next one try. | ||
} | ||
} | ||
// We did not find the class in the proxy class loaders, so proceed with 'normal' behavior. | ||
return super.loadClass(name); | ||
} | ||
|
||
@Override | ||
public URL getResource(String name) { | ||
for (ClassLoader classloader : classloaders) { | ||
URL resource = classloader.getResource(name); | ||
if (resource != null) { | ||
return resource; | ||
} | ||
// Resource == null means not found, so let the next one try. | ||
} | ||
// We could not get the resource from the proxy class loaders, so proceed with 'normal' behavior. | ||
return super.getResource(name); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.