1 package fr.in2p3.jsaga.engine.session;
2
3 import fr.in2p3.jsaga.Base;
4 import fr.in2p3.jsaga.engine.config.ConfigurationException;
5 import fr.in2p3.jsaga.generated.session.Attribute;
6 import fr.in2p3.jsaga.generated.session.JsagaDefault;
7 import fr.in2p3.jsaga.helpers.XMLFileParser;
8 import fr.in2p3.jsaga.helpers.xslt.XSLTransformerFactory;
9 import org.exolab.castor.util.LocalConfiguration;
10 import org.exolab.castor.xml.*;
11 import org.ogf.saga.context.Context;
12 import org.ogf.saga.context.ContextFactory;
13 import org.ogf.saga.error.*;
14 import org.ogf.saga.session.Session;
15 import org.w3c.dom.Document;
16
17 import java.io.*;
18 import java.net.URL;
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class SessionConfiguration {
33 private static final String JSAGA_FACTORY = Base.getSagaFactory();
34 private static final String XSD = "schema/jsaga-default-contexts.xsd";
35 protected static final String MERGE = "xsl/jsaga-default-contexts-merge.xsl";
36 protected static final String XSL = "xsl/jsaga-default-contexts.xsl";
37 private static final File DEBUG = new File(Base.JSAGA_VAR, "jsaga-default-contexts.xml");
38
39 protected JsagaDefault m_config;
40
41 public SessionConfiguration(URL sessionCfgUrl) throws ConfigurationException {
42 try {
43 if (sessionCfgUrl != null) {
44
45 InputStream sessionCfgStream = sessionCfgUrl.openStream();
46 byte[] data = new XMLFileParser(null).xinclude(sessionCfgStream);
47
48
49 XMLFileParser parser = new XMLFileParser(new String[]{XSD});
50 Document rawConfig = parser.parse(new ByteArrayInputStream(data), DEBUG);
51
52
53 XSLTransformerFactory tFactory = XSLTransformerFactory.getInstance();
54 Document merged = tFactory.create(MERGE).transformToDOM(rawConfig);
55 Document doc = tFactory.create(XSL).transformToDOM(merged);
56
57
58 Unmarshaller unmarshaller = new Unmarshaller(JsagaDefault.class);
59 unmarshaller.setIgnoreExtraAttributes(false);
60 unmarshaller.setValidation(true);
61 m_config = (JsagaDefault) unmarshaller.unmarshal(doc);
62 } else {
63 m_config = new JsagaDefault();
64 }
65 } catch (Exception e) {
66 throw new ConfigurationException(e);
67 }
68 }
69
70
71 public String toXML() throws MarshalException, ValidationException, IOException {
72 StringWriter writer = new StringWriter();
73 this.dump(writer);
74 return writer.toString();
75 }
76 protected void dump(Writer writer) throws MarshalException, ValidationException, IOException {
77 LocalConfiguration.getInstance().getProperties().setProperty("org.exolab.castor.indent", "true");
78 Marshaller marshaller = new Marshaller(writer);
79 marshaller.setValidation(true);
80 marshaller.marshal(m_config);
81 }
82
83 public void setDefaultContext(Context context) throws NotImplementedException, NoSuccessException {
84 fr.in2p3.jsaga.generated.session.Context contextCfg = this.findContextCfg(context);
85 if (contextCfg != null) {
86
87 setDefaultContext(context, contextCfg);
88 }
89 }
90
91 public void setDefaultSession(Session session) throws IncorrectStateException, NoSuccessException, TimeoutException {
92 fr.in2p3.jsaga.generated.session.Session sessionCfg = m_config.getSession();
93 if (sessionCfg != null) {
94 for (fr.in2p3.jsaga.generated.session.Context contextCfg : sessionCfg.getContext()) {
95 Context context = ContextFactory.createContext(JSAGA_FACTORY, contextCfg.getType());
96
97 setDefaultContext(context, contextCfg);
98 session.addContext(context);
99 }
100 }
101 }
102
103 public fr.in2p3.jsaga.generated.session.Context[] getSessionContextsCfg() {
104 fr.in2p3.jsaga.generated.session.Session sessionCfg = m_config.getSession();
105 if (sessionCfg != null) {
106 return sessionCfg.getContext();
107 }
108 return new fr.in2p3.jsaga.generated.session.Context[]{};
109 }
110
111 public fr.in2p3.jsaga.generated.session.Context findContextCfg(Context context) throws NotImplementedException, NoSuccessException {
112
113 String type;
114 try {
115 type = context.getAttribute(Context.TYPE);
116 }
117 catch (NotImplementedException e) {throw e;}
118 catch (NoSuccessException e) {throw e;}
119 catch (SagaException e) {throw new NoSuccessException(e);}
120
121
122 fr.in2p3.jsaga.generated.session.Contexts contextsCfg = m_config.getContexts();
123 if (contextsCfg != null) {
124 for (fr.in2p3.jsaga.generated.session.Context contextCfg : contextsCfg.getContext()) {
125 if (contextCfg.getType().equals(type)) {
126 return contextCfg;
127 }
128 }
129 }
130 return null;
131 }
132
133 public static void setDefaultContext(Context context, fr.in2p3.jsaga.generated.session.Context config) throws NoSuccessException {
134 try {
135 for (Attribute attributeCfg : config.getAttribute()) {
136 if (attributeCfg.getValue() != null) {
137 context.setAttribute(attributeCfg.getName(), attributeCfg.getValue());
138 } else if (attributeCfg.getItemCount() > 0) {
139 context.setVectorAttribute(attributeCfg.getName(), attributeCfg.getItem());
140 }
141 }
142 }
143 catch (NoSuccessException e) {throw e;}
144 catch (SagaException e) {throw new NoSuccessException(e);}
145 }
146 }