Skip to content

Commit

Permalink
registering hibernate entities from plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascs committed Feb 28, 2010
1 parent ed1f69d commit 1ba5759
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 16 deletions.
@@ -0,0 +1,47 @@
package br.com.caelum.calopsita.infra.vraptor;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.hibernate.cfg.AnnotationConfiguration;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.ComponentFactory;

@ApplicationScoped
@Component
public class AnnotationConfigurationFactory implements ComponentFactory<AnnotationConfiguration> {

private static List<Class<?>> entities = new ArrayList<Class<?>>();

private AnnotationConfiguration configuration;

public AnnotationConfigurationFactory() {
configuration = new AnnotationConfiguration();
}

@PostConstruct
public void addEntitiesToConfiguration() {
configuration.configure();
for (Class<?> entity : entities) {
configuration.addAnnotatedClass(entity);
}
}

static void addEntity(Class<?> entity) {
entities.add(entity);
}

static List<Class<?>> getEntities() {
return entities;
}

@Override
public AnnotationConfiguration getInstance() {
return null;
}

}
Expand Up @@ -12,6 +12,7 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import javax.persistence.Entity;
import javax.servlet.ServletContext;

import br.com.caelum.vraptor.ComponentRegistry;
Expand Down Expand Up @@ -62,7 +63,7 @@ private void appendMessages(InputStream input) {
throw new IllegalStateException(e);
}
}

private void move(InputStream input, FileOutputStream writer) throws IOException {
BufferedInputStream bis = new BufferedInputStream(input);
byte[] content = new byte[1024*10];
Expand All @@ -76,17 +77,22 @@ private void registerClass(JarEntry jarEntry) {
String className = jarEntry.getName().replaceFirst("\\.class$", "").replace('/', '.');
try {
Class<?> clazz = Class.forName(className);
if (isAnnotatedWithVRaptorStereotype(clazz))
registry.register(clazz, clazz);
if (isAnnotatedWithVRaptorStereotype(clazz)) {
registry.register(clazz, clazz);
}
if (clazz.isAnnotationPresent(Entity.class)) {
AnnotationConfigurationFactory.addEntity(clazz);
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(e);
}
}

private boolean isAnnotatedWithVRaptorStereotype(Class<?> clazz) {
for (Annotation annotation : clazz.getAnnotations()) {
if(annotation.annotationType().isAnnotationPresent(Stereotype.class))
if(annotation.annotationType().isAnnotationPresent(Stereotype.class)) {
return true;
}
}
return false;
}
Expand Down
@@ -0,0 +1,58 @@
/***
* Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
* 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 br.com.caelum.calopsita.infra.vraptor;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.ComponentFactory;

/**
* Creates a SessionFactory from default resource /hibernate.cfg.xml, using
* AnnotationConfiguration, and provides it to container
* @author Lucas Cavalcanti
*
*/
@ApplicationScoped
public class SessionFactoryCreator implements ComponentFactory<SessionFactory> {

private SessionFactory factory;
private final AnnotationConfiguration configuration;

public SessionFactoryCreator(AnnotationConfiguration configuration) {
this.configuration = configuration;
}

@PostConstruct
public void create() {
factory = configuration.buildSessionFactory();
}

public SessionFactory getInstance() {
return factory;
}

@PreDestroy
public void destroy() {
factory.close();
}

}
@@ -0,0 +1,8 @@
package br.com.caelum.calopsita.infra.vraptor;

import javax.persistence.Entity;

@Entity
public class AnEntity {

}
@@ -0,0 +1,35 @@
package br.com.caelum.calopsita.infra.vraptor;

import static org.mockito.Mockito.verify;
import net.vidageek.mirror.dsl.Mirror;

import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class AnnotationConfigurationTest {

private AnnotationConfigurationFactory factory;

private @Mock AnnotationConfiguration configuration;
private class MockedClass {};

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

factory = new AnnotationConfigurationFactory();

new Mirror().on(factory).set().field("configuration").withValue(configuration);
}

@Test
public void shouldAddGivenClassesToAnnotationConfiguration() throws Exception {
factory.addEntity(MockedClass.class);
factory.addEntitiesToConfiguration();
verify(configuration).addAnnotatedClass(MockedClass.class);

}
}
@@ -1,6 +1,7 @@
package br.com.caelum.calopsita.infra.vraptor;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -49,16 +50,24 @@ public void whenThereIsNoManifestDoNothing() throws IOException {
@Test
public void registersClassIfItIsAVRaptorType() throws IOException {
parser.parse(jarWithClass(AVRaptorResource.class));

verify(registry).register(AVRaptorResource.class, AVRaptorResource.class);
}


@Test
public void shouldAddEntitiesToAnnotationConfiguration() throws IOException {
parser.parse(jarWithClass(AnEntity.class));

assertThat(AnnotationConfigurationFactory.getEntities().size(), is(1));
assertEquals(AnnotationConfigurationFactory.getEntities().get(0), AnEntity.class);
}

@Test
public void doesNotRegisterClassIfItIsNotAVRaptorType() throws IOException {
parser.parse(jarWithClass(NotAVRaptorResource.class));

verify(registry, never()).register(NotAVRaptorResource.class, NotAVRaptorResource.class);
}
}

@Test
public void findsPluginMessagePropertiesAndAppendToCalopsitas() throws Exception {
Expand All @@ -67,18 +76,18 @@ public void findsPluginMessagePropertiesAndAppendToCalopsitas() throws Exception
PrintWriter writer = new PrintWriter(messages);
writer.println("some.existing = content");
writer.close();

when(context.getRealPath("/messages.properties")).thenReturn(messages.getAbsolutePath());

parser.parse(jarWithFile("messages.properties", "another = content"));

Properties properties = new Properties();
properties.load(new FileReader(messages));

assertThat(properties.getProperty("some.existing"), is("content"));
assertThat(properties.getProperty("another"), is("content"));
}

private File jarWithManifest(String manifest) throws IOException {
File tmp = File.createTempFile("test-calopsita-plugin", ".jar");
tmp.deleteOnExit();
Expand All @@ -90,9 +99,9 @@ private File jarWithManifest(String manifest) throws IOException {
out.close();
return tmp;
}



private File jarWithClass(Class<?> clazz) throws IOException {
String typePath = clazz.getName().replace('.', '/') + ".class";
InputStream resourceAsStream = CalopsitaPluginParserTest.class.getResourceAsStream("/" + typePath);
Expand Down

0 comments on commit 1ba5759

Please sign in to comment.