Skip to content

Commit

Permalink
Ensure clean shutdown of repo on container shutdown
Browse files Browse the repository at this point in the history
- Switch to ModeShapeEngine for deploy/undeploy/shutdown of repository
- Edit repo.xml files to reflect the new ModeShapeEngine and destroy hook on ModeShapeRepositoryFactoryBean
- Retool several ITs to create just a container context and get at beans via singleton pattern
- Some ITs were starting two repositories (in two different application contexts)
- Reintroduce a unit test and fix the auth webapp

Resolves: https://www.pivotaltracker.com/story/show/66454096
  • Loading branch information
gregjan authored and Andrew Woods committed Sep 19, 2014
1 parent 0d9b24c commit 4762faa
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 108 deletions.
Expand Up @@ -21,7 +21,7 @@
<bean name="authenticationProvider" class="org.fcrepo.auth.common.ServletContainerAuthenticationProvider"
p:fad-ref="fad"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory" />
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
3 changes: 1 addition & 2 deletions fcrepo-auth-common/src/test/resources/spring-test/repo.xml
Expand Up @@ -19,9 +19,8 @@

<bean name="authenticationProvider" class="org.fcrepo.auth.common.ServletContainerAuthenticationProvider"
p:fad-ref="fad"/>


<bean class="org.modeshape.jcr.JcrRepositoryFactory" />
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
Expand Up @@ -18,7 +18,7 @@
class="org.fcrepo.kernel.impl.spring.ModeShapeRepositoryFactoryBean"
p:repositoryConfiguration="${fcrepo.modeshape.configuration:/config/testing/repository.json}"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
Expand Up @@ -25,7 +25,7 @@
class="org.fcrepo.kernel.impl.spring.ModeShapeRepositoryFactoryBean"
p:repositoryConfiguration="${fcrepo.modeshape.configuration:/config/minimal-default/repository.json}"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
7 changes: 7 additions & 0 deletions fcrepo-http-api/src/main/resources/META-INF/spring/rest.xml
Expand Up @@ -15,5 +15,12 @@

<context:component-scan base-package="org.fcrepo"/>

<bean name="modeshapeRepofactory"
class="org.fcrepo.kernel.spring.ModeShapeRepositoryFactoryBean">
<property name="repositoryConfiguration" value="${fcrepo.modeshape.configuration:config/minimal-default/repository.json}"/>
</bean>

<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

</beans>

2 changes: 1 addition & 1 deletion fcrepo-http-api/src/test/resources/spring-test/repo.xml
Expand Up @@ -15,7 +15,7 @@
class="org.fcrepo.kernel.impl.spring.ModeShapeRepositoryFactoryBean"
p:repositoryConfiguration="${fcrepo.modeshape.configuration:test_repository.json}"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"/>

