public class Activator implements BundleActivator { private Register mRegister; public void start(BundleContext context) throws Exception { mRegister = new Register(); /* * . */ context.registerService(EventHandler.class.getName(), new ActivationEventHandler(mRegister), getHandlerServiceProperties( "ru/futurelink/app/web/usecase/Activator" )); /* * */ Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("contextName", "app"); context.registerService(ApplicationConfiguration.class.getName(), new ApplicationConfig(mRegister), props ); } public void stop(BundleContext context) throws Exception {} protected Dictionary<String, Object> getHandlerServiceProperties(String... topics) { Dictionary<String, Object> result = new Hashtable<String, Object>(); result.put(EventConstants.EVENT_TOPIC, topics); return result; } }
public class ApplicationConfig implements ApplicationConfiguration { private Register mRegister; public ApplicationConfig(Register register) { mRegister = register; } }
public class ActivationEventHandler implements EventHandler { private Register mRegister; public UseCaseActivationEventHandler(Register register) { mRegister = register; } @Override public void handleEvent(Event event) { String bundle = (String) event.getProperty("bundleName"); Integer activated = (Integer) event.getProperty("activated"); BundleContext context = (BundleContext)event.getProperty("bundleContext"); if (activated == 1) { mRegister.registerBundle(bundle, context); } else { mRegister.unregisterBundle(bundle); } } }
public class ClientActivator implements BundleActivator { private ServiceTracker mServiceTracker; private EventAdmin mEventAdmin; private Logger mLogger; private String mBundleName; public ClientActivator() {} public void addUsecase(UseCaseInfo info) { mBundleName = info.getBundleName(); } @Override public void start(BundleContext context) throws Exception { mServiceTracker = new ServiceTracker( context, EventAdmin.class.getName(), null); mServiceTracker.open(); mEventAdmin = (EventAdmin) mServiceTracker.getService(); postActivationEvent(context); } @Override public void stop(BundleContext context) throws Exception { postDeactivationEvent(context); } private void postActivationEvent(BundleContext context) { if (mEventAdmin != null) { // Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("bundleName", mBundleName); props.put("bundleContext", context); props.put("activated", 1); mEventAdmin.postEvent( new Event("ru/futurelink/app/web/usecase/Activator", props)); } else { mLogger.error("Cannot get to EventAdmin service!"); } } private void postDeactivationEvent(BundleContext context) { if (mEventAdmin != null) { Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("bundleName", mBundleName); props.put("activated", 0); mEventAdmin.postEvent( new Event("ru/futurelink/app/web/usecase/Activator", props)); mServiceTracker.close(); } else { System.out.println("Cannot get to EventAdmin service!"); } } protected Dictionary<String, Object> getHandlerServiceProperties(String... topics) { Dictionary<String, Object> result = new Hashtable<String, Object>(); result.put(EventConstants.EVENT_TOPIC, topics); return result; } }
Source: https://habr.com/ru/post/179675/
All Articles