Skip to content

Commit

Permalink
Enable default System Properties if they are not already set.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Woods committed Oct 21, 2013
1 parent 9bb96b9 commit f80dfc1
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
@@ -0,0 +1,94 @@
/**
* Copyright 2013 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.kernel.spring;

import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

/**
* This class loads System Properties only if:
* - the context is not an integration-test, and
* - the property is not already set
*
* @author Andrew Woods
* Date: 10/15/13
*/
public class DefaultPropertiesLoader {

private static final Logger LOGGER = getLogger(DefaultPropertiesLoader.class);

final private static String SEP = getProperty("file.separator");
final private static String BASEDIR = getProperty("java.io.tmpdir") + SEP
+ "fcrepo4-data" + SEP;

private enum PROPERTIES {
DEFAULT_OBJECT_STORE(
"com.arjuna.ats.arjuna.common.ObjectStoreEnvironmentBean.default.objectStoreDir"),
OBJECT_STORE("com.arjuna.ats.arjuna.objectstore.objectStoreDir"),
ISPN_CACHE("fcrepo.ispn.CacheDirPath"),
ISPN_BIN_CACHE("fcrepo.ispn.binary.CacheDirPath"),
MODE_INDEX("fcrepo.modeshape.index.location"),
ISPN_ALT_CACHE("fcrepo.ispn.alternative.CacheDirPath"),
ISPN_BIN_ALT_CACHE("fcrepo.ispn.binary.alternative.CacheDirPath"),
ISPN_REPO_CACHE("fcrepo.ispn.repo.CacheDirPath"),
ACTIVE_MQ("fcrepo.activemq.dir");

private String text;

private PROPERTIES(String text) {
this.text = text;
}

public String getValue() {
return text;
}
}


/**
* This method loads default System Properties if:
* - the context is not an integration-test, and
* - the property is not already set
*/
public void loadSystemProperties() {
LOGGER.info("Loading properties");

if (getProperty("integration-test") == null) {
LOGGER.debug("Setting default properties, if necessary.");

for (PROPERTIES prop : PROPERTIES.values()) {
if (getProperty(prop.getValue()) == null) {
setProperty(prop.getValue());
}
}
}

for (PROPERTIES prop : PROPERTIES.values()) {
String val = prop.getValue();
LOGGER.debug("{} = {}", val, getProperty(val));
}
}

private static String getProperty(String property) {
return System.getProperty(property);
}

private void setProperty(String prop) {
System.setProperty(prop, BASEDIR + prop);
}

}
Expand Up @@ -47,6 +47,8 @@ public class ModeShapeRepositoryFactoryBean implements
private static final Logger LOGGER =
getLogger(ModeShapeRepositoryFactoryBean.class);

private DefaultPropertiesLoader propertiesLoader;

@Inject
private JcrRepositoryFactory jcrRepositoryFactory;

Expand All @@ -56,7 +58,7 @@ public class ModeShapeRepositoryFactoryBean implements

/**
* Generate a JCR repository from the given configuration
*
*
* @throws RepositoryException
* @throws IOException
*/
Expand All @@ -67,6 +69,8 @@ public void buildRepository() throws RepositoryException, IOException {
((ClassPathResource) repositoryConfiguration).getPath());
}

getPropertiesLoader().loadSystemProperties();

repository =
(JcrRepository) jcrRepositoryFactory
.getRepository(singletonMap(URL,
Expand Down Expand Up @@ -99,4 +103,10 @@ public void setRepositoryConfiguration(
this.repositoryConfiguration = repositoryConfiguration;
}

private DefaultPropertiesLoader getPropertiesLoader() {
if (null == propertiesLoader) {
propertiesLoader = new DefaultPropertiesLoader();
}
return propertiesLoader;
}
}
@@ -0,0 +1,60 @@
/**
* Copyright 2013 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.kernel.spring;

import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* @author Andrew Woods
* Date: 10/20/13
*/
public class DefaultPropertiesLoaderTest {

private DefaultPropertiesLoader loader;

private static final String PROP_FLAG = "integration-test";
private static final String PROP_TEST = "fcrepo.ispn.repo.CacheDirPath";

@Before
public void setUp() throws Exception {
loader = new DefaultPropertiesLoader();
}

@After
public void tearDown() throws Exception {
System.clearProperty(PROP_FLAG);
System.clearProperty(PROP_TEST);
}

@Test
public void testLoadSystemProperties() throws Exception {
System.setProperty(PROP_FLAG, "true");

loader.loadSystemProperties();
Assert.assertNotNull(System.getProperty(PROP_FLAG));
Assert.assertNull(System.getProperty(PROP_TEST));
}

@Test
public void testLoadSystemPropertiesProduction() throws Exception {
loader.loadSystemProperties();
Assert.assertNull(System.getProperty(PROP_FLAG));
Assert.assertNotNull(System.getProperty(PROP_TEST));
}
}
6 changes: 6 additions & 0 deletions fcrepo-webapp/pom.xml
Expand Up @@ -115,6 +115,7 @@
<systemPropertyVariables>
<test.port>${test.port}</test.port>
<test.context.path>${test.context.path}</test.context.path>
<integration-test>true</integration-test>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down Expand Up @@ -164,6 +165,11 @@
<value>${project.build.directory}/active-mq</value>
</systemProperty>

<systemProperty>
<name>integration-test</name>
<value>true</value>
</systemProperty>

</systemProperties>

</configuration>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -609,6 +609,7 @@
<com.arjuna.ats.arjuna.objectstore.objectStoreDir>
${project.build.directory}/object-store
</com.arjuna.ats.arjuna.objectstore.objectStoreDir>
<integration-test>true</integration-test>
</systemPropertyVariables>
</configuration>
<executions>
Expand Down

0 comments on commit f80dfc1

Please sign in to comment.