View Javadoc

1   /*$Id: IvornProtocol.java,v 1.3 2005/03/31 08:34:17 nw Exp $
2    * Created on 16-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.parameter.protocol;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  import org.astrogrid.community.common.exception.CommunityException;
17  import org.astrogrid.component.descriptor.ComponentDescriptor;
18  import org.astrogrid.filemanager.client.FileManagerClient;
19  import org.astrogrid.filemanager.client.FileManagerClientFactory;
20  import org.astrogrid.filemanager.client.FileManagerNode;
21  import org.astrogrid.filemanager.common.BundlePreferences;
22  import org.astrogrid.registry.RegistryException;
23  import org.astrogrid.store.Ivorn;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  
31  import junit.framework.Test;
32  
33  /*** protocol implementation for ivo:/
34   * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
35   *
36   */
37  public class IvornProtocol implements Protocol , ComponentDescriptor{
38      /***
39       * Commons Logger for this class
40       */
41      private static final Log logger = LogFactory.getLog(IvornProtocol.class);
42  
43      /*** Construct a new IvornProtocol
44       * 
45       */
46      public IvornProtocol() {
47          super();
48          factory = new FileManagerClientFactory(NO_PREFETCH_POLICY);
49      }
50      
51      protected final FileManagerClientFactory factory;
52      /***
53       * @see org.astrogrid.applications.parameter.protocol.Protocol#getProtocolName()
54       */
55      public String getProtocolName() {
56          return Ivorn.SCHEME;
57      }
58      
59      //
60      protected static final BundlePreferences NO_PREFETCH_POLICY  = new BundlePreferences();
61      static {
62          NO_PREFETCH_POLICY.setFetchParents(false);
63          NO_PREFETCH_POLICY.setMaxExtraNodes(new Integer(0));
64          NO_PREFETCH_POLICY.setPrefetchDepth(new Integer(0));
65      }
66      /***
67       * @see org.astrogrid.applications.parameter.protocol.Protocol#createIndirectValue(java.net.URI)
68       * @todo find nice way to pass correct user value in here.
69       * @todo cache FileManagerClientFactory?
70       */
71      public ExternalValue createIndirectValue(final URI reference) throws InaccessibleExternalValueException {
72          final Ivorn ivorn;
73          try {
74              ivorn = new Ivorn(reference.toString());
75          }
76          catch (URISyntaxException e) {
77              throw new InaccessibleExternalValueException(reference.toString(),e);
78          }      
79          return new ExternalValue() {
80              FileManagerClient client= factory.login();
81             
82              public InputStream read() throws InaccessibleExternalValueException {
83                  try {
84                      FileManagerNode n = client.node(ivorn);
85                      return n.readContent();
86                  } catch (Exception e) {
87                      logger.debug("Could not read ivorn" + ivorn,e);
88                      throw new InaccessibleExternalValueException(ivorn.toString(),e);
89                  } 
90              }
91  
92              public OutputStream write() throws InaccessibleExternalValueException {
93                  try {
94                      FileManagerNode n = null;
95                      if (client.exists(ivorn) != null) {
96                          n = client.node(ivorn);
97                      } else {
98                          n = client.createFile(ivorn);
99                      }
100                 return n.writeContent();
101                 } catch (Exception e) {
102                     logger.debug("Could not write ivorn " + ivorn,e);
103                     throw new InaccessibleExternalValueException(ivorn.toString(),e);
104                 }                
105             }
106         };
107     }
108     /***
109      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
110      */
111     public String getName() {
112         return "IvornProtocol";
113     }
114     /***
115      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
116      */
117     public String getDescription() {
118         return "protocol adapter for ivo: urns";
119     }
120     /***
121      * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
122      */
123     public Test getInstallationTest() {
124         return null;
125     }
126 }
127 
128 
129 /* 
130 $Log: IvornProtocol.java,v $
131 Revision 1.3  2005/03/31 08:34:17  nw
132 fixed problem of writing to non-existent ivorns
133 
134 Revision 1.2  2005/03/13 07:13:39  clq2
135 merging jes-nww-686 common-nww-686 workflow-nww-996 scripting-nww-995 cea-nww-994
136 
137 Revision 1.1.110.1  2005/03/11 11:20:58  nw
138 replaced VoSpaceClient with FileManagerClient
139 
140 Revision 1.1  2004/07/26 12:07:38  nw
141 renamed indirect package to protocol,
142 renamed classes and methods within protocol package
143 javadocs
144 
145 Revision 1.2  2004/07/01 11:16:22  nw
146 merged in branch
147 nww-itn06-componentization
148 
149 Revision 1.1.2.2  2004/07/01 01:42:46  nw
150 final version, before merge
151 
152 Revision 1.1.2.1  2004/06/17 09:21:23  nw
153 finished all major functionality additions to core
154  
155 */