1 package fr.in2p3.jsaga.engine.factories;
2
3 import fr.in2p3.jsaga.adaptor.ClientAdaptor;
4 import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
5 import fr.in2p3.jsaga.adaptor.data.read.LogicalReader;
6 import fr.in2p3.jsaga.adaptor.data.write.LogicalWriter;
7 import fr.in2p3.jsaga.adaptor.security.SecurityCredential;
8 import fr.in2p3.jsaga.engine.descriptors.AdaptorDescriptors;
9 import fr.in2p3.jsaga.engine.descriptors.DataAdaptorDescriptor;
10 import fr.in2p3.jsaga.impl.context.ContextImpl;
11 import fr.in2p3.jsaga.impl.session.SessionImpl;
12 import org.ogf.saga.error.*;
13 import org.ogf.saga.session.Session;
14 import org.ogf.saga.url.URL;
15
16 import java.util.Map;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class DataAdaptorFactory extends ServiceAdaptorFactory {
31 public static final boolean PHYSICAL = false;
32 public static final boolean LOGICAL = true;
33
34 private DataAdaptorDescriptor m_descriptor;
35
36 public DataAdaptorFactory(AdaptorDescriptors descriptors) {
37 m_descriptor = descriptors.getDataDesc();
38 }
39
40
41
42
43
44
45
46 public DataAdaptor getDataAdaptor(URL url, Session session) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
47 if (url==null || url.getScheme()==null) {
48 throw new IncorrectURLException("No protocol found in URL: "+url);
49 }
50
51
52 ContextImpl context = ((SessionImpl)session).getBestMatchingContext(url);
53
54
55 String scheme = context.getSchemeFromAlias(url.getScheme());
56 Class clazz = m_descriptor.getClass(scheme);
57 try {
58 return (DataAdaptor) clazz.newInstance();
59 } catch (Exception e) {
60 throw new NoSuccessException(e);
61 }
62 }
63
64
65
66
67
68
69
70
71 public DataAdaptor getDataAdaptorAndConnect(URL url, Session session, boolean expectsLogical) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
72 if (url==null || url.getScheme()==null) {
73 throw new IncorrectURLException("No protocol found in URL: "+url);
74 }
75
76
77 ContextImpl context = ((SessionImpl)session).getBestMatchingContext(url);
78
79
80 String scheme = context.getSchemeFromAlias(url.getScheme());
81 Class clazz = m_descriptor.getClass(scheme);
82 DataAdaptor dataAdaptor;
83 try {
84 dataAdaptor = (DataAdaptor) clazz.newInstance();
85 } catch (Exception e) {
86 throw new NoSuccessException(e);
87 }
88
89
90 if (expectsLogical && !(dataAdaptor instanceof LogicalReader || dataAdaptor instanceof LogicalWriter)) {
91 throw new BadParameterException("Protocol '"+scheme+"' is not a logical file protocol");
92 }
93
94
95 Map attributes = getAttributes(url, context, m_descriptor.getDefaultsMap(scheme), ContextImpl.DATA_SERVICE_ATTRIBUTES);
96
97
98 this.checkAttributesValidity(attributes, dataAdaptor.getUsage());
99
100
101 SecurityCredential credential = getCredential(url, context, dataAdaptor);
102 dataAdaptor.setSecurityCredential(credential);
103
104
105 if (url.getPort()<=0 && dataAdaptor.getDefaultPort() == ClientAdaptor.NO_DEFAULT) {
106 throw new BadParameterException("Missing PORT in URL:" + url.getString());
107 }
108
109 dataAdaptor.connect(
110 url.getUserInfo(),
111 url.getHost(),
112 url.getPort()>0 ? url.getPort() : dataAdaptor.getDefaultPort(),
113 url.getPath(),
114 attributes);
115 return dataAdaptor;
116 }
117 }