View Javadoc

1   // ----------------------------------------------------------------------
2   // This code is developed as part of the Java CoG Kit project
3   // The terms of the license can be found at http://www.cogkit.org/license
4   // This message may not be removed or altered.
5   // ----------------------------------------------------------------------
6   
7   package fr.in2p3.jsaga.adaptor.job;
8   
9   import org.globus.common.CoGProperties;
10  import org.globus.io.gass.server.GassServer;
11  import org.ietf.jgss.GSSCredential;
12  
13  import java.util.*;
14  
15  public class GassServerFactory {
16      private static Map mapping = new HashMap();
17      private static Map count = new HashMap();
18      private static String cogIP = CoGProperties.getDefault().getIPAddress();
19  
20      public synchronized static GassServer getGassServer(GSSCredential credential) throws Exception {
21          if (cogIP == null) {
22              if (CoGProperties.getDefault().getIPAddress() == null) {
23                  throw new Exception("Could not determine this host's IP address. Please set an IP address in cog.properties");
24              } else {
25                  cogIP = CoGProperties.getDefault().getIPAddress();
26              }
27          } else if (!cogIP.equalsIgnoreCase(CoGProperties.getDefault().getIPAddress())) {
28              cogIP = CoGProperties.getDefault().getIPAddress();
29              shutdownGassServers();
30          }
31          GassServer server;
32          if (mapping.containsKey(credential)) {
33              server = (GassServer) mapping.get(credential);
34          } else {
35              try {
36                  server = new GassServer(credential, 0);
37              } catch (Exception e) {
38                  throw new Exception("Cannot start a gass server", e);
39              }
40              mapping.put(credential, server);
41          }
42          increaseUsageCount(server);
43          return server;
44      }
45  
46      private static synchronized void increaseUsageCount(GassServer server) {
47          Integer i = (Integer) count.get(server);
48          if (i == null) {
49              i = new Integer(1);
50          } else {
51              i = new Integer(i.intValue() + 1);
52          }
53          count.put(server, i);
54      }
55  
56      public static synchronized void decreaseUsageCount(GassServer server) {
57          Integer i = (Integer) count.get(server);
58          if (i == null) {
59              throw new IllegalStateException("No registered usage for server ("
60                      + server + ")");
61          } else if (i.intValue() == 1) {
62              count.remove(server);
63              mapping.remove(server.getCredentials());
64              server.shutdown();
65          } else {
66              count.put(server, new Integer(i.intValue() - 1));
67          }
68      }
69  
70      private static synchronized void shutdownGassServers() {
71          Iterator iterator = mapping.values().iterator();
72          while (iterator.hasNext()) {
73              GassServer gs = (GassServer) iterator.next();
74              gs.shutdown();
75          }
76          mapping.clear();
77          count.clear();
78      }
79  }