View Javadoc

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  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
20  * ***             http://cc.in2p3.fr/             ***
21  * ***************************************************
22  * File:   DataAdaptorFactory
23  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
24  * Date:   15 juin 2007
25  * ***************************************************
26  * Description:                                      */
27  /**
28   * Create and manage data adaptors
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       * Create a new instance of data adaptor for URL <code>url</code>.
42       * @param url the URL of the service
43       * @param session the security session
44       * @return the data adaptor instance
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          // get context (security + config)
52          ContextImpl context = ((SessionImpl)session).getBestMatchingContext(url);
53  
54          // create adaptor instance
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       * Create a new instance of data adaptor for URL <code>url</code> and connect to service.
66       * @param url the URL of the service
67       * @param session the security session
68       * @param expectsLogical the expected type of adaptor
69       * @return the data adaptor instance
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          // get context (security + config)
77          ContextImpl context = ((SessionImpl)session).getBestMatchingContext(url);
78  
79          // create adaptor instance
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          // check if adaptor is of expected type
90          if (expectsLogical && !(dataAdaptor instanceof LogicalReader || dataAdaptor instanceof LogicalWriter)) {
91              throw new BadParameterException("Protocol '"+scheme+"' is not a logical file protocol");
92          }
93  
94          // get service attributes
95          Map attributes = getAttributes(url, context, m_descriptor.getDefaultsMap(scheme), ContextImpl.DATA_SERVICE_ATTRIBUTES);
96  
97          // check attributes
98          this.checkAttributesValidity(attributes, dataAdaptor.getUsage());
99  
100         // set credential
101         SecurityCredential credential = getCredential(url, context, dataAdaptor);
102         dataAdaptor.setSecurityCredential(credential);
103 
104         // check port
105         if (url.getPort()<=0 && dataAdaptor.getDefaultPort() == ClientAdaptor.NO_DEFAULT) {
106         	throw new BadParameterException("Missing PORT in URL:" + url.getString());
107         }
108         // connect
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 }