View Javadoc

1   package fr.in2p3.jsaga.engine.session;
2   
3   import fr.in2p3.jsaga.engine.config.ConfigurationException;
4   import fr.in2p3.jsaga.generated.session.JsagaDefault;
5   import fr.in2p3.jsaga.helpers.xslt.XSLTransformerFactory;
6   import org.exolab.castor.xml.Marshaller;
7   import org.exolab.castor.xml.Unmarshaller;
8   import org.w3c.dom.Document;
9   
10  import java.io.*;
11  import java.net.MalformedURLException;
12  
13  /* ***************************************************
14   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
15   * ***             http://cc.in2p3.fr/             ***
16   * ***************************************************
17   * File:   EditableSessionConfiguration
18   * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
19   * ***************************************************
20   * Description:                                      */
21  
22  /**
23   *
24   */
25  public class EditableSessionConfiguration extends SessionConfiguration {
26      private File m_file;
27  
28      public EditableSessionConfiguration(File file) throws ConfigurationException, MalformedURLException {
29          super(file.toURI().toURL());
30          m_file = file;
31      }
32  
33      public void save() throws Exception {
34          // serialize bean (needed for transforming config)
35          ByteArrayOutputStream rawConfig = new ByteArrayOutputStream();
36          Marshaller marshaller = new Marshaller(new OutputStreamWriter(rawConfig));
37          marshaller.setValidation(true);
38          marshaller.marshal(m_config);
39  
40          // transform config
41          XSLTransformerFactory tFactory = XSLTransformerFactory.getInstance();
42          Document merged = tFactory.create(MERGE).transformToDOM(rawConfig.toByteArray());
43          Document doc = tFactory.create(XSL).transformToDOM(merged);
44  
45          // deserialize bean (needed for creating session objects)
46          Unmarshaller unmarshaller = new Unmarshaller(JsagaDefault.class);
47          unmarshaller.setIgnoreExtraAttributes(false);
48          unmarshaller.setValidation(true);
49          m_config = (JsagaDefault) unmarshaller.unmarshal(doc);
50  
51          // save to file
52          Writer writer = new FileWriter(m_file);
53          super.dump(writer);
54      }
55  
56      public JsagaDefault getJsagaDefault() {
57          return m_config;
58      }
59  }