View Javadoc

1   package fr.in2p3.jsaga.impl.session;
2   
3   import fr.in2p3.jsaga.EngineProperties;
4   import fr.in2p3.jsaga.helpers.cloner.SagaObjectCloner;
5   import fr.in2p3.jsaga.impl.AbstractSagaObjectImpl;
6   import fr.in2p3.jsaga.impl.context.ContextImpl;
7   import org.ogf.saga.SagaObject;
8   import org.ogf.saga.context.Context;
9   import org.ogf.saga.context.ContextFactory;
10  import org.ogf.saga.error.*;
11  import org.ogf.saga.session.Session;
12  import org.ogf.saga.url.URL;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /* ***************************************************
18  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19  * ***             http://cc.in2p3.fr/             ***
20  * ***************************************************
21  * File:   SessionImpl
22  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
23  * Date:   17 sept. 2007
24  * ***************************************************
25  * Description:                                      */
26  /**
27   *
28   */
29  public class SessionImpl extends AbstractSagaObjectImpl implements Session {
30      private List<ContextImpl> m_contexts;
31  
32      /** constructor */
33      public SessionImpl() {
34          super();
35          m_contexts = new ArrayList<ContextImpl>();
36      }
37  
38      /** clone */
39      public SagaObject clone() throws CloneNotSupportedException {
40          SessionImpl clone = (SessionImpl) super.clone();
41          clone.m_contexts = new SagaObjectCloner<Void,ContextImpl>().cloneList(m_contexts);
42          return clone;
43      }
44  
45      public void addContext(Context context) throws NoSuccessException, TimeoutException {
46          if (context instanceof ContextImpl) {
47              ContextImpl ctxImpl = (ContextImpl) context;
48              // Check if Type is set
49              try {
50                  if ("Unknown".equals((String)context.getAttribute(Context.TYPE))) {
51                      throw new NoSuccessException("The context is missing the attribute Type");
52                  }
53              } catch (NotImplementedException e) {
54                  throw new NoSuccessException(e.getMessage(), e);
55              } catch (AuthenticationFailedException e) {
56                  throw new NoSuccessException(e.getMessage(), e);
57              } catch (AuthorizationFailedException e) {
58                  throw new NoSuccessException(e.getMessage(), e);
59              } catch (PermissionDeniedException e) {
60                  throw new NoSuccessException(e.getMessage(), e);
61              } catch (IncorrectStateException e) {
62                  throw new NoSuccessException("The context is missing the attribute Type");
63              } catch (DoesNotExistException e) {
64                  throw new NoSuccessException("The context is missing the attribute Type");
65              }
66              if (! m_contexts.contains(ctxImpl)) {
67                  // check if new context will conflict with old contexts
68                  boolean check = EngineProperties.getBoolean(EngineProperties.JSAGA_DEFAULT_CONTEXTS_CHECK_CONFLICTS);
69                  if (check) {
70                      for (ContextImpl refContext : m_contexts) {
71                          ctxImpl.throwIfConflictsWith(refContext);
72                      }
73                  }
74                  // set UrlPrefix (if needed)
75                  ctxImpl.setUrlPrefix(m_contexts.size()+1);
76                  // add context
77                  m_contexts.add(ctxImpl);
78              }
79              // create or renew credential
80              try {
81                  ctxImpl.createCredential();
82              } catch (NotImplementedException e) {
83                  throw new NoSuccessException(e);
84              } catch (IncorrectStateException e) {
85                  throw new NoSuccessException(e);
86              }
87          } else {
88              throw new NoSuccessException("Unsupported context implementation: "+context.getClass());
89          }
90      }
91  
92      public void removeContext(Context context) throws DoesNotExistException {
93          if (context instanceof ContextImpl) {
94              ContextImpl ctxImpl = (ContextImpl) context;
95              m_contexts.remove(ctxImpl);
96          } else {
97              throw new DoesNotExistException("Unsupported context implementation: "+context.getClass());
98          }
99      }
100 
101     public Context[] listContexts() {
102         return m_contexts.toArray(new Context[m_contexts.size()]);
103     }
104 
105     public void close() {
106         for (ContextImpl context : m_contexts) {
107             context.close();
108         }
109     }
110 
111     public void close(float timeoutInSeconds) {
112         this.close();
113     }
114 
115     /** use this method when you need to be sure to select the right context */
116     public ContextImpl findContext(URL url) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
117         // look for matching filter
118         for (ContextImpl ctxImpl : m_contexts) {
119             if (ctxImpl.matches(url.toString())) {
120                 return ctxImpl;
121             }
122         }
123 
124         // else look for matching prefix
125         for (ContextImpl ctxImpl : m_contexts) {
126             String prefix;
127             try {
128                 prefix = ctxImpl.getAttribute(ContextImpl.URL_PREFIX)+"-";
129             } catch (IncorrectStateException e) {
130                 throw new NoSuccessException(e);
131             }
132             if (url.getScheme().startsWith(prefix)) {
133                 return ctxImpl;
134             }
135         }
136 
137         // else returns null
138         return null;
139     }
140 
141     /** use this method when you need to select the best matching context */
142     public ContextImpl getBestMatchingContext(URL url) throws NotImplementedException, IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException, BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
143         ContextImpl context = this.findContext(url);
144         if (context != null) {
145             return context;
146         } else if (m_contexts.size() == 1) {
147             return m_contexts.get(0);
148         } else {
149             try {
150                 return (ContextImpl) ContextFactory.createContext(JSAGA_FACTORY, "None");
151             } catch (IncorrectStateException e) {
152                 throw new NoSuccessException(e);
153             }
154         }
155     }
156 }