1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file FelixManager.java
23 package org.iotivity.service.ppm;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.util.ArrayList;
32 import java.util.Dictionary;
33 import java.util.HashMap;
36 import org.apache.felix.framework.Felix;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.BundleException;
39 import org.osgi.framework.Constants;
41 import android.content.Context;
42 import android.content.res.AssetManager;
43 import android.os.FileObserver;
44 import android.util.Log;
46 public class FelixManager {
47 private static Felix m_felix = null;
48 private static ArrayList<FileObserver> m_observer;
49 private static FelixManager m_felixmgr;
50 private static Context m_context;
51 private static final int TRUE = 1;
52 private static final int FALSE = 0;
54 static final String ANDROID_FRAMEWORK_PACKAGES = ("android,"
57 + "android.content.pm,"
58 + "android.content.res,"
60 + "android.database.sqlite,"
62 + "android.graphics.drawable,"
63 + "android.graphics.glutils,"
74 + "android.speech.recognition,"
75 + "android.telephony,"
76 + "android.telephony.gsm,"
78 + "android.text.format,"
79 + "android.text.method,"
80 + "android.text.style,"
81 + "android.text.util,"
84 + "android.view.animation,"
87 + "javax.xml.parsers,"
90 + "com.example.felixmanager,"
91 + "org.iotivity.base");
93 // FelixManager singleton
94 public static FelixManager getInstance(Context ctx) {
95 if (m_felixmgr == null) {
96 m_felixmgr = new FelixManager(ctx);
103 public static void LogEx(String info) {
104 Log.d("felix", info);
107 private FelixManager(Context ctx) {
109 m_observer = new ArrayList<FileObserver>();
111 Map configMap = new HashMap();
112 String mCacheDir = ctx.getDir("org.osgi.framework.storage",
113 Context.MODE_WORLD_WRITEABLE).toString();
114 configMap.put("org.osgi.framework.storage", mCacheDir);
115 configMap.put("felix.embedded.execution", "true");
116 configMap.put("org.osgi.service.http.port", "9990");
117 configMap.put("org.osgi.framework.startlevel.beginning", "5");
118 configMap.put("felix.bootdelegation.implicit", "false");
119 configMap.put("felix.service.urlhandlers", "false");
120 configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
121 ANDROID_FRAMEWORK_PACKAGES);
123 m_felix = new Felix(configMap);
127 for (org.osgi.framework.Bundle b : m_felix.getBundleContext()
129 LogEx("Bundle: " + b.getSymbolicName());
131 } catch (Throwable ex) {
132 Log.d("Felix", "could not create framework: " + ex.getMessage(), ex);
136 public static int registerPlugin(String path) {
140 flag = installPlugin(path);
142 flag = loadPluginInfoToManager(path);
147 public static int registerAllPlugin(String path) {
151 flag = findPluginRecursive(path);
153 flag = loadPluginInfoToManager(path);
158 public static int unregisterPlugin(String id) {
161 BundleContext bContext = m_felix.getBundleContext();
162 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
163 for (org.osgi.framework.Bundle b : bundles) {
164 if (b.getSymbolicName().equals(id)) {
165 Log.d("Felix", "bundle: " + b.getBundleId()
166 + " symbolicName : " + b.getSymbolicName());
168 Log.d("Felix", "uninstall end");
171 } catch (BundleException e) {
179 public static int unregisterAllPlugin() {
182 BundleContext bContext = m_felix.getBundleContext();
183 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
184 for (org.osgi.framework.Bundle b : bundles) {
185 if (!b.getSymbolicName().equals("org.apache.felix.framework")) {
186 Log.d("Felix", "bundle: " + b.getBundleId()
187 + " symbolicName : " + b.getSymbolicName());
189 Log.d("Felix", "uninstall end");
192 } catch (BundleException e) {
200 public static org.osgi.framework.Bundle[] getAllPlugins() {
202 BundleContext bContext = m_felix.getBundleContext();
203 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
205 } catch (Exception e) {
212 public static org.osgi.framework.Bundle[] findPulgins(String key,
214 BundleContext bContext = m_felix.getBundleContext();
215 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
220 public static org.osgi.framework.Bundle getPlugin(int ID) {
221 BundleContext bContext = m_felix.getBundleContext();
222 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
223 for (int i = 0; i < bundles.length; i++) {
224 if (bundles[i].getBundleId() == ID) {
231 public static int start(String id) {
233 Log.d("Felix", "String id : " + id);
235 BundleContext bContext = m_felix.getBundleContext();
236 bContext.registerService(Context.class.getName(), m_context, null);
238 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
239 for (org.osgi.framework.Bundle b : bundles) {
240 Log.d("Felix", "symbolicName : " + b.getSymbolicName());
241 if (b.getSymbolicName().equals(id)) {
242 Log.d("Felix", "bundle: " + b.getBundleId()
243 + " symbolicName : " + b.getSymbolicName());
245 Log.d("Felix", "start end");
248 } catch (BundleException e) {
255 public static int stop(String id) {
258 BundleContext bContext = m_felix.getBundleContext();
259 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
260 for (org.osgi.framework.Bundle b : bundles) {
261 if (b.getSymbolicName().equals(id)) {
262 Log.d("Felix", "bundle: " + b.getBundleId()
263 + " symbolicName : " + b.getSymbolicName());
266 Log.d("Felix", "stop end");
269 } catch (BundleException e) {
276 public static boolean isStarted(String name) {
277 String state = getState(name);
278 if (state.equals("ACTIVE") || state.equals("STARTING"))
283 public static String getValue(String name, String key) {
284 Log.d("FELIX", "getValue");
285 BundleContext bContext = m_felix.getBundleContext();
286 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
287 for (org.osgi.framework.Bundle b : bundles) {
288 Dictionary<String, String> dic = b.getHeaders();
289 String bundlename = b.getSymbolicName();
290 Log.d("FELIX", "Bundlename: " + bundlename);
292 if (bundlename.equals(name)) {
293 if (dic.get("Bundle-" + key) == null) {
294 Log.d("FELIX", name + " null");
297 Log.d("FELIX", name + " " + dic.get("Bundle-" + key));
298 return dic.get("Bundle-" + key);
304 public static String getState(String name) {
305 Log.d("FELIX", "getState");
306 BundleContext bContext = m_felix.getBundleContext();
307 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
308 for (org.osgi.framework.Bundle b : bundles) {
309 Dictionary<String, String> dic = b.getHeaders();
310 String bundlename = b.getSymbolicName();
312 if (bundlename.equals(name)) {
313 Log.d("FELIX", state_to_string(b.getState()));
314 return state_to_string(b.getState());
317 Log.d("FELIX", "null");
321 public static String printPluginList() {
323 str += "id | state | symbolname | ";
324 BundleContext bContext = m_felix.getBundleContext();
325 org.osgi.framework.Bundle[] bundles = bContext.getBundles();
326 for (org.osgi.framework.Bundle b : bundles) {
327 str += "\n" + b.getBundleId() + " "
328 + state_to_string(b.getState()) + " " + b.getSymbolicName();
333 public static void stop_felix() {
336 m_felix.waitForStop(0);
337 } catch (Exception e) {
342 public static ArrayList<String> getFileList(String path, boolean recursive) {
343 File file = new File(path);
344 File[] files = file.listFiles();
345 ArrayList<String> array = new ArrayList<String>();
346 for (int i = 0; i < files.length; i++) {
347 if (files[i].isDirectory()) {
348 if (recursive == true) {
349 ArrayList<String> directory_list = getFileList(files[i]
351 array.addAll(directory_list);
353 Log.v("Directory : ", files[i].getName());
355 if (files[i].getName().endsWith("jar")) {
356 Log.v("File :", files[i].getName());
357 if (path.charAt(path.length() - 1) != '/') {
360 array.add(path + files[i].getName());
367 private static String state_to_string(int state) {
368 String str = state + " [unknown state]";
370 if (state == org.osgi.framework.Bundle.ACTIVE) {
372 } else if (state == org.osgi.framework.Bundle.INSTALLED) {
374 } else if (state == org.osgi.framework.Bundle.RESOLVED) {
376 } else if (state == org.osgi.framework.Bundle.STARTING) {
378 } else if (state == org.osgi.framework.Bundle.STOPPING) {
380 } else if (state == org.osgi.framework.Bundle.UNINSTALLED) {
381 return "UNINSTALLED";
386 public static int installPlugin(String path) {
390 System.out.println("PluginManager path is Null\n");
393 ArrayList<String> filearray;
394 BundleContext bContext = m_felix.getBundleContext();
395 String location = "";
397 if (path.charAt(0) != '/') {
399 } else if (path.charAt(path.length() - 1) != '/') {
403 filearray = getFileList(path, false);
405 for (int i = 0; i < filearray.size(); i++) {
407 location = "file:" + filearray.get(i);
408 fin = new FileInputStream(new File(filearray.get(i)));
409 bContext.installBundle(location, fin);
410 } catch (BundleException e) {
413 } catch (FileNotFoundException e) {
422 private static String getEventString(int event) {
424 case FileObserver.ACCESS:
426 case FileObserver.MODIFY:
428 case FileObserver.ATTRIB:
430 case FileObserver.CLOSE_WRITE:
431 return "CLOSE_WRITE";
432 case FileObserver.CLOSE_NOWRITE:
433 return "CLOSE_NOWRITE";
434 case FileObserver.OPEN:
436 case FileObserver.MOVED_FROM:
438 case FileObserver.MOVED_TO:
440 case FileObserver.CREATE:
442 case FileObserver.DELETE:
444 case FileObserver.DELETE_SELF:
445 return "DELETE_SELF";
446 case FileObserver.MOVE_SELF:
453 public static int ObservePluginPath(String path) {
455 Log.d("FELIX", "ObservePluginPath" + path);
457 FileObserver observer = new FileObserver(path) {
459 public void onEvent(int event, String path) {
460 Log.d("FELIX", "Observing start : " + path);
461 Log.d("FELIX", "Observing event : " + getEventString(event));
464 observer.startWatching();
465 m_observer.add(observer);
470 public static int findPluginRecursive(String path) {
473 System.out.println("PluginManager path is Null\n");
474 Log.d("FELIX", "PluginManager path is Null\n");
479 ArrayList<String> filearray;
480 BundleContext bContext = m_felix.getBundleContext();
481 String location = "";
484 if (path.charAt(0) != '/') {
486 } else if (path.charAt(path.length() - 1) != '/') {
490 filearray = getFileList(path, true);
492 for (int i = 0; i < filearray.size(); i++) {
494 location = "file:" + filearray.get(i);
495 fin = new FileInputStream(new File(filearray.get(i)));
496 bContext.installBundle(location, fin);
497 } catch (BundleException e) {
500 } catch (FileNotFoundException e) {
509 public static int loadPluginInfoToManager(String path) {
512 flag = ObservePluginPath(path);
513 FelixManager.printPluginList();
518 public static String getPackageName() {
521 packagename = m_context.getPackageName();
526 private static void copyFiles(String path) {
527 AssetManager assetManager = m_context.getAssets();
528 String assets[] = null;
531 assets = assetManager.list(path);
533 if (assets.length == 0) {
536 String fullPath = "/data/data/"
537 + m_context.getPackageName() + "/" + path;
538 Log.d("FELIX", fullPath);
539 File dir = new File(fullPath);
543 for (int i = 0; i < assets.length; ++i) {
544 copyFiles(path + "/" + assets[i]);
547 } catch (IOException ex) {
548 Log.e("tag", "I/O Exception", ex);
552 private static void copyFile(String filename) {
553 AssetManager assetManager = m_context.getAssets();
554 InputStream in = null;
555 OutputStream out = null;
558 in = assetManager.open(filename);
559 out = m_context.openFileOutput(filename.split("/")[1], Context.MODE_PRIVATE);
561 byte[] buffer = new byte[1024];
565 while ((read = in.read(buffer)) != -1) {
566 out.write(buffer, 0, read);
574 } catch (Exception e) {
575 Log.e("tag", e.getMessage());