1 package fr.in2p3.jsaga.engine.descriptors;
2
3 import fr.in2p3.jsaga.adaptor.Adaptor;
4 import fr.in2p3.jsaga.adaptor.base.defaults.Default;
5 import fr.in2p3.jsaga.adaptor.data.DataAdaptor;
6 import fr.in2p3.jsaga.adaptor.job.control.JobControlAdaptor;
7 import fr.in2p3.jsaga.adaptor.security.SecurityAdaptor;
8 import fr.in2p3.jsaga.engine.config.ConfigurationException;
9 import fr.in2p3.jsaga.engine.schema.config.*;
10 import fr.in2p3.jsaga.engine.schema.config.types.AttributeSourceType;
11 import org.exolab.castor.util.LocalConfiguration;
12 import org.exolab.castor.xml.*;
13 import org.ogf.saga.error.IncorrectStateException;
14
15 import java.io.*;
16 import java.util.*;
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class AdaptorDescriptors {
31 private static AdaptorDescriptors _instance;
32
33 private SecurityAdaptorDescriptor m_securityDesc;
34 private DataAdaptorDescriptor m_dataDesc;
35 private JobAdaptorDescriptor m_jobDesc;
36 private Adaptors m_xml;
37
38 public synchronized static AdaptorDescriptors getInstance() throws ConfigurationException {
39 if (_instance == null) {
40 _instance = new AdaptorDescriptors();
41 }
42 return _instance;
43 }
44 private AdaptorDescriptors() throws ConfigurationException {
45 AdaptorLoader loader = new AdaptorLoader();
46 try {
47 m_securityDesc = new SecurityAdaptorDescriptor(loader.getClasses(SecurityAdaptor.class));
48 m_dataDesc = new DataAdaptorDescriptor(loader.getClasses(DataAdaptor.class), m_securityDesc);
49 m_jobDesc = new JobAdaptorDescriptor(loader.getClasses(JobControlAdaptor.class), m_securityDesc);
50 } catch(Exception e) {
51 throw new ConfigurationException(e);
52 }
53 m_xml = new Adaptors();
54 m_xml.setContext(m_securityDesc.m_xml);
55 m_xml.setProtocol(m_dataDesc.m_xml);
56 m_xml.setExecution(m_jobDesc.m_xml);
57 setRootAttributes(m_xml);
58 }
59
60 public SecurityAdaptorDescriptor getSecurityDesc() {
61 return m_securityDesc;
62 }
63
64 public DataAdaptorDescriptor getDataDesc() {
65 return m_dataDesc;
66 }
67
68 public JobAdaptorDescriptor getJobDesc() {
69 return m_jobDesc;
70 }
71
72 public byte[] toByteArray() throws IOException, ValidationException, MarshalException {
73 ByteArrayOutputStream xmlStream = new ByteArrayOutputStream();
74
75
76 LocalConfiguration.getInstance().getProperties().setProperty("org.exolab.castor.indent", "true");
77 Marshaller m = new Marshaller(new OutputStreamWriter(xmlStream));
78 m.setNamespaceMapping(null, "http://www.in2p3.fr/jsaga");
79 m.setSuppressNamespaces(true);
80 m.setValidation(false);
81 m.marshal(m_xml);
82
83
84 return xmlStream.toByteArray();
85 }
86
87 protected static Map getDefaultsMap(Adaptor adaptor) {
88 Map<String,String> map = new HashMap<String,String>();
89 Default[] defaults;
90 try {
91 defaults = adaptor.getDefaults(new HashMap());
92 } catch (IncorrectStateException e) {
93 defaults = null;
94 }
95 if (defaults != null) {
96 for (Default d : defaults) {
97 if (d.getValue() != null) {
98 map.put(d.getName(), d.getValue());
99 }
100 }
101 }
102 return map;
103 }
104
105 protected static void setDefaults(ObjectType bean, Adaptor adaptor) {
106 Default[] defaults;
107 try {
108 defaults = adaptor.getDefaults(new HashMap());
109 } catch (IncorrectStateException e) {
110 defaults = null;
111 }
112 if (defaults != null) {
113 List list = new ArrayList();
114 for (int d=0; d<defaults.length; d++) {
115 if (defaults[d].getValue() != null) {
116 Attribute attr = new Attribute();
117 attr.setName(defaults[d].getName());
118 attr.setValue(defaults[d].getValue());
119 attr.setSource(AttributeSourceType.ADAPTORDEFAULTS);
120 list.add(attr);
121 }
122 }
123 bean.setAttribute((Attribute[]) list.toArray(new Attribute[list.size()]));
124 }
125 }
126
127 private static void setRootAttributes(Adaptors xml) {
128 String tmpPath = (System.getProperty("file.separator").equals("\\")
129 ? "/"+System.getProperty("java.io.tmpdir").replaceAll("\\\\", "/")
130 : System.getProperty("java.io.tmpdir"));
131 xml.setLocalIntermediary(tmpPath);
132 }
133 }