1
2
3
4
5
6
7
8
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155