blob: d33e1fe2f0ef4c6174a08f22666ac653962b48e0 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedInputStream;
20import java.io.BufferedOutputStream;
21import java.io.FileInputStream;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.ObjectInput;
26import java.io.ObjectInputStream;
27import java.io.ObjectOutput;
28import java.io.ObjectOutputStream;
29import java.io.OutputStream;
30import java.util.List;
31
32/**
33 * Provides storage for serialized data while traversing data model tree for code generation.
34 */
35public final class SerializedDataStore {
36
37 /**
38 * Data Store types.
39 */
40 public static enum SerializedDataStoreType {
41
42 /**
43 * Methods.
44 */
45 INTERFACE_METHODS,
46
47 /**
48 * Methods.
49 */
50 BUILDER_METHODS,
51
52 /**
53 * Methods.
54 */
55 BUILDER_INTERFACE_METHODS,
56
57 /**
58 * Methods.
59 */
60 IMPL_METHODS,
61
62 /**
63 * Attributes.
64 */
65 ATTRIBUTE,
66
67 /**
68 * Imports.
69 */
70 IMPORT
71 }
72
73 /**
74 * File name string for serialized files of methods.
75 */
76 private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore";
77
78 /**
79 * File name string for serialized files of methods.
80 */
81 private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore";
82
83 /**
84 * File name string for serialized files of methods.
85 */
86 private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore";
87
88 /**
89 * File name string for serialized files of methods.
90 */
91 private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore";
92
93 /**
94 * File name string for serialized files of attributes.
95 */
96 private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore";
97
98 /**
99 * File name string for serialized files of imports.
100 */
101 private static final String IMPORT_FILE_NAME = "SerializedImportDataStore";
102
103 /**
104 * File extension of serialized files.
105 */
106 private static final String SERIALIZE_FILE_EXTENSION = ".ser";
107
108 /**
109 * Buffer size.
110 */
111 private static final int BUFFER_SIZE = 8 * 1024;
112
113 /**
114 * Default constructor.
115 */
116 private SerializedDataStore() {
117 }
118
119 /**
120 * Writes specific info to a serialized file.
121 *
122 * @param data data to be stored
123 * @param type type of serialized data store
124 * @throws IOException when fails to create a serialized data file.
125 */
126 public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException {
127
128 String fileName = "";
129 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
130 fileName = ATTRIBUTE_FILE_NAME;
131 } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
132 fileName = INTERFACE_METHOD_FILE_NAME;
133 } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
134 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
135 } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
136 fileName = BUILDER_METHOD_FILE_NAME;
137 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
138 fileName = IMPL_METHOD_FILE_NAME;
139 } else if (type.equals(SerializedDataStoreType.IMPORT)) {
140 fileName = IMPORT_FILE_NAME;
141 } else {
142 throw new IOException("Unresolved file type.");
143 }
144
145 try {
146 OutputStream file = new FileOutputStream(fileName + SERIALIZE_FILE_EXTENSION);
147 OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
148
149 ObjectOutput output = new ObjectOutputStream(buffer);
150 try {
151 output.writeObject(data);
152 } finally {
153 output.close();
154 }
155 } catch (IOException ex) {
156 throw new IOException("failed to serialize data");
157 }
158 }
159
160 /**
161 * Get the serialized data.
162 *
163 * @param type type of serialized data store
164 * @return list of attribute info.
165 * @throws IOException when fails to read from the file.
166 * @throws ClassNotFoundException when file is missing.
167 */
168 @SuppressWarnings("unchecked")
169 public static List<String> getSerializeData(SerializedDataStoreType type)
170 throws IOException, ClassNotFoundException {
171
172 String fileName = "";
173 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
174 fileName = ATTRIBUTE_FILE_NAME;
175 } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
176 fileName = INTERFACE_METHOD_FILE_NAME;
177 } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
178 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
179 } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
180 fileName = BUILDER_METHOD_FILE_NAME;
181 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
182 fileName = IMPL_METHOD_FILE_NAME;
183 } else if (type.equals(SerializedDataStoreType.IMPORT)) {
184 fileName = IMPORT_FILE_NAME;
185 } else {
186 throw new IOException("Unresolved file type.");
187 }
188
189 try {
190 InputStream file = new FileInputStream(fileName + SERIALIZE_FILE_EXTENSION);
191 InputStream buffer = new BufferedInputStream(file);
192 ObjectInput input = new ObjectInputStream(buffer);
193 try {
194 List<String> recoveredData = (List<String>) input.readObject();
195 return recoveredData;
196 } finally {
197 input.close();
198 }
199 } catch (ClassNotFoundException ex) {
200 throw new ClassNotFoundException("failed to fetch the serialized data file.");
201 }
202 }
203}