VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava; |
| 18 | |
| 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.ArrayList; |
| 22 | |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEventListenerFile; |
| 26 | import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile; |
| 27 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 28 | import 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 | */ |
| 34 | public 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 59 | 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-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 81 | /** |
| 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 88 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 89 | 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 saraswal | 250a747 | 2016-05-12 13:16:57 +0530 | [diff] [blame] | 114 | @Override |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 115 | 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 | } |