View Javadoc

1   package fr.in2p3.jsaga.adaptor.data.impl;
2   
3   import fr.in2p3.jsaga.Base;
4   import fr.in2p3.jsaga.adaptor.schema.data.emulator.*;
5   import fr.in2p3.jsaga.adaptor.security.impl.UserPassSecurityCredential;
6   import org.exolab.castor.util.LocalConfiguration;
7   import org.exolab.castor.xml.*;
8   import org.ogf.saga.error.AuthenticationFailedException;
9   import org.ogf.saga.error.AuthorizationFailedException;
10  
11  import java.io.*;
12  import java.util.Properties;
13  
14  /* ***************************************************
15  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
16  * ***             http://cc.in2p3.fr/             ***
17  * ***************************************************
18  * File:   DataEmulatorGrid
19  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
20  * Date:   26 juin 2007
21  * ***************************************************
22  * Description:                                      */
23  /**
24   *
25   */
26  public class DataEmulatorGrid {
27      private static final java.io.File FILE = new java.io.File(Base.JSAGA_VAR, "data-emulator.xml");
28      private static DataEmulatorGrid _instance = null;
29      private DataEmulator m_gridRoot;
30  
31      ////////////////////////////////////////// friend methods /////////////////////////////////////////
32  
33      static synchronized DataEmulatorGrid getInstance() throws FileNotFoundException, ValidationException, MarshalException {
34          if (_instance == null) {
35              _instance = new DataEmulatorGrid();
36          }
37          return _instance;
38      }
39      protected DataEmulatorGrid() throws FileNotFoundException, ValidationException, MarshalException {
40          if (FILE.exists()) {
41              m_gridRoot = (DataEmulator) Unmarshaller.unmarshal(DataEmulator.class, new InputStreamReader(new FileInputStream(FILE)));
42          } else {
43              m_gridRoot = new DataEmulator();
44          }
45      }
46  
47      /**
48       * commit modification made on server
49       */
50      void commit() throws IOException, ValidationException, MarshalException {
51          // marshal
52          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
53          Properties prop = LocalConfiguration.getInstance().getProperties();
54          prop.setProperty("org.exolab.castor.indent", "true");
55          Marshaller.marshal(m_gridRoot, new OutputStreamWriter(buffer));
56          // dump to file if marshaling did not throw any exception
57          OutputStream out = new FileOutputStream(FILE);
58          out.write(buffer.toByteArray());
59          out.close();
60      }
61  
62      /**
63       * find the server
64       */
65      Server connect(String protocol, String host, int port) {
66          return findServer(protocol, host, port);
67      }
68  
69      /**
70       * find the secure server
71       */
72      SecureServer connect(String protocol, String host, int port, UserPassSecurityCredential security) throws AuthenticationFailedException, AuthorizationFailedException {
73          if (security == null) {
74              throw new AuthenticationFailedException("No security context found");
75          }
76          SecureServer s = findSecureServer(protocol, host, port);
77          String login = security.getUserID();
78          String password = security.getUserPass();
79          for (int i=0; i<s.getUserCount(); i++) {
80              if (s.getUser(i).getLogin().equals(login)) {
81                  if (s.getUser(i).getPassword().equals(password)) {
82                      return s;    
83                  } else {
84                      throw new AuthorizationFailedException("Bad password for user: "+login);
85                  }
86              }
87          }
88          throw new AuthorizationFailedException("Unkown user: "+login);
89      }
90  
91      private Server findServer(String protocol, String host, int port) {
92          for (int i=0; i<m_gridRoot.getServerCount(); i++) {
93              Server s = m_gridRoot.getServer(i);
94              if (equals(s, protocol, host, port)) {
95                  return s;
96              }
97          }
98          return createTestServer(protocol, host, port);
99      }
100     private SecureServer findSecureServer(String protocol, String host, int port) {
101         for (int i=0; i<m_gridRoot.getSecureServerCount(); i++) {
102             SecureServer s = m_gridRoot.getSecureServer(i);
103             if (equals(s, protocol, host, port)) {
104                 return s;
105             }
106         }
107         return createTestSecureServer(protocol, host, port);
108     }
109     private boolean equals(ServerType s, String protocol, String host, int port) {
110         return equals(s.getProtocol(),protocol) && equals(s.getHost(),host) && equals(s.getPort(),port);
111     }
112     private boolean equals(String s1, String s2) {
113         return (s1!=null && s1.equals(s2)) || (s1==null && s2==null);
114     }
115     private boolean equals(int s1, int s2) {
116         return (s1!=0 && s1==s2) || (s1==0 && s2==0);
117     }
118 
119     void disconnect(Server server) {
120         releaseTestServer(server);
121     }
122 
123     void disconnect(SecureServer server) {
124         releaseTestSecureServer(server);
125     }
126 
127     ////////////////////////////////////////// for unit test //////////////////////////////////////////
128 
129     /**
130      * may temporarily create server
131      */
132     private Server createTestServer(String protocol, String host, int port) {
133         if (host.endsWith(".test.org")) {
134             Server server = new Server();
135             server.setProtocol(protocol);
136             server.setHost(host);
137             server.setPort(port);
138             server.setName("/");
139             m_gridRoot.addServer(server);
140             return server;
141         } else {
142             return null;
143         }
144     }
145 
146     /**
147      * may temporarily create secure server
148      */
149     private SecureServer createTestSecureServer(String protocol, String host, int port) {
150         if (host.endsWith(".test.org")) {
151             SecureServer server = new SecureServer();
152             server.setProtocol(protocol);
153             server.setHost(host);
154             server.setPort(port);
155             server.setName("/");
156             if (protocol.startsWith("s")) {
157                 User user = new User();
158                 user.setLogin("anonymous");
159                 user.setPassword("anon");
160                 server.addUser(user);
161             }
162             m_gridRoot.addSecureServer(server);
163             return server;
164         } else {
165             return null;
166         }
167     }
168 
169     /**
170      * may remove temporarily created server
171      */
172     private void releaseTestServer(Server server) {
173         if (server.getHost().endsWith(".test.org")) {
174             m_gridRoot.removeServer(server);
175         }
176     }
177 
178     /**
179      * may remove temporarily created secure server
180      */
181     private void releaseTestSecureServer(SecureServer server) {
182         if (server.getHost().endsWith(".test.org")) {
183             m_gridRoot.removeSecureServer(server);
184         }
185     }
186 }