Merge "BUG-2218: Keep existing link augmentations during discovery process"
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / utils / ObjectWriter.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.utils;
11
12 import java.io.FileNotFoundException;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.ObjectOutputStream;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Write object to write to file stream
22  *
23  */
24 public class ObjectWriter {
25     private static Logger logger = LoggerFactory.getLogger(ObjectWriter.class);
26     private FileOutputStream fos;
27     private ObjectOutputStream oos;
28
29     public ObjectWriter() {
30         fos = null;
31         oos = null;
32     }
33
34     public Status write(Object obj, String file) {
35         try {
36             fos = new FileOutputStream(file);
37             oos = new ObjectOutputStream(fos);
38             oos.writeObject(obj);
39         } catch (FileNotFoundException fex) {
40             logger.error("Cannot create {} for writing", file);
41             return new Status(StatusCode.INTERNALERROR, "IO Error");
42         } catch (IOException ioex) {
43             logger.error("Failed to write to {}", file);
44             return new Status(StatusCode.INTERNALERROR, "IO Error");
45         } finally {
46             if (oos != null) {
47                 try {
48                     oos.close();
49                 } catch (IOException ioex) {
50                     logger.error("Failed to close object output stream: {}",
51                             file);
52                 }
53             }
54             if (fos != null) {
55                 try {
56                     fos.close();
57                 } catch (IOException ioex) {
58                     logger
59                             .error("Failed to close output file stream: {}",
60                                     file);
61                 }
62             }
63         }
64         return new Status(StatusCode.SUCCESS);
65     }
66 }