//(c) University of Edinburgh, 2002 - 2005. //See OGSA-DAI-Licence.txt for licencing information. package uk.org.ogsadai.examples.dataResourceAccessor; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Random; import javax.xml.namespace.QName; import uk.org.ogsadai.common.files.DAIFileNotFoundException; import uk.org.ogsadai.common.files.DAIFileReadIOException; import uk.org.ogsadai.common.properties.Property; import uk.org.ogsadai.common.properties.StaticProperty; import uk.org.ogsadai.dataresource.DataResourceAccessorConfigException; import uk.org.ogsadai.dataresource.DataResourceAccessorMetaDataException; import uk.org.ogsadai.dataresource.DataResourceAccessorSetupException; import uk.org.ogsadai.dataresource.PersistInFiles; import uk.org.ogsadai.dataresource.DataResourceAccessor; /** * Example temperature sensor data resource accessor. *

* The range of valid temperatures is read from a file * range.txt provided within the data service resource * directory to which this accessor belongs. The file consists of a * minimum value and a maximum value each on separate lines. *

* Data service resources using this accessor expose the minimum and * maximum values through resource properties with names: *

* * @author The OGSA-DAI Project Team */ public class TemperatureDataResourceAccessor implements DataResourceAccessor, PersistInFiles, TemperatureProvider { /** Copyright statement */ private static final String COPYRIGHT_NOTICE = "The University of Edinburgh 2002 - 2005."; /** Name of data service resource */ private String mResourceName; /** Minimum temperature value */ private float minValue; /** Maximum temperature value */ private float maxValue; /** Minimum temperature value resource property */ private StaticProperty minValueProp; /** Maximum temperature value resource property */ private StaticProperty maxValueProp; /** Random number generator */ private Random rand = new Random(); /** * Constructor. */ public TemperatureDataResourceAccessor() { } /* (non-Javadoc) * @see uk.org.ogsadai.dataresource.DataResourceAccessor#setResourceName(java.lang.String) */ public void setResourceName(String resourceName) { if (resourceName == null) { throw new IllegalArgumentException( "resourceName must not be null"); } mResourceName = resourceName; } /* (non-Javadoc) * @see uk.org.ogsadai.dataresource.PersistedDataResourceAccessor#restoreFromConfig(java.io.File) */ public void restoreFromConfig(File directory) throws DataResourceAccessorConfigException, DataResourceAccessorMetaDataException, DataResourceAccessorSetupException, IllegalArgumentException { if (directory == null) { throw new IllegalArgumentException("directory must not be null"); } if (! directory.isDirectory()) { throw new IllegalArgumentException("directory " + directory.getAbsolutePath() + " must be a directory"); } // Open a reader to the configuration file. File rangeFile = new File(directory, "range.txt"); try { BufferedReader in = new BufferedReader(new FileReader(rangeFile)); // Read the min and max values from the config file. String line = in.readLine(); minValue = Float.parseFloat(line); line = in.readLine(); maxValue = Float.parseFloat(line); } catch (FileNotFoundException e) { throw new DataResourceAccessorConfigException( mResourceName, new DAIFileNotFoundException(rangeFile)); } catch (IOException e) { throw new DataResourceAccessorConfigException( mResourceName, new DAIFileReadIOException(rangeFile, e)); } // Create the properties. QName minValueQName = new QName("http://ogsadai.org.uk/demoNamespace", "minValue"); minValueProp = new StaticProperty(); minValueProp.setName(minValueQName); minValueProp.setValue("" + minValue); QName maxValueQName = new QName("http://ogsadai.org.uk/demoNamespace", "maxValue"); maxValueProp = new StaticProperty(); maxValueProp.setName(maxValueQName); maxValueProp.setValue("" + maxValue); } /* (non-Javadoc) * @see uk.org.ogsadai.dataresource.DataResource#getProperties() */ public Property[] getProperties() { return new Property[]{minValueProp, maxValueProp}; } /** * Get the current temperature of the sensor. * * @return current temperature in degrees Celsius. */ synchronized public float getTemperature() { // Typically this would contact the sensor and get the current // value. // Here we just return a random value. return rand.nextFloat() * (maxValue-minValue) + minValue; } }