View Javadoc

1   package fr.in2p3.jsaga.impl;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.EngineProperties;
5   import fr.in2p3.jsaga.engine.config.TimeoutConfiguration;
6   import org.ogf.saga.SagaObject;
7   import org.ogf.saga.error.*;
8   import org.ogf.saga.session.Session;
9   import org.ogf.saga.task.Task;
10  
11  import java.util.UUID;
12  
13  /* ***************************************************
14  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
15  * ***             http://cc.in2p3.fr/             ***
16  * ***************************************************
17  * File:   AbstractSagaObjectImpl
18  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
19  * Date:   12 juin 2007
20  * ***************************************************
21  * Description:                                      */
22  /**
23   *
24   */
25  public abstract class AbstractSagaObjectImpl implements SagaObject {
26      protected static final String JSAGA_FACTORY = Base.getSagaFactory();
27  
28      protected Session m_session;
29      private UUID m_uuid;
30  
31      /** constructor */
32      public AbstractSagaObjectImpl(Session session) {
33          m_session = session;
34          m_uuid = UUID.randomUUID();
35      }
36  
37      /** constructor */
38      public AbstractSagaObjectImpl() {
39          m_session = null;
40          m_uuid = UUID.randomUUID();
41      }
42  
43      /** clone */
44      public SagaObject clone() throws CloneNotSupportedException {
45          AbstractSagaObjectImpl clone = (AbstractSagaObjectImpl) super.clone();
46          clone.m_session = m_session;
47          clone.m_uuid = UUID.randomUUID();
48          return clone;
49      }
50  
51      /////////////////////////////////////////// implementation ///////////////////////////////////////////
52  
53      public Session getSession() throws DoesNotExistException {
54          if (m_session != null) {
55              return m_session;
56          } else {
57              throw new DoesNotExistException("This object does not have a session attached", this);
58          }
59      }
60  
61      public String getId() {
62          return m_uuid.toString();
63      }
64  
65      //////////////////////////////////////////// protected methods ////////////////////////////////////////////
66  
67      public static float getTimeout(Class itf, String methodName, String protocolScheme) throws NoSuccessException {
68          return TimeoutConfiguration.getInstance().getTimeout(itf, methodName, protocolScheme);
69      }
70  
71      public static Object getResult(Task task, float timeout)
72              throws NotImplementedException, IncorrectURLException,
73              AuthenticationFailedException, AuthorizationFailedException, PermissionDeniedException,
74              BadParameterException, IncorrectStateException, AlreadyExistsException, DoesNotExistException,
75              TimeoutException, NoSuccessException, SagaIOException
76      {
77          try {
78              task.waitFor(timeout);
79          } catch(TimeoutException e) {
80              task.cancel(true);
81              throw e;
82          }
83          switch(task.getState()) {
84              case DONE:
85                  return task.getResult();
86              case CANCELED:
87              case FAILED:
88                  task.rethrow();
89                  throw new NoSuccessException("[INTERNAL ERROR] Task failed");
90              default:
91                  try{task.cancel();} catch(Exception e){/*ignore*/}
92                  throw new TimeoutException("User timeout occured after "+timeout+" seconds. If this happens too often, modify configuration file: "+EngineProperties.getURL(EngineProperties.JSAGA_TIMEOUT));
93          }
94      }
95  }