blob: d1b0176951162d3edbf2760b6e97d81c37f70b51 [file] [log] [blame]
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +05301/*
2 * Copyright 2016-present 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.translator.tojava;
18
19import java.io.File;
20import java.io.IOException;
21import java.util.ArrayList;
22
23import org.onosproject.yangutils.datamodel.YangNode;
24
25import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile;
26import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
27import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
28import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
29
30/**
31 * Represents implementation of java bean code fragments temporary implementations.
32 * Maintains the temp files required specific for event listener java snippet generation.
33 */
34public class TempJavaEventListenerFragmentFiles
35 extends TempJavaFragmentFiles {
36
37 /**
38 * File name for generated class file for special type like union, typedef
39 * suffix.
40 */
41 private static final String EVENT_LISTENER_FILE_NAME_SUFFIX = "Listener";
42
43 /**
44 * Java file handle for event listener file.
45 */
46 private File eventListenerJavaFileHandle;
47
48 /**
49 * Creates an instance of temporary java code fragment.
50 *
51 * @param javaFileInfo generated java file info
52 * @throws IOException when fails to create new file handle
53 */
54 public TempJavaEventListenerFragmentFiles(JavaFileInfo javaFileInfo)
55 throws IOException {
56 setExtendsList(new ArrayList<>());
57 setJavaImportData(new JavaImportData());
58 setJavaFileInfo(javaFileInfo);
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053059 setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
60 getJavaFileInfo().getPackageFilePath()));
61 }
62
63 /**
64 * Returns event listeners's java file handle.
65 *
66 * @return java file handle
67 */
68 private File getEventListenerJavaFileHandle() {
69 return eventListenerJavaFileHandle;
70 }
71
72 /**
73 * Sets event's java file handle.
74 *
75 * @param eventListenerJavaFileHandle file handle for event
76 */
77 private void setEventListenerJavaFileHandle(File eventListenerJavaFileHandle) {
78 this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
79 }
80
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053081 /**
82 * Constructs java code exit.
83 *
84 * @param fileType generated file type
85 * @param curNode current YANG node
86 * @throws IOException when fails to generate java files
87 */
Bharat saraswal250a7472016-05-12 13:16:57 +053088 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053089 public void generateJavaFile(int fileType, YangNode curNode)
90 throws IOException {
91
92 createPackage(curNode);
93
94 /**
95 * Creates event listener interface file.
96 */
97 setEventListenerJavaFileHandle(getJavaFileHandle(getJavaClassName(EVENT_LISTENER_FILE_NAME_SUFFIX)));
98 generateEventListenerFile(getEventListenerJavaFileHandle(), curNode, null);
99
100 /**
101 * Close all the file handles.
102 */
103 freeTemporaryResources(false);
104 }
105
106 /**
107 * Removes all temporary file handles.
108 *
109 * @param isErrorOccurred when translator fails to generate java files we
110 * need to close all open file handles include temporary files
111 * and java files.
112 * @throws IOException when failed to delete the temporary files
113 */
Bharat saraswal250a7472016-05-12 13:16:57 +0530114 @Override
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530115 public void freeTemporaryResources(boolean isErrorOccurred)
116 throws IOException {
117 boolean isError = isErrorOccurred;
118 /**
119 * Close all java file handles and when error occurs delete the files.
120 */
121 closeFile(getEventListenerJavaFileHandle(), isError);
122
123 super.freeTemporaryResources(isErrorOccurred);
124 }
125
126}