Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / resource / StandardConfiguration.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
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 oic.simulator.serviceprovider.resource;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.Date;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import oic.simulator.serviceprovider.Activator;
30 import oic.simulator.serviceprovider.utils.Constants;
31
32 import org.eclipse.core.runtime.FileLocator;
33 import org.oic.simulator.ILogger.Level;
34
35 /**
36  * Class which loads and maintains the standard RAML configuration file list.
37  */
38 public class StandardConfiguration {
39
40     // A map of filename of standard resources as the key and the complete
41     // location of the file(including the filename) as the value.
42     Map<String, String> stdConfigFiles;
43
44     public StandardConfiguration() {
45         stdConfigFiles = new HashMap<String, String>();
46         populateStandardConfigurationList();
47     }
48
49     private void populateStandardConfigurationList() {
50         Enumeration<URL> fileList = Activator.getDefault().getBundle()
51                 .findEntries(Constants.CONFIG_DIRECTORY_PATH, "*", true);
52         if (null == fileList) {
53             Activator
54                     .getDefault()
55                     .getLogManager()
56                     .log(Level.ERROR.ordinal(), new Date(),
57                             "No configuration files exist.");
58             return;
59         }
60         URL url;
61         URL resolvedURL;
62         URI resolvedURI;
63         File file;
64         String relPath;
65         String absPath;
66         while (fileList.hasMoreElements()) {
67             url = (URL) fileList.nextElement();
68             relPath = url.getPath();
69             System.out.println(url.getPath());
70             try {
71                 resolvedURL = FileLocator.toFileURL(url);
72                 if (relPath.toLowerCase().endsWith(
73                         Constants.RAML_FILE_EXTENSION)) {
74                     resolvedURI = new URI(resolvedURL.getProtocol(),
75                             resolvedURL.getPath(), null);
76                     file = new File(resolvedURI);
77                     absPath = file.getAbsolutePath();
78                     stdConfigFiles.put(relPath, absPath);
79                     System.out.println("File path:" + absPath);
80                 }
81             } catch (URISyntaxException | IOException e) {
82                 Activator.getDefault().getLogManager()
83                         .log(Level.ERROR.ordinal(), new Date(), e.getMessage());
84             }
85         }
86     }
87
88     public Map<String, String> getStandardResourceConfigurationList() {
89         return stdConfigFiles;
90     }
91
92     public void setStandardResourceConfigurationList(
93             Map<String, String> stdConfigFiles) {
94         this.stdConfigFiles = stdConfigFiles;
95     }
96
97     public String getFilePath(String fileName) {
98         return stdConfigFiles.get(fileName);
99     }
100 }