Author: mes
Date: 2011-12-23 17:14:14 -0800 (Fri, 23 Dec 2011)
New Revision: 27873
Added:
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/AbstractConfigDirPropsReader.java
core3/api/trunk/property-api/src/test/java/org/cytoscape/property/AbstractConfigDirPropsReaderTest.java
core3/api/trunk/property-api/src/test/resources/
core3/api/trunk/property-api/src/test/resources/test.props
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/PropsReader.java
Modified:
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/CyProperty.java
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/CyActivator.java
core3/impl/trunk/property-impl/src/main/java/org/cytoscape/property/internal/PropsReader.java
Log:
added support for reading props from the config dir at startup
Added:
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/AbstractConfigDirPropsReader.java
===================================================================
---
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/AbstractConfigDirPropsReader.java
(rev 0)
+++
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/AbstractConfigDirPropsReader.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -0,0 +1,128 @@
+package org.cytoscape.property;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.Properties;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An abstract implementation of CyProperty<Properties> that attempts to
read the specified
+ * properties file first from the jar file running this code and then appends
any properties
+ * found in the local configuration directory to that properties object. The
config
+ * directory used is {@link CyProperty.DEFAULT_PROPS_CONFIG_DIR}.
+ * <br/>
+ * This class must be extended so that it will read from the proper jar file.
In general
+ * simply implementing a constructor should be sufficient:
+ * </br>
+ * <pre>
+ * public class PropsReader extends AbstractConfigDirPropsReader {
+ * PropsReader(String s, SavePolicy sp) {
+ * super(s,sp);
+ * }
+ * }
+ * </pre>
+ */
+public abstract class AbstractConfigDirPropsReader implements
CyProperty<Properties> {
+ private static final Logger logger =
LoggerFactory.getLogger(AbstractConfigDirPropsReader.class);
+
+ /**
+ * The Properties object created for this class.
+ */
+ protected final Properties props;
+
+ /**
+ * The SavePolicy of this CyProperty.
+ */
+ protected final SavePolicy savePolicy;
+
+ /**
+ * Creates a new AbstractConfigDirPropsReader object.
+ * @param propFileName The name of the java.util.Properties file to
read.
+ * @param savePolicy The save policy for this CyProperty. This value
MUST be
+ * either {@link CyProperty.SavePolicy.CONFIG_DIR} or
+ * {@link CyProperty.SavePolicy.SESSION_FILE_AND_CONFIG_DIR} so that we
can
+ * reasonably expect to find a properties file in the configuration
directory.
+ * If you'd like to use a different SavePolicy, then consider using
+ * {@link SimpleCyProperty} instead.
+ */
+ public AbstractConfigDirPropsReader(String propFileName, SavePolicy
savePolicy) {
+ if ( propFileName == null )
+ throw new NullPointerException("propFileName cannot be
null");
+
+ if ( savePolicy == null )
+ throw new NullPointerException("savePolicy cannot be
null");
+
+ if ( savePolicy != CyProperty.SavePolicy.CONFIG_DIR &&
+ savePolicy !=
CyProperty.SavePolicy.SESSION_FILE_AND_CONFIG_DIR )
+ throw new IllegalArgumentException("Save policy must
be either CONFIG_DIR or SESSION_FILE_AND_CONFIG_DIR");
+
+ this.savePolicy = savePolicy;
+ this.props = new Properties();
+
+ try {
+ readDefaultFromJar(propFileName);
+ readLocalModifications(propFileName);
+ } catch (Exception e) {
+ logger.warn("Error reading properties file '" +
propFileName + "' - using empty intance.", e);
+ props.clear();
+ }
+ }
+
+ private void readLocalModifications(String propFileName) throws
Exception {
+ InputStream is = null;
+ try {
+ final File configDir = new
File(System.getProperty("user.home"), CyProperty.DEFAULT_PROPS_CONFIG_DIR);
+ final File localPropsFile = new File(configDir,propFileName);
+
+ if (localPropsFile.exists()) {
+ is = new FileInputStream(localPropsFile);
+ props.load(is);
+ is.close();
+ }
+ } finally {
+ if (is != null) {
+ try { is.close(); } catch (IOException ioe) {}
+ is = null;
+ }
+ }
+ }
+
+ private void readDefaultFromJar(String propFileName) throws Exception {
+ InputStream is = null;
+ try {
+ String fileName;
+ if ( !propFileName.startsWith("/") )
+ fileName = "/" + propFileName;
+ else
+ fileName = propFileName;
+
+ is = getClass().getResourceAsStream(fileName);
+ if ( is != null )
+ props.load(is);
+ else
+ logger.warn("couldn't find resource '" +
propFileName + "' in jar.");
+ } finally {
+ if (is != null) {
+ try { is.close(); } catch (IOException ioe) {}
+ is = null;
+ }
+ }
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public CyProperty.SavePolicy getSavePolicy() {
+ return savePolicy;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public Properties getProperties() {
+ return props;
+ }
+}
Modified:
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/CyProperty.java
===================================================================
---
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/CyProperty.java
2011-12-24 00:52:33 UTC (rev 27872)
+++
core3/api/trunk/property-api/src/main/java/org/cytoscape/property/CyProperty.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -17,28 +17,44 @@
*/
public interface CyProperty<P> {
- /** SavePolicy specifies how the CyProperty will be saved, or if it
will not be saved.
+ /**
+ * SavePolicy specifies how the CyProperty will be saved, or if it will
not be saved.
* @CyAPI.Enum.Class
*/
enum SavePolicy {
- /** These properties are preset by Cytoscape to some default
value and
- * are never written out. */
+ /**
+ * These properties are preset by Cytoscape to some default
value and
+ * are never written out.
+ */
DO_NOT_SAVE,
- /** These properties are loaded from and written to the default
config
- * directory and are user defaults. */
+ /**
+ * These properties are loaded from and written to the default
config
+ * directory and are user defaults.
+ */
CONFIG_DIR,
- /** This properties will be loaded from and saved to session
files. */
+ /**
+ * This properties will be loaded from and saved to session
files.
+ */
SESSION_FILE,
- /** This properties will be loaded from and saved to session
files and the default
- * config directory. (Settings in a session file will
overwrite those from the
- * default config directory.) */
- SESSION_FILE_AND_CONFIG_DIR
+ /**
+ * These properties will be loaded from and saved to session
files and the default
+ * config directory. (Settings in a session file will
overwrite those from the
+ * default config directory.)
+ */
+ SESSION_FILE_AND_CONFIG_DIR
}
/**
+ * The name of the default directory where we will look for properties
files. This
+ * will be a subdirectory of the "user.home" directory defined in the
default Java
+ * system properties ({@link System#getProperties()}).
+ */
+ String DEFAULT_PROPS_CONFIG_DIR = ".cytoscape";
+
+ /**
* Return a property object
* @return A property object of type P.
*/
Added:
core3/api/trunk/property-api/src/test/java/org/cytoscape/property/AbstractConfigDirPropsReaderTest.java
===================================================================
---
core3/api/trunk/property-api/src/test/java/org/cytoscape/property/AbstractConfigDirPropsReaderTest.java
(rev 0)
+++
core3/api/trunk/property-api/src/test/java/org/cytoscape/property/AbstractConfigDirPropsReaderTest.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -0,0 +1,50 @@
+package org.cytoscape.property;
+
+
+import static org.junit.Assert.*;
+import java.util.Properties;
+import org.junit.Test;
+
+
+public class AbstractConfigDirPropsReaderTest {
+
+ private class BasicConfigDirPropsReader extends
AbstractConfigDirPropsReader {
+ BasicConfigDirPropsReader(String s, SavePolicy sp) {
+ super(s,sp);
+ }
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testBadSavePolicy() throws Exception {
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader("homer",CyProperty.SavePolicy.DO_NOT_SAVE);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testBadSavePolicy2() throws Exception {
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader("homer",CyProperty.SavePolicy.SESSION_FILE);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testNullSavePol() throws Exception {
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader("homer",null);
+ }
+
+ @Test(expected=NullPointerException.class)
+ public void testNullPropName() throws Exception {
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader(null,CyProperty.SavePolicy.CONFIG_DIR);
+ }
+
+ @Test
+ public void testGetDefaultProp(){
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader("test.props",CyProperty.SavePolicy.CONFIG_DIR);
+ assertEquals("marge",
p.getProperties().getProperty("homer").toString() );
+ }
+
+ @Test
+ public void testGetSavePolicy(){
+ BasicConfigDirPropsReader p = new
BasicConfigDirPropsReader("test.props",CyProperty.SavePolicy.CONFIG_DIR);
+ assertEquals(CyProperty.SavePolicy.CONFIG_DIR,
p.getSavePolicy());
+ }
+
+ // no tests for reading config dir since we don't want to pollute the
actual config dir with anything
+}
Added: core3/api/trunk/property-api/src/test/resources/test.props
===================================================================
--- core3/api/trunk/property-api/src/test/resources/test.props
(rev 0)
+++ core3/api/trunk/property-api/src/test/resources/test.props 2011-12-24
01:14:14 UTC (rev 27873)
@@ -0,0 +1 @@
+homer=marge
Modified:
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/CyActivator.java
===================================================================
---
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/CyActivator.java
2011-12-24 00:52:33 UTC (rev 27872)
+++
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/CyActivator.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -1,23 +1,14 @@
-
-
-
-
package org.cytoscape.linkout.internal;
import org.cytoscape.application.CyApplicationConfiguration;
import org.cytoscape.util.swing.OpenBrowser;
import org.cytoscape.service.util.CyServiceRegistrar;
-
import org.cytoscape.linkout.internal.LinkOut;
-import org.cytoscape.property.SimpleCyProperty;
-
import org.cytoscape.property.CyProperty;
+import org.cytoscape.service.util.AbstractCyActivator;
-
import org.osgi.framework.BundleContext;
-import org.cytoscape.service.util.AbstractCyActivator;
-
import java.util.Properties;
import org.slf4j.Logger;
@@ -32,21 +23,13 @@
super();
}
-
public void start(BundleContext bc) {
OpenBrowser openBrowserServiceRef =
getService(bc,OpenBrowser.class);
CyServiceRegistrar cyServiceRegistrarServiceRef =
getService(bc,CyServiceRegistrar.class);
CyApplicationConfiguration cyApplicationConfigurationServiceRef
= getService(bc,CyApplicationConfiguration.class);
-
- Properties linkoutProperties = new Properties();
- try {
- linkoutProperties.load(
getClass().getClassLoader().getResourceAsStream("linkout.props") );
- } catch (Exception e) {
- logger.warn("could not properly load linkout.props",e);
- }
+ PropsReader linkoutProps = new
PropsReader("linkout.props",CyProperty.SavePolicy.CONFIG_DIR);
- SimpleCyProperty linkoutProps = new
SimpleCyProperty(linkoutProperties,CyProperty.SavePolicy.CONFIG_DIR);
LinkOut linkout = new
LinkOut(linkoutProps,cyServiceRegistrarServiceRef,openBrowserServiceRef,cyApplicationConfigurationServiceRef);
Properties linkoutPropsProps = new Properties();
Added:
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/PropsReader.java
===================================================================
---
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/PropsReader.java
(rev 0)
+++
core3/impl/trunk/linkout-impl/src/main/java/org/cytoscape/linkout/internal/PropsReader.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -0,0 +1,12 @@
+
+package org.cytoscape.linkout.internal;
+
+import org.cytoscape.property.AbstractConfigDirPropsReader;
+import org.cytoscape.property.CyProperty;
+
+public class PropsReader extends AbstractConfigDirPropsReader {
+ PropsReader(String s, SavePolicy sp) {
+ super(s,sp);
+ }
+}
+
Modified:
core3/impl/trunk/property-impl/src/main/java/org/cytoscape/property/internal/PropsReader.java
===================================================================
---
core3/impl/trunk/property-impl/src/main/java/org/cytoscape/property/internal/PropsReader.java
2011-12-24 00:52:33 UTC (rev 27872)
+++
core3/impl/trunk/property-impl/src/main/java/org/cytoscape/property/internal/PropsReader.java
2011-12-24 01:14:14 UTC (rev 27873)
@@ -8,6 +8,7 @@
import java.util.Properties;
import org.cytoscape.property.CyProperty;
+import org.cytoscape.property.AbstractConfigDirPropsReader;
import org.cytoscape.session.CySession;
import org.cytoscape.session.events.SessionLoadedEvent;
import org.cytoscape.session.events.SessionLoadedListener;
@@ -16,71 +17,19 @@
import org.slf4j.LoggerFactory;
-public class PropsReader implements CyProperty<Properties>,
SessionLoadedListener {
+public class PropsReader extends AbstractConfigDirPropsReader
+ implements SessionLoadedListener {
+
private static final Logger logger =
LoggerFactory.getLogger(PropsReader.class);
- private Properties props;
/**
* Creates a new PropsReader object.
*/
public PropsReader(String propsName) {
- try {
- props = new Properties();
- readDefaultFromJar(props,propsName);
- readLocalModifications(props,propsName);
- } catch (Exception e) {
- logger.warn("Error reading properties file - using
empty intance.", e);
- props = new Properties();
- }
+ super(propsName,CyProperty.SavePolicy.CONFIG_DIR);
}
- private void readLocalModifications(Properties props, String propsName)
throws Exception {
- InputStream is = null;
- try {
- final File configDir = new
File(System.getProperty("user.home"), DEFAULT_CONFIG_DIR);
- final File localPropsFile = new File(configDir,propsName);
-
- if (localPropsFile.exists()) {
- is = new FileInputStream(localPropsFile);
- props.load(is);
- is.close();
- }
- } finally {
- if (is != null) {
- try { is.close(); } catch (IOException ioe) {}
- is = null;
- }
- }
- }
-
- private void readDefaultFromJar(Properties props, String propsName)
throws Exception {
- InputStream is = null;
- try {
- if (propsName == null)
- throw new NullPointerException("propsName is
null");
-
- is =
this.getClass().getClassLoader().getResourceAsStream(propsName);
-
- props.load(is);
- } finally {
- if (is != null) {
- try { is.close(); } catch (IOException ioe) {}
- is = null;
- }
- }
- }
-
@Override
- public CyProperty.SavePolicy getSavePolicy() {
- return CyProperty.SavePolicy.CONFIG_DIR;
- }
-
- @Override
- public Properties getProperties() {
- return props;
- }
-
- @Override
public void handleEvent(SessionLoadedEvent e) {
logger.debug("Updating Properties from loaded session...");
@@ -96,7 +45,8 @@
logger.warn("Could not get new properties from loaded
session - using empty properties.");
newProps = new Properties();
}
-
- this.props = newProps;
+
+ props.clear();
+ props.putAll(newProps);
}
}
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.