View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.gridservice.server;
30  
31  import uk.ac.rdg.resc.jstyx.server.StyxFile;
32  import uk.ac.rdg.resc.jstyx.server.AsyncStyxFile;
33  import uk.ac.rdg.resc.jstyx.StyxException;
34  
35  /***
36   * Class representing an element of Service Data (e.g. progress, status, 
37   * number of invocations, etc).
38   *
39   * @author Jon Blower
40   * $Revision: 462 $
41   * $Date: 2005-11-04 19:33:41 +0000 (Fri, 04 Nov 2005) $
42   * $Log$
43   * Revision 1.3  2005/11/04 19:33:41  jonblower
44   * Changed contentsChanged() to fileContentsChanged() in StyxFileChangeListener
45   *
46   * Revision 1.2  2005/03/24 17:33:51  jonblower
47   * Improved reading of service parameters from config file
48   *
49   * Revision 1.1  2005/03/16 22:16:44  jonblower
50   * Added Styx Grid Service classes to core module
51   *
52   * Revision 1.1  2005/02/16 19:22:32  jonblower
53   * Commit adding of SGS files to CVS
54   *
55   */
56  abstract class ServiceDataElement
57  {    
58      
59      private Object value;
60      private String name;
61      private boolean readOnly;
62      private float minUpdateInterval;
63      
64      private StyxFile styxFile = null;
65      private AsyncStyxFile asyncStyxFile = null;
66      
67      /***
68       * Creates a ServiceDataElement.
69       * @param name The name of the element, i.e. the name of the files that
70       * expose the element through the Styx interface
71       * @param readOnly If this is true, the value of the SDE cannot be set
72       * through the Styx interface
73       * @param minUpdateInterval The minimum amount of time between successive
74       * asynchronous updates if reading this SDE through a AsyncStyxFile
75       */
76      public ServiceDataElement(String name, boolean readOnly, float minUpdateInterval)
77      {
78          this.name = name;
79          this.readOnly = readOnly;
80          this.minUpdateInterval = minUpdateInterval;
81      }
82      
83      public ServiceDataElement(String name, boolean readOnly)
84      {
85          this(name, readOnly, 0.0f);
86      }
87      
88      /***
89       * Creates a ServiceDataElement that can be both read and written through
90       * the Styx interface
91       */
92      public ServiceDataElement(String name, float minUpdateInterval)
93      {
94          this(name, false, minUpdateInterval);
95      }
96      
97      /***
98       * Creates a ServiceDataElement that can be both read and written through
99       * the Styx interface
100      */
101     public ServiceDataElement(String name)
102     {
103         this(name, false);
104     }
105     
106     /***
107      * Gets the ServiceDataElement, represented as a byte array
108      */
109     public abstract byte[] getBytes();
110     
111     /***
112      * Converts the given byte array into an object of the appropriate type
113      */
114     public abstract Object getObject(byte[] bytes);
115     
116     /***
117      * Sets a new value for the SDE from the given byte array.
118      */
119     public void setValue(byte[] bytes)
120     {
121         Object obj = this.getObject(bytes);
122         this.setValue(obj);
123     }
124     
125     /***
126      * Gets the value of the ServiceDataElement
127      */
128     public Object getValue()
129     {
130         return this.value;
131     }
132     
133     /***
134      * Sets the ServiceDataElement to a new value and notifies the StyxFile
135      * interfaces to this SDE
136      */
137     public synchronized void setValue(Object newValue)
138     {
139         this.value = newValue;
140         if (this.styxFile != null)
141         {
142             // Notify that the contents of the underlying StyxFile have changed.
143             // This will automatically notify any waiting StyxFileChangeListeners,
144             // including the AsyncStyxFile
145             this.styxFile.contentsChanged();
146         }
147     }
148     
149     /***
150      * Gets the name of this SDE
151      */
152     public String getName()
153     {
154         return this.name;
155     }
156     
157     /***
158      * @return true if this SDE is read-only
159      */
160     public boolean isReadOnly()
161     {
162         return this.readOnly;
163     }
164     
165     /***
166      * Gets the StyxFile that provides non-blocking access to this SDE
167      */
168     public StyxFile getStyxFile() throws StyxException
169     {
170         if (this.styxFile == null)
171         {
172             this.styxFile = new SDEFile(this);
173         }
174         return this.styxFile;
175     }
176     
177     /***
178      * Gets the StyxFile that provides asynchronous access to this SDE
179      * @todo The StyxFile that is returned should be read-only
180      */
181     public StyxFile getAsyncStyxFile() throws StyxException
182     {
183         if (this.asyncStyxFile == null)
184         {
185             this.asyncStyxFile = new AsyncStyxFile(this.getStyxFile());
186             this.asyncStyxFile.setMinReplyInterval(this.minUpdateInterval);
187         }
188         return this.asyncStyxFile;
189     }
190     
191     /***
192      * Forces all waiting clients to get an update on this quantity, irrespective
193      * of how long they have waited since the last update.  Does nothing if the
194      * AsyncStyxFile has not been created (there will be no clients waiting).
195      */
196     public void flush()
197     {
198         if (this.asyncStyxFile != null)
199         {
200             this.asyncStyxFile.fileContentsChanged(true);
201         }
202     }
203     
204 }