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