blob: 9ceb50feb4d3e72bcd2bef6bd4e8916985f17c07 [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;
b.janani68c55e12016-02-24 12:23:03 +053022import java.io.FileNotFoundException;
Bharat saraswal870c56f2016-02-20 21:57:16 +053023import java.io.FileOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.ObjectInput;
27import java.io.ObjectInputStream;
28import java.io.ObjectOutput;
29import java.io.ObjectOutputStream;
30import java.io.OutputStream;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053031import java.util.ArrayList;
Bharat saraswal870c56f2016-02-20 21:57:16 +053032import java.util.List;
33
34/**
35 * Provides storage for serialized data while traversing data model tree for code generation.
36 */
37public final class SerializedDataStore {
38
39 /**
40 * Data Store types.
41 */
42 public static enum SerializedDataStoreType {
43
44 /**
45 * Methods.
46 */
47 INTERFACE_METHODS,
48
49 /**
50 * Methods.
51 */
52 BUILDER_METHODS,
53
54 /**
55 * Methods.
56 */
57 BUILDER_INTERFACE_METHODS,
58
59 /**
60 * Methods.
61 */
62 IMPL_METHODS,
63
64 /**
65 * Attributes.
66 */
67 ATTRIBUTE,
68
69 /**
70 * Imports.
71 */
72 IMPORT
73 }
74
75 /**
76 * File name string for serialized files of methods.
77 */
78 private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore";
79
80 /**
81 * File name string for serialized files of methods.
82 */
83 private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore";
84
85 /**
86 * File name string for serialized files of methods.
87 */
88 private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore";
89
90 /**
91 * File name string for serialized files of methods.
92 */
93 private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore";
94
95 /**
96 * File name string for serialized files of attributes.
97 */
98 private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore";
99
100 /**
101 * File name string for serialized files of imports.
102 */
103 private static final String IMPORT_FILE_NAME = "SerializedImportDataStore";
104
105 /**
106 * File extension of serialized files.
107 */
108 private static final String SERIALIZE_FILE_EXTENSION = ".ser";
109
110 /**
b.janani68c55e12016-02-24 12:23:03 +0530111 * Directory for generating Serialized files.
112 */
113 private static final String GEN_DIR = "target/";
114
115 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530116 * Buffer size.
117 */
118 private static final int BUFFER_SIZE = 8 * 1024;
119
120 /**
121 * Default constructor.
122 */
123 private SerializedDataStore() {
124 }
125
126 /**
127 * Writes specific info to a serialized file.
128 *
129 * @param data data to be stored
130 * @param type type of serialized data store
131 * @throws IOException when fails to create a serialized data file.
132 */
133 public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException {
134
135 String fileName = "";
136 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
137 fileName = ATTRIBUTE_FILE_NAME;
138 } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
139 fileName = INTERFACE_METHOD_FILE_NAME;
140 } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
141 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
142 } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
143 fileName = BUILDER_METHOD_FILE_NAME;
144 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
145 fileName = IMPL_METHOD_FILE_NAME;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530146 } else {
b.janani68c55e12016-02-24 12:23:03 +0530147 fileName = IMPORT_FILE_NAME;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530148 }
149
150 try {
b.janani68c55e12016-02-24 12:23:03 +0530151 OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530152 OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
153
154 ObjectOutput output = new ObjectOutputStream(buffer);
155 try {
156 output.writeObject(data);
157 } finally {
158 output.close();
159 }
160 } catch (IOException ex) {
161 throw new IOException("failed to serialize data");
162 }
163 }
164
165 /**
166 * Get the serialized data.
167 *
168 * @param type type of serialized data store
169 * @return list of attribute info.
170 * @throws IOException when fails to read from the file.
b.janani68c55e12016-02-24 12:23:03 +0530171 * @throws ClassNotFoundException when class is missing.
172 * @throws FileNotFoundException when file is missing.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173 */
Bharat saraswal870c56f2016-02-20 21:57:16 +0530174 public static List<String> getSerializeData(SerializedDataStoreType type)
b.janani68c55e12016-02-24 12:23:03 +0530175 throws IOException, FileNotFoundException, ClassNotFoundException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530176
177 String fileName = "";
178 if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
179 fileName = ATTRIBUTE_FILE_NAME;
180 } else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
181 fileName = INTERFACE_METHOD_FILE_NAME;
182 } else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
183 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
184 } else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
185 fileName = BUILDER_METHOD_FILE_NAME;
186 } else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
187 fileName = IMPL_METHOD_FILE_NAME;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530188 } else {
b.janani68c55e12016-02-24 12:23:03 +0530189 fileName = IMPORT_FILE_NAME;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530190 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530191 try {
b.janani68c55e12016-02-24 12:23:03 +0530192 InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530193 InputStream buffer = new BufferedInputStream(file);
194 ObjectInput input = new ObjectInputStream(buffer);
195 try {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530196 String data = (String) input.readObject();
197 List<String> recoveredData = new ArrayList<>();
198 recoveredData.add(data);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530199 return recoveredData;
200 } finally {
201 input.close();
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530202 file.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530203 }
b.janani68c55e12016-02-24 12:23:03 +0530204 } catch (FileNotFoundException ex) {
205 throw new FileNotFoundException("No such file or directory.");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530206 } catch (ClassNotFoundException ex) {
207 throw new ClassNotFoundException("failed to fetch the serialized data file.");
208 }
209 }
210}