aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wireless-transport/code-Carbon-SR1/apps/devicemanager/impl/src/main/java/org/opendaylight/mwtn/base/internalTypes/Resources.java
blob: 89cf29ba67adcb5e5e6023044ebafc3dfbc3c060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.opendaylight.mwtn.base.internalTypes;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Resources {

	private static final Logger LOG = LoggerFactory.getLogger(Resources.class);


	private static URL getFileURL(String resFile)
	{
		Bundle b = FrameworkUtil.getBundle(Resources.class);
		URL u=null;
		if(b==null)
		{
			LOG.warn("cannot load bundle resources");
			try {
				u=new File("src/main/resources"+resFile).toURI().toURL();
			} catch (MalformedURLException e) {
				LOG.warn(e.getMessage());
			}
		}
		else
			u= b.getEntry(resFile);
		return u;
	}

	private static File getFile(String resFile)
	{
		Bundle b = FrameworkUtil.getBundle(Resources.class);
		File f=null;
		if(b==null)
		{
			LOG.warn("cannot load bundle resources");
			f=new File("src/main/resources"+resFile);
		} else
			try {
				f=new File(b.getEntry(resFile).toURI());
			} catch (URISyntaxException e) {

			}
		return f;
	}

	private static String readFile(final URL u) throws IOException
	{
		return readFile(u.openStream());
	}
	private static String readFile(final File f) throws IOException
	{
		return readFile(new FileInputStream(f));
	}
	private static String readFile(final InputStream s) throws IOException
	{
		//read file
		BufferedReader in = new BufferedReader(new InputStreamReader(s));
        StringBuilder sb=new StringBuilder();
		String inputLine;
        while ((inputLine = in.readLine()) != null)
        {
        	sb.append(inputLine);
        }
	    in.close();
	    s.close();
	    return sb.toString();
	}

	public static String getFileContent(String resFile) throws IOException
	{
		return readFile(getFileURL(resFile));
	}
	public static List<File> getFiles(String folder,final String filter,final boolean recursive)
	{
		List<File> list=new ArrayList<File>();
		FileFilter ff=new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				if(pathname.isFile())
					return pathname.getName().contains(filter);
				else
					return true;
			}
		};
		File ffolder=getFile(folder);
		if(ffolder!=null && ffolder.isDirectory())
		{
			File[] files=ffolder.listFiles(ff);
			if(files!=null && files.length>0)
			{
				for(File f:files)
				{
					if(f.isFile())
						list.add(f);
					else if(f.isDirectory() && recursive)
					{
						getFilesRecursive(f,ff,list);
					}
				}
			}
		}
		return list;
	}
	public static List<URL> getFileURLs(String folder,final String filter,final boolean recursive) throws IOException
	{
		Bundle b = FrameworkUtil.getBundle(Resources.class);
		List<URL> list=new ArrayList<URL>();
		if(b==null)
		{
			FileFilter ff=new FileFilter() {

				@Override
				public boolean accept(File pathname) {
					if(pathname.isFile())
						return pathname.getName().contains(filter);
					else
						return true;
				}
			};
			File ffolder=getFile(folder);
			if(ffolder!=null && ffolder.isDirectory())
			{
				File[] files=ffolder.listFiles(ff);
				if(files!=null && files.length>0)
				{
					for(File f:files)
					{
						if(f.isFile())
							list.add(f.toURI().toURL());
						else if(f.isDirectory() && recursive)
						{
							getFileURLsRecursive(f,ff,list);
						}
					}
				}
			}
		}
		else
		{
			getResourceURLsTreeRecurse(b,filter,b.getEntryPaths(folder),recursive,list);
		}
		return list;
	}
	private static void getFilesRecursive(File root, FileFilter ff, List<File> list) {
		if(root!=null && root.isDirectory())
		{
			File[] files=root.listFiles(ff);
			if(files!=null && files.length>0)
			{
				for(File f:files)
				{
					if(f.isFile())
						list.add(f);
					else if(f.isDirectory())
					{
						getFilesRecursive(f,ff,list);
					}
				}
			}
		}

	}
	private static void getFileURLsRecursive(File root, FileFilter ff, List<URL> list) throws MalformedURLException {
		if(root!=null && root.isDirectory())
		{
			File[] files=root.listFiles(ff);
			if(files!=null && files.length>0)
			{
				for(File f:files)
				{
					if(f.isFile())
						list.add(f.toURI().toURL());
					else if(f.isDirectory())
					{
						getFileURLsRecursive(f,ff,list);
					}
				}
			}
		}

	}
	private static void getResourceURLsTreeRecurse(Bundle b, String filter, Enumeration<String> resource,boolean recursive,List<URL> outp) throws IOException {
		while (resource.hasMoreElements()) {
			String name = resource.nextElement();
			Enumeration<String> list = b.getEntryPaths(name);
			if (list != null) {
				if(recursive)
					getResourceURLsTreeRecurse(b, filter, list,recursive,outp);
			} else {
				//Read
				if(name.contains(filter))
				{
					LOG.debug("add "+name+ " to list");
					outp.add(b.getEntry(name));
				}
				else
					LOG.debug("filtered out "+name);
			}
		}
	}
	public static List<JSONObject> getJSONFiles(String folder,boolean recursive)
	{
		List<URL> urls=null;
		try {
			urls = getFileURLs(folder,".json",recursive);
		} catch (IOException e1) {
			LOG.warn("failed to get urls from resfolder "+folder+" : "+e1.getMessage());
		}
		LOG.debug("found "+urls==null?"no":urls.size()+" files");
		List<JSONObject> list=new ArrayList<JSONObject>();
		if(urls!=null)
		{
			for (URL u: urls)
			{
				LOG.debug("try to parse "+u.toString());
				try {
					JSONObject o=new JSONObject(readFile(u));
					list.add(o);
				} catch (JSONException | IOException e) {
					LOG.warn("problem reading/parsing file "+u+" :"+e.getMessage());
				}
			}
		}
		return list;
	}
	public static JSONObject getJSONFile(String resFile) {
		LOG.debug("loading json file "+resFile+" from res");
		URL u=getFileURL(resFile);
		if (u == null)
		{
			LOG.warn("cannot find resfile: "+resFile);
			return null;
		}
		JSONObject o=null;
		try
		{
			//parse to jsonobject
		    o=new JSONObject(readFile(u));
		}
		catch(Exception e)
		{
			LOG.warn("problem reading/parsing file: "+e.getMessage());
		}
		return o;
	}

	public static boolean extractFileTo(String resFile, File oFile) {
    	if(oFile==null)
    		return false;
    	LOG.debug("try to copy "+resFile+" from res to "+oFile.getAbsolutePath());
    	URL u=getFileURL(resFile);
		if (u == null)
		{
			LOG.warn("cannot find resfile:"+resFile);
			return false;
		}
		try {
			InputStream in = u.openStream();
			oFile.getParentFile().mkdirs();
			OutputStream outStream;
			outStream = new FileOutputStream(oFile);
			int theInt;
			while ((theInt = in.read()) >= 0) {
				outStream.write(theInt);
			}
			in.close();
			outStream.flush();
			outStream.close();
			LOG.debug("file written successfully");
		}
		catch (IOException e) {
			LOG.error("problem writing file:"+e.getMessage());
			return false;
		}
		return true;
	}

}