2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.xml
.process
;
20 import java
.io
.FileWriter
;
21 import java
.text
.SimpleDateFormat
;
22 import java
.util
.Date
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
27 import javax
.xml
.transform
.stream
.StreamResult
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.argeo
.slc
.process
.SlcExecution
;
32 import org
.argeo
.slc
.process
.SlcExecutionNotifier
;
33 import org
.argeo
.slc
.process
.SlcExecutionStep
;
34 import org
.springframework
.oxm
.Marshaller
;
36 public class FileSlcExecutionNotifier
implements SlcExecutionNotifier
{
37 private final static SimpleDateFormat sdf
= new SimpleDateFormat(
40 private String basePath
;
41 private Marshaller marshaller
;
43 private Map
<String
, String
> uuidToDir
= new HashMap
<String
, String
>();
45 public void addSteps(SlcExecution slcExecution
,
46 List
<SlcExecutionStep
> additionalSteps
) {
47 writeSlcExecution(slcExecution
);
50 public void newExecution(SlcExecution slcExecution
) {
51 String dirPath
= basePath
+ File
.separator
+ sdf
.format(new Date())
52 + '-' + slcExecution
.getUuid();
53 File dir
= new File(dirPath
);
56 uuidToDir
.put(slcExecution
.getUuid(), dirPath
);
58 writeSlcExecution(slcExecution
);
61 public void updateExecution(SlcExecution slcExecution
) {
62 writeSlcExecution(slcExecution
);
65 public void updateStatus(SlcExecution slcExecution
, String oldStatus
,
67 writeSlcExecution(slcExecution
);
70 protected void writeSlcExecution(SlcExecution slcExecution
) {
71 FileWriter out
= null;
73 out
= new FileWriter(getFilePath(slcExecution
));
74 marshaller
.marshal(slcExecution
, new StreamResult(out
));
75 } catch (Exception e
) {
76 throw new SlcException("Cannot marshall SlcExecution to "
77 + getFilePath(slcExecution
), e
);
79 IOUtils
.closeQuietly(out
);
83 protected String
getFileName(SlcExecution slcExecution
) {
84 return "SlcExecution-" + slcExecution
.getUuid() + ".xml";
87 protected String
getFilePath(SlcExecution slcExecution
) {
88 String dirPath
= uuidToDir
.get(slcExecution
.getUuid());
89 return dirPath
+ File
.separator
+ "SlcExecution-"
90 + slcExecution
.getUuid() + ".xml";
93 public void setBasePath(String basePath
) {
94 this.basePath
= basePath
;
97 public void setMarshaller(Marshaller marshaller
) {
98 this.marshaller
= marshaller
;