Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWITCHYARD-2474 Entity Manager MixIn #751

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions test/mixins/entitymanager/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Copyright 2014 Johnathan Ingram & Rainer Schamm
-
- 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.
-->
<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.switchyard.components</groupId>
<artifactId>switchyard-component-test-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>switchyard-component-test-mixin-entitymanager</artifactId>
<packaging>jar</packaging>
<name>SwitchYard: Entity Manager MixIn</name>
<description>Entity Manager MixIn classes</description>
<url>http://switchyard.org</url>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.experlog</groupId>
<artifactId>xapool</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-naming</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss.logmanager</groupId>
<artifactId>log4j-jboss-logmanager</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-test-mixin-cdi</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-test-mixin-naming</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-test-mixin-jca</artifactId>
</dependency>
<dependency>
<groupId>org.switchyard.components</groupId>
<artifactId>switchyard-component-bean</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>


<!-- external dependencies -->
<dependency>
<groupId>com.experlog</groupId>
<artifactId>xapool</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* CDIPersistenceExtension.java
*
* Copyright 2014 Johnathan Ingram ([email protected])
* All rights reserved.
*
* 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.switchyard.component.test.mixins.entitymanager;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AnnotatedConstructor;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author Johnathan Ingram ([email protected])
*/
public class CDIPersistenceExtension implements Extension {

private transient static final Logger log = LoggerFactory.getLogger(CDIPersistenceExtension.class);


// Ensure all javax.persistence.EntityManager fields with @PersistenceContext are annotated @Inject
public <T> void processJPAAnnotations(@Observes ProcessAnnotatedType<T> pat) {
final AnnotatedType<T> at = pat.getAnnotatedType();

// Override any javax.persistence.EntityManager fields @PersistenceContext annotations with @Inject
AnnotatedType<T> wrapped = new AnnotatedType<T>() {

@Override
public Class<T> getJavaClass() {
return at.getJavaClass();
}

@Override
public Set<AnnotatedConstructor<T>> getConstructors() {
return at.getConstructors();
}

@Override
public Set<AnnotatedMethod<? super T>> getMethods() {
return at.getMethods();
}

@Override
public Set<AnnotatedField<? super T>> getFields() {
Set<AnnotatedField<? super T>> result = new HashSet<AnnotatedField<? super T>>();
for (final AnnotatedField af : at.getFields()) {
// If there is a field with the @PersistenceContext of type javax.persistence.EntityManager,
// make sure the field has the @Inject
if (javax.persistence.EntityManager.class.equals(af.getJavaMember().getType())
&& af.isAnnotationPresent(PersistenceContext.class)) {
result.add(addAnnotation(af, Inject.getAnnotation()));
} else {
result.add(af);
}
}

return result;
}

@Override
public Type getBaseType() {
return at.getBaseType();
}

@Override
public Set<Type> getTypeClosure() {
return at.getTypeClosure();
}

@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
return at.getAnnotation(annotationType);
}

@Override
public Set<Annotation> getAnnotations() {
return at.getAnnotations();
}

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
return at.isAnnotationPresent(annotationType);
}
};

pat.setAnnotatedType(wrapped);
}


public static AnnotatedField addAnnotation(final AnnotatedField af, final Annotation... annotations) {

return new AnnotatedField() {
@Override
public Field getJavaMember() {
return af.getJavaMember();
}

@Override
public boolean isStatic() {
return af.isStatic();
}

@Override
public AnnotatedType getDeclaringType() {
return af.getDeclaringType();
}

@Override
public Type getBaseType() {
return af.getBaseType();
}

@Override
public Set<Type> getTypeClosure() {
return af.getTypeClosure();
}

@Override
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
for (Annotation a : annotations) {
if (a.annotationType().equals(annotationType)) {
return (T) a;
}
}
return af.getAnnotation(annotationType);
}

@Override
public Set<Annotation> getAnnotations() {
Set<Annotation> result = new HashSet<Annotation>(af.getAnnotations());
result.addAll(Arrays.asList(annotations));
return result;
}

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
for (Annotation a : annotations) {

if (a.annotationType().equals(annotationType)) {
return true;
}
}

return af.isAnnotationPresent(annotationType);
}
};
}

public static class Inject {

@javax.inject.Inject
public static Object field;

public static Annotation getAnnotation() {
try {
return Inject.class.getField("field").getAnnotation(javax.inject.Inject.class);
} catch (Exception ex) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* DataSourceDelegate.java
*
* Copyright 2014 Johnathan Ingram ([email protected])
*
* 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.switchyard.component.test.mixins.entitymanager;

import java.io.PrintWriter;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;

/**
*
* @author Johnathan Ingram ([email protected])
*/
public class DataSourceDelegate implements javax.sql.DataSource, Serializable {

private DataSource ds;

public DataSourceDelegate(DataSource ds) {
this.ds = ds;
}

@Override
public Connection getConnection() throws SQLException {
return ds.getConnection();
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return ds.getConnection(username, password);
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return ds.getLogWriter();
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {
ds.setLogWriter(out);
}

@Override
public void setLoginTimeout(int seconds) throws SQLException {
ds.setLoginTimeout(seconds);
}

@Override
public int getLoginTimeout() throws SQLException {
return ds.getLoginTimeout();
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return ds.getParentLogger();
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return ds.unwrap(iface);
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return ds.isWrapperFor(iface);
}
}
Loading