1 package fr.in2p3.jsaga.engine.factories;
2
3 import fr.in2p3.jsaga.adaptor.ClientAdaptor;
4 import fr.in2p3.jsaga.adaptor.resource.ResourceAdaptor;
5 import fr.in2p3.jsaga.adaptor.security.SecurityCredential;
6 import fr.in2p3.jsaga.engine.descriptors.AdaptorDescriptors;
7 import fr.in2p3.jsaga.engine.descriptors.ResourceAdaptorDescriptor;
8 import fr.in2p3.jsaga.impl.context.ContextImpl;
9 import org.ogf.saga.error.*;
10 import org.ogf.saga.url.URL;
11
12 import java.util.Map;
13
14
15
16
17
18 public class ResourceAdaptorFactory extends ServiceAdaptorFactory {
19 private ResourceAdaptorDescriptor m_descriptor;
20
21 public ResourceAdaptorFactory(AdaptorDescriptors descriptors) {
22 m_descriptor = descriptors.getResourceDesc();
23 }
24
25 public ResourceAdaptor getAdaptor(URL url, ContextImpl context) throws NotImplementedException, IncorrectURLException, NoSuccessException {
26 if (url==null || url.getScheme()==null) {
27 throw new IncorrectURLException("No protocol found in URL: "+url);
28 }
29
30
31 String scheme = context.getSchemeFromAlias(url.getScheme());
32 Class clazz = m_descriptor.getClass(scheme);
33 try {
34 return (ResourceAdaptor) clazz.newInstance();
35 } catch (Exception e) {
36 throw new NoSuccessException(e);
37 }
38 }
39
40 public Map getAttribute(URL url, ContextImpl context) throws NotImplementedException, NoSuccessException {
41 String scheme = context.getSchemeFromAlias(url.getScheme());
42 try {
43 return getAttributes(url, context, m_descriptor.getDefaultsMap(scheme), ContextImpl.RESOURCE_SERVICE_ATTRIBUTES);
44 } catch (BadParameterException e) {
45 throw new NoSuccessException(e);
46 }
47 }
48
49 public void connect(URL url, ResourceAdaptor adaptor, Map attributes, ContextImpl context) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, IncorrectURLException, BadParameterException, TimeoutException, NoSuccessException {
50
51 SecurityCredential credential = getCredential(url, context, adaptor);
52
53 this.checkAttributesValidity(attributes, adaptor.getUsage());
54
55
56 connect(adaptor, credential, url, attributes);
57 }
58
59 public static void connect(ResourceAdaptor adaptor, SecurityCredential credential, URL url, Map attributes) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, IncorrectURLException, BadParameterException, TimeoutException, NoSuccessException {
60
61 if (url.getPort()<=0 && adaptor.getDefaultPort() == ClientAdaptor.NO_DEFAULT) {
62 throw new BadParameterException("Missing PORT in URL:" + url.getString());
63 }
64 adaptor.setSecurityCredential(credential);
65 adaptor.connect(
66 url.getUserInfo(),
67 url.getHost(),
68 url.getPort()>0 ? url.getPort() : adaptor.getDefaultPort(),
69 url.getPath(),
70 attributes);
71 }
72 public static void disconnect(ResourceAdaptor adaptor) throws NoSuccessException {
73 adaptor.disconnect();
74 }
75 }