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 org.apache.mina.common.ByteBuffer;
32  
33  import uk.ac.rdg.resc.jstyx.server.StyxFile;
34  import uk.ac.rdg.resc.jstyx.server.StyxFileClient;
35  import uk.ac.rdg.resc.jstyx.types.ULong;
36  import uk.ac.rdg.resc.jstyx.StyxException;
37  import uk.ac.rdg.resc.jstyx.StyxUtils;
38  
39  /***
40   * A StyxFile that provides an interface to a ServiceDataElement
41   *
42   * @author Jon Blower
43   * $Revision: 401 $
44   * $Date: 2005-09-08 08:08:59 +0100 (Thu, 08 Sep 2005) $
45   * $Log$
46   * Revision 1.6  2005/09/08 07:08:59  jonblower
47   * Removed "String user" from list of parameters to StyxFile.write()
48   *
49   * Revision 1.5  2005/08/30 16:29:00  jonblower
50   * Added processAndReplyRead() helper functions to StyxFile
51   *
52   * Revision 1.4  2005/03/24 09:48:31  jonblower
53   * Changed 'count' from long to int throughout for reading and writing
54   *
55   * Revision 1.3  2005/03/19 21:47:02  jonblower
56   * Further fixes relating to releasing ByteBuffers
57   *
58   * Revision 1.2  2005/03/18 16:45:18  jonblower
59   * Released ByteBuffers after use
60   *
61   * Revision 1.1  2005/03/16 22:16:44  jonblower
62   * Added Styx Grid Service classes to core module
63   *
64   * Revision 1.2  2005/03/16 17:59:35  jonblower
65   * Changed following changes to core JStyx library (replacement of java.nio.ByteBuffers with MINA's ByteBuffers)
66   *
67   * Revision 1.1  2005/02/16 19:22:31  jonblower
68   * Commit adding of SGS files to CVS
69   *
70   */
71  class SDEFile extends StyxFile
72  {    
73      private ServiceDataElement sde;
74      
75      public SDEFile(ServiceDataElement sde) throws StyxException
76      {
77          super(sde.getName());
78          this.sde = sde;
79      }
80      
81      public void read(StyxFileClient client, long offset, int count, int tag)
82          throws StyxException
83      {
84          byte[] bytes = this.sde.getBytes();
85          this.processAndReplyRead(bytes, client, offset, count, tag);
86      }
87  
88      /***
89       * We implicitly assume that the new value of the Service Data is 
90       * contained in a single write message
91       * @todo: what happens if this isn't the case?
92       */
93      public void write(StyxFileClient client, long offset, int count,
94          ByteBuffer data, boolean truncate, int tag)
95          throws StyxException
96      {
97          // TODO: if the sde is readonly, we should alter the permission on the SDEFile
98          if(this.sde.isReadOnly())
99          {
100             throw new StyxException("Cannot write to this file");
101         }
102         // We ignore the offset and just update the file's contents with
103         // the new value
104         byte[] bytes = new byte[(int)count];
105         data.get(bytes);
106         this.sde.setValue(bytes);
107         this.replyWrite(client, count, tag);
108     }
109 
110     public ULong getLength()
111     {
112         return new ULong(this.sde.getBytes().length);
113     }
114     
115 }