Expand Down
@@ -0,0 +1,48 @@
/**
* Copyright 2014 DuraSpace, Inc.
*
* 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.fcrepo.http.commons.test.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
* Retrieves the application context in which it is configured, for testing
* purposes only.
*
* @since September 19, 2014
* @author Gregory Jansen
*/
public class SpringContextSingleton implements ApplicationContextAware {

private static ApplicationContext applicationContext;

/* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(
* org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(final ApplicationContext applicationContext)
throws BeansException {
SpringContextSingleton.applicationContext = applicationContext;
}

public static ApplicationContext getApplicationContext() {
return applicationContext;
}

}
2 changes: 1 addition & 1 deletion fcrepo-jms/src/test/resources/spring-test/repo.xml
Expand Up @@ -15,7 +15,7 @@
class="org.fcrepo.kernel.impl.spring.ModeShapeRepositoryFactoryBean"
p:repositoryConfiguration="${fcrepo.modeshape.configuration:config/testing/repository.json}"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
Expand Up @@ -15,27 +15,27 @@
*/
package org.fcrepo.kernel.impl.spring;

import static java.util.Collections.singletonMap;
import static org.modeshape.jcr.api.RepositoryFactory.URL;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.jcr.RepositoryException;

import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.JcrRepositoryFactory;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.ModeShapeEngine;
import org.modeshape.jcr.NoSuchRepositoryException;
import org.modeshape.jcr.RepositoryConfiguration;
import org.slf4j.Logger;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;

/**
* A Modeshape factory shim to make it play nice with our Spring-based
* configuration
*
*
* @author Edwin Shin
* @since Feb 7, 2013
*/
Expand All @@ -48,7 +48,7 @@ public class ModeShapeRepositoryFactoryBean implements
private DefaultPropertiesLoader propertiesLoader;

@Inject
private JcrRepositoryFactory jcrRepositoryFactory;
private ModeShapeEngine modeShapeEngine;

private Resource repositoryConfiguration;

Expand All @@ -57,22 +57,56 @@ public class ModeShapeRepositoryFactoryBean implements
/**
* Generate a JCR repository from the given configuration
*
* @throws RepositoryException
* @throws IOException
* @throws Exception
*/
@PostConstruct
public void buildRepository() throws RepositoryException, IOException {
public void buildRepository() throws Exception {
LOGGER.info("Using repo config (classpath): {}", repositoryConfiguration.getURL());

getPropertiesLoader().loadSystemProperties();

final RepositoryConfiguration config =
RepositoryConfiguration.read(repositoryConfiguration.getURL());
repository = modeShapeEngine.deploy(config);

// next line ensures that repository starts before the factory is used.
final org.modeshape.common.collection.Problems problems =
repository.getStartupProblems();
for (final org.modeshape.common.collection.Problem p : problems) {
LOGGER.error("ModeShape Start Problem: {}", p.getMessageString());
// TODO determine problems that should be runtime errors
}
}

/**
* Attempts to undeploy the repository and shutdown the ModeShape engine on
* context destroy.
*
* @throws InterruptedException
*/
@PreDestroy
public void stopRepository() throws InterruptedException {
LOGGER.info("Initiating shutdown of ModeShape");
final String repoName = repository.getName();
try {
repository = (JcrRepository) jcrRepositoryFactory.getRepository(
singletonMap(URL, repositoryConfiguration.getURL()));
} catch ( RepositoryException ex ) {
LOGGER.error("Error loading repository with configuration: {}",
repositoryConfiguration.getURL());
throw ex;
final Future<Boolean> futureUndeployRepo =
modeShapeEngine.undeploy(repoName);
futureUndeployRepo.get();
LOGGER.info("Repository {} undeployed.", repoName);
} catch (final NoSuchRepositoryException e) {
LOGGER.error("Repository {} unknown, cannot undeploy.", repoName, e);
} catch (final ExecutionException e) {
LOGGER.error("Repository {} cannot undeploy.", repoName, e);
}
final Future<Boolean> futureShutdownEngine = modeShapeEngine.shutdown();
try {
if (futureShutdownEngine.get()) {
LOGGER.info("ModeShape Engine has shutdown.");
} else {
LOGGER.error("ModeShape Engine shutdown failed without an exception, still running.");
}
} catch (final ExecutionException e) {
LOGGER.error("ModeShape Engine shutdown failed.", e);
}
}

Expand All @@ -83,7 +117,7 @@ public JcrRepository getObject() {

@Override
public Class<?> getObjectType() {
return Repository.class;
return JcrRepository.class;
}

@Override
Expand All @@ -93,7 +127,7 @@ public boolean isSingleton() {

/**
* Set the configuration to use for creating the repository
*
*
* @param repositoryConfiguration
*/
public void setRepositoryConfiguration(
Expand Down
Expand Up @@ -20,20 +20,19 @@
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import java.io.IOException;
import java.util.Map;

import javax.jcr.RepositoryException;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.modeshape.jcr.JcrRepository;
import org.modeshape.jcr.JcrRepositoryFactory;
import org.modeshape.jcr.JcrSession;
import org.modeshape.jcr.api.Repository;
import org.modeshape.jcr.ModeShapeEngine;
import org.modeshape.jcr.RepositoryConfiguration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.ArrayList;

/**
* <p>ModeShapeRepositoryFactoryBeanTest class.</p>
*
Expand All @@ -43,37 +42,43 @@ public class ModeShapeRepositoryFactoryBeanTest {

private ModeShapeRepositoryFactoryBean testObj;

@Mock
private Resource mockConfig;
private Resource config = new ClassPathResource(
"config/testing/repository.json");

@Mock
private JcrRepository mockRepo;

@Mock
private JcrRepositoryFactory mockRepos;
private ModeShapeEngine mockModeShapeEngine;

@Mock
private JcrSession mockSession;

@Mock
private org.modeshape.common.collection.Problems mockProblems;

@Before
public void setUp() throws RepositoryException {
public void setUp() throws Exception {
initMocks(this);
when(mockRepo.getStartupProblems()).thenReturn(mockProblems);
when(mockProblems.iterator()).thenReturn(new ArrayList<org.modeshape.common.collection.Problem>().iterator());
when(mockRepo.login()).thenReturn(mockSession);
testObj = new ModeShapeRepositoryFactoryBean();
testObj.setRepositoryConfiguration(mockConfig);
when(mockRepos.getRepository(any(Map.class))).thenReturn(mockRepo);
setField(testObj, "jcrRepositoryFactory", mockRepos);
testObj.setRepositoryConfiguration(config);
when(mockModeShapeEngine.deploy(any(RepositoryConfiguration.class)))
.thenReturn(mockRepo);
setField(testObj, "modeShapeEngine", mockModeShapeEngine);
}

@Test
public void testFactory() throws RepositoryException, IOException {
public void testFactory() throws Exception {
testObj.buildRepository();
assertEquals(mockRepo, testObj.getObject());
}

@Test
public void testFactoryMetadata() {
assertEquals(Repository.class, testObj.getObjectType());
assertEquals(JcrRepository.class, testObj.getObjectType());
assertEquals(true, testObj.isSingleton());
}
}
2 changes: 1 addition & 1 deletion fcrepo-kernel-impl/src/test/resources/spring-test/repo.xml
Expand Up @@ -19,7 +19,7 @@

<context:component-scan base-package="org.fcrepo"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
Expand Up @@ -15,7 +15,7 @@
class="org.fcrepo.kernel.impl.spring.ModeShapeRepositoryFactoryBean"
p:repositoryConfiguration="${fcrepo.modeshape.configuration:repository.json}"/>

<bean class="org.modeshape.jcr.JcrRepositoryFactory"/>
<bean class="org.modeshape.jcr.ModeShapeEngine" init-method="start"/>

<bean id="connectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" />

Expand Down
Expand Up @@ -19,13 +19,16 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.fcrepo.http.commons.test.util.SpringContextSingleton;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jcr.Repository;
import java.io.IOException;

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;
Expand All @@ -38,8 +41,17 @@
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractResourceIT {

protected Repository repo;

protected Logger logger;

@Before
public void setRepo() {
repo =
SpringContextSingleton.getApplicationContext().getBean(
Repository.class);
}

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
Expand Down

0 comments on commit 4762faa

Please sign in to comment.